ts_aimz_uni/common/js/net/http.js
2025-05-15 16:54:07 +08:00

79 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}