diff --git a/app.js b/app.js index dc80862..bb735f7 100644 --- a/app.js +++ b/app.js @@ -10,25 +10,7 @@ App({ var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) - // 获取用户信息 - wx.getSetting({ - success: res => { - if (res.authSetting['scope.userInfo']) { - // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 - wx.getUserInfo({ - success: res => { - // 可以将 res 发送给后台解码出 unionId - getApp().globalData.userInfo = res.userInfo - // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 - // 所以此处加入 callback 以防止这种情况 - if (getApp().userInfoReadyCallback) { - getApp().userInfoReadyCallback(res) - } - } - }) - } - } - }) + // 登录 wx.login({ success: res => { @@ -75,6 +57,7 @@ App({ // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { + console.log("userInfo" + res.userInfo) // 可以将 res 发送给后台解码出 unionId that.globalData.userInfo = res.userInfo; // this.onLoginUser(); diff --git a/pages/index/index.js b/pages/index/index.js index bc57ccb..446bf51 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -26,7 +26,7 @@ Page({ img1: '/images/title-icon.png', cityId: 440100000, typeNum: 10, - addrArray: { id: [440100000, 430426000], name: ['广州', '深圳', '上海', '长沙', '武汉', '杭州', '北京'] }, + addrArray: { id: [440100000, 430426000], name: ['长沙', '深圳', '上海', '长沙', '武汉', '杭州', '北京'] }, addrIndex: 0, }, /** @@ -48,7 +48,7 @@ Page({ url: app.gw.hostUrl + '/mall/wxa/activity/banner', method: 'get', data: { - merchantNo: 2 + merchantNo: 'SH180331153109966163' }, header: { 'Content-Type': 'application/x-www-form-urlencoded' @@ -88,7 +88,8 @@ Page({ url: app.gw.hostUrl + '/mall/wxa/activity/list', method: 'get', data: { - selectedCityId: that.data.addrArray.id[that.data.addrIndex] == undefined ? 0 : that.data.addrArray.id[that.data.addrIndex], + merchantNo: 'SH180331153109966163', + // selectedCityId: that.data.addrArray.id[that.data.addrIndex] == undefined ? 0 : that.data.addrArray.id[that.data.addrIndex], pageNum: that.data.pageNum, pageSize: that.data.pageSize }, diff --git a/pages/login/login.js b/pages/login/login.js index 3e833bb..e2137a6 100644 --- a/pages/login/login.js +++ b/pages/login/login.js @@ -138,6 +138,7 @@ Page({ success: res => { // 可以将 res 发送给后台解码出 unionId app.globalData.userInfo = res.userInfo + // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (app.userInfoReadyCallback) { @@ -171,6 +172,8 @@ Page({ that.ohShitfadeOut(); return; } + // 发送 res.code 到后台换取 openId, sessionKey, unionId + that.commitUserInfo(that.globalData.openid, res.encryptedData, res.iv); app.globalData.sessionId = data.response.sessionId; wx.setStorageSync('sessionId', data.response.sessionId); console.log('sessionId : ' + data.response.sessionId); @@ -199,6 +202,41 @@ Page({ } } }, + commitUserInfo: function (openid, encryptedData, iv) { + var that = this; + wx.request({ + url: this.gw.hostUrl + '/mall/wxa/auth/userinfo', + method: 'POST', + data: { + openid: openid, + encryptedData: encryptedData, + iv: iv + }, + header: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + success: function (res) { + //--init data + var data = res.data; + var status = data.code; + if (status != 0) { + wx.showToast({ + title: data.msg, + icon: 'none', + duration: 3000 + }); + return false; + } + }, + fail: function (e) { + wx.showToast({ + title: '网络异常!err:authlogin', + icon: 'none', + duration: 2000 + }); + }, + }); + }, onLoad: function (options) { if (app.globalData.userInfo) { this.setData({