//获取应用实例 const app = getApp() // pages/home/home.js Page({ /** * 页面的初始数据 */ data: { total:0, apiUrl: 'https://mini.nanjingchuangyou.com/addons/cms/wxapp.', }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.check(function(ret) {}); this.getdata(); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, jump:function(){ wx.navigateTo({ url: '/page/systemTest/systemTest', success: function(res) { }, fail: function(res) { }, complete: function(res) { }, }) }, //data getdata:function(){ var that=this; this.setData({ loading: true }); app.request('/api/mini/getTotal', { }, function (data, ret) { if(ret.code == 1){ that.setData({ loading: false, total:ret.data, }); } typeof cb == 'function' && cb(data); }, function (data, ret) { that.setData({ loading: false }); app.error(ret.msg); },2); }, check: function(cb) { var that = this; if (app.globalData.userInfo) { wx.setStorageSync("userInfo", app.globalData.userInfo); typeof cb == "function" && cb(app.globalData.userInfo); } else { wx.getSetting({ success: function(res) { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserProfile({ withCredentials: true, success: function(res) { that.login(cb); }, fail: function() { that.showLoginModal(cb); } }); } else { that.showLoginModal(cb); } }, fail: function() { //that.showLoginModal(cb); } }); //this.login(cb); } }, getUserInfo:function(){ return new Promise((resolve,reject) => { wx.getUserProfile({ desc: '用户登录', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { resolve(res) }, fail:(err) => { reject(err) } }) }) }, getLogin:function(){ return new Promise((resolve,reject) => { wx.login({ success (res) { resolve(res) }, fail: (err) => { reject(err) } }) }) }, login: function(cb) { var that = this; var token = wx.getStorageSync('token') || ''; let userRes = this.getUserInfo() let loginRes = this.getLogin() Promise.all([userRes,loginRes]).then((res)=>{ console.log(res) wx.request({ url: that.data.apiUrl + 'user/login', data: { code: res[1].code, rawData: res[0].rawData, token: token }, method: 'post', header: { "Content-Type": "application/x-www-form-urlencoded", }, success: function(lres) { var response = lres.data if (response.code == 1) { app.globalData.userInfo = response.data.userInfo; wx.setStorageSync("userInfo", response.data.userInfo); wx.setStorageSync('token', response.data.userInfo.token); typeof cb == "function" && cb(app.globalData.userInfo); } } }); }) }, //显示登录或授权提示 showLoginModal: function(cb) { var that = this; if (!app.globalData.userInfo) { //获取用户信息 wx.getSetting({ success: function(sres) { if (sres.authSetting['scope.userInfo']) { wx.showModal({ title: '温馨提示', content: '当前无法获取到你的个人信息,部分操作可能受到限制', confirmText: "重新登录", cancelText: "暂不登录", success: function(res) { if (res.confirm) { that.login(cb); } else { console.log('用户暂不登录'); app.globalData.userInfo = null; wx.setStorageSync("userInfo", null); } } }); } else { wx.showModal({ title: '温馨提示', content: '当前无法获取到你的个人信息,部分操作可能受到限制', confirmText: "去授权", cancelText: "暂不授权", success: function(res) { if (res.confirm) { wx.navigateTo({ //url: '/page/my/setting?type=getuserinfo', url: '/page/login/login', }); return false; wx.openSetting({ success: function(sres) { that.check(cb); } }); } else { console.log('用户暂不授权'); } } }); } } }); } else { typeof cb == "function" && cb(app.globalData.userInfo); } }, globalData: { userInfo: null, config: null, token: '', indexTabList: [], newsTabList: [], productTabList: [], apiCommonUrl:'https://mini.nanjingchuangyou.com/' } })