From e4f61bb43673d0934b549fc865f228188aa03528 Mon Sep 17 00:00:00 2001 From: YM <479443481@qq.com> Date: 星期三, 27 三月 2024 15:59:30 +0800 Subject: [PATCH] 调整登录 --- pages/home/home.js | 70 +++---------- pages/bindInfo/index.js | 28 +++-- packageBookService/pages/bookServices/list/index.js | 1 assets/js/config.js | 8 assets/js/login.js | 152 +++++++++++++++++++----------- 5 files changed, 134 insertions(+), 125 deletions(-) diff --git a/assets/js/config.js b/assets/js/config.js index b7870cb..82c8347 100644 --- a/assets/js/config.js +++ b/assets/js/config.js @@ -1,8 +1,8 @@ -export const requestCtx = "http://182.92.203.7:3001"; // 璇锋眰鍦板潃 -export const appId = 27; +// export const requestCtx = "http://182.92.203.7:3001"; // 璇锋眰鍦板潃 +// export const appId = 27; // export const requestCtx = "http://jsysf.bnuic.com"; // 璇锋眰鍦板潃 -// export const requestCtx = "https://jsek.bnuic.com" // 璇锋眰鍦板潃 -// export const appId = 3; +export const requestCtx = "https://jsek.bnuic.com" // 璇锋眰鍦板潃 +export const appId = 3; // export const epubUrl = "http://182.92.203.7:3007/epubReadMobile/"; export const epubUrl = "http://jsysf.bnuic.com/epubReadMobile/#/"; export const requestTimeOut = 300000; // 璇锋眰瓒呮椂鏃堕棿 diff --git a/assets/js/login.js b/assets/js/login.js index bf70086..8c5c2c5 100644 --- a/assets/js/login.js +++ b/assets/js/login.js @@ -1,63 +1,103 @@ import { setSessionGuid } from "./userAction" -export const checkLoginInfo = (app, callback) => { - const token = wx.getStorageSync(app.config.tokenKey) - if (!token) { - wx.login({ - success: (res) => { - wx.getUserInfo({ - success: (infoRes) => { - app.MG.identity.checkWeChatAppAccount({ - code: res.code, - appCode: app.config.appRefCode, - encryptedData: infoRes.encryptedData, - iv: infoRes.iv - }).then(loginRes => { - if (!loginRes) { - wx.navigateTo({ - url: "/pages/bindInfo/index?code=" + res.code, - }); - } else { - wx.login({ - success: (res) => { - app.MG.identity.loginByWeChatAppCode({ - code: res.code, - appRefCode: app.config.appRefCode, - platform: "weChatAppCustom", - encryptedData: infoRes.encryptedData, - iv: infoRes.iv - }).then(res => { - if (res && res.status == "Ok") { - wx.setStorageSync(app.config.tokenKey, res.token); - setSessionGuid() - callback(res.token) - } else { - console.log(res); - callback(false) - } - }) - }, - fail: (err) => { - console.log(err); - callback(false) +export const loginInfo = (app, callback) => { + wx.login({ + success: (res) => { + wx.getUserInfo({ + success: (infoRes) => { + app.MG.identity.checkWeChatAppAccount({ + code: res.code, + appCode: app.config.appRefCode, + encryptedData: infoRes.encryptedData, + iv: infoRes.iv + }).then(loginRes => { + if (!loginRes) { + const pages = getCurrentPages(); + const currentPage = pages[pages.length - 1]; + let url = `/${currentPage.route}`; + if (Object.keys(currentPage.options).length) { + let option = ""; + for (const key in currentPage.options) { + if (currentPage.options[key]) { + if (option) { + option += "&" + } + option += key + "=" + currentPage.options[key] } - }) + } + url += "?" + option } - }) - }, - fail: (err) => { - console.log(err); - callback(false) - } - }) - }, - fail: (err) => { - console.log(err); - callback(false) + debugger + console.log(url); + wx.navigateTo({ + url: "/pages/bindInfo/index?page=" + encodeURIComponent(url), + }); + } else { + wx.login({ + success: (res) => { + app.MG.identity.loginByWeChatAppCode({ + code: res.code, + appRefCode: app.config.appRefCode, + platform: "WeChatAppCustom", + encryptedData: infoRes.encryptedData, + iv: infoRes.iv + }).then(res => { + if (res && res.status == "Ok") { + // 鍌ㄥ瓨token + wx.setStorageSync(app.config.tokenKey, res.token); + // 璁板綍鐧诲綍缁熻 + setSessionGuid() + // 鑾峰彇鐢ㄦ埛淇℃伅 + getUserInfo(app, callback, res.token) + } else { + console.log(res); + callback(false) + } + }) + }, + fail: (err) => { + console.log(err); + callback(false) + } + }) + } + }) + }, + fail: (err) => { + console.log(err); + callback(false) + } + }) + }, + fail: (err) => { + console.log(err); + callback(false) + } + }) +} + +// 鑾峰彇鐧诲綍鐢ㄦ埛韬唤 +const getUserInfo = (app, callback, token) => { + app.MG.identity.getCurrentAppUser().then(res => { + // 鐢ㄦ埛淇℃伅浼樺厛绾э細鏁欏笀璁よ瘉 > 寰俊 > 瀛︾敓锛堟敞鍐屾椂榛樿锛� + if (res) { + let defaultUser = {}; + let WeChatInfo = res.infoList.find((item) => item.type === "WeChat"); + let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone') + + if (WeChatInfo) { + defaultUser = { + nickName: WeChatInfo.name, + avatarUrl: WeChatInfo.icon, + weChatId: WeChatInfo.id + } } - }) - } else { - callback(token) - } + if (phoneNumber) { + defaultUser.phoneNumber = phoneNumber.credential + } + wx.setStorageSync(app.config.userInfoKey, JSON.stringify(defaultUser)); + } + callback(token); + }); } \ No newline at end of file diff --git a/packageBookService/pages/bookServices/list/index.js b/packageBookService/pages/bookServices/list/index.js index 4c8d2b5..0f41bda 100644 --- a/packageBookService/pages/bookServices/list/index.js +++ b/packageBookService/pages/bookServices/list/index.js @@ -54,6 +54,7 @@ * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍔犺浇 */ onLoad(options) { + const systInfo = wx.getSystemInfoSync(); const menu = wx.getMenuButtonBoundingClientRect(); // 鑳跺泭淇℃伅 const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 瀵艰埅鏍忛珮搴� diff --git a/pages/bindInfo/index.js b/pages/bindInfo/index.js index 7e317c7..897d56c 100644 --- a/pages/bindInfo/index.js +++ b/pages/bindInfo/index.js @@ -1,4 +1,5 @@ // pages/index/bindInfo/index.js +import { setSessionGuid } from "../../assets/js/userAction" const app = getApp(); Page({ @@ -8,7 +9,7 @@ data: { avatarUrl: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0", nickName: "", - wxCode: null, + redirectPage: "" }, onChooseAvatar(res) { @@ -46,15 +47,24 @@ app.MG.identity.loginByWeChatAppCode({ code: res.code, appRefCode: app.config.appRefCode, - platform: "weChatApp", + platform: "WeChatAppCustom", icon: this.data.avatarUrl, encryptedData: infoRes.encryptedData, iv: infoRes.iv, name: this.data.nickName, }).then(res => { if (res && res.status == "Ok") { + // 鍌ㄥ瓨token wx.setStorageSync(app.config.tokenKey, res.token); + // 璁板綍鐧诲綍缁熻 + setSessionGuid() + // 鑾峰彇鐢ㄦ埛淇℃伅 this.getUserInfo() + } else { + wx.showToast({ + icon: "error", + title: res.message, + }); } }) }, @@ -65,11 +75,9 @@ // 鑾峰彇鐧诲綍鐢ㄦ埛韬唤 getUserInfo() { app.MG.identity.getCurrentAppUser().then(res => { - // console.log(res, "userInfo"); // 鐢ㄦ埛淇℃伅浼樺厛绾э細鏁欏笀璁よ瘉 > 寰俊 > 瀛︾敓锛堟敞鍐屾椂榛樿锛� if (res) { let defaultUser = {}; - // let secretData = res.secretList.find(i => i.type == 'LoginNameAndPassword') let WeChatInfo = res.infoList.find((item) => item.type === "WeChat"); let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone') @@ -80,18 +88,13 @@ weChatId: WeChatInfo.id } } - if (phoneNumber) { defaultUser.phoneNumber = phoneNumber.credential } - this.setData({ - userInfo: defaultUser, - loading: false - }) - wx.setStorageSync(app.config.userInfoKey, JSON.stringify(this.data.userInfo)); + wx.setStorageSync(app.config.userInfoKey, JSON.stringify(defaultUser)); } wx.switchTab({ - url: '/pages/index/index' + url: this.data.redirectPage ? this.data.redirectPage : '/pages/home/home' }) }); }, @@ -101,8 +104,9 @@ */ onLoad(options) { console.log(options, "options"); + console.log(decodeURIComponent(options.page)); this.setData({ - wxCode: options.code + redirectPage: decodeURIComponent(options.page) }) }, diff --git a/pages/home/home.js b/pages/home/home.js index a066913..7b75da0 100644 --- a/pages/home/home.js +++ b/pages/home/home.js @@ -1,6 +1,6 @@ import { fetchHome } from '../../services/home/home'; import moment from 'moment' -import { checkLoginInfo } from '../../assets/js/login'; +import { loginInfo } from '../../assets/js/login'; const app = getApp() Page({ data: { @@ -8,7 +8,6 @@ navBarHeight: '', searchVal: '', isWhite: false, - backUrl: null, userInfo: {}, tabList: [], //鐩綍 current: 1,//杞挱鍥惧弬鏁� @@ -39,7 +38,7 @@ }, - onLoad(options) { + onLoad() { const systInfo = wx.getSystemInfoSync(); const menu = wx.getMenuButtonBoundingClientRect(); // 鑳跺泭淇℃伅 const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 瀵艰埅鏍忛珮搴� @@ -47,24 +46,22 @@ barHeight: systInfo.statusBarHeight, navBarHeight: navBarHeight, }); - if (options.backUrl) { - let backUrl = JSON.parse(decodeURIComponent(options.backUrl)); - if (backUrl.options) { - for (let key in backUrl.options) { - const value = backUrl.options[key] - backUrl.backUrl += `${key}=${value}&` - } - } - this.setData({ - backUrl: backUrl.backUrl - }) - } - // checkLoginInfo(app, (data) => { - // if (data) { - // this.getCurrentUserInfo(); - // } - // }) + // 棣栭〉娴嬭瘯鐧诲綍鍔熻兘锛屽悗缁敞閲� + // 妫�鏌ョ櫥褰曠姸鎬� + const token = wx.getStorageSync(app.config.tokenKey) + if (!token) { + loginInfo(app, (data) => { + // 濡傛灉涓嶆槸绗竴娆$櫥褰曪紝浼氭墽琛屽洖璋� + if (data) { + // 鐧诲綍鎴愬姛锛岃嚜鍔ㄨ褰晅oken鍜岀敤鎴蜂俊鎭紝骞惰繑鍥瀟rue + } else { + // 鍑虹幇閿欒锛岃繑鍥瀎alse + } + }) + } else { + // 濡傛灉鏄涓�娆$櫥褰曪紝浼氳烦杞嚦缁戝畾鐢ㄦ埛淇℃伅椤甸潰锛屽~鍐欏畬鐢ㄦ埛淇℃伅鍚庤繘琛岀櫥褰曞苟鍌ㄥ瓨token鍜岀敤鎴蜂俊鎭紝缁撴潫鍚庤烦杞洖褰撳墠椤甸潰锛堟惡甯﹂〉闈㈠弬鏁帮級 + } this.init(); }, @@ -91,39 +88,6 @@ }, 500); }, - getCurrentUserInfo() { - app.MG.identity.getCurrentAppUser().then(res => { - // 鐢ㄦ埛淇℃伅浼樺厛绾э細鏁欏笀璁よ瘉 > 寰俊 > 瀛︾敓锛堟敞鍐屾椂榛樿锛� - if (res) { - let defaultUser = {}; - let WeChatInfo = res.infoList.find((item) => item.type === "WeChat"); - let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone') - if (WeChatInfo) { - defaultUser = { - nickName: WeChatInfo.name, - avatarUrl: WeChatInfo.icon, - weChatId: WeChatInfo.id - } - } - if (phoneNumber) { - defaultUser.phoneNumber = phoneNumber.credential - } - this.setData({ - userInfo: defaultUser, - }) - wx.setStorageSync(app.config.userInfoKey, JSON.stringify(this.data.userInfo)); - } - if (this.data.backUrl) { - wx.navigateTo({ - url: this.data.backUrl, - }) - } else { - wx.switchTab({ - url: '/pages/home/home' - }) - } - }); - }, getBannerList() { const list = [] app.MG.resource.getItem({ -- Gitblit v1.9.1