ajax.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import qs from 'qs'
  2. import md5 from 'js-md5'
  3. import config from '../js/config'
  4. import fn from '../js/function'
  5. import plugin from '../js/plugin'
  6. import router from '../js/router'
  7. import store from '../store'
  8. const http = {
  9. /**
  10. * ajax请求处理
  11. * @param type
  12. * @param url
  13. * @param data
  14. * @param map
  15. * @param repeat
  16. * @param awaitTime
  17. * @param isJson
  18. * @returns {Promise<*|Promise<any>|Promise<Promise<any>|*>>}
  19. */
  20. async ajax(type, url, data = {}, { map = {}, repeat = false, awaitTime = 1000, isJson = false } = {}) {
  21. const self = this
  22. let httpKey = md5(url + qs.stringify(data))
  23. let ContentType = 'application/x-www-form-urlencoded;charset=UTF-8;'
  24. // 重复请求检测
  25. if (repeat) {
  26. let httpInfo = store.state.http[httpKey]
  27. if (httpInfo && self.isRepeat(httpKey)) {
  28. console.warn('重复请求过滤')
  29. return new Promise((resolve) => {
  30. resolve({
  31. code: '-1',
  32. data: '',
  33. message: '正在处理中,请稍等...',
  34. requestData: '',
  35. })
  36. })
  37. } else {
  38. // 记录请求信息
  39. store.commit('http/add', httpKey)
  40. }
  41. }
  42. // 请求数据处理
  43. data = self.request(data, map)
  44. // get请求url处理
  45. if (type === 'GET') {
  46. data = qs.stringify(data)
  47. url = url + '?' + data
  48. }
  49. // json请求类型处理,兼容后台接口混乱问题
  50. if (isJson) {
  51. ContentType = 'application/json;charset=UTF-8;'
  52. } else {
  53. data = qs.stringify(data)
  54. }
  55. // 添加请求基地址
  56. if (url.indexOf('http') < 0) {
  57. url = config.apiBaseUrl + url
  58. }
  59. // 发送请求
  60. console.log(store.state.user,'ajax')
  61. let header = {
  62. 'Accept': 'application/json',
  63. 'Content-Type': ContentType,
  64. 'appId': store.state.user.user ? (store.state.user.user.appId || '') : '',
  65. 'companyId': store.state.user.user ? (store.state.user.user.companyId || '') : '',
  66. 'userId': store.state.user.user ? (store.state.user.user.id || '') : '',
  67. 'Authorization': 'Bearer ' + store.state.user.token,
  68. // 'StoreId': store.state.common.curShop.id || ''
  69. }
  70. if (store.state.user && store.state.user.user) {
  71. header.StoreId = store.state.user.user.storeId || '';
  72. }
  73. let response = await uni.request({
  74. url: url,
  75. data: data,
  76. header: header,
  77. method: type,
  78. })
  79. return self.response(response, {
  80. map: map,
  81. httpKey: httpKey,
  82. repeat: repeat,
  83. awaitTime: awaitTime,
  84. request: arguments,
  85. })
  86. },
  87. /**
  88. * 是否是重复请求处理
  89. * @param httpKey
  90. * @returns {boolean}
  91. */
  92. isRepeat(httpKey) {
  93. let httpInfo = store.state.http[httpKey]
  94. let nowTime = new Date().getTime()
  95. let outTime = 5
  96. return httpInfo && nowTime < httpInfo + outTime * 1000
  97. },
  98. /**
  99. * 请求参数处理
  100. * @param data
  101. * @param map
  102. * @returns {*}
  103. */
  104. request(data, map) {
  105. const self = this
  106. // data.device = data.device || 'web'
  107. // data.timestamp = data.timestamp || self.getTimestamp()
  108. // data.noncestr = data.noncestr || fn.uuid('')
  109. // 请求数据映射处理
  110. data = fn.deepClone(data)
  111. data = fn.objSetKey(data, map)
  112. for (let key in data) {
  113. if (data.hasOwnProperty(key) && (data[key] === undefined)) {
  114. delete data[key]
  115. }
  116. }
  117. // 签名处理
  118. // data = self.setSign(data)
  119. return data
  120. },
  121. /**
  122. * 获取时间戳
  123. */
  124. getTimestamp() {
  125. let differTimestamp = store.state.common.differTimestamp || 0
  126. let localTimestamp = parseInt(new Date().getTime() / 1000)
  127. return localTimestamp + differTimestamp
  128. },
  129. /**
  130. * 添加签名处理
  131. * @param data
  132. * @returns {*|{}}
  133. */
  134. setSign(data) {
  135. let sign = '' // 签名
  136. data = fn.objSort(data)
  137. sign = decodeURIComponent(qs.stringify(data))
  138. data.sign = md5(sign + config.salt)
  139. return data
  140. },
  141. /**
  142. * 请求响应处理
  143. * @param res
  144. * @param map
  145. * @param repeat
  146. * @param httpKey
  147. * @param awaitTime
  148. * @param request
  149. * @returns {Promise<any>}
  150. */
  151. response(res, { map = {}, httpKey = '', repeat = false, awaitTime = '', request = [] } = {}) {
  152. const self = this
  153. return new Promise(async resolve => {
  154. let response = res[1]
  155. let data = {
  156. success: response.data.success,
  157. affectedRows: response.data.affectedRows,
  158. code: response.data.code,
  159. message: '无法访问到服务器',
  160. data: '',
  161. }
  162. // 移除请求记录
  163. if (repeat) {
  164. setTimeout(function () {
  165. store.commit('http/success', httpKey)
  166. }, awaitTime)
  167. }
  168. // 请求成功数据处理
  169. if (response.statusCode === 200) {
  170. //图片地址单独处理
  171. if (request[1] === '/Coupon/GetQrCode') {
  172. data = response;
  173. } else {
  174. data = {
  175. success: response.data.success,
  176. affectedRows: response.data.affectedRows,
  177. code: response.data.code,
  178. message: response.data.message || '',
  179. data: fn.objSetKey(response.data.data, map, true),
  180. }
  181. }
  182. } else if (response.statusCode === 401) {
  183. await plugin.getToken()
  184. data = await self.ajax(request[0], request[1], request[2], request[3])
  185. // router.push({
  186. // path:'/pages/index/login',
  187. // isTabBar:true
  188. // });
  189. } else {
  190. console.error(response.data)
  191. }
  192. resolve(data)
  193. })
  194. },
  195. }
  196. export default {
  197. /**
  198. * get请求(获取)
  199. * @param url
  200. * @param data
  201. * @param config
  202. * @returns {*}
  203. */
  204. get(url, data, config) {
  205. return http.ajax('GET', url, data, config)
  206. },
  207. /**
  208. * post请求(添加)
  209. * @param url
  210. * @param data
  211. * @param config
  212. * @returns {*}
  213. */
  214. post(url, data, config) {
  215. return http.ajax('POST', url, data, config)
  216. },
  217. /**
  218. * put请求(修改)
  219. * @param url
  220. * @param data
  221. * @param config
  222. * @returns {*}
  223. */
  224. put(url, data, config) {
  225. return http.ajax('PUT', url, data, config)
  226. },
  227. /**
  228. * delete请求(删除)
  229. * @param url
  230. * @param data
  231. * @param config
  232. * @returns {*}
  233. */
  234. delete(url, data, config) {
  235. return http.ajax('DELETE', url, data, config)
  236. },
  237. }