request.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. _options.header["Authorization"] = `Bearer ${sessionStorage.getItem("token")}`
  65. }
  66. let _config = _options
  67. if (that.interceptor.request && typeof that.interceptor.request === 'function') {
  68. _config = that.interceptor.request(_options)
  69. }
  70. let task = undefined
  71. let promise = new Promise((resolve, reject) => {
  72. let extras = {}
  73. that._prepare(that, _config, extras)
  74. if (_config.contentType === 'file') {
  75. task = uni.uploadFile({
  76. ..._config,
  77. success: res => {
  78. that._success(that, _config, res, resolve, reject)
  79. },
  80. fail: res => {
  81. that._fail(that, _config, res, resolve, reject)
  82. },
  83. complete: (res) => {
  84. that._complete(that, _config, res, extras)
  85. }
  86. })
  87. if (_config.progress && typeof _config.progress === 'function') {
  88. task.onProgressUpdate(_res => {
  89. _config.progress(_res, task)
  90. })
  91. }
  92. } else {
  93. console.log(_config)
  94. task = uni.request({
  95. ..._config,
  96. success: res => {
  97. that._success(that, _config, res, resolve, reject)
  98. },
  99. fail: res => {
  100. console.log("----2222--->公用方法_config放回数据", res)
  101. that._fail(that, _config, res, resolve, reject)
  102. },
  103. complete: (res) => {
  104. that._complete(that, _config, res, extras)
  105. }
  106. })
  107. }
  108. })
  109. if (_config.success || _config.fail || _config.complete) {
  110. return task;
  111. }
  112. return promise;
  113. }
  114. aa(){
  115. console.log('掉到了')
  116. }
  117. get(options = {}) {
  118. options.method = 'GET'
  119. return this.request(options)
  120. }
  121. post(options = {}) {
  122. options.method = 'POST'
  123. let data = {
  124. data: CryptoJS.AesEncrypt(JSON.stringify(options.data))
  125. }
  126. options.data = data;
  127. return this.request(options)
  128. }
  129. put(options = {}) {
  130. options.method = 'PUT'
  131. return this.request(options)
  132. }
  133. delete(options = {}) {
  134. options.method = 'DELETE'
  135. return this.request(options)
  136. }
  137. upload(options = {}) {
  138. options.method = 'POST'
  139. options.contentType = 'file'
  140. options.baseUrl = photoUrl
  141. return this.request(options)
  142. }
  143. _success = function(that, _config, res, resolve, reject) {
  144. if (res.data.code == "401") {
  145. that._fail(that, _config, res, resolve, reject);
  146. return
  147. }
  148. let re = null;
  149. if (_config.contentType == 'file') {
  150. re = JSON.parse(res?.data);
  151. } else {
  152. re = JSON.parse(CryptoJS.AesDecrypt(res?.data))
  153. }
  154. // console.log("登录返回信息2123", re)
  155. if (re.code == "200" || re.code == 200) {
  156. console.log("解密--------->", re)
  157. // var result = re
  158. // console.log("---12result---->公用方法放回数据", result)
  159. // var parseFileJson = _config.contentType === 'file' && typeof result === 'string' && (_config
  160. // .dataType ===
  161. // undefined || _config.dataType === 'json')
  162. // if (parseFileJson) {
  163. // result = re.data;
  164. // }
  165. // var skip = _config.skipInterceptorResponse
  166. // if (that.interceptor.response && typeof that.interceptor.response === 'function' && !skip) {
  167. // result = that.interceptor.response(result, _config)
  168. // if (_config.businessSuccess) {
  169. // var _data = _config.business ? result[_config.business] : result;
  170. // _config.success ? _config.success(_data) : resolve(_data)
  171. // return;
  172. // }
  173. // } else {
  174. // _config.success ? _config.success(result) : resolve(result)
  175. // return;
  176. // }
  177. return _config.success ? _config.success(re) : resolve(re)
  178. }
  179. console.log("---12---->公用方法放回数据", res)
  180. that._fail(that, _config, res, resolve, reject)
  181. }
  182. _fail = function(that, _config, re, resolve, reject) {
  183. let res = CryptoJS.AesDecrypt(re?.data)
  184. console.log("----数据失败返回信息", res)
  185. // console.log("----parBase64Decode--->", parBase64Decode);
  186. if (res.errMsg === 'request:fail') {
  187. return
  188. }
  189. var result = res
  190. if (that.interceptor.fail && typeof that.interceptor.fail === 'function') {
  191. result = that.interceptor.fail(res, _config)
  192. }
  193. _config.fail ? _config.fail(result) : reject(result)
  194. }
  195. _prepare = function(that, _config, obj = {}) {
  196. if (that.interceptor.prepare && typeof that.interceptor.prepare === 'function') {
  197. that.interceptor.prepare(_config, obj)
  198. return
  199. }
  200. obj.startTime = Date.now()
  201. if (_config.loadingTip) {
  202. uni.showLoading({
  203. title: _config.loadingTip
  204. })
  205. }
  206. if (_config.contentType === 'file') {
  207. if (_config.formData === undefined || _config.formData === null) {
  208. _config.formData = _config.data
  209. delete _config.data
  210. }
  211. delete _config.header['Content-Type']
  212. delete _config.header['Referer']
  213. _config.method = 'POST'
  214. }
  215. }
  216. _complete = function(that, _config, res, obj = {}) {
  217. if (that.interceptor.complete && typeof that.interceptor.complete === 'function') {
  218. that.interceptor.complete(_config, obj, res)
  219. return
  220. }
  221. obj.endTime = Date.now()
  222. if (_config.loadingTip) {
  223. let diff = obj.endTime - obj.startTime;
  224. let duration = _config.loadingDuration || 500
  225. if (diff < duration) {
  226. diff = duration - diff
  227. } else {
  228. diff = 0
  229. }
  230. setTimeout(function() {
  231. uni.hideLoading()
  232. }, diff)
  233. }
  234. if (_config.complete) {
  235. _config.complete(res)
  236. }
  237. }
  238. }
  239. var request = new Request()
  240. request.setConfig({
  241. baseUrl: baseUrl,
  242. debug: true
  243. })
  244. export default request
  245. //云支付 https://blog.csdn.net/qq_32340877/article/details/125461425