From 40a8157440e3b906da8f52e07d939d78c3f4c313 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期日, 12 四月 2026 16:14:06 +0800
Subject: [PATCH] feat: 任务增加统计、同步增加通知
---
app/pages/qylogin.vue | 258 +++++++++++++++++++++++++++++++++++----------------
1 files changed, 178 insertions(+), 80 deletions(-)
diff --git a/app/pages/qylogin.vue b/app/pages/qylogin.vue
index 80ff189..2e47b30 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() {
@@ -33,33 +34,68 @@
},
onLoad(options) {
- // 淇濆瓨椤甸潰鍙傛暟
- this.pageOptions = options || {};
- // 椤甸潰鍔犺浇鏃舵墽琛屽厤鐧绘祦绋�
- this.qyWechatAutoLogin();
+ // 娣诲姞瀹归敊澶勭悊
+ try {
+ // 淇濆瓨椤甸潰鍙傛暟
+ this.pageOptions = options || {};
+
+ // 鎵撳嵃鐜淇℃伅
+ this.logEnvironmentInfo();
+
+ // 椤甸潰鍔犺浇鏃舵墽琛屽厤鐧绘祦绋�
+ this.qyWechatAutoLogin();
+ } catch (e) {
+ console.error('椤甸潰鍔犺浇鍑洪敊:', e);
+ this.loading = false;
+ this.error = true;
+ this.errorMessage = "椤甸潰鍒濆鍖栧け璐ワ紝璇烽噸璇�";
+ }
},
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));
- },
- });
- });
+ /**
+ * 鎵撳嵃鐜淇℃伅锛堣皟璇曠敤锛�
+ */
+ logEnvironmentInfo() {
+ // #ifdef MP-WEIXIN
+ console.log('========== 鐜淇℃伅 ==========');
+ console.log('wx 鏄惁瀛樺湪:', typeof wx !== 'undefined');
+ console.log('wx.qy 鏄惁瀛樺湪:', typeof wx !== 'undefined' && typeof wx.qy !== 'undefined');
+
+ if (typeof wx !== 'undefined') {
+ console.log('wx.login 鏄惁瀛樺湪:', typeof wx.login === 'function');
+
+ if (typeof wx.qy !== 'undefined') {
+ console.log('wx.qy.login 鏄惁瀛樺湪:', typeof wx.qy.login === 'function');
+ console.log('wx.qy 瀵硅薄:', wx.qy);
+ } else {
+ console.warn('鈿狅笍 wx.qy 涓嶅瓨鍦紒');
+ console.warn('鍙兘鍘熷洜锛�');
+ console.warn('1. 褰撳墠鍦ㄥ紑鍙戣�呭伐鍏蜂腑锛寃x.qy API 鍙兘涓嶅彲鐢�');
+ console.warn('2. 闇�瑕佸湪鐪熸満涓婅皟璇�');
+ console.warn('3. appid 閰嶇疆鍙兘涓嶆纭�');
+ }
+
+ // 鑾峰彇绯荤粺淇℃伅
+ try {
+ const systemInfo = wx.getSystemInfoSync();
+ console.log('绯荤粺淇℃伅:', systemInfo);
+ } catch (e) {
+ console.error('鑾峰彇绯荤粺淇℃伅澶辫触:', e);
+ }
+
+ // 鑾峰彇璐﹀彿淇℃伅
+ try {
+ const accountInfo = wx.getAccountInfoSync();
+ console.log('璐﹀彿淇℃伅:', accountInfo);
+ } catch (e) {
+ console.error('鑾峰彇璐﹀彿淇℃伅澶辫触:', e);
+ }
+ }
+ console.log('==============================');
+ // #endif
},
+
/**
* 浼佷笟寰俊鍏嶇櫥娴佺▼
*/
@@ -72,13 +108,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 +123,7 @@
const token = response.data.token;
this.$store.commit('SET_TOKEN', token)
- // 蹇呴』璋冪敤setToken淇濆瓨鍒版湰鍦板瓨鍌�
+ // 蹇呴』璋冪敤setToken淇濆瓨鍒版湰鍦板瓨鍌�
const { setToken } = require('@/utils/auth')
setToken(token)
// 鑾峰彇鐢ㄦ埛淇℃伅
@@ -109,10 +143,10 @@
}
// 鑾峰彇URL鍙傛暟涓殑code
- const codeH5 = this.getUrlParam("code");
-
+ let codeH5 = this.getUrlParam("code");
+
+ // 濡傛灉URL涓病鏈塩ode锛屽垯璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
if (!codeH5) {
- // 濡傛灉娌℃湁code锛屽垯璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
this.redirectToWxWorkAuth();
return;
}
@@ -148,56 +182,100 @@
},
/**
- * 澶勭悊浼佷笟寰俊鐧诲綍
+ * 绛夊緟 wx.qy 瀵硅薄鍒濆鍖�
*/
- 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 || "鍏嶇櫥澶辫触锛岃绋嶅悗閲嶈瘯";
+ waitForWxQy(timeout = 5000) {
+ return new Promise((resolve, reject) => {
+ const startTime = Date.now();
+ let checkCount = 0;
+
+ const checkWxQy = () => {
+ checkCount++;
+ const elapsed = Date.now() - startTime;
+
+ if (typeof wx !== 'undefined' && typeof wx.qy !== 'undefined' && typeof wx.qy.login === 'function') {
+ console.log(`鉁� wx.qy 宸插垵濮嬪寲 (鑰楁椂: ${elapsed}ms, 妫�鏌ユ鏁�: ${checkCount})`);
+ resolve(true);
+ } else if (elapsed > timeout) {
+ console.error(`鉂� 绛夊緟 wx.qy 鍒濆鍖栬秴鏃� (${timeout}ms)`);
+ console.error('褰撳墠鐘舵��:', {
+ wx瀛樺湪: typeof wx !== 'undefined',
+ 'wx.qy瀛樺湪': typeof wx !== 'undefined' && typeof wx.qy !== 'undefined',
+ 妫�鏌ユ鏁�: checkCount
});
- },
- fail: (err) => {
- console.error("浼佷笟寰俊鐧诲綍澶辫触:", err);
- this.loading = false;
- this.error = true;
- this.errorMessage = "浼佷笟寰俊鐧诲綍澶辫触锛岃绋嶅悗閲嶈瘯";
- },
+
+ // 鎻愪緵鏇磋缁嗙殑閿欒淇℃伅
+ const errorMsg = '浼佷笟寰俊鐜鍒濆鍖栬秴鏃躲�俓n\n' +
+ '鍙兘鐨勫師鍥狅細\n' +
+ '1. 褰撳墠鍦ㄥ紑鍙戣�呭伐鍏蜂腑锛寃x.qy API 涓嶅彲鐢╘n' +
+ '2. 璇蜂娇鐢ㄧ湡鏈洪瑙堟垨鐪熸満璋冭瘯\n' +
+ '3. 妫�鏌ュ皬绋嬪簭 appid 鏄惁閰嶇疆姝g‘';
+ reject(new Error(errorMsg));
+ } else {
+ if (checkCount % 10 === 1) {
+ console.log(`鈴� 绛夊緟 wx.qy 鍒濆鍖�... (${elapsed}ms, 绗�${checkCount}娆℃鏌�)`);
+ }
+ setTimeout(checkWxQy, 100);
+ }
+ };
+
+ checkWxQy();
});
- // #endif
+ },
+
+ /**
+ * 鑾峰彇浼佷笟寰俊鐧诲綍code
+ */
+ getLoginCode() {
+ return new Promise(async (resolve, reject) => {
+ // #ifdef MP-WEIXIN
+ try {
+ // 妫�鏌ユ槸鍚﹀湪浼佷笟寰俊灏忕▼搴忕幆澧�
+ console.log('妫�鏌ヤ紒涓氬井淇$幆澧冿紝wx.qy 鏄惁瀛樺湪:', typeof wx !== 'undefined' && typeof wx.qy !== 'undefined');
+
+ // 绛夊緟 wx.qy 鍒濆鍖栧畬鎴�
+ await this.waitForWxQy();
+
+ // 浣跨敤浼佷笟寰俊灏忕▼搴廇PI鑾峰彇code
+ console.log('浣跨敤 wx.qy.login 鑾峰彇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 || '鏈煡閿欒')));
+ },
+ });
+ } catch (error) {
+ console.error('鑾峰彇鐧诲綍code寮傚父:', error);
+ reject(error);
+ }
+ // #endif
+
+ // #ifndef MP-WEIXIN
+ resolve(null);
+ // #endif
+ });
},
/**
* 妫�鏌ユ槸鍚﹀湪浼佷笟寰俊鐜
*/
isWxWorkEnvironment() {
+ // #ifdef H5
const userAgent = navigator.userAgent.toLowerCase();
return userAgent.includes("wxwork");
+ // #endif
+
+ // #ifndef H5
+ return false;
+ // #endif
},
/**
@@ -211,17 +289,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 +313,36 @@
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");
+
+ // 濡傛灉娌℃湁redirect鍙傛暟锛屾鏌ユ槸鍚︽湁淇濆瓨鐨勭洰鏍囬〉闈�
+ if (!redirectUrl) {
+ const { getTargetUrl } = require('@/utils/auth')
+ redirectUrl = getTargetUrl()
+ }
+
+ 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");
}
},
--
Gitblit v1.9.1