// pages/login/login.js var app = getApp(); const util = require('../../utils/util.js'); Page({ /** * 页面的初始数据 */ data: { imgPath: app.globalData.imgPath, app_title: '重庆市城市管理局指挥大厅轻应用', phone: '', vCode: '', sendText: '发送验证码', disabled: false, roleList: [{ code: 'ADMIN', page: '../index-admin/index-admin' }, { code: 'OPERATION', page: '../index/index' }, { code: 'GUEST', page: '../index-guest/index-guest' }] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, /** * 手机号赋值 * @param {*} e */ setPhone: function (e) { this.setData({ phone: e.detail.value }); }, /** * 验证码赋值 * @param {*} e */ setVcode: function (e) { this.setData({ vCode: e.detail.value }); }, /** * 获取验证码 */ getVerifyCode: function () { let that = this; let disabled = that.data.disabled; if (disabled) { return; } let phone = this.data.phone; if (!phone) { wx.showToast({ title: '请输入手机号', icon: 'error' }); return false; } let reg = /1[3-9]\d{9}/; if (!reg.test(phone)) { wx.showToast({ title: '手机号格式有误', icon: 'error' }); return false; } this.setData({ sendText: '60s后再次发送', disabled: true }); this.timeTask(); util.post({ url: '/api/sms/sendSms/' + that.data.phone, success: (res) => { wx.hideLoading(); console.info(res); if (res.code != 200) { util.toast(res.data.msg); } else { util.toast(res.data.msg); } }, }); }, /** * 60s倒计时定时器 */ timeTask: function () { let that = this; let i = 60; let timer = setInterval(function () { i--; if (i < 1) { that.setData({ sendText: '发送验证码', disabled: false }); clearInterval(timer); } else { that.setData({ sendText: i + 's后再次发送' }); } }, 1000); }, /** * 登录系统 */ loginSys: function (e) { let that = this; let values = e.detail.value; if (!values.phone) { wx.showToast({ title: '请输入手机号', icon: 'error' }); return false; } let reg = /1[3-9]\d{9}/; if (!reg.test(values.phone)) { wx.showToast({ title: '手机号格式有误', icon: 'error' }); return false; } if (!values.code) { wx.showToast({ title: '请输入验证码', icon: 'error' }); return false; } values.type = 'CAPTCHA'; util.post({ url: '/login/mobile', data: values, header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: (res) => { console.info(res); let code = res.data.code; if ('200' != code) { wx.showToast({ title: res.data.msg, icon: 'error' }); } else { app.globalData.Authorization = res.data.data.Authorization; that.getUserInfo(); } } }); }, /** * 登录验证通过后获取用户信息和角色信息 */ getUserInfo: function () { let that = this; util.get({ url: '/sys/userInfoAndRole', success: (res) => { wx.hideLoading(); if (res.data.code != 200) { util.toast(res.data.msg); } else { let datas = res.data.data; console.info(datas); app.globalData.userInfo = datas; app.globalData.roleInfo = datas.roleList[0]; let roleList = that.data.roleList; for (let i in roleList) { if (roleList[i].code == datas.roleList[0].code) { wx.redirectTo({ url: roleList[i].page }); break; } } } } }); }, /** * 微信获取手机号快捷登录 * @param {*} res */ getPhoneNumber: function (res) { let that = this; //用户按了允许授权按钮 if (res.detail.encryptedData) { util.post({ url: '/login/mobile', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: { code: res.detail.code, type: 'QUICK' }, success: (res) => { wx.hideLoading(); console.info(res) if (res.data.code != 200) { util.toast(res.data.msg); } else { app.globalData.Authorization = res.data.data.Authorization; that.getUserInfo(); } } }); } else {//用户按了拒绝按钮 wx.showModal({ title: '警告', content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!', showCancel: false, confirmText: '返回授权', success: function (res) { if (res.confirm) { console.log('用户点击了“返回授权”'); } } }); } }, })