// app.js App({ onLaunch () { // 用户默认登录 // 微信小程序流程 function user (app) { return new Promise((resolve, reject) => { const accountinfo = wx.getAccountInfoSync(); app.globalData.appid = accountinfo.miniProgram.appId; // login登录 wx.login({ success: res => { let code = res.code; app.globalData.code = code; // 账号信息 wx.getUserInfo({ success: function (res) { let { encryptedData, iv, userInfo } = res; app.globalData.encryptedData = encryptedData; app.globalData.iv = iv; app.globalData.avatarUrl = userInfo.avatarUrl; resolve(); } }) }, fail: function () { reject("wx.login获取失败") }, }) }) } //用户自动登录 function login (app) { return new Promise((resolve, reject) => { let { code, appid } = app.globalData, token = ''; app.globalData.axios({ url: "/account/code2Session/", method: "post", token: false, data: { code, appid }, success (res) { wx.setStorage({ data: '', key: 'token', }) try { token = res.data.data.token; } catch (err) { }; !!token ? wx.setStorage({ data: 'JWT ' + token, key: 'token', }) : ''; let data = res.data.data; let user = { lock: !!token, ...data } app.globalData = { ...app.globalData, user }; resolve(token); } }) }) } user(this).then(res => login(this)).then(res => { this.globalData.codeSession = true; }).catch(res => { if (wx.hasOwnProperty("lxd") && wx.lxd.hasOwnProprety("Notify")) { wx.lxd.Notify({ message: res, icon: "warn-o" }) } else { wx.showModal({ confirmText: '确认', content: res, showCancel: false, title: '注意', }) } } ) }, globalData: { // 定义tabber tabber: { tabberlist: [{ name: '我要订餐', url: "/pages/index/index", //对应路径 icon: "hot-o",//不自定义图片,使用vant的图标 }, { name: '请假管理', url: "/pages/leave/leave", //对应路径 icon: "todo-list-o",//不自定义图片,使用vant的图标 }, { name: "我的", url: "/pages/login/login", src: "", icon: "friends-o", }],//跳转链接 active: 0,//默认活动窗口 activeColor: "#1989fa",//选中时的颜色 inactiveColor: "#7d7e80",//未选中时的颜色 }, // 用户登录状态 user: { lock: false, name: '未登录', tel: "", face: "https://img.yzcdn.cn/vant/cat.jpeg", }, // 支付时,用户地址 useraddress: { id: null }, // 广告 hot: true, // 全局请求方法 axios ({ url: _url = '', token: _token = true, method: _method = 'post', data: _data = {}, success: _success = () => { }, complete: _complete = () => { } }) { let globalurl = "http://192.168.2.45:8888", error = (data) => { if (wx.hasOwnProprety("lxd") && wx.lxd.hasOwnProperty("Notify")) { wx.lxd.Notify({ message: data, icon: "warn-o" }) } else { wx.showModal({ confirmText: '确认', content: data, showCancel: false, title: '注意', }) } }; globalurl = "https://lsr.zzly.vip"; let header_token = {}; if (_token) header_token = { 'Authorization': !wx.getStorageSync('token') ? '' : wx.getStorageSync('token') }; return new Promise(resolve => { wx.request({ url: globalurl + _url, method: _method, header: { 'Content-Type': 'application/x-www-form-urlencoded', ...header_token }, success (res) { if (!!res.data.msg) { error(res.data.msg); return false; } _success(res) }, data: _data, fail () { error("请求失败") }, complete () { _complete(); resolve() } }) }) }, // 用户登录状态 token () { let state = wx.getStorageSync('token'); return !!state; } } })