3.request.ts文件封装
标签:--,微信,request,ts,url,res,data,wx From: https://www.cnblogs.com/meiyanstar/p/17786421.htmlapi.ts
import request from './request' //获取token export function init(data: object){ return request({ url:'/api/wechat_mini/auth/wx_init', data }) }request.ts
const app = getApp(); //提示语方法 function Toast( title: string | WechatMiniprogram.IAnyObject | ArrayBuffer, icon = "none" ) { wx.showToast({ title, icon, duration: 2000, }); }//这里ts校验还有一些问题,后续改正 //封装request请求 const request = (params: any) => { const { url, method = "POST", data = {}, contentType = "application/json", } = params; console.log('url:',url) console.log('method:',method) console.log('data:',data) console.log('contentType:',contentType) const token = wx.getStorageSync("token") || ""; return new Promise(function (resolve, reject) { wx.request({ url: `${app.globalData.baseUrl}/home/${url}`, data: data, method: method, header: { "Content-Type": contentType, Authorization: token, }, success: function (res) { if (res.header.Authorization || res.header.authorization) { wx.setStorageSync( "token", res.header.Authorization || res.header.authorization ); } var code = res.statusCode; switch (code) { case 401: wx.showModal({ title: "登录提示", content: "身份已过期,请重新登录后再来操作!", success(res) { if (res.confirm) { wx.navigateTo({ url: "../accredit/accredit", }); } }, }); break; case 402: Toast("该用户账号被冻结"); break; case 403: wx.showModal({ title: "温馨提示", content: "请认证后再来操作!", success(res) { if (res.confirm) { wx.navigateTo({ url: "../register/register", }); } }, }); break; case 422: Toast(res.data); resolve(res); break; default: resolve(res); break; } }, fail: function (err: any) { Toast("网络错误"); reject(err); }, }); }); }; export default request;