| | |
| | | |
| | | <script> |
| | | import { qyWechatAutoLogin } from "@/api/login"; |
| | | import { buildUrlWithParams } from "@/utils/wechat"; |
| | | |
| | | export default { |
| | | data() { |
| | |
| | | }, |
| | | |
| | | methods: { |
| | | async getLoginCode() { |
| | | //这里要调用wx.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)); |
| | | }, |
| | | }); |
| | | }); |
| | | }, |
| | | /** |
| | | * 企业微信免登流程 |
| | | */ |
| | |
| | | // 在微信小程序环境中,通过企业微信免登 |
| | | console.log("企业微信小程序环境免登"); |
| | | |
| | | // 获取URL参数中的code |
| | | // 直接通过企业微信API获取code,不使用URL中的code |
| | | const code = await this.getLoginCode(); |
| | | |
| | | if (!code) { |
| | | // 如果没有code,尝试通过企业微信API获取 |
| | | this.handleWxWorkLogin(); |
| | | return; |
| | | throw new Error("无法获取企业微信授权code"); |
| | | } |
| | | |
| | | // 调用后端接口进行免登 |
| | |
| | | const token = response.data.token; |
| | | |
| | | this.$store.commit('SET_TOKEN', token) |
| | | // 必须调用setToken保存到本地存储 |
| | | // 必须调用setToken保存到本地存储 |
| | | const { setToken } = require('@/utils/auth') |
| | | setToken(token) |
| | | // 获取用户信息 |
| | |
| | | } |
| | | |
| | | // 获取URL参数中的code |
| | | const codeH5 = this.getUrlParam("code"); |
| | | |
| | | let codeH5 = this.getUrlParam("code"); |
| | | |
| | | // 如果URL中没有code,则跳转到企业微信授权页面 |
| | | if (!codeH5) { |
| | | // 如果没有code,则跳转到企业微信授权页面 |
| | | this.redirectToWxWorkAuth(); |
| | | return; |
| | | } |
| | |
| | | }, |
| | | |
| | | /** |
| | | * 处理企业微信登录 |
| | | * 获取企业微信登录code |
| | | */ |
| | | handleWxWorkLogin() { |
| | | // #ifdef MP-WEIXIN |
| | | // 在企业微信小程序中,可以直接调用企业微信登录API |
| | | uni.login({ |
| | | provider: "weixin", |
| | | success: (loginRes) => { |
| | | console.log("企业微信登录成功", loginRes); |
| | | // 调用后端接口进行免登 |
| | | qyWechatAutoLogin(loginRes.code) |
| | | .then((response) => { |
| | | if (response.code === 200) { |
| | | // 免登成功,保存token |
| | | 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 |
| | | // 使用企业微信小程序API获取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 |
| | | }, |
| | | |
| | | /** |
| | |
| | | // #endif |
| | | |
| | | // #ifdef MP-WEIXIN |
| | | // 在小程序中可以通过其他方式获取参数 |
| | | // 这里简化处理,实际项目中可以根据需要调整 |
| | | // 在小程序中,参数通过onLoad的options传递 |
| | | 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 |
| | |
| | | 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) { |
| | | // 解码redirect参数 |
| | | const redirectUrl = decodeURIComponent(this.pageOptions.redirect); |
| | | this.$tab.reLaunch(redirectUrl); |
| | | } else { |
| | | // 默认跳转到首页 |
| | | try { |
| | | // 检查是否有redirect参数指定跳转页面 |
| | | let redirectUrl = this.getUrlParam("redirect"); |
| | | |
| | | if (redirectUrl) { |
| | | // 解码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"); |
| | | } |
| | | }, |