wlzboy
2025-12-30 a4b14a35a2209a30e53472e6333b13aa4a55b0eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<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">正在企业微信免登中...</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";
import { buildUrlWithParams } from "@/utils/wechat";
 
export default {
  data() {
    return {
      loading: true,
      error: false,
      errorMessage: "",
      // 保存页面参数
      pageOptions: {},
    };
  },
 
  onLoad(options) {
    // 添加容错处理
    try {
      // 保存页面参数
      this.pageOptions = options || {};
      
      // 打印环境信息
      this.logEnvironmentInfo();
      
      // 页面加载时执行免登流程
      this.qyWechatAutoLogin();
    } catch (e) {
      console.error('页面加载出错:', e);
      this.loading = false;
      this.error = true;
      this.errorMessage = "页面初始化失败,请重试";
    }
  },
 
  methods: {
    /**
     * 打印环境信息(调试用)
     */
    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. 当前在开发者工具中,wx.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
    },
 
    /**
     * 企业微信免登流程
     */
    async qyWechatAutoLogin() {
      try {
        this.loading = true;
        this.error = false;
 
        // #ifdef MP-WEIXIN
        // 在微信小程序环境中,通过企业微信免登
        console.log("企业微信小程序环境免登");
 
        // 直接通过企业微信API获取code,不使用URL中的code
        const code = await this.getLoginCode();
 
        if (!code) {
          throw new Error("无法获取企业微信授权code");
        }
 
        // 调用后端接口进行免登
        const response = await qyWechatAutoLogin(code);
 
        if (response.code === 200) {
          // 免登成功,保存token
          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
        let codeH5 = this.getUrlParam("code");
        
        // 如果URL中没有code,则跳转到企业微信授权页面
        if (!codeH5) {
          this.redirectToWxWorkAuth();
          return;
        }
 
        // 调用后端接口进行免登
        const responseH5 = await qyWechatAutoLogin(codeH5);
 
        if (responseH5.code === 200) {
          // 免登成功,保存token
          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 || "免登失败,请稍后重试";
      }
    },
 
    /**
     * 等待 wx.qy 对象初始化
     */
    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
            });
            
            // 提供更详细的错误信息
            const errorMsg = '企业微信环境初始化超时。\n\n' +
                           '可能的原因:\n' +
                           '1. 当前在开发者工具中,wx.qy API 不可用\n' +
                           '2. 请使用真机预览或真机调试\n' +
                           '3. 检查小程序 appid 是否配置正确';
            reject(new Error(errorMsg));
          } else {
            if (checkCount % 10 === 1) {
              console.log(`⏳ 等待 wx.qy 初始化... (${elapsed}ms, 第${checkCount}次检查)`);
            }
            setTimeout(checkWxQy, 100);
          }
        };
        
        checkWxQy();
      });
    },
 
    /**
     * 获取企业微信登录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();
          
          // 使用企业微信小程序API获取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
    },
 
    /**
     * 获取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的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 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;
      // #endif
    },
 
    /**
     * 登录成功后的跳转处理
     */
    redirectAfterLogin() {
      try {
        // 检查是否有redirect参数指定跳转页面
        let redirectUrl = this.getUrlParam("redirect");
        
        // 如果没有redirect参数,检查是否有保存的目标页面
        if (!redirectUrl) {
          const { getTargetUrl } = require('@/utils/auth')
          redirectUrl = getTargetUrl()
        }
        
        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");
      }
    },
 
    /**
     * 重新尝试登录
     */
    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>