//app.js App({ onLaunch: function() { var baseInfo = wx.getStorageSync('laomenkuangBaseInfo'); if (!baseInfo) { // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId var that = this; wx.request({ url: that.globalData.apiUrl + 'openid', header: { 'code': res.code, 'content-type': 'application/json' // 默认值 }, success(res) { res = res.data; if (res.code == 200) { that.globalData.baseInfo = res.data; wx.setStorageSync('laomenkuangBaseInfo', res.data) that.getInfo() } else { wx.showToast({ title: res.message, icon: 'none', duration: 2000 }) } } }) } }) } else { this.globalData.baseInfo = baseInfo this.getInfo() } // 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId // console.log(res.userInfo) this.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }) } } }) }, setUserInfo(userInfo){ userInfo.nickname = userInfo.nickName userInfo.headimgurl = userInfo.avatarUrl return userInfo }, getInfo: function() { var accountInfo = wx.getStorageSync('laomenkuangAccountInfo'); if (accountInfo){//已有账户信息-已登录 this.globalData.userState = 1; this.globalData.storeData = accountInfo; if (this.userStateReadyCallback) { this.userStateReadyCallback(1) } var userInfo = wx.getStorageSync('laomenkuangUserInfo'); if (userInfo){ this.globalData.userInfo = userInfo; } }else{ this.requestGet('getinfo', "", res => { if (res.code == 200) { this.globalData.userState = 1; wx.setStorageSync('laomenkuangAccountInfo', res.data);//老门框账户信息-id this.globalData.storeData = res.data; if (res.data.user_info) { res.data.user_info.nickName = res.data.user_info.nickname res.data.user_info.avatarUrl = res.data.user_info.headimgurl } wx.setStorageSync('laomenkuangUserInfo', res.data.user_info);//老门框用户信息-头像昵称 this.globalData.userInfo = res.data.user_info; } else { this.globalData.userState = 0; } if (this.userStateReadyCallback) { this.userStateReadyCallback(this.globalData.userState) } }) } }, requestGet: function(url, data, callBack) { var that=this; wx.request({ url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址 data: data, header: { 'openid': this.globalData.baseInfo.openid, 'token': this.globalData.couponToken, 'content-type': 'application/json' // 默认值 }, success(res) { if(res.data.code==-1003){ that.globalData.userState=0; wx.navigateTo({ url: '/pages/login/login' }) }else{ callBack(res.data) } } }) }, requestPost: function(url, data, callBack) { var that=this; wx.request({ url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址 data: data, header: { 'openid': this.globalData.baseInfo.openid, 'token': this.globalData.couponToken, 'content-type': 'application/x-www-form-urlencoded' // 默认值 }, method: "POST", success(res) { if(res.data.code==-1003){ that.globalData.userState=0; wx.navigateTo({ url: '/pages/login/login' }) }else{ callBack(res.data) } } }) }, globalData: { userInfo: null, userState: -1, //-1未获取 0未注册 1已注册 storeData:null,//店铺信息 apiUrl: 'https://laomenkuang.jiyou-tech.com/apiWxAdmin/', baseInfo: null, couponData:null,//优惠券信息 couponToken:null,//优惠券Token } })