80 lines
1.7 KiB
JavaScript
80 lines
1.7 KiB
JavaScript
import { basUrl } from './config'
|
||
|
||
import cloud from '@tbmp/mp-cloud-sdk';
|
||
cloud.init({
|
||
//test、online
|
||
env: 'test'
|
||
});
|
||
|
||
const httpRequest = async ({ path, method = 'GET', params = {}, body = {}, exts = {} }) => {
|
||
|
||
const dataUid = my.getStorageSync({ key: 'uid' }).data;
|
||
let isBody = {};
|
||
let isParems = {};
|
||
if (method == 'POST') {
|
||
isBody = {
|
||
...body,
|
||
}
|
||
if (dataUid && dataUid.uid) {
|
||
isBody.uid = dataUid.uid;
|
||
}
|
||
|
||
}
|
||
|
||
if (method == 'GET') {
|
||
isParems = {
|
||
...params
|
||
}
|
||
if (dataUid && dataUid.uid) {
|
||
isParems.uid = dataUid.uid;
|
||
}
|
||
}
|
||
|
||
try {
|
||
const result = await cloud.application.httpRequest({
|
||
//不需要完整域名,只需要接口访问路径即可
|
||
path: path,
|
||
method: method,
|
||
//POST请求需要指定下请求格式,只支持application/json。 如:"content-type":"application/json;charset=UTF-8"
|
||
headers: { "Content-Type": "application/json;charset=UTF-8" },
|
||
params: {
|
||
...isParems,
|
||
},
|
||
body: {
|
||
// uid: uid,
|
||
...isBody,
|
||
},
|
||
//对于一个小程序关联多个云应用的场景,调用非默认云应用,需要指定对应的云应用Id,超时时间单位ms
|
||
exts: {
|
||
// cloudAppId: "49944",
|
||
domain: basUrl,
|
||
...exts,
|
||
}
|
||
});
|
||
// console.log(JSON.stringify(result),"JSON.stringify(resul")
|
||
if (JSON.stringify(result) != '{}' && result) {
|
||
const isResult = JSON.parse(result);
|
||
if (isResult.status == 1) {
|
||
return isResult;
|
||
}
|
||
if (isResult.status == 2) {
|
||
my.showToast({
|
||
content: isResult.msg,
|
||
duration: 3000,
|
||
});
|
||
return Promise.reject(isResult);
|
||
}
|
||
}
|
||
return result;
|
||
// console.log(JSON.stringify(result));
|
||
} catch (err) {
|
||
|
||
console.log(err, 'httpRequest_err')
|
||
}
|
||
};
|
||
|
||
export {
|
||
httpRequest,
|
||
cloud
|
||
}
|