2024-03-11 15:53:28 +08:00

83 lines
1.8 KiB
JavaScript
Raw Permalink 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 { 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 = {}, errShowT = false }) => {
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) {
if (!errShowT) {
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
}