256 lines
6.3 KiB
JavaScript
256 lines
6.3 KiB
JavaScript
import request from '@/utils/request'
|
||
import {
|
||
mockUserInfo,
|
||
mockFinancialData,
|
||
mockAgentStats,
|
||
mockAgentList,
|
||
mockWithdrawInfo,
|
||
mockBanks,
|
||
createMockResponse,
|
||
} from './mockData.js'
|
||
import { uploadFile } from '@/utils/request.js'
|
||
|
||
/**
|
||
* 获取用户信息
|
||
* @returns {Promise} 返回用户信息
|
||
*/
|
||
export function getUserInfo() {
|
||
return request({
|
||
url: '/app/user/getUser',
|
||
method: 'GET',
|
||
showLoading: false,
|
||
})
|
||
.then(response => {
|
||
// 如果API调用成功,将用户ID存入本地存储
|
||
if (response.code === 200 && response.data && response.data.userId) {
|
||
uni.setStorageSync('userId', response.data.userId)
|
||
console.log('用户ID已存入本地存储:', response.data.userId)
|
||
}
|
||
return response
|
||
})
|
||
.catch(error => {
|
||
console.warn('用户信息API调用失败,使用模拟数据:', error)
|
||
// 如果API调用失败,返回模拟数据
|
||
const mockResponse = createMockResponse(mockUserInfo)
|
||
// 模拟数据中如果有用户ID,也存入本地存储
|
||
if (mockResponse.data && mockResponse.data.userId) {
|
||
uni.setStorageSync('userId', mockResponse.data.userId)
|
||
console.log('模拟数据用户ID已存入本地存储:', mockResponse.data.userId)
|
||
}
|
||
return mockResponse
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取用户财务数据
|
||
* @returns {Promise} 返回财务数据
|
||
*/
|
||
export function getUserFinancialData() {
|
||
return request({
|
||
url: '/app/user/getBill',
|
||
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(mockAgentStats)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取代理统计数据
|
||
* @returns {Promise} 返回代理统计信息
|
||
*/
|
||
export function getAgentCount() {
|
||
return request({
|
||
url: '/app/order/agentCount',
|
||
method: 'GET',
|
||
showLoading: false,
|
||
}).catch(error => {
|
||
console.warn('代理统计API调用失败,使用模拟数据:', error)
|
||
// 如果API调用失败,返回模拟数据
|
||
return createMockResponse({
|
||
userNum: 4,
|
||
deviceNum: 1,
|
||
rentAmount: 2,
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取代理用户列表
|
||
* @param {Object} params - 查询参数
|
||
* @param {string} params.beginTime - 开始时间
|
||
* @param {string} params.endTime - 结束时间
|
||
* @param {string} params.name - 用户昵称搜索
|
||
* @returns {Promise}
|
||
*/
|
||
export function getAgentList(params = {}) {
|
||
return request({
|
||
url: '/app/order/agentList',
|
||
method: 'GET',
|
||
params,
|
||
showLoading: false,
|
||
}).catch(error => {
|
||
console.warn('代理用户列表API调用失败,使用模拟数据:', error)
|
||
// 如果API调用失败,返回模拟数据
|
||
return createMockResponse(mockAgentList)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取用户列表 (使用代理列表接口)
|
||
* @param {Object} params - 查询参数
|
||
* @returns {Promise}
|
||
*/
|
||
export function getUserList(params = {}) {
|
||
return getAgentList(params)
|
||
}
|
||
|
||
/**
|
||
* 更新用户信息
|
||
* @param {Object} data - 用户信息数据
|
||
* @returns {Promise} 返回更新结果
|
||
*/
|
||
export function updateUserInfo(data) {
|
||
return request({
|
||
url: '/app/user/update',
|
||
method: 'POST',
|
||
data,
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 更新用户昵称
|
||
* @param {string} nickName - 新的昵称
|
||
* @returns {Promise} 返回更新结果
|
||
*/
|
||
export function updateNickName(nickName) {
|
||
return request({
|
||
url: '/app/user/updateNickName',
|
||
method: 'PUT',
|
||
params: { nickName },
|
||
showLoading: true,
|
||
// 强制将params作为查询参数发送
|
||
useQueryParams: true,
|
||
}).catch(error => {
|
||
console.warn('更新昵称API调用失败:', error)
|
||
throw error
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取提现信息
|
||
* @returns {Promise} 返回提现相关信息
|
||
*/
|
||
export function getWithdrawInfo() {
|
||
return request({
|
||
url: '/app/withdraw',
|
||
method: 'GET',
|
||
showLoading: false,
|
||
}).catch(error => {
|
||
console.warn('提现信息API调用失败,使用模拟数据:', error)
|
||
return createMockResponse(mockWithdrawInfo)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取服务费用明细
|
||
* @returns {Promise} 返回服务费用明细
|
||
*/
|
||
export function computedServiceAmount(amount) {
|
||
return request({
|
||
url: '/app/withdraw/serviceAmount',
|
||
method: 'GET',
|
||
showLoading: false,
|
||
params: { amount },
|
||
}).catch(error => {
|
||
console.warn('服务费用API调用失败,使用模拟数据:', error)
|
||
return createMockResponse(mockWithdrawInfo)
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 提交提现申请
|
||
* @param {Object} data 提现数据
|
||
* @param {number} data.amount 提现金额
|
||
* @param {string} data.bankId 银行ID
|
||
* @returns {Promise} 返回提现申请结果
|
||
*/
|
||
export function submitWithdraw(data) {
|
||
return request({
|
||
url: '/app/withdraw',
|
||
method: 'POST',
|
||
data,
|
||
showLoading: true,
|
||
}).catch(error => {
|
||
console.warn('提现申请API调用失败:', error)
|
||
throw error
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 上传头像
|
||
* @param {string} filePath - 文件路径
|
||
* @returns {Promise} 返回上传结果
|
||
*/
|
||
export function uploadAvatar(filePath) {
|
||
return uploadFile(
|
||
'/app/user/avatar',
|
||
filePath,
|
||
'avatarfile',
|
||
{},
|
||
{
|
||
timeout: 60000,
|
||
}
|
||
)
|
||
.then(data => {
|
||
// 上传成功后更新本地存储
|
||
const userInfo = uni.getStorageSync('userInfo') || {}
|
||
userInfo.avatar = data.data?.avatar || data.data
|
||
uni.setStorageSync('userInfo', userInfo)
|
||
|
||
// 通知其他页面更新头像
|
||
uni.$emit('avatarUpdated', userInfo.avatar)
|
||
|
||
console.log('头像上传成功,已更新本地存储:', userInfo.avatar)
|
||
return data
|
||
})
|
||
.catch(error => {
|
||
console.error('头像上传失败:', error)
|
||
throw error
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取本地存储的用户ID
|
||
* @returns {string|null} 返回用户ID,如果不存在则返回null
|
||
*/
|
||
export function getLocalUserId() {
|
||
return uni.getStorageSync('userId') || null
|
||
}
|
||
|
||
/**
|
||
* 清除本地存储的用户ID
|
||
*/
|
||
export function clearLocalUserId() {
|
||
uni.removeStorageSync('userId')
|
||
console.log('本地用户ID已清除')
|
||
}
|