request.js 4.4 KB

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