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: '', password: '', code: '', uuid: '' }, isPwd: true, isLogining: false, saveAccount: false }, onShow() { }, onLoad() { wx.showShareMenu({ withShareTicket: true, menus: ['shareAppMessage', 'shareTimeline'] }) console.log(wx.getStorageSync('isSave'),'get1') if(wx.getStorageSync('isSave')){ this.setData({ saveAccount: true, form: { username: wx.getStorageSync('username') || '', password: wx.getStorageSync('pwd') || '', code: '', uuid: '' } }) } }, 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 // 动态更新对应字段 }); }, switchChange(e){ this.setData({ saveAccount: e.detail.value }) wx.setStorageSync('isSave',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'); // api请求 api.login(data).then(res => { console.log(res,'res') if (res.code === 200) { this.setData({ isLogining: false }); wx.setStorageSync("username", 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({ 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 }); } });