// 导入TDesign弹窗 import Toast from 'tdesign-miniprogram/toast/index'; const app = getApp();//新建页面时 默认引入 const req = app.simpleReq();//初始化一个的request() 实例 // 导入RSA加密包 import WxmpRsa from 'wxmp-rsa' Page({ data: { }, // 此处为周期事件 /** * 生命周期函数--监听页面加载 */ onLoad(options) { if(options&&options.sn&&options.sn!=null&&options.sn!=''){ console.log('有数据'+options.sn) wx.showToast({ title:options.sn, icon: 'none', duration: 5000 }) this.login('nfc',options.sn) }else{ console.log('无数据') } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /*---------------------------------------------*/ //以下为自定义事件 /**跳转到获取晶石页面 */ go2Get(){ // wx.navigateTo({ // url: '/pages/index/out/out' // }) console.log('跳转获取晶石页面'); }, /** * 登录失败提示 */ loginErrorToast(mode) { Toast({ context: this, selector: '#t-toast', message: mode=='scan'?'扫码登录失败\n 二维码有误':'精灵链接失败\n 请再试一次', theme: 'error', direction: 'column', }); }, /**扫码事件 */ scanCodeEvent(){ var that = this; var DEV_CODE = "" //扫码代码 wx.scanCode({ onlyFromCamera: true,// 只允许从相机扫码 success(res){ that.login('scan',res.result) }, }) }, /** * 登录事件, * mode为模式参数,scan为扫码,nfc为nfc模式 * code为用户码参数,用户码为空则代表为扫码登录 */ login(mode,code){ //如果有token,直接进入 if(wx.getStorageSync('token')){ wx.showToast({ title:'有token'+wx.getStorageSync('token'), icon: 'none', duration: 5000 }) wx.switchTab({ url: '/pages/home/xy/index' }) } wx.showToast({ title:'没有token', icon: 'none', duration: 5000 }) console.log('进入登录了!!!') var DEV_CODE = code var PUBLIC_KEY = "" req.getRequest('/api/dict/value/login-public-key',{}).then((res)=>{ console.log(res,'11111111'); if(res.data.code==200){//获取公钥成功 PUBLIC_KEY = res.data.data // 实例化rsa const rsa = new WxmpRsa() // 设置公钥 rsa.setPublicKey(PUBLIC_KEY) // 加密 const mac = rsa.encryptLong(DEV_CODE) // 此处执行登录获取token操作 req.postRequest('/api/user/login',{'mac':mac}).then((res)=>{ if(res.data.code==200){ wx.setStorageSync('token', res.data.data) // 设置当前设备码 wx.setStorageSync('code', DEV_CODE) // 设置为初次识别NFC wx.setStorageSync('nfc', false) wx.showToast({ title:mode=='scan'?'扫码登录成功':'NFC识别成功', icon: 'none', duration: 1000 }) this.getLetter() }else if(res.data.code==500){ this.loginErrorToast(mode) }else{ this.loginErrorToast(mode) } } ) }else{ wx.showToast({ title:'扫码失败,请确认您的二维码是否正确!', icon: 'none', duration: 1000 }) } }).catch((err)=>{ console.log(err); }) }, // 判断是否用未读信札 getLetter(){ console.log(wx.getStorageSync('token')); wx.request({ url: 'https://xzjl-api.windymuse.cn/api/user/curt', header: {'Authorization': wx.getStorageSync('token')}, method: 'GET', success: res => { console.log(res.data.data.letterStatus); const letterStatus = res.data.data.letterStatus // 这里可以判断是否有未读信札 if (letterStatus === 'sending') { console.log('跳转信札页面'); wx.redirectTo({ url: '/pages/xz/index' }) }else { wx.switchTab({ url: '/pages/home/xy/index' }) } } }) } })