// 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", 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 => { if (!!res) { // this.globalData({}) console.log(this.globalData) } }).catch(res => this.globalData.error(res)) // 请求用户地址 this.globalData["useraddress"] = { name: "张三", tel: "188xxxx5629", lock: true, //默认 address: "北京北京市房山区东海道场" }; }, globalData: { // 定义tabber tabber: { tabberlist: [{ name: '首页', url: "/pages/index/index", //对应路径 icon: "home-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: {}, // 全局请求方法 axios ({ url: _url = '', method: _method = 'post', data: _data = {}, success: _success = () => { }, complete: _complete = () => { } }) { let globalurl = "http://192.168.2.45:8887", error = (data) => { console.log(data) }; return new Promise(resolve => { wx.request({ url: globalurl + _url, method: _method, header: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': !wx.getStorageSync('token') ? '' : wx.getStorageSync('token') }, success (res) { if (!!res.data.msg) { error(res.data.msg); return false; } _success(res) }, data: _data, fail () { error("请求失败") }, complete () { _complete(); resolve() } }) }) }, } })