From 7de1396e315896dbc72a9d54e44f77434ea90f18 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期日, 14 十二月 2025 23:47:34 +0800
Subject: [PATCH] feat:增加企业微信自动登录
---
app/pages/qylogin.vue | 323 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 323 insertions(+), 0 deletions(-)
diff --git a/app/pages/qylogin.vue b/app/pages/qylogin.vue
new file mode 100644
index 0000000..80ff189
--- /dev/null
+++ b/app/pages/qylogin.vue
@@ -0,0 +1,323 @@
+<template>
+ <view class="container">
+ <view class="loading-content" v-if="loading">
+ <image class="loading-icon" src="/static/images/loading.gif"></image>
+ <text class="loading-text">姝e湪浼佷笟寰俊鍏嶇櫥涓�...</text>
+ </view>
+
+ <view class="error-content" v-else-if="error">
+ <image class="error-icon" src="/static/images/error.png"></image>
+ <text class="error-text">{{ errorMessage }}</text>
+ <button class="retry-btn" @click="retryLogin">閲嶆柊灏濊瘯</button>
+ </view>
+
+ <view class="success-content" v-else>
+ <image class="success-icon" src="/static/images/success.png"></image>
+ <text class="success-text">浼佷笟寰俊鍏嶇櫥鎴愬姛</text>
+ </view>
+ </view>
+</template>
+
+<script>
+import { qyWechatAutoLogin } from "@/api/login";
+
+export default {
+ data() {
+ return {
+ loading: true,
+ error: false,
+ errorMessage: "",
+ // 淇濆瓨椤甸潰鍙傛暟
+ pageOptions: {},
+ };
+ },
+
+ onLoad(options) {
+ // 淇濆瓨椤甸潰鍙傛暟
+ this.pageOptions = options || {};
+ // 椤甸潰鍔犺浇鏃舵墽琛屽厤鐧绘祦绋�
+ this.qyWechatAutoLogin();
+ },
+
+ 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));
+ },
+ });
+ });
+ },
+ /**
+ * 浼佷笟寰俊鍏嶇櫥娴佺▼
+ */
+ async qyWechatAutoLogin() {
+ try {
+ this.loading = true;
+ this.error = false;
+
+ // #ifdef MP-WEIXIN
+ // 鍦ㄥ井淇″皬绋嬪簭鐜涓紝閫氳繃浼佷笟寰俊鍏嶇櫥
+ console.log("浼佷笟寰俊灏忕▼搴忕幆澧冨厤鐧�");
+
+ // 鑾峰彇URL鍙傛暟涓殑code
+ const code = await this.getLoginCode();
+
+ if (!code) {
+ // 濡傛灉娌℃湁code锛屽皾璇曢�氳繃浼佷笟寰俊API鑾峰彇
+ this.handleWxWorkLogin();
+ return;
+ }
+
+ // 璋冪敤鍚庣鎺ュ彛杩涜鍏嶇櫥
+ const response = await qyWechatAutoLogin(code);
+
+ if (response.code === 200) {
+ // 鍏嶇櫥鎴愬姛锛屼繚瀛榯oken
+ const token = response.data.token;
+
+ this.$store.commit('SET_TOKEN', token)
+ // 蹇呴』璋冪敤setToken淇濆瓨鍒版湰鍦板瓨鍌�
+ const { setToken } = require('@/utils/auth')
+ setToken(token)
+ // 鑾峰彇鐢ㄦ埛淇℃伅
+ await this.$store.dispatch("GetInfo");
+
+ // 璺宠浆鍒伴椤垫垨鍏朵粬鎸囧畾椤甸潰
+ this.redirectAfterLogin();
+ } else {
+ throw new Error(response.msg || "鍏嶇櫥澶辫触");
+ }
+ // #endif
+
+ // #ifdef H5
+ // 妫�鏌ユ槸鍚﹀湪浼佷笟寰俊鐜涓�
+ if (!this.isWxWorkEnvironment()) {
+ throw new Error("璇峰湪浼佷笟寰俊瀹㈡埛绔腑鎵撳紑");
+ }
+
+ // 鑾峰彇URL鍙傛暟涓殑code
+ const codeH5 = this.getUrlParam("code");
+
+ if (!codeH5) {
+ // 濡傛灉娌℃湁code锛屽垯璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
+ this.redirectToWxWorkAuth();
+ return;
+ }
+
+ // 璋冪敤鍚庣鎺ュ彛杩涜鍏嶇櫥
+ const responseH5 = await qyWechatAutoLogin(codeH5);
+
+ if (responseH5.code === 200) {
+ // 鍏嶇櫥鎴愬姛锛屼繚瀛榯oken
+ const token = responseH5.data.token;
+ this.$store.commit("SET_TOKEN", token);
+ uni.setStorageSync("token", token);
+
+ // 鑾峰彇鐢ㄦ埛淇℃伅
+ await this.$store.dispatch("GetInfo");
+
+ // 璺宠浆鍒伴椤垫垨鍏朵粬鎸囧畾椤甸潰
+ this.redirectAfterLogin();
+ } else {
+ throw new Error(responseH5.msg || "鍏嶇櫥澶辫触");
+ }
+ // #endif
+
+ // #ifndef MP-WEIXIN || H5
+ throw new Error("璇ュ姛鑳戒粎鏀寔鍦ㄤ紒涓氬井淇′腑浣跨敤");
+ // #endif
+ } catch (err) {
+ console.error("浼佷笟寰俊鍏嶇櫥澶辫触:", err);
+ this.loading = false;
+ this.error = true;
+ this.errorMessage = err.message || "鍏嶇櫥澶辫触锛岃绋嶅悗閲嶈瘯";
+ }
+ },
+
+ /**
+ * 澶勭悊浼佷笟寰俊鐧诲綍
+ */
+ 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 = "浼佷笟寰俊鐧诲綍澶辫触锛岃绋嶅悗閲嶈瘯";
+ },
+ });
+ // #endif
+ },
+
+ /**
+ * 妫�鏌ユ槸鍚﹀湪浼佷笟寰俊鐜
+ */
+ isWxWorkEnvironment() {
+ const userAgent = navigator.userAgent.toLowerCase();
+ return userAgent.includes("wxwork");
+ },
+
+ /**
+ * 鑾峰彇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
+ // 鍦ㄥ皬绋嬪簭涓彲浠ラ�氳繃鍏朵粬鏂瑰紡鑾峰彇鍙傛暟
+ // 杩欓噷绠�鍖栧鐞嗭紝瀹為檯椤圭洰涓彲浠ユ牴鎹渶瑕佽皟鏁�
+ // #endif
+
+ return null;
+ },
+
+ /**
+ * 璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
+ */
+ redirectToWxWorkAuth() {
+ // 浠庡叏灞�閰嶇疆涓幏鍙栦紒涓氬井淇¢厤缃�
+ const config = getApp().globalData.config;
+ const corpId = config.qyWechatCorpId || "your_corp_id"; // 浼佷笟ID
+ const agentId = config.qyWechatAgentId || "your_agent_id"; // 搴旂敤ID
+ const redirectUri = encodeURIComponent(window.location.href);
+ const state = Date.now(); // 闃查噸鏀炬敾鍑�
+
+ const authUrl = `https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=${corpId}&agentid=${agentId}&redirect_uri=${redirectUri}&state=${state}`;
+
+ window.location.href = authUrl;
+ },
+
+ /**
+ * 鐧诲綍鎴愬姛鍚庣殑璺宠浆澶勭悊
+ */
+ redirectAfterLogin() {
+ // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
+ if (this.pageOptions.redirect) {
+ // 瑙g爜redirect鍙傛暟
+ const redirectUrl = decodeURIComponent(this.pageOptions.redirect);
+ this.$tab.reLaunch(redirectUrl);
+ } else {
+ // 榛樿璺宠浆鍒伴椤�
+ this.$tab.reLaunch("/pages/index");
+ }
+ },
+
+ /**
+ * 閲嶆柊灏濊瘯鐧诲綍
+ */
+ retryLogin() {
+ this.qyWechatAutoLogin();
+ },
+ },
+};
+</script>
+
+<style lang="scss" scoped>
+.container {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ justify-content: center;
+ min-height: 100vh;
+ padding: 40rpx;
+ background-color: #f8f8f8;
+}
+
+.loading-content,
+.error-content,
+.success-content {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ justify-content: center;
+ text-align: center;
+}
+
+.loading-icon,
+.error-icon,
+.success-icon {
+ width: 120rpx;
+ height: 120rpx;
+ margin-bottom: 30rpx;
+}
+
+.loading-text,
+.error-text,
+.success-text {
+ font-size: 32rpx;
+ color: #333;
+ margin-bottom: 40rpx;
+}
+
+.error-text,
+.success-text {
+ font-weight: bold;
+}
+
+.error-text {
+ color: #ff0000;
+}
+
+.success-text {
+ color: #00cc00;
+}
+
+.retry-btn {
+ width: 80%;
+ height: 80rpx;
+ background-color: #007aff;
+ color: #fff;
+ border-radius: 10rpx;
+ font-size: 32rpx;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+}
+</style>
\ No newline at end of file
--
Gitblit v1.9.1