|
|
@@ -136,13 +136,13 @@ export default {
|
|
|
});
|
|
|
|
|
|
if (res && res.statusCode === 200) {
|
|
|
- console.log('用户信息获取成功-----------');
|
|
|
+ //console.log('用户信息获取成功-----------');
|
|
|
const data = res.data;
|
|
|
- console.log(data);
|
|
|
- console.log(data.openId);
|
|
|
+ //console.log(data);
|
|
|
+ //console.log(data.openId);
|
|
|
// 将微信用户的 openId 存到本地
|
|
|
uni.setStorageSync('openId', data.openId);
|
|
|
- console.log('getStorageSync-> ', uni.getStorageSync('openId'));
|
|
|
+ //console.log('getStorageSync-> ', uni.getStorageSync('openId'));
|
|
|
} else {
|
|
|
console.error('获取用户信息失败:', res.data);
|
|
|
}
|
|
|
@@ -164,11 +164,11 @@ export default {
|
|
|
|
|
|
if (res && res.statusCode === 200) {
|
|
|
const data = res.data;
|
|
|
- console.log('allOpenIds:', data);
|
|
|
+ //console.log('allOpenIds:', data);
|
|
|
// 确保 data 是数组
|
|
|
if (Array.isArray(data) && data.includes(localOpenId)) {
|
|
|
// 包含的话,则直接根据 openId来实现登录
|
|
|
- console.log(`data 包含 openId: ${localOpenId}`);
|
|
|
+ //console.log(`data 包含 openId: ${localOpenId}`);
|
|
|
const [err, res] = await uni.request({
|
|
|
url: BASE_URL + `/auth-server/user/sys/wxLogin`,
|
|
|
method: 'POST',
|
|
|
@@ -202,7 +202,7 @@ export default {
|
|
|
return;
|
|
|
}
|
|
|
const flowObject = JSON.parse(decodeURIComponent(flowParam));
|
|
|
- console.log("flowObject.openId---> ", flowObject.openId)
|
|
|
+ //console.log("flowObject.openId---> ", flowObject.openId)
|
|
|
if (flowObject.openId) {
|
|
|
const [err, res] = await uni.request({
|
|
|
url: BASE_URL + `/auth-server/user/sys/wxLogin`,
|
|
|
@@ -241,77 +241,132 @@ export default {
|
|
|
loadDingTalkJs() {
|
|
|
return Promise.resolve(dd);
|
|
|
},
|
|
|
- requestDingTalkAuthCode(corpId) {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- if (!dd) {
|
|
|
- reject(new Error('钉钉资源未加载成功,请联系管理员'));
|
|
|
- return;
|
|
|
- }
|
|
|
- dd.getAuthCode({
|
|
|
- corpId: corpId,
|
|
|
- onSuccess: (res) => resolve(res.code || res.authCode),
|
|
|
- onFail: reject
|
|
|
- });
|
|
|
- dd.error(reject);
|
|
|
- });
|
|
|
- },
|
|
|
+ requestDingTalkAuthCode(corpId) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ // 节点1:进入方法
|
|
|
+ //console.log('1. 进入获取钉钉授权码函数,corpId:' + corpId);
|
|
|
+
|
|
|
+ // 节点2:判断dd是否存在
|
|
|
+ if (!dd) {
|
|
|
+ const errMsg = '钉钉资源未加载成功,请联系管理员';
|
|
|
+ //console.log('2. 异常:' + errMsg);
|
|
|
+ reject(new Error(errMsg));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //console.log('2. dd对象存在,准备调用getAuthCode');
|
|
|
+
|
|
|
+ let hasCallbackTriggered = false;
|
|
|
+
|
|
|
+ dd.getAuthCode({
|
|
|
+ corpId: corpId,
|
|
|
+ onSuccess: (res) => {
|
|
|
+ if (hasCallbackTriggered) return;
|
|
|
+ hasCallbackTriggered = true;
|
|
|
+ //console.log('3. getAuthCode成功,返回数据:' + JSON.stringify(res));
|
|
|
+ const code = res.code || res.authCode;
|
|
|
+ //console.log('4. 提取授权码=' + code);
|
|
|
+ resolve(code);
|
|
|
+ },
|
|
|
+ onFail: (err) => {
|
|
|
+ if (hasCallbackTriggered) return;
|
|
|
+ hasCallbackTriggered = true;
|
|
|
+ //console.log('3. getAuthCode失败,错误信息:' + JSON.stringify(err));
|
|
|
+ reject(err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 钉钉全局异常监听
|
|
|
+ dd.error((err) => {
|
|
|
+ if (hasCallbackTriggered) return;
|
|
|
+ hasCallbackTriggered = true;
|
|
|
+ //console.log('5. 钉钉全局捕获异常:' + JSON.stringify(err));
|
|
|
+ reject(err);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ },
|
|
|
async handleDingTalkLoginSuccess(data) {
|
|
|
this.$store.commit('SET_TOKEN', data.token);
|
|
|
await this.refreshUserInfo();
|
|
|
},
|
|
|
- async tryDingTalkAutoLogin(urlParams) {
|
|
|
- if (!this.isDingTalkEnv()) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (this.$store.state.user.token) {
|
|
|
- return true;
|
|
|
- }
|
|
|
+ async tryDingTalkAutoLogin(urlParams) {
|
|
|
+ //console.log('[钉钉自动登录] 1、进入 tryDingTalkAutoLogin 函数');
|
|
|
|
|
|
- const tenantId = urlParams.get('tenantId') || uni.getStorageSync('dingTalkTenantId') || '10009';
|
|
|
+ const isDingEnv = this.isDingTalkEnv();
|
|
|
+ //console.log('[钉钉自动登录] 2、钉钉环境检测结果:', isDingEnv);
|
|
|
+ if (!isDingEnv) {
|
|
|
+ //console.log('[钉钉自动登录] 分支:非钉钉环境,return false,流程终止');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- try {
|
|
|
- const clientConfig = await loginService.dingTalkClientConfig(tenantId);
|
|
|
+ const existToken = !!this.$store.state.user.token;
|
|
|
+ //console.log('[钉钉自动登录] 3、当前是否存在token:', existToken);
|
|
|
+ if (this.$store.state.user.token) {
|
|
|
+ //console.log('[钉钉自动登录] 分支:已有token,自动跳转首页');
|
|
|
+ // 已有登录态,直接跳首页,解决你不跳转的问题
|
|
|
+ setTimeout(() => {
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/index/index'
|
|
|
+ });
|
|
|
+ }, 200);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
- if (!clientConfig || !clientConfig.corpId) {
|
|
|
+ //console.log('[钉钉自动登录] 4、无token,继续往下执行免登逻辑');
|
|
|
+ const tenantId = urlParams.get('tenantId') || uni.getStorageSync('dingTalkTenantId') || '10009';
|
|
|
+ //console.log('[钉钉自动登录] 5、解析得到tenantId:', tenantId);
|
|
|
|
|
|
- throw new Error('DingTalk corpId is missing');
|
|
|
- }
|
|
|
+ try {
|
|
|
+ //console.log('[钉钉自动登录] 6、开始请求后端 dingTalkClientConfig 获取corpId配置');
|
|
|
+ const clientConfig = await loginService.dingTalkClientConfig(tenantId);
|
|
|
+ //console.log('[钉钉自动登录] 7、后端返回clientConfig:', clientConfig);
|
|
|
|
|
|
- await this.loadDingTalkJs();
|
|
|
+ if (!clientConfig || !clientConfig.corpId) {
|
|
|
+ //console.log('[钉钉自动登录] 分支:clientConfig为空 或 corpId不存在,抛出异常');
|
|
|
+ throw new Error('DingTalk corpId is missing');
|
|
|
+ }
|
|
|
+ //console.log('[钉钉自动登录] 8、拿到有效corpId:', clientConfig.corpId);
|
|
|
|
|
|
- const authCode = await this.requestDingTalkAuthCode(clientConfig.corpId);
|
|
|
+ //console.log('[钉钉自动登录] 9、即将执行 loadDingTalkJs 校验钉钉SDK是否加载');
|
|
|
+ await this.loadDingTalkJs();
|
|
|
+ //console.log('[钉钉自动登录] 10、loadDingTalkJs执行完成,准备调用 requestDingTalkAuthCode');
|
|
|
|
|
|
- const loginResult = await loginService.dingTalkLogin({
|
|
|
- authCode: authCode,
|
|
|
- tenantId: tenantId
|
|
|
- });
|
|
|
+ //console.log('>>>>>>>>>> 即将进入 requestDingTalkAuthCode 方法 <<<<<<<<<<');
|
|
|
+ const authCode = await this.requestDingTalkAuthCode(clientConfig.corpId);
|
|
|
+ //console.log('[钉钉自动登录] 11、requestDingTalkAuthCode执行完成,获取到authCode:', authCode);
|
|
|
|
|
|
- if (loginResult.bindRequired) {
|
|
|
- uni.setStorageSync('dingTalkBindKey', loginResult.bindKey);
|
|
|
- uni.setStorageSync('dingTalkTenantId', loginResult.tenantId || tenantId);
|
|
|
- uni.reLaunch({
|
|
|
- url: '/pages/login/login?bind=dingtalk'
|
|
|
- });
|
|
|
- return true;
|
|
|
- }
|
|
|
- await this.handleDingTalkLoginSuccess(loginResult);
|
|
|
- uni.reLaunch({
|
|
|
- url: '/pages/index/index'
|
|
|
- });
|
|
|
- } catch (e) {
|
|
|
- uni.showToast({
|
|
|
- title: JSON.stringify(e),
|
|
|
- icon: 'none'
|
|
|
- });
|
|
|
- // if (!e.isTrusted) {
|
|
|
- // uni.showToast({
|
|
|
- // title: '钉钉异常,请联系管理员',
|
|
|
- // icon: 'none'
|
|
|
- // });
|
|
|
- // }
|
|
|
- }
|
|
|
+ //console.log('[钉钉自动登录] 12、调用后端钉钉登录接口 dingTalkLogin');
|
|
|
+ const loginResult = await loginService.dingTalkLogin({
|
|
|
+ authCode: authCode,
|
|
|
+ tenantId: tenantId
|
|
|
+ });
|
|
|
+ //console.log('[钉钉自动登录] 13、登录接口返回结果:', loginResult);
|
|
|
+
|
|
|
+ if (loginResult.bindRequired) {
|
|
|
+ //console.log('[钉钉自动登录] 分支:需要账号绑定,跳转绑定页面');
|
|
|
+ uni.setStorageSync('dingTalkBindKey', loginResult.bindKey);
|
|
|
+ uni.setStorageSync('dingTalkTenantId', loginResult.tenantId || tenantId);
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/login/login?bind=dingtalk'
|
|
|
+ });
|
|
|
return true;
|
|
|
- },
|
|
|
+ }
|
|
|
+ //console.log('[钉钉自动登录] 14、登录成功,执行保存用户信息');
|
|
|
+ await this.handleDingTalkLoginSuccess(loginResult);
|
|
|
+ //console.log('[钉钉自动登录] 15、跳转首页');
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/index/index'
|
|
|
+ });
|
|
|
+ } catch (e) {
|
|
|
+ console.error('[钉钉自动登录] 捕获异常,流程中断,错误对象:', e);
|
|
|
+ console.error('[钉钉自动登录] 异常信息message:', e.message);
|
|
|
+ uni.showToast({
|
|
|
+ title: JSON.stringify(e),
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //console.log('[钉钉自动登录] 函数末尾,return true');
|
|
|
+ return true;
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
}
|