You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

465 lines
9.3 KiB

const baseImgURL = 'https://www.ourpyw.com/static/uni-app/';
import {
isEmpty
} from './TextUtils.js'
/**
* 通用网络请求
* loading: 是否显示网络请求时的loading,默认不提示【允许不传值】
* showError: 是否显示错误信息,默认显示(如有特殊code需要自己判断逻辑时,请改成false)【允许不传值】
* method: 网络请求方式 GET/POST/PUT【禁止不传值】
* paramsType: 网络请求传参方式 JSON/FORM【禁止不传值】
* autoUrl: 自动拼接网址,默认是 getApp().globalData.baseURL+ 相对地址【允许不传值】false认为非本项目域名是第三方接口
* 响应:
* 响应到success则代表网络请求成功,直接返回对象,需自行判断逻辑
* 响应到fail则代表无网络或无连接情况
*/
const http = (options) => {
console.log(getApp().globalData.token)
if (getApp().globalData.isDebug) {
console.log('Http网络请求info', options)
}
if (isEmpty(options.loading)) {
// 显示loading(默认不显示loading)
options.loading = false
}
if (isEmpty(options.showError)) {
// 失败弹出提示(默认提示错误信息)
options.showError = true
}
// if (isEmpty(options.method) || (options.method != "POST" && options.method != "GET"&& options.method != "PUT")) {
if (isEmpty(options.method) || (options.method != "POST" && options.method != "GET" && options.method != "PUT" && options.method != "DELETE")) {
uni.showToast({
title: "网络请求 'method' 为 POST/GET/PUT",
icon: 'none',
duration: 4000
})
return
}
if ("JSON" == options.paramsType) {
options.paramsType = "application/json"
} else if ("FORM" == options.paramsType) {
options.paramsType = "application/x-www-form-urlencoded"
} else {
uni.showToast({
title: "网络请求 'paramsType' 为 JSON/FORM",
icon: 'none',
duration: 4000
})
return
}
if (isEmpty(options.autoUrl)) {
// 默认拼接网址
options.autoUrl = true
}
return new Promise((resolve, reject) => {
if (options.loading) {
uni.showLoading({
title: '加载中...',
// 默认遮罩出现可以继续操作
mask: options.load || true
});
}
var url = options.autoUrl ? getApp().globalData.baseURL + options.url : options.url
uni.request({
// 组装请求地址
url: url,
// 请求方式 GET POST
method: options.method,
header: {
// 传参方式
'content-type': options.paramsType,
'token': getApp().globalData.token
},
// 具体参数
data: options.data,
success: res => {
// 关闭显示框
uni.hideLoading();
console.log(res)
if (getApp().globalData.isDebug) {
console.log('Http网络路径', url)
console.log('Http网络请求结果', JSON.parse(JSON.stringify(res.data)))
}
if (res.statusCode == 200) {
// 自动拼接类型,认为是公司后台提供接口服务,按照规定标准进行检查返回内容
if (options.autoUrl) {
// 进行success字段检查
if (!res.data.success) {
if ("5000" == res.data.code) {
uni.setStorageSync("memberSid", null);
getApp().globalData.isLogin = false
getApp().globalData.memberSid = null
uni.showToast({
title: "出错了,请重试",
// 保证文字长度
icon: "none",
duration: 3000
})
return
} else {
// 错误提示
if (options.showError) {
if (isEmpty(res.data)) {
// 未成功获取到服务器返回的json
uni.showToast({
// 不能超过7个字
title: "服务器响应为空",
icon: "error"
})
} else {
let errorMsg = res.data.msg
if (isEmpty(errorMsg)) {
errorMsg = url + "服务器未返回错误信息"
}
uni.showToast({
title: errorMsg,
// 保证文字长度
icon: "none",
duration: 3000
})
}
}
}
}
}
// 直接返回Response
resolve(res.data)
} else {
uni.showToast({
title: res.statusCode + ":" + res.data.error,
// 保证文字长度
icon: "none",
duration: 3000
})
// 返回错误数据
reject(res.data.error)
}
},
fail: (err) => {
// 关闭显示框
uni.hideLoading();
if (getApp().globalData.isDebug) {
console.log("Http网络请求fail", err)
}
uni.showToast({
title: '请检查网络',
icon: 'error'
})
// 返回错误数据
reject(err)
},
complete: () => {
}
});
})
}
const upload = (options) => {
if (getApp().globalData.isDebug) {
console.log('Http网络请求info', options)
}
if (options.loading == undefined || options.loading == null || options.loading === "") {
options.loading = false
}
if (options.key == undefined || options.key == null || options.key === "") {
options.key = "file"
}
return new Promise((resolve, reject) => {
if (options.loading) {
uni.showLoading({
title: '加载中...',
mask: options.load || true // 默认遮罩出现可以继续操作
});
}
try {
uni.uploadFile({
url: getApp().globalData.baseURL + options.url,
filePath: options.filePath,
name: options.key,
formData: options.data,
success: (res) => {
uni.hideLoading();
// 不知道什么谜之操作 需要手动转换成json对象
res.data = JSON.parse(res.data)
if (getApp().globalData.isDebug) {
console.log('Http网络路径', url)
console.log('Http网络请求结果', JSON.stringify(res.data))
}
if (res.data.success) {
// 成功提示
if (options.toast) {
uni.showToast({
title: res.data.msg,
icon: "none"
})
}
// 成功数据返回
resolve(res.data)
} else {
// 错误提示
uni.showToast({
title: res.data.msg,
icon: "none"
})
// 返回错误数据
reject(res.data)
}
},
fail: (err) => {
uni.hideLoading();
if (getApp().globalData.isDebug) {
console.log(err)
}
uni.showToast({
title: '请检查网络连接',
icon: 'none'
})
// 返回错误数据
reject(err)
},
complete: () => {
}
});
} catch (e) {
uni.hideLoading();
uni.showToast({
title: '服务端异常',
icon: 'none'
})
// 返回错误数据
reject(err)
}
})
}
const httpCookie = (options) => {
if (isDebug) {
console.log('Http网络请求info', options)
}
if (options.loading == undefined || options.loading == null || options.loading === "") {
options.loading = false
}
return new Promise((resolve, reject) => {
if (options.loading) {
uni.showLoading({
title: '加载中...',
mask: options.load || true // 默认遮罩出现可以继续操作
});
}
try {
uni.request({
url: baseURL + options.url,
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded',
// #ifdef MP-WEIXIN
'Cookie': options.cookie
// #endif
},
data: options.data,
success: res => {
uni.hideLoading();
if (isDebug) {
console.log('Http网络路径', url)
console.log('Http网络请求结果', res.data)
}
if (res.data.success) {
// 成功提示
if (options.toast) {
uni.showToast({
title: res.data.msg,
icon: "none"
})
}
// 成功数据返回
resolve(res.data)
} else {
// 错误提示
uni.showToast({
title: res.data.msg,
icon: "none"
})
// 返回错误数据
reject(res.data)
}
},
fail: (err) => {
uni.hideLoading();
if (isDebug) {
console.log(err)
}
uni.showToast({
title: '请检查网络连接',
icon: 'none'
})
// 返回错误数据
reject(err)
},
complete: () => {
}
});
} catch (e) {
uni.hideLoading();
uni.showToast({
title: '服务端异常',
icon: 'none'
})
// 返回错误数据
reject(err)
}
})
}
// 加载其他网站的地址
const httpOtherUrl = (options) => {
if (getApp().globalData.isDebug) {
console.log('Http第三方网络请求info', options)
}
if (options.loading == undefined || options.loading == null || options.loading === "") {
options.loading = false
}
return new Promise((resolve, reject) => {
if (options.loading) {
uni.showLoading({
title: '加载中...',
mask: options.load || true // 默认遮罩出现可以继续操作
});
}
try {
uni.request({
url: options.url,
method: 'GET',
data: options.data,
success: res => {
if (getApp().globalData.isDebug) {
console.log('Http第三方网络请求结果', res.data)
}
uni.hideLoading();
resolve(res.data)
},
fail: (err) => {
if (getApp().globalData.isDebug) {
console.log('Http第三方网络请求错误', err)
}
uni.hideLoading();
reject(err)
uni.showToast({
title: '请检查网络连接',
icon: 'none'
})
},
complete: () => {
}
});
} catch (e) {
uni.hideLoading();
reject(e)
uni.showToast({
title: '服务端异常',
icon: 'none'
})
}
})
}
export {
http,
httpOtherUrl,
baseImgURL,
httpCookie,
upload
}