Files
jnpf_app/utils/request.js

119 lines
2.7 KiB
JavaScript
Raw Normal View History

2026-01-04 11:09:06 +08:00
import define from './define'
import {
useLocale
} from '@/locale/useLocale';
const {
getBackLocale
} = useLocale();
2026-01-19 17:34:15 +08:00
let host = define.baseURL
2026-01-04 11:09:06 +08:00
const defaultOpt = {
load: true
}
// 示例
// async xxxx(code) {
// var res = await this.request({
// url: '/api/System/DictionaryData/All',
// method: 'GET',
// data,
// options: {
// load: false
// }
// })
// if (!res) return
// console.log(res)
// }
function request(config) {
config.options = Object.assign(defaultOpt, config.options)
const token = uni.getStorageSync('token') || ''
2026-01-19 17:34:15 +08:00
const tenantId = uni.getStorageSync('tenantId') || ''
2026-01-04 11:09:06 +08:00
const systemCode = uni.getStorageSync('systemCode') || ''
const locale = getBackLocale()
let header = {
2026-01-19 17:34:15 +08:00
"accept" : 'application/json, text/plain, */*',
2026-01-04 11:09:06 +08:00
"App-Code": systemCode,
"Content-Type": "application/json;charset=UTF-8",
"Jnpf-Origin": "app",
"Vue-Version": "3",
"Accept-Language": locale,
2026-01-19 17:34:15 +08:00
"tenant-id": tenantId,
2026-01-04 11:09:06 +08:00
...config.header
}
header['App-Code'] = encodeURIComponent(header['App-Code'])
if (token) header['Authorization'] = token
2026-01-19 17:34:15 +08:00
// 测试 todo
console.log(config.url,config.url.includes('admin-api'),'config.url.includes---')
if(config.url.includes('admin-api')){
host = 'http://10.28.117.48:48080'
}else {
host = define.baseURL
}
2026-01-04 11:09:06 +08:00
let url = config.url.indexOf('http') > -1 ? config.url : host + config.url
2026-01-19 17:34:15 +08:00
2026-01-04 11:09:06 +08:00
if (config.options.load) {
uni.showLoading({
title: config.options.loadText || '正在加载'
})
}
return new Promise((resolve, reject) => {
uni.request({
url: url,
data: config.data || {},
method: config.method || 'GET',
header: header,
timeout: define.timeout,
success: res => {
if (res.statusCode === 200) {
2026-01-19 17:34:15 +08:00
if (res.data.code == 200 || res.data.code == 0) {
2026-01-04 11:09:06 +08:00
resolve(res.data)
} else {
ajaxError(res.data)
reject(res.data.msg)
}
} else {
ajaxError(res.data)
reject(res.errMsg)
}
uni.hideLoading();
},
fail: err => {
uni.showToast({
title: '连接服务器失败',
icon: 'none',
})
setTimeout(function () {
uni.hideLoading();
}, 2000);
reject(err)
}
})
})
}
function ajaxError(data) {
uni.showToast({
title: data.msg || '请求出错,请重试',
icon: 'none',
complete() {
2026-01-19 17:34:15 +08:00
if (data.code === 600 || data.code === 601 || data.code === 602 || data.code === 401) {
2026-01-04 11:09:06 +08:00
setTimeout(() => {
uni.removeStorageSync('token')
uni.removeStorageSync('cid')
uni.removeStorageSync('userInfo')
uni.removeStorageSync('permissionList')
uni.removeStorageSync('sysVersion')
uni.removeStorageSync('dynamicModelExtra')
uni.reLaunch({
url: '/pages/login/index'
})
}, 1500)
}
}
})
}
export default request