import { getToken } from '@/utils/auth' import { toast } from '@/utils/common' // 参数转URL查询字符串 function tansParams(params) { let result = '' for (const propName of Object.keys(params)) { const value = params[propName] var part = encodeURIComponent(propName) + "=" if (value !== null && value !== "" && typeof (value) !== "undefined") { if (typeof value === 'object') { for (const key of Object.keys(value)) { if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') { let params = propName + '[' + key + ']' var subPart = encodeURIComponent(params) + "=" result += subPart + encodeURIComponent(value[key]) + "&" } } } else { result += part + encodeURIComponent(value) + "&" } } } return result } // 专门用于8081端口的请求 const request8081 = config => { config.header = config.header || {} // 8081端口可能使用不同的认证方式 const token = getToken() console.log('=== 8081端口请求详情 ===') console.log('Token值:', token) console.log('Token存在:', !!token) if (token) { // 尝试多种认证方式 // 方式1:Bearer Token(标准JWT) config.header['Authorization'] = 'Bearer ' + token // 方式2:直接使用token(不带Bearer) // config.header['Authorization'] = token // 方式3:自定义认证头 // config.header['X-Auth-Token'] = token // 方式4:Cookie方式 // config.header['Cookie'] = 'token=' + token } // 处理GET请求参数 let requestUrl = config.baseUrl + config.url if (config.params) { let url = requestUrl + '?' + tansParams(config.params) requestUrl = url.slice(0, -1) } console.log('使用的认证方式:', 'Bearer Token') console.log('请求头:', config.header) console.log('请求URL:', requestUrl) console.log('请求参数:', config.params) console.log('请求方法:', config.method) return new Promise((resolve, reject) => { // 为PUT请求设置Content-Type if (config.method && config.method.toLowerCase() === 'put') { config.header = { ...config.header, 'Content-Type': 'application/json' } } uni.request({ method: config.method || 'get', timeout: config.timeout || 10000, url: requestUrl, data: config.data, header: config.header, dataType: 'json' }).then(response => { let [error, res] = response if (error) { toast('后端接口连接异常') reject('后端接口连接异常') return } console.log('=== 8081端口响应详情 ===') // console.log('响应状态码:', res.statusCode) console.log('响应头:', res.header) console.log('响应数据:', res.data) console.log('响应数据类型:', typeof res.data) const code = res.data.code || 200 const msg = res.data.msg || '请求失败' // console.log('业务状态码:', code) // console.log('业务消息:', msg) if (code === 401) { toast('认证失败,请重新登录') reject('401') } else if (code === 403) { // console.log('=== 403错误详细信息 ===') // console.log('完整响应:', JSON.stringify(res, null, 2)) toast('没有权限访问该资源') reject('403') } else if (code === 500) { toast(msg) reject('500') } else if (code !== 200) { toast(msg) reject(code) } resolve(res.data) }).catch(error => { console.error('8081端口请求异常:', error) toast('网络请求失败') reject(error) }) }) } // 获取商品列表 export function getProductList(params) { const token = getToken() // console.log('=== 商品列表请求诊断 ===') // console.log('1. Token值:', token) // console.log('2. Token长度:', token ? token.length : 0) // console.log('3. Token前缀:', token ? token.substring(0, 20) + '...' : '无') // console.log('4. 请求URL:', 'http://193.112.94.36:8081/mall/product/list') // console.log('5. 认证方式:', 'Bearer Token (与8080共享)') return request8081({ baseUrl: 'http://193.112.94.36:8081', url: '/mall/product/list', method: 'get', params: params }) } // 删除商品 export function deleteProduct(id) { return request8081({ baseUrl: 'http://193.112.94.36:8081', url: `/mall/product/delete/${id}`, method: 'delete' }) } // 新增商品 export function addProduct(data) { return request8081({ baseUrl: 'http://193.112.94.36:8081', url: '/mall/product/add', method: 'post', data: data }) } // 新增商品(带文件上传) export function addProductWithFile(filePath, formData) { return new Promise((resolve, reject) => { uni.uploadFile({ url: 'http://193.112.94.36:8081/mall/product/add', filePath: filePath, name: 'file', formData: formData, header: { 'Authorization': 'Bearer ' + getToken() }, success: (uploadRes) => { const res = JSON.parse(uploadRes.data); resolve(res); }, fail: (error) => { reject(error); } }); }); } // 获取商品详情 export function getProductDetail(id) { return request8081({ baseUrl: 'http://193.112.94.36:8081', url: `/mall/product/${id}`, method: 'get' }) } // 修改商品(form-data格式,支持文件上传) export function updateProductWithFile(filePath, formData) { return new Promise((resolve, reject) => { uni.uploadFile({ url: 'http://193.112.94.36:8081/mall/product/update', filePath: filePath, name: 'file', formData: formData, header: { 'Authorization': 'Bearer ' + getToken() }, success: (uploadRes) => { const res = JSON.parse(uploadRes.data); resolve(res); }, fail: (error) => { reject(error); } }); }); } // 修改商品 export function updateProduct(data) { return request8081({ baseUrl: 'http://193.112.94.36:8081', url: '/mall/product/update', method: 'post', data: data }) } // 商品数据导入(文件上传) export function importProductData(filePath, formData) { return new Promise((resolve, reject) => { uni.uploadFile({ url: 'http://193.112.94.36:8081/mall/product/importData', filePath: filePath, name: 'file', formData: formData, header: { 'Authorization': 'Bearer ' + getToken() }, success: (uploadRes) => { const res = JSON.parse(uploadRes.data); resolve(res); }, fail: (error) => { reject(error); } }); }); } // 获取导入记录 export function getImportRecord() { return request8081({ baseUrl: 'http://192.168.0.7:8081', url: '/mall/product/importRecord', method: 'get' }); }