const config = require('./config') const AUTH_KEY = 'jh_wrs_auth' const getAuth = () => wx.getStorageSync(AUTH_KEY) || null const setAuth = auth => { wx.setStorageSync(AUTH_KEY, auth) return auth } const clearAuth = () => { wx.removeStorageSync(AUTH_KEY) } const buildQuery = params => Object.keys(params || {}) .filter(key => params[key] !== undefined && params[key] !== null && params[key] !== '') .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) .join('&') const formatErrorDetail = detail => { if (Array.isArray(detail)) { return detail.map(item => item.msg || item.message || JSON.stringify(item)).join(';') } if (detail && typeof detail === 'object') { return detail.msg || detail.message || JSON.stringify(detail) } return detail } const request = options => new Promise((resolve, reject) => { const auth = getAuth() const query = buildQuery(options.params) const url = `${config.apiBaseUrl}${options.url}${query ? `?${query}` : ''}` wx.request({ url, method: options.method || 'GET', data: options.data || undefined, timeout: options.timeout || 15000, header: { 'content-type': 'application/json', ...(auth && auth.accessToken ? { Authorization: `Bearer ${auth.accessToken}` } : {}), ...(options.header || {}), }, success: res => { if (res.statusCode >= 200 && res.statusCode < 300) { resolve(res.data) return } if (res.statusCode === 401) { clearAuth() } const detail = res.data && (res.data.detail || res.data.message) const message = formatErrorDetail(detail) || `接口请求失败:${res.statusCode}` const error = new Error(message) error.statusCode = res.statusCode error.detail = detail reject(error) }, fail: err => { reject(new Error(err.errMsg || '网络请求失败')) }, }) }) const upload = options => new Promise((resolve, reject) => { const auth = getAuth() wx.uploadFile({ url: `${config.apiBaseUrl}${options.url}`, filePath: options.filePath, name: options.name || 'file', timeout: options.timeout || 30000, header: { ...(auth && auth.accessToken ? { Authorization: `Bearer ${auth.accessToken}` } : {}), ...(options.header || {}), }, success: res => { let data = res.data try { data = data ? JSON.parse(data) : {} } catch (error) { data = { raw: res.data } } if (res.statusCode >= 200 && res.statusCode < 300) { resolve(data) return } reject(new Error(formatErrorDetail(data && (data.detail || data.message)) || `上传失败:${res.statusCode}`)) }, fail: err => { reject(new Error(err.errMsg || '上传失败')) }, }) }) const download = options => new Promise((resolve, reject) => { const auth = getAuth() const query = buildQuery(options.params) wx.downloadFile({ url: `${config.apiBaseUrl}${options.url}${query ? `?${query}` : ''}`, header: { ...(auth && auth.accessToken ? { Authorization: `Bearer ${auth.accessToken}` } : {}), ...(options.header || {}), }, timeout: options.timeout || 30000, success: res => { if (res.statusCode >= 200 && res.statusCode < 300) { resolve(res.tempFilePath) return } if (res.statusCode === 401) { clearAuth() } const error = new Error(`下载失败:${res.statusCode}`) error.statusCode = res.statusCode reject(error) }, fail: err => { reject(new Error(err.errMsg || '下载失败')) }, }) }) const downloadUrl = options => new Promise((resolve, reject) => { const auth = getAuth() wx.downloadFile({ url: options.url, filePath: options.filePath, header: { ...(auth && auth.accessToken ? { Authorization: `Bearer ${auth.accessToken}` } : {}), ...(options.header || {}), }, timeout: options.timeout || 30000, success: res => { if (res.statusCode >= 200 && res.statusCode < 300) { resolve(res.filePath || res.tempFilePath || options.filePath) return } if (res.statusCode === 401) { clearAuth() } const error = new Error(`下载失败:${res.statusCode}`) error.statusCode = res.statusCode reject(error) }, fail: err => { reject(new Error(err.errMsg || '下载失败')) }, }) }) module.exports = { clearAuth, download, downloadUrl, getAuth, request, setAuth, upload, }