"use strict"; const common_vendor = require("./vendor.js"); const common_config = require("./config.js"); const utils_aes_util = require("../utils/aes_util.js"); class Request { constructor() { this.config = { baseUrl: common_config.baseUrl, business: "data" }; this.interceptor = { request: void 0, response: void 0, fail: void 0, complete: void 0 // since 1.2.0 }; this._success = function(that, _config, res, resolve, reject) { if (res.data.code == "401") { that._fail(that, _config, res, resolve, reject); return; } let re = null; if (_config.contentType == "file") { re = JSON.parse(res == null ? void 0 : res.data); } else { re = JSON.parse(utils_aes_util.CryptoJS.AesDecrypt(res == null ? void 0 : res.data)); } if (re.code == "200" || re.code == 200) { console.log("解密--------->", re); return _config.success ? _config.success(re) : resolve(re); } console.log("---12---->公用方法放回数据", res); that._fail(that, _config, res, resolve, reject); }; this._fail = function(that, _config, re, resolve, reject) { let res = utils_aes_util.CryptoJS.AesDecrypt(re == null ? void 0 : re.data); console.log("----数据失败返回信息", res); if (res.errMsg === "request:fail") { return; } var result = res; if (that.interceptor.fail && typeof that.interceptor.fail === "function") { result = that.interceptor.fail(res, _config); } _config.fail ? _config.fail(result) : reject(result); }; this._prepare = function(that, _config, obj = {}) { if (that.interceptor.prepare && typeof that.interceptor.prepare === "function") { that.interceptor.prepare(_config, obj); return; } obj.startTime = Date.now(); if (_config.loadingTip) { common_vendor.index.showLoading({ title: _config.loadingTip }); } if (_config.contentType === "file") { if (_config.formData === void 0 || _config.formData === null) { _config.formData = _config.data; delete _config.data; } delete _config.header["Content-Type"]; delete _config.header["Referer"]; _config.method = "POST"; } }; this._complete = function(that, _config, res, obj = {}) { if (that.interceptor.complete && typeof that.interceptor.complete === "function") { that.interceptor.complete(_config, obj, res); return; } obj.endTime = Date.now(); if (_config.loadingTip) { let diff = obj.endTime - obj.startTime; let duration = _config.loadingDuration || 500; if (diff < duration) { diff = duration - diff; } else { diff = 0; } setTimeout(function() { common_vendor.index.hideLoading(); }, diff); } if (_config.complete) { _config.complete(res); } }; } static posUrl(url) { return /(http|https):\/\/([\w.]+\/?)\S*/.test(url); } static getUrl(config) { let url = config.url || ""; let abs = Request.posUrl(url); if (!abs) { let f = config.slashAbsoluteUrl; if (f) { abs = /^\/([\w.]+\/?)\S*/.test(url); } } return abs ? url : config.baseUrl + url; } static getContentType(config) { var type = config.contentType || "json"; var charset = config.encoding || "UTF-8"; if (type === "json") { return "application/json;charset=" + charset; } else if (type === "form") { return "application/x-www-form-urlencoded;charset=" + charset; } else if (type === "file") { return "multipart/form-data;charset=" + charset; } else if (type === "text") { return "text/plain;charset=" + charset; } else if (type === "html") { return "text/html;charset=" + charset; } else { throw new Error("unsupported content type : " + type); } } setConfig(config) { this.config = Object.assign(this.config, config); } request(options = {}) { var that = this; if (options.data === void 0) { options.data = {}; } if (options.header === void 0) { options.header = {}; } let _options = Object.assign({}, this.config, options); _options = Object.assign(options, _options); _options.url = Request.getUrl(_options); if (!_options.header["Content-Type"]) { _options.header["Content-Type"] = Request.getContentType(_options); _options.header["Authorization"] = `Bearer ${sessionStorage.getItem("token")}`; } let _config = _options; if (that.interceptor.request && typeof that.interceptor.request === "function") { _config = that.interceptor.request(_options); } let task = void 0; let promise = new Promise((resolve, reject) => { let extras = {}; that._prepare(that, _config, extras); if (_config.contentType === "file") { task = common_vendor.index.uploadFile({ ..._config, success: (res) => { that._success(that, _config, res, resolve, reject); }, fail: (res) => { that._fail(that, _config, res, resolve, reject); }, complete: (res) => { that._complete(that, _config, res, extras); } }); if (_config.progress && typeof _config.progress === "function") { task.onProgressUpdate((_res) => { _config.progress(_res, task); }); } } else { console.log(_config); task = common_vendor.index.request({ ..._config, success: (res) => { that._success(that, _config, res, resolve, reject); }, fail: (res) => { console.log("----2222--->公用方法_config放回数据", res); that._fail(that, _config, res, resolve, reject); }, complete: (res) => { that._complete(that, _config, res, extras); } }); } }); if (_config.success || _config.fail || _config.complete) { return task; } return promise; } aa() { console.log("掉到了"); } get(options = {}) { options.method = "GET"; return this.request(options); } post(options = {}) { options.method = "POST"; let data = { data: utils_aes_util.CryptoJS.AesEncrypt(JSON.stringify(options.data)) }; options.data = data; return this.request(options); } put(options = {}) { options.method = "PUT"; return this.request(options); } delete(options = {}) { options.method = "DELETE"; return this.request(options); } upload(options = {}) { options.method = "POST"; options.contentType = "file"; options.baseUrl = common_config.photoUrl; return this.request(options); } } var request = new Request(); request.setConfig({ baseUrl: common_config.baseUrl, debug: true }); exports.request = request;