//获取应用实例 const app = getApp() Page({ data: { mobile:'', code:'', text:'', mobile_lock:0, mobile_count:60, isLogin:false }, //事件处理函数 inputMobile:function(e){//输入手机号事件监听 this.setData({ mobile: e.detail.value }) }, inputCode: function (e) {//输入短信动态码事件监听 this.setData({ code: e.detail.value }) }, getCode:function(e){//获取动态码事件监听 let mobile = this.data.mobile; this.checkForm(mobile,'手机号','mobile'); if (this.data.mobile_lock === 0) { this.setData({ mobile_lock:1, }); let that=this; wx.request({ url: app.gw.hostUrl + '/sms/code', data: { mobileNumber: that.data.mobile,codeType:1,clientType:10 }, header: { "Content-Type": "application/json" }, method: 'GET', dataType: 'json', responseType: 'text', success: function (res) { if (res.data.code == 0) { that.setData({ mobile_count: 60 }); wx.showToast({ title: '验证码发送成功', icon: 'success', duration: 2000 }) that.btnCount(); } else { that.setData({ mobile_lock: 0 }); that.setData( { popErrorMsg: res.data.msg } ); that.ohShitfadeOut(); return; } }, fail: function (res) { }, complete: function (res) { }, }) } }, ohShitfadeOut() {//错误提示显示3秒后消失,清空计时器 var fadeOutTimeout = setTimeout(() => { this.setData({ popErrorMsg: '' }); clearTimeout(fadeOutTimeout); }, 3000); }, btnCount() { let that=this; if (this.data.mobile_count == 0) { that.setData( { text: "重新获取" } ); that.setData({ mobile_lock: 0 }); clearTimeout(that.mobile_timeout); } else { let count = this.data.mobile_count; count--; that.setData({ mobile_count: count }); that.setData( { text: "重新发送(" + count.toString() + ")秒" } ); that.mobile_timeout = setTimeout(that.btnCount, 1000); } }, checkForm: function (sign, title,flag) { let that = this; if (sign == '') { that.setData( { popErrorMsg: title + "不能为空" } ); that.ohShitfadeOut(); return false; } else if (flag == 'mobile') { if (!(/^1[345678]\d{9}$/.test(sign))) { that.setData( { popErrorMsg: title + "格式错误,请重填" } ); that.ohShitfadeOut(); return false; } } }, bindFormSubmit: function (e) {//点击登录事件监听 console.log(e.detail) let that = this; let mobile = this.data.mobile; let code = this.data.code; let str = JSON.stringify(e.currentTarget.dataset.item); var pages = getCurrentPages(); var currPage = pages[pages.length - 1]; //当前页面 var prevPage = pages[pages.length - 2]; //上一个页面 let checkMobile = that.checkForm(mobile,'手机号','mobile'); if(checkMobile!=false){ let checkCode = that.checkForm(code,'验证码','code'); if(checkCode!=false){ if(that.data.isLogin==false){ that.setData({ isLogin: true }) // 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId app.globalData.userInfo = res.userInfo // 发送 res.code 到后台换取 openId, sessionKey, unionId if (app.globalData.openid != undefined && app.globalData.openid !=""){ that.commitUserInfo(app.globalData.openid, res.encryptedData, res.iv); }; // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (app.userInfoReadyCallback) { app.userInfoReadyCallback(res) } } }) } } }) let user = app.globalData.userInfo; wx.request({//获取sessionId url: app.gw.hostUrl + '/mall/wxa/auth/mobile/bind', method: 'post', data: { mobile: mobile, verifyCode: code, openid: app.globalData.openid }, header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function (res) { //--init data let data = res.data; let status = data.code; if (status != 0) { that.setData( { popErrorMsg: data.msg } ); that.ohShitfadeOut(); return; } app.globalData.sessionId = data.response.sessionId; wx.setStorageSync('sessionId', data.response.sessionId); console.log('sessionId : ' + data.response.sessionId); wx.showToast({ title: '登录成功', icon: 'success', duration: 2000 }) wx.navigateBack({ success: function (e) { var page = getCurrentPages().pop(); if (page == undefined || page == null) return; console.log(page); page.onLoad(); } });//返回到之前页面 }, fail: function (e) { that.setData( { popErrorMsg: '网络异常!err:authlogin' } ); that.ohShitfadeOut(); }, }); } } } }, commitUserInfo: function (openid, encryptedData, iv) { wx.request({ url: app.gw.hostUrl + '/mall/wxa/auth/userinfo', method: 'POST', data: { openid: openid, encryptedData: encryptedData, iv: iv }, header: { 'Content-Type': 'application/x-www-form-urlencoded', }, success: function (res) { //--init data var data = res.data; var status = data.code; if (status != 0) { wx.showToast({ title: data.msg, icon: 'none', duration: 3000 }); return false; } }, fail: function (e) { wx.showToast({ title: '网络异常!err:authlogin', icon: 'none', duration: 2000 }); }, }); }, onLoad: function (options) { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse) { // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } } })