|
- // pages/login/login.js
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- form: {
- username: '',
- password: ''
- },
- remember: false,
- formRules: {
- username: {
- validator: function(value) {
- return value.trim() != ''
- },
- tips: '请输入用户名',
- placeholder: '请输入用户名'
- },
- password: {
- validator: function(value) {
- return value.trim() != ''
- },
- tips: '请输入密码',
- placeholder: '请输入密码'
- }
- }
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
-
- },
-
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
-
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- // 隐藏返回图标
- wx.hideHomeButton();
- },
- checkboxChange(e) {
- let checked = this.data.remember
- this.setData({remember: !checked})
- },
- bindValue(e) {
- let name = e.currentTarget.dataset.name;
- let form = this.data.form;
- form[name] = e.detail.value;
- this.setData({
- form,
- })
- this.validate(name, e.detail.value)
- },
- validate(name) {
- let formRules = this.data.formRules;
- let validator = this.data.formRules[name].validator
- let result = validator ? !validator(this.data.form[name]) : false;
- formRules[name].warning = result
- this.setData({
- formRules
- })
- return result
- },
- validateForm() {
- return new Promise((resolve, reject) => {
- try {
- let formRules = this.data.formRules;
- let result = false;
- for (let key in formRules) {
- let temp = this.validate(key)
- if (temp) {
- result = temp
- }
- }
- resolve(!result)
- } catch (e) {
- reject(e)
- }
- })
- },
- submitForm() {
- this.validateForm().then(res => {
- if (res) {
- wx.showLoading({title: "登录中", mask:true});
- // 判断是否有记住密码
- if(this.data.remember) {
- wx.setStorageSync('remember', true)
- wx.setStorageSync('loginForm', this.data.form)
- } else {
- wx.removeStorageSync('remember')
- wx.removeStorageSync('loginForm')
- }
- wx.request({
- url: baseURL+"/member/login",
- method: "POST",
- data: this.data.form,
- header: {
- 'content-type': 'application/json'
- },
- success(res) {
- if (res.data.code == 0) {
- // 以token获取用户信息
- let token = res.data.data
- wx.setStorageSync('token', token) // token存入storage
- wx.request({
- url: baseURL+'/member/user',
- method: "POST",
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- "authorization": token
- },
- success: (res) => {
- wx.setStorageSync('userInfo', res.data.data)
- let app=getApp();
- let observe=app.globalData.observe;
- let getToDoNum=app.getToDoNum;
- // 跳转至tabBar
- wx.switchTab({
- url: '/pages/home/home'
- })
- getToDoNum(observe).then((res)=>{
- if(res){
- if (observe.value > 0) {
- wx.setTabBarBadge({
- index: 1,
- text: observe.value + "",
- })
- } else {
- wx.removeTabBarBadge({
- index: 1,
- })
- }
- }else{
- wx.removeTabBarBadge({
- index: 1,
- })
- }
- }).catch(e=>{console.log(e)});
- },
- fail: (res) => {
- },
- complete: (res) => {
- wx.hideLoading()
- },
- })
- }
- else{
- wx.hideLoading()
- wx.showToast({
- title: res.data.msg,
- icon: 'error',
- image: '',
- duration: 1500,
- });
- }
- },
- fail(error) {
- wx.hideLoading();
- wx.showToast({
- title:error.errMsg,
- icon:"error",
- duration:1000,
- })
- }
- })
- }
- })
- }
- })
|