|
@@ -13,6 +13,9 @@ import {
|
|
|
wxLogin,
|
|
wxLogin,
|
|
|
saveLogin
|
|
saveLogin
|
|
|
} from '../../services/wx.js'
|
|
} from '../../services/wx.js'
|
|
|
|
|
+import {
|
|
|
|
|
+ showToast
|
|
|
|
|
+} from '../../utils/tips.js'
|
|
|
var app = getApp()
|
|
var app = getApp()
|
|
|
Page({
|
|
Page({
|
|
|
data: {
|
|
data: {
|
|
@@ -29,53 +32,104 @@ Page({
|
|
|
wx.checkSession({
|
|
wx.checkSession({
|
|
|
success() {
|
|
success() {
|
|
|
//session_key 未过期,并且在本生命周期一直有效
|
|
//session_key 未过期,并且在本生命周期一直有效
|
|
|
|
|
+ getWxCode().then(code => {
|
|
|
|
|
+ return wxLogin({
|
|
|
|
|
+ code: code
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ .then(res => {
|
|
|
|
|
+ console.log("dd", res)
|
|
|
|
|
+ app.globalData.openId = res.data.openId ? res.data.openId : '';
|
|
|
|
|
+ app.globalData.sessionKey = res.data.sessionKey ? res.data.sessionKey : '';
|
|
|
|
|
+ })
|
|
|
|
|
+ .then(data => {
|
|
|
|
|
+ if (e.detail.userInfo) {
|
|
|
|
|
+ return getUserInfo()
|
|
|
|
|
+ } else {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .then(res => {
|
|
|
|
|
+ console.log("ddf", res)
|
|
|
|
|
+ if (res) {
|
|
|
|
|
+ saveLogin({
|
|
|
|
|
+ encryptedData: encodeURIComponent(res.encryptedData),
|
|
|
|
|
+ iv: encodeURIComponent(res.iv),
|
|
|
|
|
+ openid: app.globalData.openId,
|
|
|
|
|
+ session_key: app.globalData.sessionKey
|
|
|
|
|
+ }).then(data => {
|
|
|
|
|
+ console.log("save", data)
|
|
|
|
|
+ wx.setStorageSync("userInfo", res.userInfo);
|
|
|
|
|
+ wx.setStorageSync("hpuser", data.data.hpUser);
|
|
|
|
|
+ wx.setStorageSync("hpMember", data.data.hpMember);
|
|
|
|
|
+ wx.setStorageSync("user_id", data.data.hpUser.id);
|
|
|
|
|
+ wx.setStorageSync("member_id", data.data.hpMember.id);
|
|
|
|
|
+ wx.setStorageSync("user_token", data.data.hpUser.userToken);
|
|
|
|
|
+ // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
|
|
|
|
|
+ // 所以此处加入 callback 以防止这种情况
|
|
|
|
|
+ if (app.userInfoReadyCallback) {
|
|
|
|
|
+ app.userInfoReadyCallback(res)
|
|
|
|
|
+ }
|
|
|
|
|
+ showToast("登录成功");
|
|
|
|
|
+ setTimeout(function() {
|
|
|
|
|
+ wx.navigateBack();
|
|
|
|
|
+ }, 500)
|
|
|
|
|
+ }).catch(data => {
|
|
|
|
|
+ console.log("fgh", data)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
},
|
|
},
|
|
|
fail() {
|
|
fail() {
|
|
|
// session_key 已经失效,需要重新执行登录流程
|
|
// session_key 已经失效,需要重新执行登录流程
|
|
|
|
|
+ getWxCode().then(code => {
|
|
|
|
|
+ return wxLogin({
|
|
|
|
|
+ code: code
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ .then(res => {
|
|
|
|
|
+ console.log("dd", res)
|
|
|
|
|
+ app.globalData.openId = res.data.openId ? res.data.openId : '';
|
|
|
|
|
+ app.globalData.sessionKey = res.data.sessionKey ? res.data.sessionKey : '';
|
|
|
|
|
+ })
|
|
|
|
|
+ .then(data => {
|
|
|
|
|
+ if (e.detail.userInfo) {
|
|
|
|
|
+ return getUserInfo()
|
|
|
|
|
+ } else {
|
|
|
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .then(res => {
|
|
|
|
|
+ if (res) {
|
|
|
|
|
+ saveLogin({
|
|
|
|
|
+ encryptedData: encodeURIComponent(res.encryptedData),
|
|
|
|
|
+ iv: encodeURIComponent(res.iv),
|
|
|
|
|
+ openid: app.globalData.openId,
|
|
|
|
|
+ session_key: app.globalData.sessionKey
|
|
|
|
|
+ }).then(data => {
|
|
|
|
|
+ console.log("save", data)
|
|
|
|
|
+ wx.setStorageSync("userInfo", res.userInfo);
|
|
|
|
|
+ wx.setStorageSync("hpuser", data.data.hpUser);
|
|
|
|
|
+ wx.setStorageSync("hpMember", data.data.hpMember);
|
|
|
|
|
+ wx.setStorageSync("user_id", data.data.hpUser.id);
|
|
|
|
|
+ wx.setStorageSync("member_id", data.data.hpMember.id);
|
|
|
|
|
+ wx.setStorageSync("user_token", data.data.hpUser.userToken);
|
|
|
|
|
+ // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
|
|
|
|
|
+ // 所以此处加入 callback 以防止这种情况
|
|
|
|
|
+ if (app.userInfoReadyCallback) {
|
|
|
|
|
+ app.userInfoReadyCallback(res)
|
|
|
|
|
+ }
|
|
|
|
|
+ showToast("登录成功");
|
|
|
|
|
+ setTimeout(function() {
|
|
|
|
|
+ wx.navigateBack();
|
|
|
|
|
+ }, 500)
|
|
|
|
|
+ }).catch(data => {
|
|
|
|
|
+ console.log("fgh", data)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
- if (e.detail.userInfo) {
|
|
|
|
|
- //用户按了允许授权按钮
|
|
|
|
|
- getUserInfo().then(res => {
|
|
|
|
|
- saveLogin({
|
|
|
|
|
- encryptedData: encodeURIComponent(res.encryptedData),
|
|
|
|
|
- iv: encodeURIComponent(res.iv),
|
|
|
|
|
- openid: app.globalData.openId,
|
|
|
|
|
- session_key: app.globalData.sessionKey
|
|
|
|
|
- }).then(data => {
|
|
|
|
|
- console.log(data)
|
|
|
|
|
- app.globalData.userId = data.data.hpUser.id ? data.data.hpUser.id : '';
|
|
|
|
|
- app.globalData.userToken = data.data.hpUser.userToken ? data.data.hpUser.userToken : '';
|
|
|
|
|
- app.globalData.userInfo = res.userInfo;
|
|
|
|
|
- updataStorageData('shareToken', data.data.hpUser.userToken || '') //用户识别码
|
|
|
|
|
- wx.setStorageSync("hpuser", data.data.hpUser);
|
|
|
|
|
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
|
|
|
|
|
- // 所以此处加入 callback 以防止这种情况
|
|
|
|
|
- if (app.userInfoReadyCallback) {
|
|
|
|
|
- app.userInfoReadyCallback(res)
|
|
|
|
|
- }
|
|
|
|
|
- }).catch(data => {
|
|
|
|
|
- console.log(data)
|
|
|
|
|
- })
|
|
|
|
|
- wx.navigateBack()
|
|
|
|
|
- }).catch(data => {
|
|
|
|
|
- console.log(data)
|
|
|
|
|
- })
|
|
|
|
|
- } else {
|
|
|
|
|
- //用户按了拒绝按钮
|
|
|
|
|
- wx.showModal({
|
|
|
|
|
- title: '提示',
|
|
|
|
|
- content: '您点击了拒绝授权,将无法正常使用该小程序,请授权之后再使用。',
|
|
|
|
|
- showCancel: false,
|
|
|
|
|
- confirmText: '返回授权',
|
|
|
|
|
- success: function(res) {
|
|
|
|
|
- if (res.confirm) {
|
|
|
|
|
- console.log('用户点击了“返回授权”')
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
})
|
|
})
|