request.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. const common_vendor = require("./vendor.js");
  3. const common_config = require("./config.js");
  4. const utils_aes_util = require("../utils/aes_util.js");
  5. class Request {
  6. constructor() {
  7. this.config = {
  8. baseUrl: common_config.baseUrl,
  9. business: "data"
  10. };
  11. this.interceptor = {
  12. request: void 0,
  13. response: void 0,
  14. fail: void 0,
  15. complete: void 0
  16. // since 1.2.0
  17. };
  18. this._success = function(that, _config, res, resolve, reject) {
  19. if (res.data.code == "401") {
  20. that._fail(that, _config, res, resolve, reject);
  21. return;
  22. }
  23. let re = null;
  24. if (_config.contentType == "file") {
  25. re = JSON.parse(res == null ? void 0 : res.data);
  26. } else {
  27. re = JSON.parse(utils_aes_util.CryptoJS.AesDecrypt(res == null ? void 0 : res.data));
  28. }
  29. if (re.code == "200" || re.code == 200) {
  30. console.log("解密--------->", re);
  31. return _config.success ? _config.success(re) : resolve(re);
  32. }
  33. console.log("---12---->公用方法放回数据", res);
  34. that._fail(that, _config, res, resolve, reject);
  35. };
  36. this._fail = function(that, _config, re, resolve, reject) {
  37. let res = utils_aes_util.CryptoJS.AesDecrypt(re == null ? void 0 : re.data);
  38. console.log("----数据失败返回信息", res);
  39. if (res.errMsg === "request:fail") {
  40. return;
  41. }
  42. var result = res;
  43. if (that.interceptor.fail && typeof that.interceptor.fail === "function") {
  44. result = that.interceptor.fail(res, _config);
  45. }
  46. _config.fail ? _config.fail(result) : reject(result);
  47. };
  48. this._prepare = function(that, _config, obj = {}) {
  49. if (that.interceptor.prepare && typeof that.interceptor.prepare === "function") {
  50. that.interceptor.prepare(_config, obj);
  51. return;
  52. }
  53. obj.startTime = Date.now();
  54. if (_config.loadingTip) {
  55. common_vendor.index.showLoading({
  56. title: _config.loadingTip
  57. });
  58. }
  59. if (_config.contentType === "file") {
  60. if (_config.formData === void 0 || _config.formData === null) {
  61. _config.formData = _config.data;
  62. delete _config.data;
  63. }
  64. delete _config.header["Content-Type"];
  65. delete _config.header["Referer"];
  66. _config.method = "POST";
  67. }
  68. };
  69. this._complete = function(that, _config, res, obj = {}) {
  70. if (that.interceptor.complete && typeof that.interceptor.complete === "function") {
  71. that.interceptor.complete(_config, obj, res);
  72. return;
  73. }
  74. obj.endTime = Date.now();
  75. if (_config.loadingTip) {
  76. let diff = obj.endTime - obj.startTime;
  77. let duration = _config.loadingDuration || 500;
  78. if (diff < duration) {
  79. diff = duration - diff;
  80. } else {
  81. diff = 0;
  82. }
  83. setTimeout(function() {
  84. common_vendor.index.hideLoading();
  85. }, diff);
  86. }
  87. if (_config.complete) {
  88. _config.complete(res);
  89. }
  90. };
  91. }
  92. static posUrl(url) {
  93. return /(http|https):\/\/([\w.]+\/?)\S*/.test(url);
  94. }
  95. static getUrl(config) {
  96. let url = config.url || "";
  97. let abs = Request.posUrl(url);
  98. if (!abs) {
  99. let f = config.slashAbsoluteUrl;
  100. if (f) {
  101. abs = /^\/([\w.]+\/?)\S*/.test(url);
  102. }
  103. }
  104. return abs ? url : config.baseUrl + url;
  105. }
  106. static getContentType(config) {
  107. var type = config.contentType || "json";
  108. var charset = config.encoding || "UTF-8";
  109. if (type === "json") {
  110. return "application/json;charset=" + charset;
  111. } else if (type === "form") {
  112. return "application/x-www-form-urlencoded;charset=" + charset;
  113. } else if (type === "file") {
  114. return "multipart/form-data;charset=" + charset;
  115. } else if (type === "text") {
  116. return "text/plain;charset=" + charset;
  117. } else if (type === "html") {
  118. return "text/html;charset=" + charset;
  119. } else {
  120. throw new Error("unsupported content type : " + type);
  121. }
  122. }
  123. setConfig(config) {
  124. this.config = Object.assign(this.config, config);
  125. }
  126. request(options = {}) {
  127. var that = this;
  128. if (options.data === void 0) {
  129. options.data = {};
  130. }
  131. if (options.header === void 0) {
  132. options.header = {};
  133. }
  134. let _options = Object.assign({}, this.config, options);
  135. _options = Object.assign(options, _options);
  136. _options.url = Request.getUrl(_options);
  137. if (!_options.header["Content-Type"]) {
  138. _options.header["Content-Type"] = Request.getContentType(_options);
  139. _options.header["Authorization"] = `Bearer ${sessionStorage.getItem("token")}`;
  140. }
  141. let _config = _options;
  142. if (that.interceptor.request && typeof that.interceptor.request === "function") {
  143. _config = that.interceptor.request(_options);
  144. }
  145. let task = void 0;
  146. let promise = new Promise((resolve, reject) => {
  147. let extras = {};
  148. that._prepare(that, _config, extras);
  149. if (_config.contentType === "file") {
  150. task = common_vendor.index.uploadFile({
  151. ..._config,
  152. success: (res) => {
  153. that._success(that, _config, res, resolve, reject);
  154. },
  155. fail: (res) => {
  156. that._fail(that, _config, res, resolve, reject);
  157. },
  158. complete: (res) => {
  159. that._complete(that, _config, res, extras);
  160. }
  161. });
  162. if (_config.progress && typeof _config.progress === "function") {
  163. task.onProgressUpdate((_res) => {
  164. _config.progress(_res, task);
  165. });
  166. }
  167. } else {
  168. console.log(_config);
  169. task = common_vendor.index.request({
  170. ..._config,
  171. success: (res) => {
  172. that._success(that, _config, res, resolve, reject);
  173. },
  174. fail: (res) => {
  175. console.log("----2222--->公用方法_config放回数据", res);
  176. that._fail(that, _config, res, resolve, reject);
  177. },
  178. complete: (res) => {
  179. that._complete(that, _config, res, extras);
  180. }
  181. });
  182. }
  183. });
  184. if (_config.success || _config.fail || _config.complete) {
  185. return task;
  186. }
  187. return promise;
  188. }
  189. aa() {
  190. console.log("掉到了");
  191. }
  192. get(options = {}) {
  193. options.method = "GET";
  194. return this.request(options);
  195. }
  196. post(options = {}) {
  197. options.method = "POST";
  198. let data = {
  199. data: utils_aes_util.CryptoJS.AesEncrypt(JSON.stringify(options.data))
  200. };
  201. options.data = data;
  202. return this.request(options);
  203. }
  204. put(options = {}) {
  205. options.method = "PUT";
  206. return this.request(options);
  207. }
  208. delete(options = {}) {
  209. options.method = "DELETE";
  210. return this.request(options);
  211. }
  212. upload(options = {}) {
  213. options.method = "POST";
  214. options.contentType = "file";
  215. options.baseUrl = common_config.photoUrl;
  216. return this.request(options);
  217. }
  218. }
  219. var request = new Request();
  220. request.setConfig({
  221. baseUrl: common_config.baseUrl,
  222. debug: true
  223. });
  224. exports.request = request;