Files
shanghaiganxiangtuzhuangwor…/utils/request.js
赵正易 a48ea6f110 feat: 新增物料图标和优化UI组件
fix(入库页面): 修复重复箱检测和批次检查逻辑

refactor(请求模块): 优化错误处理和防抖机制

style(全局字体): 更改默认字体为数黑体

perf(扫码组件): 添加防抖处理避免重复提交

feat(卡片组件): 增加删除功能并优化样式

docs(工具函数): 添加入库工具函数文档

chore: 更新依赖和配置文件
2025-08-22 18:28:25 +08:00

106 lines
3.1 KiB
JavaScript

import store from '@/store'
import config from '@/config'
import {
getBaseUrl,
setBaseUrl,
removeBaseUrl
} from '@/utils/baseUrl';
import {
getToken
} from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import {
toast,
showConfirm,
tansParams
} from '@/utils/common'
let timeout = 10000
const request = config => {
// 浏览器测试用
// const storageBaseUrl = 'http://' + getBaseUrl() + '/api';
// 真机测试以及部署用
const storageBaseUrl = 'http://' + getBaseUrl();
// 是否需要设置 token
const isToken = (config.headers || {}).isToken === false
config.header = config.header || {}
// 假设你使用的是PHP作为你的后端语言
config.header['Access-Control-Allow-Origin'] = '*'; // 允许任何源访问
config.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'; // 允许的HTTP方法
config.header['Access-Control-Allow-Headers'] = 'X-Requested-With'; // 允许的HTTP头
if (getToken() && !isToken) {
config.header['Authorization'] = 'Bearer ' + getToken()
config.header['userName'] = encodeURIComponent(store.getters.nickName ?? "未知用户")
}
// get请求映射params参数
if (config.params) {
let url = config.url + '?' + tansParams(config.params)
url = url.slice(0, -1)
config.url = url
}
return new Promise((resolve, reject) => {
uni.request({
method: config.method || 'get',
timeout: config.timeout || timeout,
url: config.baseUrl || (storageBaseUrl + config.url),
data: config.data,
header: config.header,
dataType: 'json'
}).then(response => {
let [error, res] = response
//console.log('token:','Bearer ' + getToAccess-Control-Allow-Originken());
//console.log('request:',storageBaseUrl + config.url,response);
if (error) {
if (error.errMsg.includes('Failed to connect')) {
toast('后台地址连接失败,请检查:' + (config.baseUrl || (storageBaseUrl + config.url)))
} else {
toast('后端接口连接异常')
}
reject('后端接口连接异常')
return
}
const code = res.data.code || 200
const msg = errorCode[code] || res.data.msg || errorCode['default']
if (code === 401) {
showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
if (res.confirm) {
store.dispatch('LogOut').then(res => {
uni.reLaunch({
url: '/pages/login'
})
})
}
})
reject('无效的会话,或者会话已过期,请重新登录。')
} else if (code === 500) {
// toast(msg)
showConfirm(msg)
reject('500')
} else if (code !== 200) {
// toast(msg)
showConfirm(msg)
reject(code)
}
resolve(res.data)
})
.catch(error => {
let {
message
} = error
if (message === 'Network Error') {
message = '后端接口连接异常,Network Error'
} else if (message.includes('timeout')) {
message = '系统接口请求超时'
} else if (message.includes('Request failed with status code')) {
message = '系统接口' + message.substr(message.length - 3) + '异常'
}
// toast(message)
showConfirm(message)
reject(error)
})
})
}
export default request