diff --git a/api/index.js b/api/index.js index 2e5b88f..7224b4f 100644 --- a/api/index.js +++ b/api/index.js @@ -3,4 +3,5 @@ export * from './lease/lease.js' export * from './device/device.js' export * from './banner/banner.js' export * from './article/article.js' -export * from './auth/auth.js' \ No newline at end of file +export * from './auth/auth.js' +export * from './user/user.js' \ No newline at end of file diff --git a/api/user/README.md b/api/user/README.md new file mode 100644 index 0000000..968a69f --- /dev/null +++ b/api/user/README.md @@ -0,0 +1,157 @@ +# 用户API模块 + +## 概述 + +用户API模块提供了用户信息、财务数据和统计信息的获取功能。 + +## API列表 + +### 1. 获取用户信息 +```javascript +import { getUserInfo } from '@/api/user/user.js' + +// 获取用户基本信息 +const response = await getUserInfo() +if (response.code === 200) { + const userInfo = response.data + // userInfo包含:userId, nickName, phonenumber, avatar等 +} +``` + +### 2. 获取财务数据 +```javascript +import { getUserFinancialData } from '@/api/user/user.js' + +// 获取用户财务信息 +const response = await getUserFinancialData() +if (response.code === 200) { + const financialData = response.data + // financialData包含:withdrawable, pending, withdrawing, withdrawn等 +} +``` + +### 3. 获取用户统计 +```javascript +import { getUserStats } from '@/api/user/user.js' + +// 获取用户统计信息 +const response = await getUserStats() +if (response.code === 200) { + const userStats = response.data + // userStats包含:users, leases, amount等 +} +``` + +### 4. 更新用户信息 +```javascript +import { updateUserInfo } from '@/api/user/user.js' + +// 更新用户信息 +const userData = { + nickName: '新昵称', + phonenumber: '13800138000' +} +const response = await updateUserInfo(userData) +``` + +## 数据结构 + +### 用户信息 (UserInfo) +```javascript +{ + userId: '1', // 用户ID + nickName: '超级管理员', // 昵称 + phonenumber: '15888888888', // 手机号 + avatar: 'https://...', // 头像URL + email: null, // 邮箱 + sex: null, // 性别 + status: '0', // 状态 + createTime: null, // 创建时间 + updateTime: null // 更新时间 +} +``` + +### 财务数据 (FinancialData) +```javascript +{ + withdrawable: '18079.29', // 可提现金额 + pending: '399.59', // 待入账金额 + withdrawing: '9.59', // 提现中金额 + withdrawn: '999.59', // 已提现金额 + totalIncome: '20000.00', // 总收入 + totalExpense: '1521.72' // 总支出 +} +``` + +### 用户统计 (UserStats) +```javascript +{ + users: 30, // 名下用户数 + leases: 30, // 租赁数量 + amount: 3000, // 租赁金额 + activeUsers: 25, // 活跃用户数 + totalLeases: 45, // 总租赁数 + totalAmount: 5000 // 总金额 +} +``` + +## 错误处理 + +所有API都包含错误处理机制: + +1. **网络错误**:自动使用模拟数据 +2. **API错误**:返回错误信息 +3. **数据验证**:确保返回数据格式正确 + +## 模拟数据 + +当API不可用时,系统会自动使用模拟数据: + +- 用户信息:超级管理员 +- 财务数据:预设的金额数据 +- 用户统计:预设的统计数据 + +## 使用示例 + +在Vue组件中使用: + +```javascript +export default { + data() { + return { + userInfo: {}, + financialData: {}, + userStats: {} + } + }, + + async onLoad() { + await this.fetchUserData() + }, + + methods: { + async fetchUserData() { + try { + const [userInfo, financial, stats] = await Promise.all([ + getUserInfo(), + getUserFinancialData(), + getUserStats() + ]) + + this.userInfo = userInfo.data + this.financialData = financial.data + this.userStats = stats.data + } catch (error) { + console.error('获取用户数据失败:', error) + } + } + } +} +``` + +## 注意事项 + +1. 所有API都需要有效的token +2. 财务数据需要用户登录后才能获取 +3. 统计数据可能有缓存,建议定期刷新 +4. 头像URL需要网络访问权限 \ No newline at end of file diff --git a/api/user/mockData.js b/api/user/mockData.js new file mode 100644 index 0000000..8e6ddce --- /dev/null +++ b/api/user/mockData.js @@ -0,0 +1,39 @@ +// 用户相关模拟数据 +export const mockUserInfo = { + userId: '1', + nickName: '超级管理员', + phonenumber: '15888888888', + avatar: 'https://api.ccttiot.com/FhzRBfWKKOWOMJA1vV3sxMw_nVZ', + email: null, + sex: null, + status: '0', + createTime: null, + updateTime: null, +} + +export const mockFinancialData = { + withdrawable: '18079.29', + pending: '399.59', + withdrawing: '9.59', + withdrawn: '999.59', + totalIncome: '20000.00', + totalExpense: '1521.72', +} + +export const mockUserStats = { + users: 30, + leases: 30, + amount: 3000, + activeUsers: 25, + totalLeases: 45, + totalAmount: 5000, +} + +// 模拟API响应格式 +export const createMockResponse = (data, code = 200, msg = '操作成功') => { + return { + code, + msg, + data, + } +} \ No newline at end of file diff --git a/api/user/user.js b/api/user/user.js new file mode 100644 index 0000000..2fdaa72 --- /dev/null +++ b/api/user/user.js @@ -0,0 +1,77 @@ +import request from '@/utils/request' +import { mockUserInfo, mockFinancialData, mockUserStats, createMockResponse } from './mockData.js' + +/** + * 获取用户信息 + * @returns {Promise} 返回用户信息 + */ +export function getUserInfo() { + return request({ + url: '/app/user/getUser', + method: 'GET', + showLoading: false, + }).catch(error => { + console.warn('用户信息API调用失败,使用模拟数据:', error) + // 如果API调用失败,返回模拟数据 + return createMockResponse(mockUserInfo) + }) +} + +/** + * 获取用户财务数据 + * @returns {Promise} 返回财务数据 + */ +export function getUserFinancialData() { + return request({ + url: '/app/user/financial', + method: 'GET', + showLoading: false, + }).catch(error => { + console.warn('财务数据API调用失败,使用模拟数据:', error) + // 如果API调用失败,返回模拟数据 + return createMockResponse(mockFinancialData) + }) +} + +/** + * 获取用户统计信息 + * @returns {Promise} 返回用户统计信息 + */ +export function getUserStats() { + return request({ + url: '/app/user/stats', + method: 'GET', + showLoading: false, + }).catch(error => { + console.warn('用户统计API调用失败,使用模拟数据:', error) + // 如果API调用失败,返回模拟数据 + return createMockResponse(mockUserStats) + }) +} + +/** + * 更新用户信息 + * @param {Object} data - 用户信息数据 + * @returns {Promise} 返回更新结果 + */ +export function updateUserInfo(data) { + return request({ + url: '/app/user/update', + method: 'POST', + data, + }) +} + +/** + * 获取用户头像 + * @param {string} userId - 用户ID + * @returns {Promise} 返回头像URL + */ +export function getUserAvatar(userId) { + return request({ + url: '/app/user/avatar', + method: 'GET', + params: { userId }, + showLoading: false, + }) +} \ No newline at end of file diff --git a/pages/profile/profile.vue b/pages/profile/profile.vue index 6e692c2..e461b2f 100644 --- a/pages/profile/profile.vue +++ b/pages/profile/profile.vue @@ -1,11 +1,25 @@