123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- function postUrlAction(url: string, parameter: any) {
- let params = "";
- for (let attr in parameter) {
- params += attr + "=" + parameter[attr] + "&";
- }
- params = "?" + params.substr(0, params.length - 1);
- return url + params;
- }
- module.exports = function (options: any) {
- const App = getApp();
- const baseUrl = App.globalData?.baseUrl;
- return new Promise((resolve, reject) => {
- let requestType = "";
- // 是否不显示提示
- let hideLoading = options.hideLoading || false
- let url = baseUrl + options.url;
- if (options.requestType === "form") {
- requestType = "application/x-www-form-urlencoded";
- // url = postUrlAction(url, options.data);
- } else if (options.requestType === "data") {
- requestType = "multipart/form-data";
- } else {
- requestType = "application/json; charset=UTF-8";
- }
- // token
- const Authorization = wx.getStorageSync("token");
- // open 接口不需要传token
- const isOpenApi = options.url.includes('/open/')
- const isCodeApi = options.url.includes('/code/')
- const noToken = isOpenApi || isCodeApi || options.noToken
- wx.request({
- url: url,
- method: options.method,
- data: options.data,
- header: noToken ? {
- "Content-Type": requestType,
- } : Authorization
- ? {
- "Content-Type": requestType,
- Authorization,
- // 'x-token': 'x-token'
- }
- : {
- "Content-Type": requestType,
- // 'x-token': 'x-token'
- },
- success: function (res: any) {
- // console.log(res.data.code)
- if (res.data.code == 500) {
- // console.log(res.data.msg,'cuowu====>')
- if(!hideLoading) {
- setTimeout(() => {
- wx.showToast({
- title: res.data.msg,
- icon: "none",
- });
- }, 100);
- }
- reject(res.data.msg);
- } else if (res.data.code == 403) {
- wx.setStorageSync("token", "");
- if(!hideLoading) {
- setTimeout(() => {
- wx.showToast({
- title: "登录超时",
- icon: "none",
- });
- }, 100);
- }
- const pages = getCurrentPages();
- // console.log(pages, 'pages')
- const currentPage = pages[pages.length - 1];
- // console.log(currentPage.route,'currentPage==>')
- // 为了处理发版处理;
- // https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&key=11669729383k7cis&version=1&lang=zh_CN&platform=2
- const route = currentPage.route === 'pages/purchaseRecord/record' ? currentPage.route : ''
- if (currentPage.route != "pages/login/index") {
- wx.redirectTo({
- url: "/pages/login/index?redirectUrl=" + route,
- });
- }
- } else {
- resolve(res);
- }
- //这里可以加状态判断
- },
- fail: function (error) {
- reject(error);
- },
- complete: () => {
- // setTimeout(() => {
- // wx.hideLoading();
- // }, 100);
- },
- });
- });
- };
|