request.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // const token = uni.getStorageSync('token')
  60. // config.data = JSON.stringify(config.data);
  61. config.headers = {
  62. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  63. }
  64. // if (token) { // 判断是否存在token,如果存在的话,则每个http header都加上token
  65. // // axios.defaults.headers.common['token'] = token;
  66. // config.headers.token = token
  67. // }
  68. // config.headers.HkdsRequestFrom = window.location.href
  69. // config.header.userRegion = Base64.encode(uni.getStorageSync('userRegion')) || null
  70. // config.header['x-client-hash'] = uni.getStorageSync('client_hash') || null
  71. const userRegion = Base64.encode(uni.getStorageSync('userRegion')) || null
  72. // if (userRegion) {
  73. // config.header.userRegion = userRegion
  74. // }
  75. const account = uni.getStorageSync('account') || null
  76. if (account) {
  77. config.header['account'] = account
  78. }
  79. // const source = uni.getStorageSync('source') || null
  80. // if (source) {
  81. // config.header['source'] = source
  82. // }
  83. if (constant.source == 'mobile') {
  84. const shopId = uni.getStorageSync('shopId') || 0
  85. if (shopId) {
  86. config.header['shopId'] = shopId
  87. }
  88. }
  89. if (config.config.loading) {
  90. uni.showLoading({
  91. title: '',
  92. mask: true
  93. })
  94. }
  95. return config
  96. }
  97. requestComFun(response) {
  98. if (response.config.config.loading) {
  99. uni.hideLoading()
  100. }
  101. // const client_hash = response.header.client_hash || null
  102. // console.log('X-Client-Hash', response.header['X-Client-Hash'])
  103. // if (client_hash) {
  104. // uni.setStorageSync('client_hash', client_hash)
  105. // }
  106. return response
  107. }
  108. setConfig(f) {
  109. this.config = f(this.config)
  110. }
  111. request(options = {}, config) {
  112. options.baseUrl = options.baseUrl || this.config.baseUrl
  113. options.dataType = options.dataType || this.config.dataType
  114. options.url = Request.posUrl(options.url) ? options.url : (options.baseUrl + options.url)
  115. options.data = qs.stringify(options.data) || {}
  116. options.header = options.header || this.config.header
  117. options.method = options.method || this.config.method
  118. return new Promise((resolve, reject) => {
  119. let next = true
  120. let _config = null
  121. options.complete = (response) => {
  122. const statusCode = response.statusCode
  123. response.config = _config
  124. response.config.config = config
  125. response = this.requestComFun(response)
  126. // 全局忽略错误,在单个请求里面自行处理
  127. if (config.hideError) {
  128. resolve(response.data)
  129. }
  130. if (statusCode === 200) {
  131. if (response.data.code === 1) {
  132. resolve(response.data)
  133. } else {
  134. if (response.data.code == 0) {
  135. if (!config.hideError) {
  136. uni.showToast({ title: response.data.msg, icon: 'none', mask: true, duration: 2000 })
  137. }
  138. reject(response.data)
  139. } else if (response.data.code && response.data.code == -1) {
  140. } else if (response.data.code == 2) {
  141. uni.removeStorageSync('token')
  142. uni.removeStorageSync('shopUser')
  143. uni.removeStorageSync('account')
  144. } else {
  145. if (!config.hideError) {
  146. uni.showToast({ title: response.data.msg, icon: 'none', mask: true })
  147. }
  148. reject(response.data)
  149. }
  150. }
  151. } else {
  152. reject(response)
  153. }
  154. }
  155. const cancel = (t = 'handle cancel') => {
  156. const err = {
  157. errMsg: t,
  158. config: afC
  159. }
  160. reject(err)
  161. next = false
  162. }
  163. const afC = { ...this.config, ...options, config: config }
  164. _config = { ...afC, ...this.requestBeforeFun(afC, cancel) }
  165. if (!next) return
  166. uni.request(_config)
  167. })
  168. }
  169. get(url, data = {}, options = {}, config = {}) {
  170. // options.url = url + common.queryData(data, true)
  171. options.url = url + parse(data)
  172. options.data = {}
  173. options.method = 'GET'
  174. return this.request(options, config)
  175. }
  176. post(url, data = {}, options = {}, config = {}) {
  177. options.url = url
  178. options.data = data
  179. options.method = 'POST'
  180. return this.request(options, config)
  181. }
  182. }
  183. export default new Request()