79 lines
1.6 KiB
JavaScript
79 lines
1.6 KiB
JavaScript
import {
|
||
set,
|
||
get
|
||
} from '../cache/storage.js';
|
||
import {
|
||
operatorUrl,
|
||
operatorPlug,
|
||
uploadImgUrl,
|
||
previewUrl,
|
||
copyrightUrl,
|
||
imgAssets,
|
||
appUrl
|
||
} from '../net/mainUrl'
|
||
|
||
/**
|
||
* 传入请求参数,返回Promise支持链试调用
|
||
* @param url 请求地址
|
||
* @param method 请求方法类型,不传入默认是"GET"
|
||
* @param data 请求体数据
|
||
* @param params 请求参数
|
||
*/
|
||
function request(url, method = "GET", data = {}, params = {}, project = "copyright", needToken = true) {
|
||
const header = {
|
||
"content-type": "application/json"
|
||
// 有其他content-type需求加点逻辑判断处理即可
|
||
}
|
||
//是否需要token
|
||
if (needToken) {
|
||
const token = get('token')
|
||
if (token) {
|
||
header.Auth = `Bearer ${token}`;
|
||
}
|
||
}
|
||
//判断项目
|
||
var baseUrl = operatorUrl
|
||
if (project == 'operator') {
|
||
baseUrl = operatorUrl
|
||
} else if (project == 'copyright') {
|
||
baseUrl = copyrightUrl
|
||
} else if (project == 'plug') {
|
||
baseUrl = operatorPlug
|
||
}
|
||
return new Promise(function(resolve, reject) {
|
||
uni.request({
|
||
url: baseUrl + url,
|
||
timeout: 8000,
|
||
method,
|
||
data,
|
||
dataType: "json", // 微信官方文档中介绍会对数据进行一次JSON.parse
|
||
header,
|
||
success(res) {
|
||
if (res.statusCode === 200) {
|
||
resolve(res.data)
|
||
} else {
|
||
console.log('错误success')
|
||
reject(res.data)
|
||
}
|
||
},
|
||
fail(err) {
|
||
console.log('错误')
|
||
if (err.data) {
|
||
reject(err.data)
|
||
} else {
|
||
reject(err)
|
||
}
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
// 导出请求和服务地址
|
||
export {
|
||
request,
|
||
uploadImgUrl,
|
||
previewUrl,
|
||
copyrightUrl,
|
||
imgAssets,
|
||
appUrl
|
||
} |