diff --git a/src/api/upload.js b/src/api/upload.js new file mode 100644 index 0000000..66cdb36 --- /dev/null +++ b/src/api/upload.js @@ -0,0 +1,14 @@ +import { get, post } from '../utils/http.js' + +// 上传图片 +export const uploadImg = async (file) => { + try { + const formData = new FormData() + formData.append('file', file) // 这里的'file'需要与后端接口期望的参数名一致 + const response = await post('/external/oss/upload', formData) + return response + } catch (error) { + console.error('Failed to upload img:', error) + throw error + } +} diff --git a/src/utils/applyStore.js b/src/utils/applyStore.js new file mode 100644 index 0000000..bfcb1dd --- /dev/null +++ b/src/utils/applyStore.js @@ -0,0 +1,39 @@ +let applyInfo = null + +/** + * 设置申请资料缓存 + * @param {Object} applyInfo - 申请资料 + */ +export function setApplyInfo(data) { + applyInfo = data + + // 可选:存储到localStorage以便持久化 + if (data) { + localStorage.setItem('applyInfo', JSON.stringify(data)) + } else { + localStorage.removeItem('applyInfo') + } +} + +/** + * 获取申请资料 + * @returns {Object|null} + */ +export function getApplyInfo() { + if (applyInfo) { + return applyInfo + } + + // 从localStorage恢复 + const stored = localStorage.getItem('applyInfo') + if (stored) { + try { + applyInfo = JSON.parse(stored) + return applyInfo + } catch (error) { + console.error('解析申请资料缓存失败:', error) + } + } + + return null +} diff --git a/src/utils/http.js b/src/utils/http.js index 632b2e7..9cd0785 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -1,5 +1,5 @@ // HTTP请求配置 -import {getApiBaseUrl, getCurrentEnv, getUserAuth, isDebugMode} from './env.js' +import { getApiBaseUrl, getCurrentEnv, getUserAuth, isDebugMode } from './env.js' /** * 自定义API错误类 @@ -8,14 +8,14 @@ class ApiError extends Error { constructor(message, options = {}) { super(message) this.name = 'ApiError' - this.status = options.status || null // HTTP状态码 - this.errorCode = options.errorCode || null // 业务错误码 (如: 6010) + this.status = options.status || null // HTTP状态码 + this.errorCode = options.errorCode || null // 业务错误码 (如: 6010) this.errorCodeName = options.errorCodeName || null // 错误码名称 (如: "TEAM_NOT_FOUND") - this.errorMsg = options.errorMsg || null // 业务错误消息 - this.time = options.time || null // API响应时间戳 - this.extValues = options.extValues || null // 扩展值 - this.response = options.response || null // 原始响应数据 - this.type = options.type || 'unknown' // 错误类型: 'http' | 'business' | 'network' + this.errorMsg = options.errorMsg || null // 业务错误消息 + this.time = options.time || null // API响应时间戳 + this.extValues = options.extValues || null // 扩展值 + this.response = options.response || null // 原始响应数据 + this.type = options.type || 'unknown' // 错误类型: 'http' | 'business' | 'network' } } @@ -32,7 +32,7 @@ if (DEBUG_MODE) { const token = getUserAuth() // 默认公共请求头配置 let COMMON_HEADERS = { - 'authorization': `Bearer ${token}`, + authorization: `Bearer ${token}`, 'req-app-intel': 'version=1.0.0;build=1;model=SM-G9550;sysVersion=9;channel=Google', 'req-client': 'Android', 'req-imei': '8fa54d728ab449e04f9292329ed44bda', @@ -49,7 +49,7 @@ let COMMON_HEADERS = { export function updateCommonHeaders(headers) { COMMON_HEADERS = { ...COMMON_HEADERS, - ...headers + ...headers, } console.debug('🔄 Updated common headers:', COMMON_HEADERS) } @@ -110,37 +110,40 @@ export { COMMON_HEADERS, ApiError } * @returns {Promise} - 返回Promise对象 */ export async function request(url, options = {}) { - const { - method = 'GET', - data = null, - headers = {}, - ...otherOptions - } = options - + const { method = 'GET', data = null, headers = {}, ...otherOptions } = options const fullUrl = url.startsWith('http') ? url : `${API_BASE_URL}${url}` const config = { method, headers: { - 'Content-Type': 'application/json', ...COMMON_HEADERS, - ...headers + ...headers, }, - ...otherOptions + ...otherOptions, } // 如果是POST、PUT等方法且有数据,添加body if (data && ['POST', 'PUT', 'PATCH'].includes(method.toUpperCase())) { - config.body = JSON.stringify(data) + if (data instanceof FormData) { + // FormData类型:由浏览器自动设置Content-Type + config.body = data + // 删除手动设置的Content-Type(重要!) + if (config.headers['Content-Type']) { + delete config.headers['Content-Type'] + } + } else { + // 非FormData:按JSON处理 + config.body = JSON.stringify(data) + config.headers['Content-Type'] = 'application/json' + } } try { - const response = await fetch(fullUrl, config) if (!response.ok) { // 尝试解析响应体获取更多错误信息 - let errorData = await response.json() + let errorData = await response.json() throw new ApiError(`HTTP Error: ${response.status} ${response.statusText}`, { type: 'http', @@ -149,7 +152,7 @@ export async function request(url, options = {}) { errorCode: errorData.errorCode, errorCodeName: errorData.errorCodeName, errorMsg: errorData.errorMsg, - time: errorData.time + time: errorData.time, }) } @@ -180,7 +183,7 @@ export async function request(url, options = {}) { throw new ApiError(error.message || 'Network or unknown error', { type: 'network', originalError: error, - code: 'NETWORK_ERROR' + code: 'NETWORK_ERROR', }) } diff --git a/src/views/Activities/Lottery/Lottery.vue b/src/views/Activities/Lottery/Lottery.vue index f40a128..eb90239 100644 --- a/src/views/Activities/Lottery/Lottery.vue +++ b/src/views/Activities/Lottery/Lottery.vue @@ -1399,15 +1399,19 @@ const getWinners = async () => { } } -//获取中奖记录 +//获取领取任务奖励 const receiveTaskReward = async (code) => { let data = { taskCode: code, } - const resReceive = await receiveTickets(data) - if (resReceive.status && resReceive.body) { - getTaskList() //获取任务列表 - getTickets() //获取拥有的抽奖券 + try { + const resReceive = await receiveTickets(data) + if (resReceive.status && resReceive.body) { + getTaskList() //获取任务列表 + getTickets() //获取拥有的抽奖券 + } + } catch (error) { + showError(error.errorMsg) } } diff --git a/src/views/Wallet/CashOut/CashOut.vue b/src/views/Wallet/CashOut/CashOut.vue index c9d6734..1861644 100644 --- a/src/views/Wallet/CashOut/CashOut.vue +++ b/src/views/Wallet/CashOut/CashOut.vue @@ -70,7 +70,9 @@