//index.js //获取应用实例 const app = getApp() Page({ data: { text: "This is page data.", windowHeight: app.globalData.windowHeight }, onLoad: function (options) { var that = this // 页面创建时执行 //获取本地数据 wx.getStorage({ key: 'userName', success: function (res) { console.log(res.data); that.setData({ userName: res.data }); } }); wx.getStorage({ key: 'userPassword', success: function (res) { console.log(res.data); that.setData({ userPassword: res.data }); } }); }, onShow: function () { // 页面出现在前台时执行 }, onReady: function () { // 页面首次渲染完毕时执行 }, onHide: function () { // 页面从前台变为后台时执行 }, onUnload: function () { // 页面销毁时执行 }, onPullDownRefresh: function () { // 触发下拉刷新时执行 }, onReachBottom: function () { // 页面触底时执行 }, onShareAppMessage: function () { // 页面被用户分享时执行 }, onPageScroll: function () { // 页面滚动时执行 }, onResize: function () { // 页面尺寸变化时执行 }, onTabItemTap(item) { // tab 点击时执行 console.log(item.index) console.log(item.pagePath) console.log(item.text) }, // 事件响应函数 viewTap: function () { this.setData({ text: 'Set some data for updating view.' }, function () { // this is setData callback }) }, // 自由数据 customData: { hi: 'MINA' }, //测试登录 formSubmit: function (e) { //获得表单数据 var objData = e.detail.value; if (objData.userName && objData.userPassword) { // 同步方式存储表单数据 wx.setStorage({ key: 'userName', data: objData.userName }); wx.setStorage({ key: 'userPassword', data: objData.userPassword }); var userName = e.detail.value.userName var userPassword = e.detail.value.userPassword var that = this; var isrightful = that.checkInput(userName, userPassword); if (isrightful) { wx.request({ url: 'https://wx.yfzhwlw.com/wechat_login', header: { "Content-Type": "application/x-www-form-urlencoded" }, method: "post", data: { username: userName, password: userPassword }, success: function (res) { if (res.statusCode != 200) { app.showError() } else { wx.hideLoading() //判断是否登录成功 if (res.data.code == '1') { wx.showToast({ title: '密码错误,请重试', //这里成功 icon: 'none', duration: 1000 }); } else if (res.data.code == '2') { wx.showToast({ title: '用户名错误,请重试', //这里成功 icon: 'none', duration: 1000 }); } else if (res.data.code == '0') { wx.showToast({ title: '登陆成功', //这里成功 icon: 'success', duration: 1000 }); that.setData({ isLogin: true, }) //将账号名保存在本地 wx.setStorage({ key: "login", data: res.data.username }) //跳转到home页面 wx.switchTab({ url: '../home/home' }) } } }, fail: function (err) { app.showError() } }); } } }, //检测输入值 checkInput: function (userName, userPassword) { var that = this if (userName == "" || userName == null || userName == undefined) { that.showErrorToastUtils('请输入用户名'); return false; } else if (userPassword == "" || userPassword == null || userPassword == undefined) { that.showErrorToastUtils('请输入密码'); return false; } else if (userPassword.length < 6) { that.showErrorToastUtils('密码至少要6位'); return false; } else { // wx.showToast({ // title: '填写正确', // icon: 'success', // duration: 1500 // }) wx.showLoading({ title: '登录中...', }) return true; } }, })