123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- "use strict";
- const common_vendor = require("../common/vendor.js");
- const utils_errorHandler = require("./errorHandler.js");
- const BASE_URL = "https://minlong.raycos.com.cn";
- const TIMEOUT = 6e4;
- const requestInterceptor = (config) => {
- const token = common_vendor.index.getStorageSync("token");
- const csrfToken = common_vendor.index.getStorageSync("csrfToken");
- if (!config.header) {
- config.header = {};
- }
- if (token) {
- config.header["Authorization"] = `Bearer ${token}`;
- }
- if (csrfToken) {
- config.header["X-CSRF-Token"] = csrfToken;
- }
- if (!config.header["Content-Type"]) {
- config.header["Content-Type"] = "application/json";
- }
- if (!config.url.startsWith("http")) {
- config.url = BASE_URL + config.url;
- }
- if (config.method === "POST" && config.data) {
- config.data._csrf = csrfToken;
- }
- return config;
- };
- const showError = (message, duration = 2e3) => {
- if (showError.lastMessage === message && Date.now() - showError.lastTime < 3e3) {
- return;
- }
- common_vendor.index.showToast({
- title: message,
- icon: "none",
- duration
- });
- showError.lastMessage = message;
- showError.lastTime = Date.now();
- };
- showError.lastMessage = "";
- showError.lastTime = 0;
- const responseInterceptor = (response) => {
- if (response.statusCode === 200) {
- const { data } = response;
- if (data.status === 999 || data.code === 999) {
- const error = new Error(data.message || "微信登录失败: code无效");
- error.status = 999;
- return Promise.reject(error);
- }
- if (data.status === 2e3 || data.code === 0 || data.code === 2e3) {
- if (data.data) {
- return data.data;
- }
- if (data.openid || data.userId || data.user_id) {
- return data;
- }
- return data;
- } else {
- const error = utils_errorHandler.errorHandler.handleBusinessError(data);
- return Promise.reject(error);
- }
- } else {
- const error = utils_errorHandler.errorHandler.handleHttpError(response.statusCode, response);
- return Promise.reject(error);
- }
- };
- const request = (options = {}) => {
- return new Promise((resolve, reject) => {
- options = requestInterceptor(options);
- if (!options.timeout) {
- options.timeout = TIMEOUT;
- }
- common_vendor.index.request({
- ...options,
- success: (res) => {
- try {
- const data = responseInterceptor(res);
- resolve(data);
- } catch (error) {
- utils_errorHandler.errorHandler.logError(error, "request.responseInterceptor");
- reject(error);
- }
- },
- fail: (err) => {
- const error = utils_errorHandler.errorHandler.handleRequestFail(err);
- utils_errorHandler.errorHandler.logError(error, "request.fail");
- reject(error);
- }
- });
- });
- };
- const http = {
- get(url, data = {}, options = {}) {
- return request({
- url,
- data,
- method: "GET",
- ...options
- });
- },
- post(url, data = {}, options = {}) {
- return request({
- url,
- data,
- method: "POST",
- ...options
- });
- },
- put(url, data = {}, options = {}) {
- return request({
- url,
- data,
- method: "PUT",
- ...options
- });
- },
- delete(url, data = {}, options = {}) {
- return request({
- url,
- data,
- method: "DELETE",
- ...options
- });
- },
- // 上传文件
- upload(url, filePath, name = "file", formData = {}, options = {}) {
- return new Promise((resolve, reject) => {
- const token = common_vendor.index.getStorageSync("token");
- const header = options.header || {};
- if (token) {
- header["Authorization"] = `Bearer ${token}`;
- }
- if (!url.startsWith("http")) {
- url = BASE_URL + url;
- }
- common_vendor.index.uploadFile({
- url,
- filePath,
- name,
- formData,
- header,
- success: (res) => {
- try {
- if (typeof res.data === "string") {
- res.data = JSON.parse(res.data);
- }
- const data = responseInterceptor({
- statusCode: res.statusCode,
- data: res.data
- });
- resolve(data);
- } catch (error) {
- reject(error);
- }
- },
- fail: (err) => {
- showError("文件上传失败");
- reject(err);
- }
- });
- });
- }
- };
- exports.http = http;
|