request.js 6.1 KB

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