const app = getApp(); const loginTop = '../../static/loginBg.png'; const loginPic = '../../static/loginPic.png'; const api = require('../../utils/api'); const cloudApi = require('../../utils/cloudApi') Page({ data: { userName: '', loginTop: loginTop, loginPic: loginPic, userFocused: false, codeFocused: false, form: { username: 'qyyh12345', password: 'Gkhy@c413', code: '', uuid: '' }, isPwd: true, isLogining: false }, onShow() { if(wx.getStorageSync('name') && wx.getStorageSync('pwd')){ }else{ wx.clearStorageSync(); } }, onLoad() { }, base64Encode(e) { const utf8Bytes = unescape(encodeURIComponent(e)); const array = new Uint8Array(utf8Bytes.length); for (let i = 0; i < utf8Bytes.length; i++) { array[i] = utf8Bytes.charCodeAt(i); } return wx.arrayBufferToBase64(array); }, // 输入框实现双向绑定 onFormInput(e) { const field = e.currentTarget.dataset.field; // 获取字段名 this.setData({ [field]: e.detail.value // 动态更新对应字段 }); }, Login(){ if(this.data.form.username == ''){ wx.showToast({ title: '请输入用户名', duration: 1000 }) return } if(this.data.form.password == ''){ wx.showToast({ title: '请输入密码', duration: 1000 }) return } this.setData({ isLogining: true }); const {username, password, code, uuid} = this.data.form; const data = {username, password, code, uuid}; data.password = this.base64Encode(data.password); wx.removeStorageSync('tk'); // 云开发请求 cloudApi.queryPost('userList',{username: data.username,password: data.password}).then(res=>{ if(res.data.length>0){ this.setData({ isLogining: false }); const info = cloudApi.changeKey(res.data) wx.setStorageSync("username", this.data.form.username); wx.setStorageSync("pwd", this.data.form.password); wx.setStorageSync("uid", info[0].Id); wx.setStorageSync('user', info[0]); wx.switchTab({ url: '/pages/tabBar/firstPage/firstPage' }); }else{ cloudApi.queryPost('userList',{name: data.username}).then(res=>{ if(res.data.length>0){ wx.showToast({ icon: "none", title: '密码错误' }); this.setData({ 'form.password': '', isLogining: false }); }else{ wx.showToast({ icon: "none", title: '用户不存在' }); this.setData({ form: { username: '', password: '', code: '', uuid: '' }, isLogining: false }); } }) } }) // api请求 // api.login(data).then(res => { // console.log(res,'res') // if (res.code === 200) { // this.setData({ // isLogining: false // }); // wx.setStorageSync("name", this.data.form.username); // wx.setStorageSync("pwd", this.data.form.password); // wx.setStorageSync("tk", res.data.token); // wx.setStorageSync("uid", res.data.id); // wx.setStorageSync('user', res.data); // wx.switchTab({ // url: '/pages/tabBar/firstPage/firstPage' // }); // } else { // wx.showToast({ // icon: "none", // title: res.message // }); // this.setData({ // 'form.password': '', // isLogining: false // }); // } // }).catch(err => { // this.setData({ // 'form.password': '', // isLogining: false // }); // }); }, focusUser(){ this.setData({ userFocused: true, codeFocused: false }); }, focusCode(){ this.setData({ codeFocused: true, userFocused: false }); }, togglePwd(){ this.setData({ isPwd: !this.data.isPwd }); } });