|
|
@@ -1,64 +1,95 @@
|
|
|
-// src/stores/index.ts
|
|
|
import { defineStore } from 'pinia';
|
|
|
-import { getUserInfo,login } from '@/apis/user'
|
|
|
+import { ref, computed } from 'vue';
|
|
|
+import { getUserInfo, login } from '@/apis/user';
|
|
|
+import tokenInfo from '@/stores/modules/token';
|
|
|
|
|
|
|
|
|
-const useUserInfo = defineStore('userInfo', {
|
|
|
- state: () => ({
|
|
|
- token: '',
|
|
|
- loginShow:false,
|
|
|
- userInfo: {},
|
|
|
- }),
|
|
|
- actions: {
|
|
|
- // 添加必要的 actions
|
|
|
- updateUserInfo(data: any ) {
|
|
|
- this.userInfo = data;
|
|
|
- },
|
|
|
- // 添加必要的 actions
|
|
|
- updateToken(data: string ) {
|
|
|
- this.token = data;
|
|
|
- },
|
|
|
- updateLoginShow(data: boolean) {
|
|
|
- this.loginShow = data;
|
|
|
- },
|
|
|
- async login(data: any ) {
|
|
|
- return new Promise(async (resolve, reject) => {
|
|
|
- const res = await login(data).catch(error => {
|
|
|
- reject(error)
|
|
|
- })
|
|
|
- await this.updateToken(res.data.token)
|
|
|
- // this.updateLoginShow(false)
|
|
|
- resolve(res)
|
|
|
- })
|
|
|
- },
|
|
|
- async getInfo(){
|
|
|
- return new Promise(async (resolve, reject) => {
|
|
|
- const res = await getUserInfo()
|
|
|
- const { data } = res
|
|
|
- if (!data) {
|
|
|
- this.updateToken({})
|
|
|
- reject('请重新登录!')
|
|
|
- }
|
|
|
- this.updateUserInfo(data)
|
|
|
- resolve(data)
|
|
|
+export const useUserInfo = defineStore('userInfo', () => {
|
|
|
+ // 状态(State)
|
|
|
|
|
|
- })
|
|
|
- },
|
|
|
- loginExtra(){
|
|
|
+ const tokenInfoStore = new tokenInfo()
|
|
|
|
|
|
- }
|
|
|
- },
|
|
|
- getters: {
|
|
|
- // 添加必要的 getters
|
|
|
- getToken(state): string {
|
|
|
- return state.token;
|
|
|
- }
|
|
|
- },
|
|
|
- persist: {
|
|
|
- key: 'userinfo',
|
|
|
- storage: localStorage,
|
|
|
- paths: ['token']
|
|
|
+ const loginShow = ref(false); // 控制登录弹窗的显示状态
|
|
|
+ const userInfo = ref({}); // 用户的详细信息
|
|
|
+
|
|
|
+ // Actions(操作)
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户信息。
|
|
|
+ *
|
|
|
+ * @param {any} data - 新的用户信息数据。
|
|
|
+ */
|
|
|
+ const updateUserInfo = (data: any) => {
|
|
|
+ userInfo.value = data;
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户的登录令牌。
|
|
|
+ *
|
|
|
+ * @param {string} data - 新的登录令牌。
|
|
|
+ */
|
|
|
+ const updateToken = (data: string) => {
|
|
|
+ tokenInfoStore.updateToken(data)
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新登录弹窗的显示状态。
|
|
|
+ *
|
|
|
+ * @param {boolean} data - 是否显示登录弹窗。
|
|
|
+ */
|
|
|
+ const updateLoginShow = (data: boolean) => {
|
|
|
+ loginShow.value = data;
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行用户登录操作。
|
|
|
+ *
|
|
|
+ * @param {any} data - 登录所需的用户凭据。
|
|
|
+ * @returns {Promise<any>} 登录接口返回的结果。
|
|
|
+ * @throws {Error} 如果登录失败,抛出错误。
|
|
|
+ */
|
|
|
+ const loginAction = async (data: any) => {
|
|
|
+ try {
|
|
|
+ const res = await login(data); // 调用登录接口
|
|
|
+ await updateToken(res.data.token); // 更新登录令牌
|
|
|
+ await getInfo(); // 获取用户信息
|
|
|
+ return res;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('登录失败:', error);
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户信息并更新状态。
|
|
|
+ *
|
|
|
+ * @returns {Promise<any>} 用户信息数据。
|
|
|
+ * @throws {Error} 如果获取用户信息失败,抛出错误。
|
|
|
+ */
|
|
|
+ const getInfo = async () => {
|
|
|
+ try {
|
|
|
+ const res = await getUserInfo(); // 调用获取用户信息接口
|
|
|
+ const { data } = res;
|
|
|
+ if (!data) {
|
|
|
+ updateToken(''); // 如果没有数据,清空令牌
|
|
|
+ throw new Error('请重新登录!');
|
|
|
+ }
|
|
|
+ updateUserInfo(data); // 更新用户信息
|
|
|
+ return data;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取用户信息失败:', error);
|
|
|
+ throw error;
|
|
|
}
|
|
|
+ };
|
|
|
+
|
|
|
+ return {
|
|
|
+ loginShow,
|
|
|
+ userInfo,
|
|
|
+ updateUserInfo,
|
|
|
+ updateLoginShow,
|
|
|
+ loginAction,
|
|
|
+ getInfo,
|
|
|
+ };
|
|
|
});
|
|
|
|
|
|
export default useUserInfo;
|