const { $Message} = require('/component/iView/base/index'); const md5 = require('./utils/md5.js'); const secret= "117c0743819088468e590246464cc75e" App({ globalData: { // baseAPI: "http://dev.ndjsxh.cn:8888/weixin/", baseAPI:"http://localhost:8000/weixin/", pageSize: 10, userInfo: {}, }, onLaunch: function() { }, doLogin: function(cb){ let _this = this wx.login({ success(wxres) { if (wxres.code) { _this.formPost('Auth.wxLogin', { "code": wxres.code }).then(res => { if (res.code == 200) { _this.setUserInfo( res.data ) cb && cb( res.data ); } else if (res.code == 401) { wx.reLaunch({ url: '/pages/user/register/index', }); } else { _this.message(res.message, 'error') } }).catch(e => { _this.message(e, 'error') }) } else { _this.message(res.errMsg, 'error') } } }) }, message: function(content, type) { $Message({ content: content, type: type }); }, reLogin( cb ){ this.globalData.userInfo = {}; this.checkLogin( cb ) }, checkLogin: function(cb){ let userInfo = this.globalData.userInfo; if (!userInfo || !userInfo.token) { this.doLogin( cb ) }else{ cb&&cb( userInfo ) } }, formPost: function(action, data) { let _this = this const token = wx.getStorageSync('token') const user_id = wx.getStorageSync('userId')||"" let timestamp = Date.now() action = action.toLowerCase() let signstr=`weixin_${token}_${action}_${timestamp}_${secret}` let signsure = md5.md5( signstr ).toLowerCase() let headers = { 'Content-Type': 'application/json', 'x-signsure': signsure, 'x-timestamp': timestamp, 'x-userId': user_id } return new Promise(function(resolve, reject) { wx.showNavigationBarLoading(); wx.request({ url: _this.globalData.baseAPI + action, header: headers, method: 'POST', data, success(res) { if (res.statusCode !== 200 || typeof res.data !== 'object') { reject('网络出错') return false; } if( res.data.code == 200){ resolve(res.data); return true; } if (res.data.code === 401) { wx.reLaunch({ url: '/pages/user/bind/index', }); return false; } reject(res.data.msg) return false }, fail(res) { reject(res.errMsg) return false; }, complete(res) { wx.hideNavigationBarLoading(); } }) }) }, setUserInfo:function(userInfo){ this.globalData.userInfo = userInfo; wx.setStorageSync('token', userInfo.token ) wx.setStorageSync('userId', userInfo.userId) } })