xiangpei
2024-11-12 79b9c73e0717c244391ab1e15c6f6da9975f2610
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
<template>
  <div id="content" class="content">
    <!-- <div>
      <div style="text-align:center">钉钉扫码登录</div>
      <div id="self_defined_element" class="self-defined-classname"></div>
    </div> -->
  </div>
</template>
<script setup name="Oss" lang="ts">
import { useRoute } from 'vue-router';
import { dingdingLogin } from '@/api/system/user';
import { setToken } from '@/utils/auth';
 
import { ElMessage } from 'element-plus';
import { any } from 'vue-types';
 
const getUserDate = async (authCode: string, code: string) => {
  await dingdingLogin({
    authCode: authCode,
    code: code
  })
    .then((res: any) => {
      if (res) {
        var data = { value: res, expirse: new Date().getTime() };
 
        setToken(JSON.stringify(data));
        window.location.href = '/index';
      } else {
        ElMessage({ message: '无权访问', type: 'error' });
        setTimeout(() => {
          window.location.href = '/login';
        }, 3000);
      }
    })
    .catch((e: any) => {
      setTimeout(() => {
        window.location.href = '/login';
      }, 3000);
    });
};
onMounted(() => {
  const router = useRoute();
  let code = router.query.code;
  let authCode = router.query.authCode;
  if (code) {
    getUserDate(authCode, code);
  } else {
    var mode = 'openid corpid';
    window.location.href =
      'https://login.dingtalk.com/oauth2/auth?redirect_uri=http://171.221.173.53:8801/login&response_type=code&client_id=dingl5dxahaj3uzfug66&scope=' +
      encodeURIComponent(mode) +
      '&state=dddd&prompt=consent';
    // // STEP3:在需要的时候,调用 window.DTFrameLogin 方法构造登录二维码,并处理登录成功或失败的回调。
    // window.DTFrameLogin(
    //   {
    //     id: 'self_defined_element',
    //     width: 300,
    //     height: 300
    //   },
    //   {
    //     redirect_uri: encodeURIComponent('http://127.0.0.1:8080/auth/dingdingLogin'),
    //     client_id: 'dingl5dxahaj3uzfug66',
    //     scope: 'openid',
    //     response_type: 'code',
    //     state: 'STATE',
    //     prompt: 'consent'
    //   },
    //   (loginResult: any) => {
    //     console.log('loginResult:' + loginResult);
 
    //     const { redirectUrl, authCode, state } = loginResult;
    //     // 这里可以直接进行重定向
    //     window.location.href = redirectUrl;
    //     // 也可以在不跳转页面的情况下,使用code进行授权
    //     console.log(authCode);
    //   },
    //   (errorMsg: any) => {
    //     // 这里一般需要展示登录失败的具体原因,可以使用toast等轻提示
    //     console.error(`errorMsg of errorCbk: ${errorMsg}`);
    //   }
    // );
  }
});
</script>
 
<style lang="scss" scoped>
.content {
  display: flex;
  align-items: center;
  justify-content: center;
}
.self-defined-classname {
  // width: 300px;
  // height: 300px;
}
</style>