// pages/login/codeLogin/codeLogin.js var app = getApp(); var util = require('../../../utils/md5.js') import request from '../../../api/request.js' Page({ /** * 页面的初始数据 */ data: { pageShow: false, //是否显示登录页面内容 codeCont: '获取验证码', code: '', loginCode: '', //wx.login获取到的code userName: '', session_key: '', encryptedData: '', iv: '', canClick: true, // 节流 clockTimer: null, // 定时器 path: '', //登录后跳转页面 modelShow: false, //完善信息 phone: '', //getUserInfo获取到的手机号 }, onLoad: function (options) { console.log('登录页options:', options) if (options.path) { this.setData({ path: options.path }) } let userInfo = wx.getStorageSync('userInfo') if (userInfo) { //如果有路径,并且缓存有数据,直接跳转 if (userInfo.userName && userInfo.graduateSchool) { this.loginSuccess() // console.log('已完善信息,允许登录'); return; } if (userInfo.phone) { this.setData({ phone: userInfo.phone }, () => { this.getUserInfo() }) } } this.setData({ pageShow: true }) wx.login({ success: (res) => { if (res.code) { this.setData({ loginCode: res.code }) this.getOpenId() } else { console.log('登录失败!' + res.errMsg) } } }) }, login() { if (!this.data.encryptedData || !this.data.session_key || !this.data.iv) { wx.showToast({ title: '一键登陆失败,请重试', icon: 'none', duration: 2000 }) } request({ url: `h5/user/Oneclicklogin`, method: 'get', data: { "encryptedData": this.data.encryptedData, "session_key": this.data.session_key, "iv": this.data.iv, }, }).then(res => { if (res.success) { if (res.data) { this.setData({ phone: res.data.phone }, () => { this.getUserInfo() }) } else { wx.showToast({ title: '一键登陆失败,请重试', icon: 'none', duration: 2000 }) } } }) }, loginSuccess() { if (this.data.path) { // 获取三位一体的数据 wx.redirectTo({ url: this.data.path, }) } else { wx.switchTab({ url: '/pages/index/index' }) } }, getUserInfo() { let that = this request({ url: `h5/user/getuserInfoNew`, method: 'get', data: { // PhoneName: '13355752969' PhoneName: this.data.phone }, }).then((res) => { if (res.success) { if (res.data) { wx.setStorageSync('userInfo', res.data) wx.setStorageSync('showModal', '') } } }) }, getOpenId() { return request({ url: `h5/MyVoluntary/GetOpenId`, method: 'get', data: { code: this.data.loginCode }, }).then(res2 => { // console.log(res2.data); this.setData({ session_key: JSON.parse(res2.data).session_key }) }) }, bindUserName: function (e) { this.setData({ userName: e.detail.value }) }, bindCode: function (e) { this.setData({ code: e.detail.value }) }, // 点击下一步 next() { var username = this.data.userName; var code = this.data.code; if (!username) { wx.showToast({ title: '请填写手机号', icon: 'none', duration: 1500 }) return false; } if (!code) { wx.showToast({ title: '请填写验证码', icon: 'none', duration: 1500 }) return false; } // 然后判断手机号码是否正确 if (this.checkPhone(this.data.userName)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 2000 }) return } wx.showLoading({ title: '请求中', }) wx.request({ url: api + 'h5/MyVoluntary/CheckCodeIsRight', data: { captcha: code, mobile: username }, method: 'get', success: (res) => { wx.hideLoading(); if (res.data.success) { if (res.data.data) { this.setData({ phone: res.data.data.phone }, () => { this.getUserInfo() }) } else { wx.setStorage({ key: "phone", data: username, success: function () { wx.navigateTo({ url: '/pages/login/loginInfo/loginInfo?phone=' + username, }) } }) } } else { wx.showToast({ title: res.data.message, icon: 'none', duration: 1000 }) } }, fail: function () { wx.hideLoading(); wx.showToast({ title: '请求失败', icon: 'none', duration: 1000 }) } }) }, checkPhone(phone) { if (!(/^1[3456789]\d{9}$/.test(phone))) { return true } else { return false } }, getCode() { if (!this.data.canClick) return // 节流 防止频繁访问接口 this.setData({ canClick: false }) var codeCont = this.data.codeCont; // 获取验证码的时候 先判断是否输入的手机号 if (!this.data.userName) { wx.showToast({ title: '请输入手机号', icon: 'none', duration: 2000 }) return } // 然后判断手机号码是否正确 if (this.checkPhone(this.data.userName)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 2000 }) return } if (codeCont == '获取验证码') { wx.showLoading({ title: '请求中', }) wx.request({ url: api + 'h5/MyVoluntary/AddVerificationCode', // util.hex_md5 header: { sign: util.hex_md5("1" + this.data.userName + "206") }, data: { code: 206, mobile: this.data.userName, type: 1, //写死 }, method: 'get', success: (res) => { wx.hideLoading(); if (res.data.success) { var num = 60; this.data.clockTimer = setInterval(() => { num--; if (num < 0) { clearInterval(this.data.clockTimer); this.setData({ canClick: true, codeCont: '获取验证码' }) } else { this.setData({ codeCont: num }) } }, 1000) } }, fail: function () { wx.hideLoading(); wx.showToast({ title: '请求失败', icon: 'none', duration: 1000 }) } }) } }, getPhoneNumber(e) { // console.log(e.detail) if (e.detail.errMsg == 'getPhoneNumber:ok') { this.setData({ encryptedData: e.detail.encryptedData, iv: e.detail.iv, }, () => { this.login() }) } }, //去查看协议 toProtocol() { wx.navigateTo({ url: '../protocol/protocol', }) }, //去查看政策 toPolicy() { wx.navigateTo({ url: '../policy/policy', }) }, goIndex() { wx.switchTab({ url: '/pages/index/index', }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { clearInterval(this.data.clockTimer); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { clearInterval(this.data.clockTimer); }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })