|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- // pages/login/login.js
- import {userLogin, getUser} from '../../api/login.js'
- import baseURL from '../../environments.js'
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- form: {
- username: '',
- password: ''
- },
- remember: false,
- formRules: {
- username: {
- validator: function(value) {
- return value.trim() != ''
- },
- tips: '请输入用户名',
- placeholder: '请输入用户名',
- isPassword: false
- },
- password: {
- validator: function(value) {
- return value.trim() != ''
- },
- tips: '请输入密码',
- placeholder: '请输入密码',
- isPassword: true
- }
- }
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
-
- },
-
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
-
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- // 隐藏返回图标
- wx.hideHomeButton();
- // 判断是否有记住密码
- if(wx.getStorageSync('remember')) {
- this.setData({
- rememberChecked: true
- })
- this.setData({
- form: wx.getStorageSync('loginForm')
- })
- }
- },
- 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')
- }
- // 登录
- userLogin(this.data.form).then(res=> {
- wx.hideLoading()
- if (res?.code === 0) {
- // 以token获取用户信息
- let token = res.data.token
- wx.setStorageSync('token', token) // token存入storage
- // 获取用户信息
- getUser().then(response=> {
- if(response.code === 0) {
- wx.setStorageSync('userInfo', response.data)
- // 跳转至tabBar
- wx.switchTab({
- url: '/pages/task/task'
- })
- }
- }).catch(e=> {
- console.log(e);
- })
- } else{
- wx.showToast({
- title: res?.msg || '用户名密码错误',
- icon: 'none',
- duration: 2000,
- });
- }
- })
- }
- })
- }
- })
|