From e4f61bb43673d0934b549fc865f228188aa03528 Mon Sep 17 00:00:00 2001
From: YM <479443481@qq.com>
Date: 星期三, 27 三月 2024 15:59:30 +0800
Subject: [PATCH] 调整登录

---
 assets/js/login.js |  152 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 96 insertions(+), 56 deletions(-)

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

--
Gitblit v1.9.1