1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import Taro from '@tarojs/taro';
|
||||
import httpService, { ApiResponse } from './httpService';
|
||||
import httpService from './httpService';
|
||||
import tokenManager from '../utils/tokenManager';
|
||||
import { useUser } from '@/store/userStore';
|
||||
|
||||
// 微信用户信息接口
|
||||
export interface WechatUserInfo {
|
||||
@@ -34,25 +35,12 @@ export interface VerifyCodeResponse {
|
||||
token?: string;
|
||||
user_info?: WechatUserInfo;
|
||||
}
|
||||
// 用户详细信息
|
||||
export interface UserInfoType {
|
||||
id: number
|
||||
openid: string
|
||||
unionid: string
|
||||
session_key: string
|
||||
nickname: string
|
||||
avatar_url: string
|
||||
gender: string
|
||||
country: string
|
||||
province: string
|
||||
city: string
|
||||
language: string
|
||||
phone: string
|
||||
is_subscribed: string
|
||||
latitude: number
|
||||
longitude: number
|
||||
subscribe_time: string
|
||||
last_login_time: string
|
||||
|
||||
// 手机号验证码登录接口参数
|
||||
export interface PhoneLoginParams {
|
||||
phone: string;
|
||||
verification_code: string;
|
||||
user_code: string
|
||||
}
|
||||
|
||||
|
||||
@@ -76,6 +64,13 @@ export const wechat_auth_login = async (phone_code?: string): Promise<LoginRespo
|
||||
});
|
||||
|
||||
if (auth_response.code === 0) {
|
||||
// 登录成功后,更新用户信息到 store
|
||||
try {
|
||||
await useUser.getState().fetchUserInfo();
|
||||
} catch (error) {
|
||||
console.error('更新用户信息到 store 失败:', error);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: '微信登录成功',
|
||||
@@ -116,6 +111,13 @@ export const phone_auth_login = async (params: PhoneLoginParams): Promise<LoginR
|
||||
|
||||
|
||||
if (verify_response.code === 0) {
|
||||
// 登录成功后,更新用户信息到 store
|
||||
try {
|
||||
await useUser.getState().fetchUserInfo();
|
||||
} catch (error) {
|
||||
console.error('更新用户信息到 store 失败:', error);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: '登录成功',
|
||||
@@ -276,19 +278,6 @@ export const get_token_status = () => {
|
||||
}
|
||||
};
|
||||
|
||||
// 获取用户信息
|
||||
export const get_user_info = (): WechatUserInfo | null => {
|
||||
try {
|
||||
let userinfo = Taro.getStorageSync('user_info')
|
||||
if (userinfo) {
|
||||
return JSON.parse(userinfo)
|
||||
}
|
||||
return null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// 获取用户token
|
||||
export const get_user_token = (): string | null => {
|
||||
try {
|
||||
@@ -330,24 +319,9 @@ export const refresh_login_status = async (): Promise<boolean> => {
|
||||
}
|
||||
};
|
||||
|
||||
// 获取用户详细信息
|
||||
export const fetchUserProfile = async (): Promise<ApiResponse<UserInfoType>> => {
|
||||
try {
|
||||
const response = await httpService.post('user/detail');
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// 更新用户信息
|
||||
export const updateUserProfile = async (payload: Partial<UserInfoType>) => {
|
||||
try {
|
||||
const response = await httpService.post('/user/update', payload);
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('更新用户信息失败:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user