From 7db4e38c6d967d511f0c1248bf22ceaf6a6f55d3 Mon Sep 17 00:00:00 2001
From: zhuguifei <zhuguifei@zhuguifeideiMac.local>
Date: 星期五, 18 四月 2025 18:33:24 +0800
Subject: [PATCH] 配置http,完成登录信息缓存,添加部分pinia配置

---
 eims-ui-mobile/src/store/access.ts         |    2 
 eims-ui-mobile/src/service/menu.d.ts       |   36 +++++++
 eims-ui-mobile/src/interceptors/request.ts |    8 +
 eims-ui-mobile/src/static/menu/menu2.png   |    0 
 eims-ui-mobile/src/service/login.d.ts      |   71 ++++++++++++++
 eims-ui-mobile/src/service/login.ts        |   13 +-
 eims-ui-mobile/src/service/menu.ts         |    6 +
 eims-ui-mobile/src/static/images/pic1.png  |    0 
 eims-ui-mobile/src/store/user.ts           |   11 +-
 eims-ui-mobile/src/pages/login/index.vue   |   76 +++++++++++---
 eims-ui-mobile/src/static/menu/menu1.png   |    0 
 eims-ui-mobile/src/static/images/pic4.jpeg |    0 
 eims-ui-mobile/src/static/images/menu1.png |    0 
 eims-ui-mobile/src/static/images/pic3.png  |    0 
 eims-ui-mobile/src/store/system-config.ts  |   37 +++++++
 eims-ui-mobile/src/static/images/pic2.jpg  |    0 
 eims-ui-mobile/src/utils/http.ts           |    4 
 eims-ui-mobile/src/pages/home/index.vue    |   19 +--
 eims-ui-mobile/src/store/index.ts          |    1 
 19 files changed, 239 insertions(+), 45 deletions(-)

diff --git a/eims-ui-mobile/src/interceptors/request.ts b/eims-ui-mobile/src/interceptors/request.ts
index 1f84b31..5ea4b62 100644
--- a/eims-ui-mobile/src/interceptors/request.ts
+++ b/eims-ui-mobile/src/interceptors/request.ts
@@ -1,8 +1,9 @@
 /* eslint-disable no-param-reassign */
 import qs from 'qs'
-import { useUserStore } from '@/store'
+import { useAccessStore } from '@/store'
 import { platform } from '@/utils/platform'
 import { getEnvBaseUrl } from '@/utils'
+const clientId = import.meta.env.VITE_APP_CLIENT_ID
 
 export type CustomRequestOptions = UniApp.RequestOptions & {
   query?: Record<string, any>
@@ -50,10 +51,11 @@
       ...options.header,
     }
     // 3. 娣诲姞 token 璇锋眰澶存爣璇�
-    const userStore = useUserStore()
-    const { token } = userStore.userInfo as unknown as IUserInfo
+    const accessStore = useAccessStore()
+    const token = accessStore.accessInfo.access_token
     if (token) {
       options.header.Authorization = `Bearer ${token}`
+      options.header.clientid = clientId
     }
   },
 }
diff --git a/eims-ui-mobile/src/pages/home/index.vue b/eims-ui-mobile/src/pages/home/index.vue
index 636a1e2..cb1e18e 100644
--- a/eims-ui-mobile/src/pages/home/index.vue
+++ b/eims-ui-mobile/src/pages/home/index.vue
@@ -128,8 +128,7 @@
 </template>
 
 <script lang="ts" setup>
-import { TestEnum } from '@/typings'
-import PLATFORM from '@/utils/platform'
+import { getAllMenusApi } from '@/service/menu'
 
 defineOptions({
   name: 'Home',
@@ -137,14 +136,14 @@
 
 // 鑾峰彇灞忓箷杈圭晫鍒板畨鍏ㄥ尯鍩熻窛绂�
 const { safeAreaInsets } = uni.getSystemInfoSync()
-const author = ref('鑿查附')
-const description = ref(
-  'unibest 鏄竴涓泦鎴愪簡澶氱宸ュ叿鍜屾妧鏈殑 uniapp 寮�鍙戞ā鏉匡紝鐢� uniapp + Vue3 + Ts + Vite4 + UnoCss + UniUI + VSCode 鏋勫缓锛屾ā鏉垮叿鏈変唬鐮佹彁绀恒�佽嚜鍔ㄦ牸寮忓寲銆佺粺涓�閰嶇疆銆佷唬鐮佺墖娈电瓑鍔熻兘锛屽苟鍐呯疆浜嗚澶氬父鐢ㄧ殑鍩烘湰缁勪欢鍜屽熀鏈姛鑳斤紝璁╀綘缂栧啓 uniapp 鎷ユ湁 best 浣撻獙銆�',
-)
-// 娴嬭瘯 uni API 鑷姩寮曞叆
+
+const getAllMenus = async () => {
+  const menuList = await getAllMenusApi()
+  console.error(menuList)
+}
+
 onLoad(() => {
-  console.log(author)
-  console.log(TestEnum.A)
+  // getAllMenus()
 })
 
 const menuList = reactive([
@@ -161,7 +160,7 @@
 ])
 
 function handleUserInfo() {
-  console.error('12121')
+  getAllMenus()
 }
 
 
diff --git a/eims-ui-mobile/src/pages/login/index.vue b/eims-ui-mobile/src/pages/login/index.vue
index f26a05d..46626e7 100644
--- a/eims-ui-mobile/src/pages/login/index.vue
+++ b/eims-ui-mobile/src/pages/login/index.vue
@@ -13,7 +13,7 @@
     <wd-cell-group border>
       <wd-input
         label="鐢ㄦ埛鍚�"
-        label-width="100px"
+        label-width="200rpx"
         prop="username"
         clearable
         v-model="model.username"
@@ -22,7 +22,7 @@
       />
       <wd-input
         label="瀵嗙爜"
-        label-width="100px"
+        label-width="200rpx"
         prop="password"
         show-password
         clearable
@@ -31,28 +31,43 @@
         :rules="[{ required: true, message: '璇峰~鍐欏瘑鐮�' }]"
       />
     </wd-cell-group>
-    <view class="footer mt-6">
-      <wd-button type="primary" size="large" @click="handleSubmit" block>鎻愪氦</wd-button>
+    <view class="footer">
+      <view class="w-full text-end">
+        <wd-checkbox v-model="rember" shape="square" @change="handleChange">
+          璁颁綇瀵嗙爜
+        </wd-checkbox>
+      </view>
+      <wd-button class="mt-6" type="primary" size="large" @click="handleSubmit" block>
+        鎻愪氦
+      </wd-button>
     </view>
   </wd-form>
 </template>
 
 <script setup lang="ts">
 import { currRoute } from '@/utils'
-import { useUserStore, useAccessStore } from '@/store'
+import { useUserStore, useAccessStore, useSystemConfigStore } from '@/store'
 import { useToast } from 'wot-design-uni'
-import { login } from '@/service/login'
+import { login, getUserInfo } from '@/service/login'
+import type { UserInfo } from '@/service/login.d'
+import { TestEnum } from '@/typings'
 const userStore = useUserStore()
 const accessStore = useAccessStore()
+const configStore = useSystemConfigStore()
 const { success: showSuccess } = useToast()
 
 const model = reactive<{
   username: string
   password: string
 }>({
-  username: 'admin',
-  password: 'admin123',
+  username: '',
+  password: '',
 })
+const rember = ref<boolean>(false)
+
+function handleChange({ value }) {
+  console.log(value)
+}
 
 const form = ref()
 function handleSubmit() {
@@ -67,21 +82,48 @@
       console.log(error, 'error')
     })
 }
+
+onLoad(() => {
+  const { remberPassword, username, password } = configStore.systemConfigInfo
+  if (remberPassword) {
+    rember.value = true
+    model.username = username
+    model.password = password
+  }
+})
+
 const toLogin = async () => {
+  // 璁颁綇瀵嗙爜
+  if (rember.value) {
+    configStore.setConfigInfo({ ...model, ...{ remberPassword: true } })
+  }
+
   const res = await login(model)
-  console.error(res)
   accessStore.setAccessInfo(res as any)
-  console.error(accessStore.accessInfo.access_token)
-  // userStore.setUserInfo({ nickname: '鑿查附', avatar: '', token: 'abcdef' })
+  const backUserInfo: any = await getUserInfo()
+  /**
+   * 鐧诲綍瓒呮椂鐨勬儏鍐�
+   */
+  if (!backUserInfo) {
+    throw new Error('鑾峰彇鐢ㄦ埛淇℃伅澶辫触.')
+  }
+  const { permissions = [], roles = [], user } = backUserInfo
+  /**
+   * 浠庡悗鍙皍ser -> vben user杞崲
+   */
+  const userInfo: UserInfo = {
+    avatar: user.avatar ?? '',
+    permissions,
+    realName: user.nickName,
+    roles,
+    userId: user.userId,
+    deptId: user.deptId,
+    username: user.userName,
+  }
+  userStore.setUserInfo(userInfo)
   const { query } = currRoute()
-  console.error(query.redirect)
   uni.switchTab({ url: query.redirect })
 }
-
-
-
-
-
 </script>
 
 <style scoped lang="scss">
diff --git a/eims-ui-mobile/src/service/login.d.ts b/eims-ui-mobile/src/service/login.d.ts
index 04cec7d..1b73077 100644
--- a/eims-ui-mobile/src/service/login.d.ts
+++ b/eims-ui-mobile/src/service/login.d.ts
@@ -5,3 +5,74 @@
   tenantId?: string;
   grantType?: string;
 }
+
+
+export interface User {
+  avatar: string;
+  createTime: string;
+  deptId: number;
+  deptName: string;
+  email: string;
+  loginDate: string;
+  loginIp: string;
+  nickName: string;
+  phonenumber: string;
+  remark: string;
+  roles: Role[];
+  sex: string;
+  status: string;
+  tenantId: string;
+  userId: number;
+  userName: string;
+  userType: string;
+}
+
+
+export interface UserInfoResp {
+  permissions: string[];
+  roles: string[];
+  user: User;
+}
+
+
+
+interface BasicUserInfo {
+  /**
+   * 澶村儚
+   */
+  avatar: string;
+  /**
+   * 閮ㄩ棬id
+   */
+  deptId: number;
+  /**
+   * 鐢ㄦ埛鏉冮檺
+   */
+  permissions: string[];
+  /**
+   * 鐢ㄦ埛鏄电О
+   */
+  realName: string;
+  /**
+   * 鐢ㄦ埛瑙掕壊
+   */
+  roles: string[];
+  /**
+   * 鐢ㄦ埛id
+   */
+  userId: number | string;
+
+  /**
+   * 鐢ㄦ埛鍚�
+   */
+  username: string;
+}
+
+
+/** 鐢ㄦ埛淇℃伅 */
+interface UserInfo extends BasicUserInfo {
+  /**
+   * 鎷撳睍浣跨敤
+   */
+  [key: string]: any;
+}
diff --git a/eims-ui-mobile/src/service/login.ts b/eims-ui-mobile/src/service/login.ts
index 05a4c58..330a514 100644
--- a/eims-ui-mobile/src/service/login.ts
+++ b/eims-ui-mobile/src/service/login.ts
@@ -1,16 +1,11 @@
 import { http } from '@/utils/http'
-import type { LoginParams } from './login.d'
+import type { LoginParams, UserInfoResp } from './login.d'
 const clientId = import.meta.env.VITE_APP_CLIENT_ID
 
 const DEFAULT_TENANT_ID = '000000'
 const GRANT_TYPE = 'password'
 
-/** GET 璇锋眰 */
-export const getFooAPI = (name: string) => {
-  return http.get<LoginParams>('/foo', { name })
-}
-
-/** POST 璇锋眰 */
+/** get 璇锋眰 */
 export const login = (params: LoginParams) => {
   const { username, password } = params
   // 鏋勯�犳柊鐨勮姹傚弬鏁帮紝閬垮厤鐩存帴淇敼鍘熷瀵硅薄
@@ -23,3 +18,7 @@
   }
   return http.post<any>('/auth/login', requestData)
 }
+
+export const getUserInfo = () => {
+  return http.get<null | UserInfoResp>('/system/user/getInfo')
+}
diff --git a/eims-ui-mobile/src/service/menu.d.ts b/eims-ui-mobile/src/service/menu.d.ts
new file mode 100644
index 0000000..05683d4
--- /dev/null
+++ b/eims-ui-mobile/src/service/menu.d.ts
@@ -0,0 +1,36 @@
+/**
+ * @description: 鑿滃崟meta
+ * @param title 鑿滃崟鍚�
+ * @param icon 鑿滃崟鍥炬爣
+ * @param noCache 鏄惁涓嶇紦瀛�
+ * @param link 澶栭摼閾炬帴
+ */
+export interface MenuMeta {
+  icon: string;
+  link?: string;
+  noCache: boolean;
+  title: string;
+}
+
+/**
+ * @description: 鑿滃崟
+ * @param name 鑿滃崟鍚�
+ * @param path 鑿滃崟璺緞
+ * @param hidden 鏄惁闅愯棌
+ * @param component 缁勪欢鍚嶇О Laout
+ * @param alwaysShow 鎬绘槸鏄剧ず
+ * @param query 璺敱鍙傛暟(json褰㈠紡)
+ * @param meta 璺敱淇℃伅
+ * @param children 瀛愯矾鐢变俊鎭�
+ */
+export interface Menu {
+  alwaysShow?: boolean;
+  children: Menu[];
+  component: string;
+  hidden: boolean;
+  meta: MenuMeta;
+  name: string;
+  path: string;
+  query?: string;
+  redirect?: string;
+}
diff --git a/eims-ui-mobile/src/service/menu.ts b/eims-ui-mobile/src/service/menu.ts
new file mode 100644
index 0000000..852a126
--- /dev/null
+++ b/eims-ui-mobile/src/service/menu.ts
@@ -0,0 +1,6 @@
+import { http } from '@/utils/http'
+import type { Menu } from './menu.d'
+
+export const getAllMenusApi = () => {
+  return http.get<Menu>('/system/menu/getRouters')
+}
diff --git a/eims-ui-mobile/src/static/images/menu1.png b/eims-ui-mobile/src/static/images/menu1.png
new file mode 100644
index 0000000..6f2fa77
--- /dev/null
+++ b/eims-ui-mobile/src/static/images/menu1.png
Binary files differ
diff --git a/eims-ui-mobile/src/static/images/pic1.png b/eims-ui-mobile/src/static/images/pic1.png
new file mode 100644
index 0000000..90f69dd
--- /dev/null
+++ b/eims-ui-mobile/src/static/images/pic1.png
Binary files differ
diff --git a/eims-ui-mobile/src/static/images/pic2.jpg b/eims-ui-mobile/src/static/images/pic2.jpg
new file mode 100644
index 0000000..f4134d9
--- /dev/null
+++ b/eims-ui-mobile/src/static/images/pic2.jpg
Binary files differ
diff --git a/eims-ui-mobile/src/static/images/pic3.png b/eims-ui-mobile/src/static/images/pic3.png
new file mode 100644
index 0000000..a15b54f
--- /dev/null
+++ b/eims-ui-mobile/src/static/images/pic3.png
Binary files differ
diff --git a/eims-ui-mobile/src/static/images/pic4.jpeg b/eims-ui-mobile/src/static/images/pic4.jpeg
new file mode 100644
index 0000000..439d18e
--- /dev/null
+++ b/eims-ui-mobile/src/static/images/pic4.jpeg
Binary files differ
diff --git a/eims-ui-mobile/src/static/menu/menu1.png b/eims-ui-mobile/src/static/menu/menu1.png
new file mode 100644
index 0000000..6f2fa77
--- /dev/null
+++ b/eims-ui-mobile/src/static/menu/menu1.png
Binary files differ
diff --git a/eims-ui-mobile/src/static/menu/menu2.png b/eims-ui-mobile/src/static/menu/menu2.png
new file mode 100644
index 0000000..5720ac0
--- /dev/null
+++ b/eims-ui-mobile/src/static/menu/menu2.png
Binary files differ
diff --git a/eims-ui-mobile/src/store/access.ts b/eims-ui-mobile/src/store/access.ts
index 5b7e266..1f0a631 100644
--- a/eims-ui-mobile/src/store/access.ts
+++ b/eims-ui-mobile/src/store/access.ts
@@ -44,7 +44,7 @@
 }
 
 export const useAccessStore = defineStore(
-  'access',
+  'accessInfo',
   () => {
     const accessInfo = ref<AccessInfo>({ ...initState })
 
diff --git a/eims-ui-mobile/src/store/index.ts b/eims-ui-mobile/src/store/index.ts
index 7eca81f..c3bf4d9 100644
--- a/eims-ui-mobile/src/store/index.ts
+++ b/eims-ui-mobile/src/store/index.ts
@@ -16,3 +16,4 @@
 // 妯″潡缁熶竴瀵煎嚭
 export * from './user'
 export * from './access'
+export * from './system-config'
diff --git a/eims-ui-mobile/src/store/system-config.ts b/eims-ui-mobile/src/store/system-config.ts
new file mode 100644
index 0000000..024183f
--- /dev/null
+++ b/eims-ui-mobile/src/store/system-config.ts
@@ -0,0 +1,37 @@
+import { defineStore } from 'pinia'
+import { ref } from 'vue'
+
+const initState = {
+  remberPassword: false,
+  username: '',
+  password: '',
+}
+
+export const useSystemConfigStore = defineStore(
+  'systemConfig',
+  () => {
+    const systemConfigInfo = ref<any>({ ...initState })
+
+    const setConfigInfo = (val: any) => {
+      systemConfigInfo.value = val
+    }
+
+    const clearConfigInfo = () => {
+      systemConfigInfo.value = { ...initState }
+    }
+    // 涓�鑸病鏈塺eset闇�姹傦紝涓嶉渶瑕佺殑鍙互鍒犻櫎
+    const reset = () => {
+      systemConfigInfo.value = { ...initState }
+    }
+
+    return {
+      systemConfigInfo,
+      setConfigInfo,
+      clearConfigInfo,
+      reset,
+    }
+  },
+  {
+    persist: true,
+  },
+)
diff --git a/eims-ui-mobile/src/store/user.ts b/eims-ui-mobile/src/store/user.ts
index 82bd873..73058d3 100644
--- a/eims-ui-mobile/src/store/user.ts
+++ b/eims-ui-mobile/src/store/user.ts
@@ -1,14 +1,15 @@
 import { defineStore } from 'pinia'
 import { ref } from 'vue'
+import type { UserInfoResp, UserInfo } from '@/service/login.d'
 
-const initState = { nickname: '', avatar: '' }
+const initState = null
 
 export const useUserStore = defineStore(
-  'user',
+  'userInfo',
   () => {
-    const userInfo = ref<IUserInfo>({ ...initState })
+    const userInfo = ref<UserInfo>({ ...initState })
 
-    const setUserInfo = (val: IUserInfo) => {
+    const setUserInfo = (val: UserInfo) => {
       userInfo.value = val
     }
 
@@ -19,13 +20,11 @@
     const reset = () => {
       userInfo.value = { ...initState }
     }
-    const isLogined = computed(() => !!userInfo.value.token)
 
     return {
       userInfo,
       setUserInfo,
       clearUserInfo,
-      isLogined,
       reset,
     }
   },
diff --git a/eims-ui-mobile/src/utils/http.ts b/eims-ui-mobile/src/utils/http.ts
index f98f884..4106b0b 100644
--- a/eims-ui-mobile/src/utils/http.ts
+++ b/eims-ui-mobile/src/utils/http.ts
@@ -1,4 +1,5 @@
 import { CustomRequestOptions } from '@/interceptors/request'
+import { useUserStore, useAccessStore } from '@/store'
 
 export const http = <T>(options: CustomRequestOptions) => {
   // 1. 杩斿洖 Promise 瀵硅薄
@@ -24,7 +25,8 @@
           }
         } else if (res.statusCode === 401) {
           // 401閿欒  -> 娓呯悊鐢ㄦ埛淇℃伅锛岃烦杞埌鐧诲綍椤�
-          // userStore.clearUserInfo()
+          useAccessStore().clearAccessInfo()
+          useUserStore().clearUserInfo()
           uni.navigateTo({ url: '/pages/login/index' })
           reject(res)
         } else {

--
Gitblit v1.9.3