// pages/login/login.js const WXAPI = require('../../utils/request') const app=getApp() import md5 from "../../utils/ICINPakage/md5.min"; import moment from "../../utils/moment" Page({ /** * 页面的初始数据 */ data: { value: "", password: '', username: "", passwordOne: '', usernameOne: "", code: '', columns: ['请选择客户','测试', 'SZ', 'FJ'], index:'0', active:0, corid:'', checked:false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log(options,'options') // options.corid="ww02d28bea4b6efc1c" var username=wx.getStorageSync('username')||'' var password=wx.getStorageSync('password')||'' var usernameOne=wx.getStorageSync('usernameOne')||'' var passwordOne=wx.getStorageSync('passwordOne')||'' var checked=wx.getStorageSync('checked')||false this.setData({ username,password,usernameOne,passwordOne,checked, corid:options.corid }) // this.noCodeLogin() }, refresh:WXAPI.throttle(function(e){ this.noCodeLogin() },1000), dlFunction(){ this.setData({ corid:"ww02d28bea4b6efc1c" }) this.noCodeLogin() }, noCodeLogin(){ var that=this if(this.data.corid){ wx.login({ success (res) { if (res.code) { var rqTime=new Date().getTime() var key=rqTime+that.data.corid var str="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXKHkNKieiGOTjXZJ0lFBjhRCaZTaAebTIfB9nnnVmNVIp//yCfyFXSlxbAj58CQwCVhwy109ROYf3BcsQNMZz7Djo8a5V0JThNye4ihxLiYLawLRjb2y2/IhzV0/JZiFtaM83xUt0tbqTKxD00Up7dy8c9OXc0h//0uQSraf6JwIDAQAB" var signature=WXAPI.setCode(str, key) var data={ code:res.code, rqTime:rqTime, signature:signature } WXAPI.toWXCorpUser(data,'0').then(res2 => { if(res2.success){ wx.setStorageSync('signature', res2.data.signature) wx.setStorageSync('deviceinfoList', res2.data.data) wx.setStorageSync('userRole', '1') wx.navigateTo({ url: '/pages/indexUser/indexUser', }) }else{ wx.showToast({ title: res2.message, duration:2000, icon:'none' }) } }) } else { console.log('登录失败!' + res.errMsg) } } }) } }, // 记住密码 checkboxChange() { this.setData({ checked: !this.data.checked }) }, // 获取电话 getPhoneNumber(e){ console.log(e.detail) }, showPopup() { this.setData({ show: true }); }, onChangeActive(event){ this.setData({ active:event.detail.index }) }, onClose() { this.setData({ show: false }); }, bindPickerChange: function(e) { if(e.detail.value == 1){ wx.setStorageSync('tenantId', 100) }else if(e.detail.value == 2){ wx.setStorageSync('tenantId', 200) }else if(e.detail.value == 3){ wx.setStorageSync('tenantId', 300) } this.setData({ index: e.detail.value }) }, onChange(event) { var username = event.detail if(this.data.active == '0' ){ this.setData({ username:username }) }else if(this.data.active == '1'){ this.setData({ usernameOne:username }) } }, onChange1(event) { // event.detail 为当前输入的值 var password = event.detail if(this.data.active == '0' ){ this.setData({ password:password }) }else if(this.data.active == '1'){ this.setData({ passwordOne:password }) } }, // 登录提交 loginIn() { var _this = this if(this.data.active==0){ WXAPI.oauthKey('').then(res => { if (res.success) { var data = { client_id: "iot-client", client_secret: WXAPI.setCode(res.data, "iot-client"), grant_type: "password", scope: "all", username: this.data.username, password: WXAPI.setCode(res.data, this.data.password) } if (this.data.username == '' || this.data.password == '') { wx.showToast({ title: "请输入账号和密码!", duration: 2000, icon: "none" }) } else { WXAPI.login(data).then(res => { if(res.access_token){ wx.setStorageSync('token', res) if (this.data.checked) { wx.setStorageSync('password', this.data.password) wx.setStorageSync('username', this.data.username) wx.setStorageSync('checked', this.data.checked) } else { wx.removeStorageSync('password') wx.removeStorageSync('username') wx.removeStorageSync('checked') } wx.setStorageSync('userRole', '0') app.globalData.token=res.access_token WXAPI.getuserInfo('').then(res=>{ let tenantId=res.data.tenantId.split(',')[0] wx.setStorageSync('tenantId', tenantId) WXAPI.setTenantId(tenantId) wx.setStorageSync("userInfo", res.data) wx.switchTab({ url: '/pages/index/index', }) }) }else{ if(res.message=="Username or password is incorrect"){ wx.showToast({ title: "请输入正确的账号密码", duration:2000, icon:"none" }) }else{ wx.showToast({ title: "未找到该用户", duration:2000, icon:"none" }) } } }) } } }) }else if(this.data.active==1){ if (this.data.usernameOne == '' || this.data.passwordOne == '') { wx.showToast({ title: "请输入账号和密码!", duration: 2000, icon: "none" }) } else { var rqTime=moment().format("X") var str=`${this.data.usernameOne}+${this.data.passwordOne}+${rqTime}` var signature=md5(str) var data = { signature:signature, rqTime:rqTime, number:this.data.usernameOne } WXAPI.atCardUser(data).then(res => { console.log(res); if(res.code==200){ let token={ access_token:signature } if (this.data.checked) { wx.setStorageSync('passwordOne', this.data.passwordOne) wx.setStorageSync('usernameOne', this.data.usernameOne) wx.setStorageSync('checked', this.data.checked) } else { wx.removeStorageSync('passwordOne') wx.removeStorageSync('usernameOne') wx.removeStorageSync('checked') } // wx.setStorageSync('password', this.data.password) // wx.setStorageSync('username', this.data.username) wx.setStorageSync('signature', signature) wx.setStorageSync('token', token) wx.setStorageSync('deviceinfoList', res.data) wx.setStorageSync('userRole', '1') wx.navigateTo({ url: '/pages/indexUser/indexUser', }) }else{ wx.showToast({ title: res.message, icon:"none" }) } }) } } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })