request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Request 0.0.7
  3. * @Class uni-app request网络请求库
  4. * @Author lu-ch
  5. * @Date 2019-07-11
  6. * @Email webwork.s@qq.com
  7. * http://ext.dcloud.net.cn/plugin?id=392
  8. * **/
  9. import qs from 'qs'
  10. import constant from '@/constant/index'
  11. import { parse, getCheckLogin } from '@/utils/util'
  12. const Base64 = require('js-base64').Base64
  13. class Request {
  14. config = {
  15. baseUrl: constant.hostUrl,
  16. header: {
  17. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  18. },
  19. data: {},
  20. method: 'GET',
  21. dataType: 'json', /* 如设为json,会对返回的数据做一次 JSON.parse */
  22. responseType: 'text',
  23. success() { },
  24. fail() { },
  25. complete() { }
  26. }
  27. static posUrl(url) { /* 判断url是否为绝对路径 */
  28. return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
  29. }
  30. interceptor = {
  31. request: (f) => {
  32. if (f && typeof f === 'function') {
  33. this.requestBeforeFun = f
  34. }
  35. },
  36. response: (f) => {
  37. if (f && typeof f === 'function') {
  38. this.requestComFun = f
  39. }
  40. }
  41. }
  42. requestBeforeFun(config, cancel) {
  43. let token = ''
  44. try {
  45. const st = uni.getStorageSync('token')
  46. if (st) {
  47. token = st
  48. }
  49. } catch (e) {
  50. console.error(e, '获取token异常')
  51. }
  52. config.header = {
  53. ...config.header
  54. }
  55. // 将token 放入header
  56. if (token) {
  57. config.header['token'] = token
  58. }
  59. config.header['appVersion'] = 2
  60. // const token = uni.getStorageSync('token')
  61. // config.data = JSON.stringify(config.data);
  62. config.headers = {
  63. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  64. }
  65. // if (token) { // 判断是否存在token,如果存在的话,则每个http header都加上token
  66. // // axios.defaults.headers.common['token'] = token;
  67. // config.headers.token = token
  68. // }
  69. // config.headers.HkdsRequestFrom = window.location.href
  70. // config.header.userRegion = Base64.encode(uni.getStorageSync('userRegion')) || null
  71. // config.header['x-client-hash'] = uni.getStorageSync('client_hash') || null
  72. const userRegion = Base64.encode(uni.getStorageSync('userRegion')) || null
  73. // if (userRegion) {
  74. // config.header.userRegion = userRegion
  75. // }
  76. const account = uni.getStorageSync('account') || null
  77. if (account) {
  78. config.header['account'] = account
  79. }
  80. // const source = uni.getStorageSync('source') || null
  81. // if (source) {
  82. // config.header['source'] = source
  83. // }
  84. if (constant.source == 'mobile') {
  85. const shopId = uni.getStorageSync('shopId') || 0
  86. if (shopId) {
  87. config.header['shopId'] = shopId
  88. }
  89. }
  90. if (config.config.loading) {
  91. uni.showLoading({
  92. title: '',
  93. mask: true
  94. })
  95. }
  96. return config
  97. }
  98. requestComFun(response) {
  99. if (response.config.config.loading) {
  100. uni.hideLoading()
  101. }
  102. // const client_hash = response.header.client_hash || null
  103. // console.log('X-Client-Hash', response.header['X-Client-Hash'])
  104. // if (client_hash) {
  105. // uni.setStorageSync('client_hash', client_hash)
  106. // }
  107. return response
  108. }
  109. setConfig(f) {
  110. this.config = f(this.config)
  111. }
  112. request(options = {}, config) {
  113. options.baseUrl = options.baseUrl || this.config.baseUrl
  114. options.dataType = options.dataType || this.config.dataType
  115. options.url = Request.posUrl(options.url) ? options.url : (options.baseUrl + options.url)
  116. options.data = qs.stringify(options.data) || {}
  117. options.header = options.header || this.config.header
  118. options.method = options.method || this.config.method
  119. return new Promise((resolve, reject) => {
  120. let next = true
  121. let _config = null
  122. options.complete = (response) => {
  123. const statusCode = response.statusCode
  124. response.config = _config
  125. response.config.config = config
  126. response = this.requestComFun(response)
  127. // 全局忽略错误,在单个请求里面自行处理
  128. if (config.hideError) {
  129. resolve(response.data)
  130. }
  131. if (statusCode === 200) {
  132. if (response.data.code === 1) {
  133. resolve(response.data)
  134. } else {
  135. // 返回错误信息(0 与 -1 均由后端 msg 提示并 reject)
  136. if (response.data.code == 0 || response.data.code == -1) {
  137. if (!config.hideError) {
  138. uni.showToast({ title: response.data.msg, icon: 'none', mask: false, duration: 2000 })
  139. }
  140. reject(response.data)
  141. } else if (response.data.code && response.data.code == -10) {
  142. //-10主要用于没有登录的提示流程,不要占用 ssh 20260518
  143. } else if (response.data.code == 2) {
  144. uni.removeStorageSync('token')
  145. uni.removeStorageSync('shopUser')
  146. uni.removeStorageSync('account')
  147. } else {
  148. if (!config.hideError) {
  149. uni.showToast({ title: response.data.msg, icon: 'none', mask: false })
  150. }
  151. reject(response.data)
  152. }
  153. }
  154. } else {
  155. reject(response)
  156. }
  157. }
  158. const cancel = (t = 'handle cancel') => {
  159. const err = {
  160. errMsg: t,
  161. config: afC
  162. }
  163. reject(err)
  164. next = false
  165. }
  166. const afC = { ...this.config, ...options, config: config }
  167. _config = { ...afC, ...this.requestBeforeFun(afC, cancel) }
  168. if (!next) return
  169. uni.request(_config)
  170. })
  171. }
  172. get(url, data = {}, options = {}, config = {}) {
  173. // options.url = url + common.queryData(data, true)
  174. options.url = url + parse(data)
  175. options.data = {}
  176. options.method = 'GET'
  177. return this.request(options, config)
  178. }
  179. post(url, data = {}, options = {}, config = {}) {
  180. options.url = url
  181. options.data = data
  182. options.method = 'POST'
  183. return this.request(options, config)
  184. }
  185. }
  186. export default new Request()