songxy преди 1 година
родител
ревизия
3e58a6abaf
променени са 7 файла, в които са добавени 156 реда и са изтрити 13 реда
  1. 0 1
      client_h5/index.html
  2. 3 0
      client_h5/package.json
  3. 59 12
      client_h5/src/App.vue
  4. 10 0
      client_h5/src/stores/index.ts
  5. 38 0
      client_h5/src/stores/modules/user.ts
  6. 17 0
      client_h5/src/utils/common.ts
  7. 29 0
      client_h5/src/utils/request.ts

+ 0 - 1
client_h5/index.html

@@ -4,7 +4,6 @@
     <meta charset="UTF-8" />
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1">
-    <script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.10.3/dingtalk.open.js"></script>
     <title></title>
   </head>
   <body>

+ 3 - 0
client_h5/package.json

@@ -9,6 +9,9 @@
     "preview": "vite preview"
   },
   "dependencies": {
+    "axios": "^1.6.8",
+    "dingtalk-jsapi": "^3.0.33",
+    "pinia": "^2.1.7",
     "vue": "^3.4.19",
     "vue-router": "^4.2.5"
   },

+ 59 - 12
client_h5/src/App.vue

@@ -1,16 +1,63 @@
 <script setup lang="ts">
-dd.getAuthCode({
-  corpId: 'ding12345xxx',
-  success: (res) => {
-      const { authCode } = res;
-      if (authCode) {
-        // 授权成功
-        console.log(authCode);
-      }
-  },
-  fail: () => {},
-  complete: () => {},
-});
+import { getAuthCode } from 'dingtalk-jsapi'; 
+import { getUrlParams } from "@/utils/common";
+import reqest from "@/utils/request";
+
+const socialAuthRedirect = (type: number, redirectUri: string) => {
+    return reqest.get('/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri)
+}
+const initUserInfoHandler = async (type: number) => {
+    const redirectUri:string ='http://10.10.10.7:48080/admin-api/system/auth/social-login?type=' + type
+    const res = await socialAuthRedirect(type, encodeURIComponent(redirectUri))
+    if (res && res.data) {
+        const map = getUrlParams(res.data);
+        const state: string = map?.get("state") as string
+        const code: string = await getDDAuthCode() as string
+        const userIdResult = await getUserIdByDD({
+            type,
+            code,
+            state
+        })
+        if (userIdResult && userIdResult.data) { 
+            getUserInfoById(userIdResult.data)
+        }
+    }
+}
+initUserInfoHandler(20)
+function getDDAuthCode() {
+    return new Promise((resolve, reject) => {
+        getAuthCode({
+            corpId: 'ding65143abf9aeea2ec',
+            success: (res) => {
+                const { authCode } = res;
+                if (authCode) {
+                    resolve(authCode);
+                }
+            },
+            fail: (ex: any) => {
+                reject(ex);
+            },
+            complete: () => {},
+        });
+    });
+}
+interface UserIdParam {
+    type: number
+    code: string
+    state: string
+}
+const getUserIdByDD = async (data: Required<UserIdParam>) => {
+    return reqest.post({
+        url: '/system/auth/social-login',
+        data: data
+    })
+}
+const getUserInfoById = async (idStr: Required<string>) => {
+    return reqest.get({
+        url: '/system/user/get',
+        params: {id: idStr}
+    })
+}
 </script>
 
 <template>

+ 10 - 0
client_h5/src/stores/index.ts

@@ -0,0 +1,10 @@
+import type {App} from "vue"
+import { createPinia } from 'pinia'
+
+const store = createPinia()
+
+export const setupStore = (app: App<Element>) => {
+  app.use(store)
+}
+
+export { store }

+ 38 - 0
client_h5/src/stores/modules/user.ts

@@ -0,0 +1,38 @@
+import { defineStore } from "pinia";
+
+interface UserInterface {
+    username: string
+    nickname: string
+    deptId: string
+    deptIds: string[]
+    email: string
+    mobile: string
+    sex: string
+    avatar: string
+}
+const UserStore = defineStore("UserStore", {
+    state: () => ({
+        userInfo: {
+            username: "",
+            nickname: "",
+            deptId: "",
+            deptIds: [],
+            email: "",
+            mobile: "",
+            sex: "",
+            avatar: ""
+        }
+    }),
+    getters: {
+        getUser(state) {
+            return state.userInfo;
+        }
+    },
+    actions: {
+        setUser(user: UserInterface) {
+            this.userInfo = user;
+        }
+    }
+});
+
+export default UserStore;

+ 17 - 0
client_h5/src/utils/common.ts

@@ -3,4 +3,21 @@
  * **/
 export const getAssetsURI = (uri: string): string => {
   return new URL(uri, import.meta.url).href;
+}
+
+/**
+ * 获取URL字符串GET传参参数
+ * **/
+export const getUrlParams: (url: Required<string>)=>Map<string, string> | null = (url) => {
+    const params: string = url.split("?")[1];
+    if (params) {
+        const arr = params.split("&");
+        const map: Map<string, string> = new Map();
+        arr.forEach((item) => {
+            const arr2 = item.split("=");
+            map.set(arr2[0], arr2[1]);
+        });
+        return map;
+    }
+    return null;
 }

+ 29 - 0
client_h5/src/utils/request.ts

@@ -0,0 +1,29 @@
+import axios from 'axios';
+ 
+ 
+const defaultConfig = {
+  baseURL:'http://10.10.10.7:48080/admin-api/'
+};
+ 
+const instance = axios.create(Object.assign({}, defaultConfig));
+ 
+instance.interceptors.request.use(
+    (config) => {
+        return config;
+    },
+    (error) => {
+        return Promise.reject(error);
+    }
+);
+instance.interceptors.response.use(
+    (response) => {
+        if (response.data) { 
+            return response.data;
+        }
+        return response;
+    },
+    (error) => {
+        return Promise.reject(error);
+    }
+);
+export default instance;