2025-11-03 12:30:37 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* @Descripttion:
|
|
|
|
|
|
* @Author: lip
|
|
|
|
|
|
* @Date: 2023-05-12 08:44:49
|
|
|
|
|
|
* @LastEditors: shirlwang
|
|
|
|
|
|
*/
|
|
|
|
|
|
import store from './store/index.js'
|
|
|
|
|
|
import configRc from './config.js'
|
|
|
|
|
|
import { getToken } from '@/utilsRc/auth'
|
|
|
|
|
|
import errorCode from '@/utilsRc/errorCode'
|
|
|
|
|
|
import { toast, showConfirm, tansParams } from '@/utilsRc/common'
|
|
|
|
|
|
|
|
|
|
|
|
let timeout = 10000
|
|
|
|
|
|
const baseUrl = configRc.baseUrl
|
2025-11-12 12:20:58 +08:00
|
|
|
|
const zytpBaseUrl = configRc.zytpBaseUrl || ''
|
2025-11-03 12:30:37 +08:00
|
|
|
|
|
|
|
|
|
|
const request = config => {
|
|
|
|
|
|
// 是否需要设置 token
|
|
|
|
|
|
const isToken = (config.headers || {}).isToken === false
|
|
|
|
|
|
config.header = config.header || {}
|
2025-11-12 19:31:46 +08:00
|
|
|
|
// 从存储中获取微信登录的 token
|
|
|
|
|
|
const token = getToken()
|
|
|
|
|
|
if (token && !isToken) {
|
|
|
|
|
|
config.header['Authorization'] = 'Bearer ' + token
|
2025-11-03 12:30:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
// get请求映射params参数
|
2025-11-12 12:20:58 +08:00
|
|
|
|
const baseType = config.baseUrlType
|
|
|
|
|
|
const requestBaseUrl = baseType === 'zytp' && zytpBaseUrl ? zytpBaseUrl : baseUrl
|
|
|
|
|
|
let requestUrl = config.fullUrl ? config.fullUrl : (requestBaseUrl + (config.url || ''))
|
|
|
|
|
|
|
2025-11-03 12:30:37 +08:00
|
|
|
|
if (config.params) {
|
2025-11-12 12:20:58 +08:00
|
|
|
|
let url = tansParams(config.params)
|
2025-11-03 12:30:37 +08:00
|
|
|
|
url = url.slice(0, -1)
|
2025-11-12 12:20:58 +08:00
|
|
|
|
if (url) {
|
|
|
|
|
|
requestUrl += (requestUrl.includes('?') ? '&' : '?') + url
|
|
|
|
|
|
}
|
2025-11-03 12:30:37 +08:00
|
|
|
|
}
|
2025-11-12 19:31:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果是 getJobPathById 接口,打印完整 URL
|
|
|
|
|
|
if (config.url && config.url.includes('getJobPathById')) {
|
|
|
|
|
|
console.log('[请求URL] getJobPathById 完整请求URL:', requestUrl);
|
|
|
|
|
|
console.log('[请求URL] baseUrl:', requestBaseUrl);
|
|
|
|
|
|
console.log('[请求URL] 接口路径:', config.url);
|
|
|
|
|
|
console.log('[请求URL] 请求参数:', config.params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是 recommendJob 接口,打印详细信息
|
|
|
|
|
|
if (config.url && config.url.includes('recommendJob')) {
|
|
|
|
|
|
console.log('[请求URL] recommendJob 完整请求URL:', requestUrl);
|
|
|
|
|
|
console.log('[请求URL] baseUrl:', requestBaseUrl);
|
|
|
|
|
|
console.log('[请求URL] 接口路径:', config.url);
|
|
|
|
|
|
console.log('[请求URL] 请求方法:', config.method);
|
|
|
|
|
|
console.log('[请求URL] 请求参数 (params):', config.params);
|
|
|
|
|
|
console.log('[请求URL] 请求数据 (data):', config.data);
|
|
|
|
|
|
console.log('[请求URL] Content-Type:', config.header?.['content-type']);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-03 12:30:37 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
uni.request({
|
|
|
|
|
|
method: config.method || 'get',
|
|
|
|
|
|
timeout: config.timeout || timeout,
|
2025-11-12 12:20:58 +08:00
|
|
|
|
url: requestUrl,
|
2025-11-03 12:30:37 +08:00
|
|
|
|
// url: 'https://gccrcdh.sd-talent.cn:80/zhq' + config.url,
|
|
|
|
|
|
data: config.data,
|
|
|
|
|
|
header: config.header,
|
2025-11-12 12:20:58 +08:00
|
|
|
|
dataType: 'json',
|
|
|
|
|
|
responseType: config.responseType || 'text'
|
2025-11-03 12:30:37 +08:00
|
|
|
|
}).then(response => {
|
|
|
|
|
|
let res = response.data
|
|
|
|
|
|
let error = response.errMsg!='request:ok'
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
toast('网络出小差,请稍后再试')
|
|
|
|
|
|
reject('网络出小差,请稍后再试')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-12 19:31:46 +08:00
|
|
|
|
// 处理 code,0 和 200 都表示成功
|
|
|
|
|
|
const code = res.code !== undefined && res.code !== null ? res.code : 200
|
2025-11-03 12:30:37 +08:00
|
|
|
|
console.log(code, 'const code = res.code || 200')
|
|
|
|
|
|
const msg = errorCode[code] || res.msg || errorCode['default']
|
|
|
|
|
|
const isShowModel = getApp().globalData.isShowModel
|
2025-11-12 19:31:46 +08:00
|
|
|
|
if (code === 200 || code === 0) {
|
2025-11-03 12:30:37 +08:00
|
|
|
|
resolve(res)
|
|
|
|
|
|
}else if (code === 401) {
|
|
|
|
|
|
if(isShowModel === false) {
|
|
|
|
|
|
getApp().globalData.isShowModel = true
|
|
|
|
|
|
showConfirm('您好!登录后,您才可以继续操作。', '取消', '去登录').then(res => {
|
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
|
getApp().globalData.isShowModel = false
|
|
|
|
|
|
store.dispatch('LogOut').then(res => {
|
|
|
|
|
|
uni.reLaunch({ url: '/pages/login/login-one' })
|
|
|
|
|
|
})
|
|
|
|
|
|
} else if(res.cancel) {
|
|
|
|
|
|
getApp().globalData.isShowModel = false
|
|
|
|
|
|
uni.navigateBack()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
// reject(code)
|
|
|
|
|
|
}
|
|
|
|
|
|
// if (code === 401) {
|
|
|
|
|
|
// showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
|
|
|
|
|
|
// if (res.confirm) {
|
|
|
|
|
|
// store.dispatch('LogOut').then(res => {
|
|
|
|
|
|
// uni.reLaunch({ url: '/page_other/login/index' })
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
|
|
|
|
|
// })
|
|
|
|
|
|
// reject('无效的会话,或者会话已过期,请重新登录。')
|
|
|
|
|
|
// } else
|
|
|
|
|
|
else if (code === 500) {
|
|
|
|
|
|
toast(msg)
|
|
|
|
|
|
reject('500')
|
2025-11-12 19:31:46 +08:00
|
|
|
|
} else if (code !== 200 && code !== 0 && code !== 9999) {
|
2025-11-03 12:30:37 +08:00
|
|
|
|
// 9999是正常的业务状态码,不应该被当作错误处理
|
2025-11-12 19:31:46 +08:00
|
|
|
|
// 0 和 200 都表示成功
|
2025-11-03 12:30:37 +08:00
|
|
|
|
toast(msg)
|
|
|
|
|
|
reject(code)
|
|
|
|
|
|
}
|
2025-11-12 19:31:46 +08:00
|
|
|
|
// 如果 code 是 0 或 200,返回完整响应对象,而不是 res.data
|
|
|
|
|
|
// 因为有些接口的 data 在 res.data 中,有些在 res 中
|
|
|
|
|
|
resolve(res)
|
2025-11-03 12:30:37 +08:00
|
|
|
|
})
|
|
|
|
|
|
.catch(error => {
|
|
|
|
|
|
console.log(error, 'error')
|
|
|
|
|
|
let message = error.errMsg
|
|
|
|
|
|
if (message === 'Network Error') {
|
|
|
|
|
|
message = '网络出小差,请稍后再试'
|
|
|
|
|
|
} else if (message.includes('timeout')) {
|
|
|
|
|
|
message = '系统接口请求超时'
|
|
|
|
|
|
} else if (message.includes('Request failed with status code')) {
|
|
|
|
|
|
message = '系统接口' + message.substr(message.length - 3) + '异常'
|
|
|
|
|
|
}
|
|
|
|
|
|
toast(message)
|
|
|
|
|
|
reject(error)
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default request
|