request.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 { APIHOST } from '@/config'
  10. // 基础配置
  11. const constant = {
  12. hostUrl: APIHOST,
  13. source: 'mobile'
  14. }
  15. class Request {
  16. config = {
  17. baseUrl: constant.hostUrl,
  18. header: {
  19. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  20. },
  21. data: {},
  22. method: 'GET',
  23. dataType: 'json', /* 如设为json,会对返回的数据做一次 JSON.parse */
  24. responseType: 'text',
  25. success() { },
  26. fail() { },
  27. complete() { }
  28. }
  29. static posUrl(url) { /* 判断url是否为绝对路径 */
  30. return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
  31. }
  32. interceptor = {
  33. request: (f) => {
  34. if (f && typeof f === 'function') {
  35. this.requestBeforeFun = f
  36. }
  37. },
  38. response: (f) => {
  39. if (f && typeof f === 'function') {
  40. this.requestComFun = f
  41. }
  42. }
  43. }
  44. requestBeforeFun(config, cancel) {
  45. let token = ''
  46. try {
  47. const st = uni.getStorageSync('token')
  48. if (st) {
  49. token = st
  50. }
  51. } catch (e) {
  52. console.error(e, '获取token异常')
  53. }
  54. config.header = {
  55. ...config.header
  56. }
  57. // 将token 放入header
  58. if (token) {
  59. config.header['token'] = token
  60. }
  61. config.header['appVersion'] = 2
  62. // const token = uni.getStorageSync('token')
  63. // config.data = JSON.stringify(config.data);
  64. config.headers = {
  65. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  66. }
  67. // if (token) { // 判断是否存在token,如果存在的话,则每个http header都加上token
  68. // // axios.defaults.headers.common['token'] = token;
  69. // config.headers.token = token
  70. // }
  71. // config.headers.HkdsRequestFrom = window.location.href
  72. // config.header.userRegion = Base64.encode(uni.getStorageSync('userRegion')) || null
  73. // config.header['x-client-hash'] = uni.getStorageSync('client_hash') || null
  74. // const userRegion = Base64.encode(uni.getStorageSync('userRegion')) || null
  75. // if (userRegion) {
  76. // config.header.userRegion = userRegion
  77. // }
  78. const account = uni.getStorageSync('account') || null
  79. if (account) {
  80. config.header['account'] = account
  81. }
  82. // const source = uni.getStorageSync('source') || null
  83. // if (source) {
  84. // config.header['source'] = source
  85. // }
  86. if (constant.source == 'mobile') {
  87. const shopId = uni.getStorageSync('shopId') || 0
  88. if (shopId) {
  89. config.header['shopId'] = shopId
  90. }
  91. }
  92. if (config.config.loading) {
  93. uni.showLoading({
  94. title: '',
  95. mask: true
  96. })
  97. }
  98. return config
  99. }
  100. requestComFun(response) {
  101. if (response.config.config.loading) {
  102. uni.hideLoading()
  103. }
  104. return response
  105. }
  106. setConfig(f) {
  107. this.config = f(this.config)
  108. }
  109. request(options = {}, config) {
  110. options.baseUrl = options.baseUrl || this.config.baseUrl
  111. options.dataType = options.dataType || this.config.dataType
  112. options.url = Request.posUrl(options.url) ? options.url : (options.baseUrl + options.url)
  113. options.data = options.data || this.config.data
  114. options.header = options.header || this.config.header
  115. options.method = options.method || this.config.method
  116. return new Promise((resolve, reject) => {
  117. let next = true
  118. let _config = null
  119. options.complete = (response) => {
  120. const statusCode = response.statusCode
  121. response.config = _config
  122. response.config.config = config
  123. response = this.requestComFun(response)
  124. // 全局忽略错误,在单个请求里面自行处理
  125. if (config && config.hideError) {
  126. resolve(response.data)
  127. return
  128. }
  129. if (statusCode === 200) {
  130. if (response.data.code === 1) {
  131. resolve(response.data)
  132. } else {
  133. if (response.data.code == 0) {
  134. if (!config.hideError) {
  135. uni.showToast({ title: response.data.msg, icon: 'none', mask: false, duration: 2000 })
  136. }
  137. reject(response.data)
  138. } else if (response.data.code && response.data.code == -1) {
  139. // 特殊处理
  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: false })
  147. }
  148. reject(response.data)
  149. }
  150. }
  151. } else {
  152. if (!config.hideError) {
  153. uni.showToast({
  154. title: response.data.message || '请求失败',
  155. icon: 'none'
  156. })
  157. }
  158. reject(response)
  159. }
  160. }
  161. const cancel = (t = 'handle cancel') => {
  162. const err = {
  163. errMsg: t,
  164. config: afC
  165. }
  166. reject(err)
  167. next = false
  168. }
  169. const afC = { ...this.config, ...options, config: config }
  170. _config = { ...afC, ...this.requestBeforeFun(afC, cancel) }
  171. if (!next) return
  172. uni.request(_config)
  173. })
  174. }
  175. get(url, data = {}, options = {}, config = {}) {
  176. return this.request({
  177. url,
  178. data,
  179. method: 'GET',
  180. ...options
  181. }, config)
  182. }
  183. post(url, data = {}, options = {}, config = {}) {
  184. return this.request({
  185. url,
  186. data,
  187. method: 'POST',
  188. ...options
  189. }, config)
  190. }
  191. }
  192. const https = new Request()
  193. export default https