Commit afeb57c9 by 李伟康

```

refactor(api): 调整HTTP请求类的方法格式化和错误处理

- 统一方法名后的空格格式,所有方法添加适当空格
- 优化shukeRequest方法的参数命名,data改为params提高可读性
- 增强错误处理逻辑,在网络无连接时提供更好的错误提示
- 添加对响应数据的验证,当数据为空或状态码非200时进行错误处理
- 改进认证失败(401)的处理流程,自动清理会话并跳转登录页
- 修复请求头格式缩进问题,统一代码风格
- 在Promise中添加reject处理,完善异步操作的错误捕获
```
1 parent b8619668
......@@ -7,13 +7,13 @@ const HOST = {
ip: 'http://192.168.1.125:8080'
}
class Request {
get(params, returnAll = false, hostType) {
get (params, returnAll = false, hostType) {
return this.request('GET', params, returnAll, hostType)
}
post(params, returnAll = false, hostType) {
post (params, returnAll = false, hostType) {
return this.request('POST', params, returnAll, hostType)
}
fetch(method, data, returnAll = true, hostType) {
fetch (method, data, returnAll = true, hostType) {
return this.post(
{
method,
......@@ -23,11 +23,11 @@ class Request {
hostType
)
}
request(RequestMethods, params, returnAll, hostType = 'dj') {
request (RequestMethods, params, returnAll, hostType = 'dj') {
return new Promise((resolve, reject) => {
let _this = this
uni.getNetworkType({
success(res) {
success (res) {
if (res.networkType == 'none') {
uni.hideLoading()
uni.showToast({
......@@ -40,11 +40,11 @@ class Request {
let header =
RequestMethods === 'POST'
? {
'content-type': 'application/x-www-form-urlencoded'
}
'content-type': 'application/x-www-form-urlencoded'
}
: {
'Cache-Control': 'no-cache'
}
'Cache-Control': 'no-cache'
}
let projectName = params.projectName
if (!projectName) projectName = _this.getRopProject(params.method)
const path = pathEnum[projectName]
......@@ -56,7 +56,7 @@ class Request {
header,
dataType: 'json',
responseType: 'text',
success(response) {
success (response) {
try {
/**业务代码 */
let { data, statusCode } = response
......@@ -100,7 +100,7 @@ class Request {
reject(error)
}
},
fail(error) {
fail (error) {
uni.hideLoading()
uni.$logger.error(`HTTP Request Fail: ${JSON.stringify(params)}->${error.message}`)
// console.error(`${params.method}接口异常:`, error)
......@@ -110,7 +110,7 @@ class Request {
})
reject(error)
},
complete() {
complete () {
uni.stopPullDownRefresh()
}
})
......@@ -118,7 +118,7 @@ class Request {
})
})
}
getData(params, hostType = 'scrm') {
getData (params, hostType = 'scrm') {
let projectName = params.projectName
if (!projectName) projectName = this.getRopProject(params.method)
else delete params.projectName
......@@ -150,11 +150,11 @@ class Request {
params.appKey = appKey
return sign(params, secret, noSignAttr)
}
getRopProject(method) {
getRopProject (method) {
const projectName = method.split('.')[2]
return projectName
}
uni_request({ url, method = 'GET', data = {}, header = {}, dataType = 'json', responseType = 'text' }) {
uni_request ({ url, method = 'GET', data = {}, header = {}, dataType = 'json', responseType = 'text' }) {
return new Promise(resolve => {
uni.request({
url,
......@@ -163,27 +163,27 @@ class Request {
header,
dataType,
responseType,
success(response) {
success (response) {
resolve(response.data)
}
})
})
}
shukeRequest(url, data = {}, method = 'GET', returnAll = false) {
shukeRequest (url, params = {}, method = 'GET', returnAll = false) {
let header =
method === 'POST'
? {
'content-type': 'application/json'
}
'content-type': 'application/json'
}
: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/json'
}
'Cache-Control': 'no-cache',
'Content-Type': 'application/json'
}
let { sessionId } = uni.$storage.get()
if (sessionId) header.Authorization = sessionId
return new Promise(resolve => {
return new Promise((resolve, reject) => {
uni.getNetworkType({
success(res) {
success (res) {
if (res.networkType == 'none') {
uni.hideLoading()
uni.showToast({
......@@ -195,19 +195,46 @@ class Request {
}
uni.request({
url: `${shukeDomain}/${url}`,
data,
data: params,
method,
header,
dataType: 'json',
responseType: 'text',
success(response) {
let { data, statusCode } = response
if (returnAll) {
resolve(data)
return
success (response) {
try {
let { data, statusCode } = response
if (!data || statusCode != 200) {
uni.hideLoading()
let errMsg = ''
if (!data) errMsg = '获取数据失败,请重试。'
else errMsg = '服务器暂时不可用,请稍后再次尝试。'
uni.$logger.error(`Request Fail: ${JSON.stringify(params)}->${errMsg}`)
return reject(errMsg)
}
if (returnAll) {
resolve(data)
return
}
let { code, msg } = data
if (code == 401) {
uni.$storage.set('sessionId', '')
uni.$storage.set('userInfo', {})
uni.hideLoading()
const pages = getCurrentPages(),
currentPage = pages[pages.length - 1]
uni.$logger.local.warn(`HTTP Triggered Logout: params:${JSON.stringify(params)} method: ${params.method}->code:${code} msg:${msg} currentPage:${currentPage.route}`)
uni.hideLoading()
if (currentPage.route != 'pages/common/login') {
uni.reLaunch({
url: '/pages/common/login'
})
}
} else resolve(data.data)
} catch (error) {
console.log(error)
uni.$logger.error(`${params.method}处理异常:${error.msg}`)
reject(error)
}
resolve(data.data)
}
})
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!