|
|
@@ -67,7 +67,7 @@ export const initGuidInfo = (): void => {
|
|
|
* @returns {Promise<void>}
|
|
|
*/
|
|
|
export const doWecomLogin = async (code: string): Promise<void> => {
|
|
|
- if (isLogging) return; // 正在登录中,忽略重复调用
|
|
|
+ if (isLogging) return; // 正在登录中,忽略重复调用
|
|
|
isLogging = true;
|
|
|
try {
|
|
|
const lastCodeStr = window.localStorage.getItem('lastCode');
|
|
|
@@ -86,26 +86,24 @@ export const doWecomLogin = async (code: string): Promise<void> => {
|
|
|
// formData.append("code", 'QWert!@345');
|
|
|
// 调用接口兑换 AIToken
|
|
|
const res: WecomAuthResponse = await wecomAuth(formData);
|
|
|
- if (res.StatusCode === 200 && res.Data && res.Data.token) {
|
|
|
+ if (res && res.StatusCode === 200 && res.Data && res.Data.token) {
|
|
|
// 登录成功:存储 AIToken
|
|
|
window.localStorage.setItem('AIToken', res.Data.token);
|
|
|
isLogging = false;
|
|
|
// console.log("router.currentRoute.fullPath=",router.currentRoute.fullPath)
|
|
|
// 重新跳转目标页面(此时登录状态已满足)
|
|
|
// router.push(router.currentRoute.fullPath);
|
|
|
- } else if (res.StatusCode === 403) {
|
|
|
+ } else if (res && res.StatusCode === 403) {
|
|
|
// 无权限 → 跳错误页
|
|
|
isLogging = false;
|
|
|
router.push('/error');
|
|
|
- } else if (res.StatusCode === 420) {
|
|
|
+ } else if (res && res.StatusCode === 420) {
|
|
|
// 需重新获取 code → 清除缓存并重定向
|
|
|
isLogging = false;
|
|
|
- window.localStorage.clear();
|
|
|
getQyCode(); // 你的获取企业微信二维码/授权链接的函数
|
|
|
} else {
|
|
|
isLogging = false;
|
|
|
- // 兼容写法:判断 res.Message 是否存在(去掉 ?.)
|
|
|
- throw new Error(res.Message ? res.Message : '登录失败');
|
|
|
+ throw new Error(res && res.Message ? res.Message : '登录失败');
|
|
|
}
|
|
|
} catch (error) {
|
|
|
isLogging = false;
|
|
|
@@ -123,11 +121,36 @@ export const getGuid = () => {
|
|
|
});
|
|
|
}
|
|
|
export const getQyCode = () => {
|
|
|
+ window.localStorage.clear();
|
|
|
let url, appid, agentid;
|
|
|
// url = encodeURIComponent(process.env.VUE_APP_AUTHURL);
|
|
|
url = encodeURIComponent(window.location.href);
|
|
|
+ // console.log("-url=",url)
|
|
|
appid = process.env.VUE_APP_APPID;
|
|
|
agentid = process.env.VUE_APP_AGENTID;
|
|
|
-
|
|
|
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_base&state=&agentid=${agentid}&t=${new Date().getTime()}#wechat_redirect`;
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+// utils/authLock.js
|
|
|
+export const authLock = {
|
|
|
+ // 内存锁:实时判断
|
|
|
+ isAuthorizing: false,
|
|
|
+ // 初始化:从本地缓存恢复锁状态(防止页面刷新后锁失效)
|
|
|
+ init() {
|
|
|
+ const lock = window.localStorage.getItem('isWechatAuthorizing');
|
|
|
+ this.isAuthorizing = lock === 'true';
|
|
|
+ },
|
|
|
+ // 加锁:同时更新内存和本地缓存
|
|
|
+ lock() {
|
|
|
+ this.isAuthorizing = true;
|
|
|
+ window.localStorage.setItem('isWechatAuthorizing', 'true');
|
|
|
+ },
|
|
|
+ // 解锁:同时更新内存和本地缓存
|
|
|
+ unlock() {
|
|
|
+ this.isAuthorizing = false;
|
|
|
+ window.localStorage.removeItem('isWechatAuthorizing');
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 初始化锁
|
|
|
+authLock.init();
|