//1、首先创建一个工具包utils //2、创建一个api.js文件 //3、编写api.js内容: //创建一个常量 conset BASE_URL作为默认url地址 const BEST_URL = "https://tcapidemo.tripodeck.com"; export const myRequest = (option) => { //将封装好的函数导入出去 return new Promise((resolve, reject) => { uni.request({ url: BEST_URL + option.url, //传入url地址 data: option.data || {}, //请求数据 method: option.method || "GET", //请求方法 header: option.header || { // 根据实际接口设计 key 取 token 或者 authorization lang: "zh-CN", // #ifdef MP-WEIXIN app_type: "miniprogram", // #endif // #ifndef MP-WEIXIN app_type: "app", // #endif }, success: (res) => { console.log("全局", res); if (res.data.status == 3) { console.log("接口删除token"); // uni.$u.toast(res.data.msg); } //相应成功回调函数 if (res.statusCode !== 201 && res.statusCode !== 200) { console.log("获取数据失败!"); // uni.showToast({ // title: "获取数据失败!", // icon: "none", // }); } resolve(res); // 将请求结果resolve出去 }, fail: (err) => { console.log("请求接口失败"); // uni.showToast({ // title: "请求接口失败", // icon: "none", // }); // rej; // ect(err); }, }); }); };