12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- "use strict";
- const common_vendor = require("../common/vendor.js");
- const baseURL = "http://192.168.168.71:8080";
- const httpInterceptor = {
- // 拦截前触发
- invoke(options) {
- if (!options.url.startsWith("http")) {
- options.url = baseURL + options.url;
- }
- options.timeout = 3e4;
- options.header = {
- ...options.header,
- "source-client": "miniapp"
- };
- }
- };
- common_vendor.index.addInterceptor("request", httpInterceptor);
- const http = (options) => {
- return new Promise((resolve, reject) => {
- common_vendor.index.request({
- ...options,
- // 响应成功
- success(res) {
- if (res.statusCode >= 200 && res.statusCode < 300) {
- if (res.data.code == 401) {
- common_vendor.index.redirectTo({ url: "/" });
- } else {
- resolve(res.data);
- }
- } else if (res.statusCode === 401) {
- common_vendor.index.redirectTo({ url: "/" });
- reject(res);
- } else {
- common_vendor.index.showToast({
- icon: "none",
- title: res.data.msg || "请求错误"
- });
- reject(res);
- }
- },
- // 响应失败
- fail(err) {
- common_vendor.index.showToast({
- icon: "none",
- title: "网络错误,换个网络试试"
- });
- reject(err);
- }
- });
- });
- };
- exports.http = http;
|