interface.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import qs from 'qs';
  2. import config from '../config/index'
  3. import {
  4. getPlatform,
  5. objParseUrlAndParam
  6. } from '../utils/index'
  7. /**
  8. * 通用uni-app网络请求
  9. * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
  10. */
  11. export class HttpInterface {
  12. constructor() {
  13. this.config = {
  14. baseUrl: config.api, // 请求域名
  15. header: {
  16. // 'Content-Type': 'application/json;charset=UTF-8',
  17. 'Content-Type': 'application/x-www-form-urlencoded',
  18. },
  19. data: {},
  20. method: 'GET',
  21. // dataType: 'json' /* 如设为json,会对返回的数据做一次 JSON.parse */,
  22. // responseType: 'text',
  23. success() {},
  24. fail() {},
  25. complete() {}
  26. }
  27. this.interceptor = {
  28. before: _config => {
  29. return _config
  30. },
  31. after: null
  32. }
  33. }
  34. request(options) {
  35. let that = this
  36. if (!options) {
  37. options = {}
  38. }
  39. options.baseUrl = options.baseUrl ||
  40. this.config.baseUrl
  41. //if (process.env.NODE_ENV ===
  42. // 'development') {
  43. // options.url = this.addTemp(options.baseUrl +
  44. // 'devapi/' + options.url) // 增加时间戳
  45. //} else {
  46. options.url = this.addTemp(options.baseUrl +
  47. options.url) // 增加时间戳
  48. //}
  49. options.url = this.addPlatform(
  50. options.url) // 增加使用平台
  51. options.data = qs.stringify(options.data ||
  52. {})
  53. options.method = options.method ||
  54. this.config.method
  55. if (options.method === 'get' ||
  56. options.method === 'GET') {
  57. options.url = objParseUrlAndParam(
  58. options.url, options.data)
  59. }
  60. // TODO 加密数据
  61. // TODO 数据签名
  62. return new Promise((resolve, reject) => {
  63. let _config = null
  64. options.complete = response => {
  65. let statusCode = response.statusCode
  66. response.config = _config
  67. // 请求后的拦截器
  68. if (this.interceptor.after) {
  69. let newResponse = this.interceptor
  70. .after(response)
  71. if (newResponse) {
  72. response = newResponse
  73. }
  74. }
  75. // 统一的响应日志记录
  76. that._reslog(response)
  77. if (statusCode === 200) {
  78. // 成功
  79. resolve(response.data)
  80. } else {
  81. reject({code: 0, msg: '请求出错'})
  82. }
  83. }
  84. _config = Object.assign({}, this.config,
  85. options)
  86. _config.requestId = new Date().getTime()
  87. // 请求前的拦截器
  88. if (this.interceptor.before) {
  89. let newConfig = this.interceptor
  90. .before(_config)
  91. if (newConfig) {
  92. _config = newConfig
  93. } else {
  94. return
  95. }
  96. }
  97. // 统一的请求日志记录
  98. that._reqlog(_config)
  99. uni.request(_config)
  100. })
  101. }
  102. get(options) {
  103. if (!options) {
  104. options = {}
  105. }
  106. options.method = 'GET'
  107. return this.request(options)
  108. }
  109. post(options) {
  110. if (!options) {
  111. options = {}
  112. }
  113. options.method = 'POST'
  114. return this.request(options)
  115. }
  116. /**
  117. * 统一的响应日志记录
  118. * @param {*} req
  119. */
  120. _reqlog(req) {
  121. if (process.env.NODE_ENV ===
  122. 'development') {
  123. if (req.data) {
  124. }
  125. }
  126. // TODO 调接口异步写入日志数据库
  127. }
  128. /**
  129. * 统一的请求日志记录
  130. * @param {*} res
  131. */
  132. _reslog(res) {
  133. let _statusCode = res.statusCode
  134. if (process.env.NODE_ENV ===
  135. 'development') {
  136. if (res.config.data) {
  137. }
  138. }
  139. // TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  140. switch (_statusCode) {
  141. case 200:
  142. break
  143. case 401:
  144. break
  145. case 404:
  146. break
  147. default:
  148. break
  149. }
  150. }
  151. /**
  152. * 增加时间戳,用于防止请求缓存
  153. * @param {*} url
  154. */
  155. addTemp(url) {
  156. if (url.indexOf('?') === -1) {
  157. url =
  158. `${url}?_=${new Date().getTime()}`
  159. } else {
  160. url = url.replace('?',
  161. `?_=${new Date().getTime()}&`)
  162. }
  163. return url
  164. }
  165. /**
  166. * 增加使用平台信息
  167. * @param {*} url
  168. */
  169. addPlatform(url) {
  170. if (url.indexOf('?') === -1) {
  171. url =
  172. `${url}?_platform_=${getPlatform()}`
  173. } else {
  174. url = url.replace('?',
  175. `?_platform_=${getPlatform()}&`)
  176. }
  177. return url
  178. }
  179. }