From 3bbd80a63ac7728ac01b641a48a26befcb171a0f Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期一, 15 十二月 2025 07:19:21 +0800
Subject: [PATCH] feat:增加企业微信登录判断
---
app/pages/index.vue | 2
app/pages/login.vue | 88 ++++++++---
app/utils/wechat.js | 106 +++++++++++++++
app/pages/qylogin.vue | 139 ++++++++-----------
app/App.vue | 56 -------
5 files changed, 232 insertions(+), 159 deletions(-)
diff --git a/app/App.vue b/app/App.vue
index 15eb1ac..dd3a43f 100644
--- a/app/App.vue
+++ b/app/App.vue
@@ -4,6 +4,7 @@
import { getToken } from '@/utils/auth'
import { getUnreadCount } from '@/api/message'
import storage from '@/utils/storage'
+ import { redirectToLoginByEnvironment } from '@/utils/wechat'
export default {
data() {
@@ -87,59 +88,8 @@
// 妫�鏌ョ櫥褰曠姸鎬佸苟鑷姩璺宠浆鍒板悎閫傜殑鐧诲綍椤甸潰
checkLoginAndRedirect(options) {
if (!getToken()) {
- // 妫�鏌ヨ繍琛岀幆澧冨苟璺宠浆鍒板搴旂殑鐧诲綍椤甸潰
- // #ifdef MP-WEIXIN
- // 鍦ㄥ井淇″皬绋嬪簭鐜涓�
- try {
- // 鑾峰彇绯荤粺淇℃伅
- const systemInfo = uni.getSystemInfoSync()
- console.log('绯荤粺淇℃伅:', systemInfo)
-
- // 妫�鏌nvironment瀛楁鏄惁涓簑xwork
- if (systemInfo.environment === 'wxwork') {
- console.log('妫�娴嬪埌浼佷笟寰俊鐜锛岃烦杞埌浼佷笟寰俊鍏嶇櫥椤甸潰')
- // 鏋勯�犲甫鍙傛暟鐨刄RL
- let url = '/pages/qylogin'
- if (options && options.query) {
- const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
- if (queryParams) {
- url += '?' + queryParams
- }
- }
- this.$tab.reLaunch(url)
- return
- } else {
- console.log('妫�娴嬪埌鏅�氬井淇$幆澧冿紝璺宠浆鍒板井淇$櫥褰曢〉闈�')
- // 鏋勯�犲甫鍙傛暟鐨刄RL
- let url = '/pages/login'
- if (options && options.query) {
- const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
- if (queryParams) {
- url += '?' + queryParams
- }
- }
- this.$tab.reLaunch(url)
- return
- }
- } catch (e) {
- console.error('鑾峰彇绯荤粺淇℃伅澶辫触:', e)
- // 榛樿璺宠浆鍒版櫘閫氱櫥褰曢〉闈�
- this.$tab.reLaunch('/pages/login')
- }
- // #endif
-
- // #ifndef MP-WEIXIN
- // 闈炲井淇″皬绋嬪簭鐜锛岃烦杞埌鏅�氱櫥褰曢〉闈�
- console.log('闈炲井淇″皬绋嬪簭鐜锛岃烦杞埌鏅�氱櫥褰曢〉闈�')
- let url = '/pages/login'
- if (options && options.query) {
- const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
- if (queryParams) {
- url += '?' + queryParams
- }
- }
- this.$tab.reLaunch(url)
- // #endif
+ // 浣跨敤宸ュ叿绫绘牴鎹幆澧冭嚜鍔ㄨ烦杞埌鍚堥�傜殑鐧诲綍椤甸潰
+ redirectToLoginByEnvironment(options, this.$tab);
}
},
diff --git a/app/pages/index.vue b/app/pages/index.vue
index 1260e73..f1e687a 100644
--- a/app/pages/index.vue
+++ b/app/pages/index.vue
@@ -279,7 +279,7 @@
this.hasSubscribed = true;//subscribeManager.checkLocalSubscribeStatus();
// 鑷姩璁㈤槄锛堝鏋滄湭璁㈤槄鍒欐樉绀虹‘璁ゅ脊绐楋級
- this.autoSubscribeOnLaunch();
+ // this.autoSubscribeOnLaunch();
// 鍔犺浇鐢ㄦ埛缁戝畾杞﹁締淇℃伅
this.loadUserVehicle();
diff --git a/app/pages/login.vue b/app/pages/login.vue
index 700706b..8230e42 100644
--- a/app/pages/login.vue
+++ b/app/pages/login.vue
@@ -64,6 +64,7 @@
<script>
import { getCodeImg, loginByOpenId, loginByWechatPhone } from '@/api/login'
+ import { isWxWorkEnvironment,redirectToQyLogin } from '@/utils/wechat'
export default {
data() {
@@ -92,6 +93,9 @@
onLoad(options) {
// 淇濆瓨椤甸潰鍙傛暟
this.pageOptions = options || {}
+ if(isWxWorkEnvironment()){
+ redirectToQyLogin(options,this.$tab);
+ }
},
created() {
this.getCode()
@@ -159,15 +163,8 @@
this.$store.dispatch('GetInfo').then(res => {
// 瑙﹀彂鐧诲綍鎴愬姛浜嬩欢锛屽惎鍔ㄦ秷鎭疆璇�
uni.$emit('user-login')
- // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
- if (this.pageOptions.redirect) {
- // 瑙g爜redirect鍙傛暟
- const redirectUrl = decodeURIComponent(this.pageOptions.redirect)
- this.$tab.reLaunch(redirectUrl)
- } else {
- // 榛樿璺宠浆鍒伴椤�
- this.$tab.reLaunch('/pages/index')
- }
+ // 澶勭悊鑷姩璺宠浆閫昏緫
+ this.redirectAfterLogin()
})
},
@@ -184,6 +181,8 @@
this.isWechat = false
console.log('褰撳墠鐜:闈炲井淇″皬绋嬪簭')
// #endif
+
+
},
// 灏濊瘯鑷姩鐧诲綍(妫�鏌ユ湰鍦版槸鍚︽湁淇濆瓨鐨凮penID)
@@ -251,15 +250,10 @@
const { setToken } = require('@/utils/auth')
setToken(token)
- // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
- if (this.pageOptions.redirect) {
- // 瑙g爜redirect鍙傛暟
- const redirectUrl = decodeURIComponent(this.pageOptions.redirect)
- this.$tab.reLaunch(redirectUrl)
- } else {
- // 榛樿璺宠浆鍒伴椤�
- this.$tab.reLaunch('/pages/index')
- }
+ // 鑾峰彇鐢ㄦ埛淇℃伅骞跺鐞嗚嚜鍔ㄨ烦杞�昏緫
+ this.$store.dispatch('GetInfo').then(() => {
+ this.redirectAfterLogin()
+ });
} else {
this.$modal.msgError(response.msg || '鐧诲綍澶辫触')
}
@@ -297,15 +291,10 @@
const { setToken } = require('@/utils/auth')
setToken(token)
- // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
- if (this.pageOptions.redirect) {
- // 瑙g爜redirect鍙傛暟
- const redirectUrl = decodeURIComponent(this.pageOptions.redirect)
- this.$tab.reLaunch(redirectUrl)
- } else {
- // 榛樿璺宠浆鍒伴椤�
- this.$tab.reLaunch('/pages/index')
- }
+ // 鑾峰彇鐢ㄦ埛淇℃伅骞跺鐞嗚嚜鍔ㄨ烦杞�昏緫
+ this.$store.dispatch('GetInfo').then(() => {
+ this.redirectAfterLogin()
+ });
} else {
// OpenID鏈粦瀹氭垨楠岃瘉澶辫触锛岄渶瑕佽幏鍙栨墜鏈哄彿缁戝畾
console.log('璇penID灏氭湭缁戝畾鎴栭獙璇佸け璐ワ紝闇�瑕佽幏鍙栨墜鏈哄彿')
@@ -317,6 +306,51 @@
console.error('鐧诲綍澶辫触:', error)
this.$modal.msgError('鐧诲綍澶辫触锛岃閲嶈瘯')
})
+ },
+
+ /**
+ * 鑾峰彇URL鍙傛暟
+ */
+ getUrlParam(name) {
+ // #ifdef H5
+ const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
+ const r = window.location.search.substr(1).match(reg);
+ if (r != null) return decodeURIComponent(r[2]);
+ // #endif
+
+ // #ifdef MP-WEIXIN
+ // 鍦ㄥ皬绋嬪簭涓紝鍙傛暟閫氳繃onLoad鐨刼ptions浼犻��
+ if (this.pageOptions && this.pageOptions[name]) {
+ return decodeURIComponent(this.pageOptions[name]);
+ }
+ // #endif
+
+ return null;
+ },
+
+ /**
+ * 鐧诲綍鎴愬姛鍚庣殑璺宠浆澶勭悊
+ */
+ redirectAfterLogin() {
+ try {
+ // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
+ let redirectUrl = this.getUrlParam("redirect");
+
+ if (redirectUrl) {
+ // 瑙g爜redirect鍙傛暟
+ redirectUrl = decodeURIComponent(redirectUrl);
+ console.log("鑷姩璺宠浆鍒版寚瀹氶〉闈�:", redirectUrl);
+ this.$tab.reLaunch(redirectUrl);
+ } else {
+ // 榛樿璺宠浆鍒伴椤�
+ console.log("璺宠浆鍒伴椤�");
+ this.$tab.reLaunch('/pages/index');
+ }
+ } catch (e) {
+ console.error("璺宠浆澶辫触锛屼娇鐢ㄩ粯璁よ烦杞�:", e);
+ // 鍑虹幇寮傚父鏃讹紝榛樿璺宠浆鍒伴椤�
+ this.$tab.reLaunch('/pages/index');
+ }
}
}
}
diff --git a/app/pages/qylogin.vue b/app/pages/qylogin.vue
index 80ff189..dacb592 100644
--- a/app/pages/qylogin.vue
+++ b/app/pages/qylogin.vue
@@ -20,6 +20,7 @@
<script>
import { qyWechatAutoLogin } from "@/api/login";
+import { buildUrlWithParams } from "@/utils/wechat";
export default {
data() {
@@ -40,26 +41,6 @@
},
methods: {
- async getLoginCode() {
- //杩欓噷瑕佽皟鐢╳x.qy.login鑾峰彇code,鏄惁鏈夐泦鎴愬埌uni-app涓憿
- return new Promise((resolve, reject) => {
- wx.qy.login({
- success: (res) => {
- if (res.code) {
- console.log("浼佷笟寰俊灏忕▼搴� ---> code锛�", res.code);
- resolve(res.code); // 杩斿洖 code 缁欏悗绔�
- } else {
- reject(new Error("鑾峰彇 code 澶辫触锛�" + res.errMsg));
- }
- },
- fail: (err) => {
- console.error("wx.qy.login 璋冪敤澶辫触锛�", err);
- // 甯歌澶辫触鍘熷洜锛氶潪浼佷笟寰俊瀹㈡埛绔墦寮�銆佸皬绋嬪簭鏈叧鑱斾紒涓氬井淇$瓑
- reject(new Error("浼佷笟寰俊鐧诲綍鎺ュ彛璋冪敤澶辫触锛�" + err.errMsg));
- },
- });
- });
- },
/**
* 浼佷笟寰俊鍏嶇櫥娴佺▼
*/
@@ -72,13 +53,11 @@
// 鍦ㄥ井淇″皬绋嬪簭鐜涓紝閫氳繃浼佷笟寰俊鍏嶇櫥
console.log("浼佷笟寰俊灏忕▼搴忕幆澧冨厤鐧�");
- // 鑾峰彇URL鍙傛暟涓殑code
+ // 鐩存帴閫氳繃浼佷笟寰俊API鑾峰彇code锛屼笉浣跨敤URL涓殑code
const code = await this.getLoginCode();
if (!code) {
- // 濡傛灉娌℃湁code锛屽皾璇曢�氳繃浼佷笟寰俊API鑾峰彇
- this.handleWxWorkLogin();
- return;
+ throw new Error("鏃犳硶鑾峰彇浼佷笟寰俊鎺堟潈code");
}
// 璋冪敤鍚庣鎺ュ彛杩涜鍏嶇櫥
@@ -89,7 +68,7 @@
const token = response.data.token;
this.$store.commit('SET_TOKEN', token)
- // 蹇呴』璋冪敤setToken淇濆瓨鍒版湰鍦板瓨鍌�
+ // 蹇呴』璋冪敤setToken淇濆瓨鍒版湰鍦板瓨鍌�
const { setToken } = require('@/utils/auth')
setToken(token)
// 鑾峰彇鐢ㄦ埛淇℃伅
@@ -109,10 +88,10 @@
}
// 鑾峰彇URL鍙傛暟涓殑code
- const codeH5 = this.getUrlParam("code");
-
+ let codeH5 = this.getUrlParam("code");
+
+ // 濡傛灉URL涓病鏈塩ode锛屽垯璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
if (!codeH5) {
- // 濡傛灉娌℃湁code锛屽垯璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
this.redirectToWxWorkAuth();
return;
}
@@ -148,56 +127,46 @@
},
/**
- * 澶勭悊浼佷笟寰俊鐧诲綍
+ * 鑾峰彇浼佷笟寰俊鐧诲綍code
*/
- handleWxWorkLogin() {
- // #ifdef MP-WEIXIN
- // 鍦ㄤ紒涓氬井淇″皬绋嬪簭涓紝鍙互鐩存帴璋冪敤浼佷笟寰俊鐧诲綍API
- uni.login({
- provider: "weixin",
- success: (loginRes) => {
- console.log("浼佷笟寰俊鐧诲綍鎴愬姛", loginRes);
- // 璋冪敤鍚庣鎺ュ彛杩涜鍏嶇櫥
- qyWechatAutoLogin(loginRes.code)
- .then((response) => {
- if (response.code === 200) {
- // 鍏嶇櫥鎴愬姛锛屼繚瀛榯oken
- const token = response.data.token;
- this.$store.commit("SET_TOKEN", token);
- uni.setStorageSync("token", token);
-
- // 鑾峰彇鐢ㄦ埛淇℃伅
- this.$store.dispatch("GetInfo").then(() => {
- // 璺宠浆鍒伴椤垫垨鍏朵粬鎸囧畾椤甸潰
- this.redirectAfterLogin();
- });
- } else {
- throw new Error(response.msg || "鍏嶇櫥澶辫触");
- }
- })
- .catch((error) => {
- console.error("鍏嶇櫥澶辫触:", error);
- this.loading = false;
- this.error = true;
- this.errorMessage = error.message || "鍏嶇櫥澶辫触锛岃绋嶅悗閲嶈瘯";
- });
- },
- fail: (err) => {
- console.error("浼佷笟寰俊鐧诲綍澶辫触:", err);
- this.loading = false;
- this.error = true;
- this.errorMessage = "浼佷笟寰俊鐧诲綍澶辫触锛岃绋嶅悗閲嶈瘯";
- },
+ getLoginCode() {
+ return new Promise((resolve, reject) => {
+ // #ifdef MP-WEIXIN
+ // 浣跨敤浼佷笟寰俊灏忕▼搴廇PI鑾峰彇code
+ wx.qy.login({
+ success: (res) => {
+ if (res.code) {
+ console.log("浼佷笟寰俊灏忕▼搴� ---> code锛�", res.code);
+ resolve(res.code);
+ } else {
+ reject(new Error("鑾峰彇code澶辫触锛�" + res.errMsg));
+ }
+ },
+ fail: (err) => {
+ console.error("wx.qy.login璋冪敤澶辫触锛�", err);
+ reject(new Error("浼佷笟寰俊鐧诲綍鎺ュ彛璋冪敤澶辫触锛�" + err.errMsg));
+ },
+ });
+ // #endif
+
+ // #ifndef MP-WEIXIN
+ resolve(null);
+ // #endif
});
- // #endif
},
/**
* 妫�鏌ユ槸鍚﹀湪浼佷笟寰俊鐜
*/
isWxWorkEnvironment() {
+ // #ifdef H5
const userAgent = navigator.userAgent.toLowerCase();
return userAgent.includes("wxwork");
+ // #endif
+
+ // #ifndef H5
+ return false;
+ // #endif
},
/**
@@ -211,17 +180,20 @@
// #endif
// #ifdef MP-WEIXIN
- // 鍦ㄥ皬绋嬪簭涓彲浠ラ�氳繃鍏朵粬鏂瑰紡鑾峰彇鍙傛暟
- // 杩欓噷绠�鍖栧鐞嗭紝瀹為檯椤圭洰涓彲浠ユ牴鎹渶瑕佽皟鏁�
+ // 鍦ㄥ皬绋嬪簭涓紝鍙傛暟閫氳繃onLoad鐨刼ptions浼犻��
+ if (this.pageOptions && this.pageOptions[name]) {
+ return decodeURIComponent(this.pageOptions[name]);
+ }
// #endif
return null;
},
/**
- * 璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
+ * 璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈紙H5鐜锛�
*/
redirectToWxWorkAuth() {
+ // #ifdef H5
// 浠庡叏灞�閰嶇疆涓幏鍙栦紒涓氬井淇¢厤缃�
const config = getApp().globalData.config;
const corpId = config.qyWechatCorpId || "your_corp_id"; // 浼佷笟ID
@@ -232,19 +204,30 @@
const authUrl = `https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=${corpId}&agentid=${agentId}&redirect_uri=${redirectUri}&state=${state}`;
window.location.href = authUrl;
+ // #endif
},
/**
* 鐧诲綍鎴愬姛鍚庣殑璺宠浆澶勭悊
*/
redirectAfterLogin() {
- // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
- if (this.pageOptions.redirect) {
- // 瑙g爜redirect鍙傛暟
- const redirectUrl = decodeURIComponent(this.pageOptions.redirect);
- this.$tab.reLaunch(redirectUrl);
- } else {
- // 榛樿璺宠浆鍒伴椤�
+ try {
+ // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
+ let redirectUrl = this.getUrlParam("redirect");
+
+ if (redirectUrl) {
+ // 瑙g爜redirect鍙傛暟
+ redirectUrl = decodeURIComponent(redirectUrl);
+ console.log("鑷姩璺宠浆鍒版寚瀹氶〉闈�:", redirectUrl);
+ this.$tab.reLaunch(redirectUrl);
+ } else {
+ // 榛樿璺宠浆鍒伴椤�
+ console.log("璺宠浆鍒伴椤�");
+ this.$tab.reLaunch("/pages/index");
+ }
+ } catch (e) {
+ console.error("璺宠浆澶辫触锛屼娇鐢ㄩ粯璁よ烦杞�:", e);
+ // 鍑虹幇寮傚父鏃讹紝榛樿璺宠浆鍒伴椤�
this.$tab.reLaunch("/pages/index");
}
},
diff --git a/app/utils/wechat.js b/app/utils/wechat.js
new file mode 100644
index 0000000..afd572c
--- /dev/null
+++ b/app/utils/wechat.js
@@ -0,0 +1,106 @@
+/**
+ * 寰俊鐜妫�娴嬪拰璺宠浆宸ュ叿绫�
+ */
+
+/**
+ * 妫�鏌ユ槸鍚︿负浼佷笟寰俊鐜
+ * @returns {Promise<boolean>} 鏄惁涓轰紒涓氬井淇$幆澧�
+ */
+export function isWxWorkEnvironment() {
+ return new Promise((resolve) => {
+ // #ifdef MP-WEIXIN
+ try {
+ // 鑾峰彇绯荤粺淇℃伅
+ const systemInfo = uni.getSystemInfoSync();
+ console.log('绯荤粺淇℃伅:', systemInfo);
+
+ // 妫�鏌nvironment瀛楁鏄惁涓簑xwork
+ resolve(systemInfo.environment === 'wxwork');
+ } catch (e) {
+ console.error('鑾峰彇绯荤粺淇℃伅澶辫触:', e);
+ resolve(false);
+ }
+ // #endif
+
+ // #ifndef MP-WEIXIN
+ resolve(false);
+ // #endif
+ });
+}
+
+/**
+ * 鏋勯�犲甫鍙傛暟鐨刄RL
+ * @param {string} baseUrl 鍩虹URL
+ * @param {Object} queryParameters 鏌ヨ鍙傛暟瀵硅薄
+ * @returns {string} 鏋勯�犲悗鐨刄RL
+ */
+export function buildUrlWithParams(baseUrl, queryParameters) {
+ if (!queryParameters) return baseUrl;
+
+ const queryParams = Object.keys(queryParameters)
+ .map(key => `${key}=${encodeURIComponent(queryParameters[key])}`)
+ .join('&');
+
+ if (queryParams) {
+ return baseUrl + '?' + queryParams;
+ }
+
+ return baseUrl;
+}
+
+/**
+ * 鏍规嵁鐜鑷姩璺宠浆鍒板悎閫傜殑鐧诲綍椤甸潰
+ * @param {Object} options 鍚姩鍙傛暟
+ * @param {Object} tab tab瀵艰埅瀵硅薄
+ */
+export async function redirectToLoginByEnvironment(options, tab) {
+ // #ifdef MP-WEIXIN
+ // 鍦ㄥ井淇″皬绋嬪簭鐜涓�
+ try {
+ const isWxWork = await isWxWorkEnvironment();
+
+ if (isWxWork) {
+ console.log('妫�娴嬪埌浼佷笟寰俊鐜锛岃烦杞埌浼佷笟寰俊鍏嶇櫥椤甸潰');
+ // 鏋勯�犲甫鍙傛暟鐨刄RL
+ redirectToQyLogin(options,tab);
+ return;
+ } else {
+ console.log('妫�娴嬪埌鏅�氬井淇$幆澧冿紝璺宠浆鍒板井淇$櫥褰曢〉闈�');
+ redirectToLogin(options,tab);
+ return;
+ }
+ } catch (e) {
+ console.error('鐜妫�娴嬪け璐�:', e);
+ redirectToLogin(options,tab);
+ }
+ // #endif
+
+ // #ifndef MP-WEIXIN
+ // 闈炲井淇″皬绋嬪簭鐜锛岃烦杞埌鏅�氱櫥褰曢〉闈�
+ console.log('闈炲井淇″皬绋嬪簭鐜锛岃烦杞埌鏅�氱櫥褰曢〉闈�');
+ redirectToLogin(options,tab);
+ // #endif
+}
+
+
+export function redirectToQyLogin(options,tab){
+ let url = '/pages/qylogin';
+ if (options && options.query) {
+ url = buildUrlWithParams(url, options.query);
+ }
+ tab.reLaunch(url);
+}
+export function redirectToLogin(options,tab){
+ let url = '/pages/login';
+ if (options && options.query) {
+ url = buildUrlWithParams(url, options.query);
+ }
+ tab.reLaunch(url);
+}
+export default {
+ isWxWorkEnvironment,
+ buildUrlWithParams,
+ redirectToLogin,
+ redirectToQyLogin,
+ redirectToLoginByEnvironment
+};
\ No newline at end of file
--
Gitblit v1.9.1