first commit
Some checks failed
Node CI / build (14.x, macOS-latest) (push) Has been cancelled
Node CI / build (14.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (14.x, windows-latest) (push) Has been cancelled
Node CI / build (16.x, macOS-latest) (push) Has been cancelled
Node CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (16.x, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
coverage CI / build (push) Has been cancelled
Node pnpm CI / build (16.x, macOS-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, windows-latest) (push) Has been cancelled
Some checks failed
Node CI / build (14.x, macOS-latest) (push) Has been cancelled
Node CI / build (14.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (14.x, windows-latest) (push) Has been cancelled
Node CI / build (16.x, macOS-latest) (push) Has been cancelled
Node CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (16.x, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
coverage CI / build (push) Has been cancelled
Node pnpm CI / build (16.x, macOS-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, windows-latest) (push) Has been cancelled
This commit is contained in:
71
src/models/jobportal/user.ts
Normal file
71
src/models/jobportal/user.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { getAppUserInfo } from '@/services/jobportal/user';
|
||||
import { message } from 'antd';
|
||||
|
||||
export default function useAppUserModel() {
|
||||
// 从缓存初始化用户信息
|
||||
const getCachedUserInfo = (): API.AppUserInfo | null => {
|
||||
try {
|
||||
const cached = localStorage.getItem('userInfo');
|
||||
if (cached) {
|
||||
return JSON.parse(cached);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('读取缓存用户信息失败:', error);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const [userInfo, setUserInfo] = useState<API.AppUserInfo | null>(getCachedUserInfo());
|
||||
const [loading, setLoading] = useState<boolean>(false);
|
||||
|
||||
// 获取用户信息
|
||||
const fetchUserInfo = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await getAppUserInfo();
|
||||
if (response?.code === 200 && response?.data) {
|
||||
setUserInfo(response.data);
|
||||
// 保存到缓存
|
||||
try {
|
||||
localStorage.setItem('userInfo', JSON.stringify(response.data));
|
||||
} catch (error) {
|
||||
console.error('保存用户信息到缓存失败:', error);
|
||||
}
|
||||
return response.data;
|
||||
} else {
|
||||
message.error(response?.msg || '获取用户信息失败');
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error);
|
||||
message.error('获取用户信息失败');
|
||||
return null;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// 更新用户信息
|
||||
const updateUserInfo = useCallback((info: API.AppUserInfo | null) => {
|
||||
setUserInfo(info);
|
||||
// 同时更新缓存
|
||||
if (info) {
|
||||
try {
|
||||
localStorage.setItem('userInfo', JSON.stringify(info));
|
||||
} catch (error) {
|
||||
console.error('更新用户信息到缓存失败:', error);
|
||||
}
|
||||
} else {
|
||||
localStorage.removeItem('userInfo');
|
||||
}
|
||||
}, []);
|
||||
|
||||
return {
|
||||
userInfo,
|
||||
loading,
|
||||
fetchUserInfo,
|
||||
updateUserInfo,
|
||||
};
|
||||
}
|
||||
|
||||
11
src/models/testModel.ts
Normal file
11
src/models/testModel.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
// src/models/counterModel.ts
|
||||
import { useCallback, useState } from 'react';
|
||||
|
||||
export default function Page() {
|
||||
const [counter, setCounter] = useState(0);
|
||||
|
||||
const increment = useCallback(() => setCounter((c) => c + 1), []);
|
||||
const decrement = useCallback(() => setCounter((c) => c - 1), []);
|
||||
|
||||
return { counter, increment, decrement };
|
||||
}
|
||||
Reference in New Issue
Block a user