request.js 7.0 KB

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