interface.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * 通用uni-app网络请求
  3. * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
  4. */
  5. /*
  6. */
  7. export default {
  8. config: {
  9. baseUrl: "",
  10. header: {},
  11. data: {},
  12. method: "GET",
  13. dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
  14. responseType: "text",
  15. success() {},
  16. fail() {},
  17. complete() {}
  18. },
  19. interceptor: {
  20. request: null,
  21. response: null,
  22. },
  23. reqInterceptors : null,
  24. resInterceptors : null,
  25. request(options) {
  26. if (!options) {
  27. options = {}
  28. }
  29. options.baseUrl = options.baseUrl || this.config.baseUrl
  30. options.dataType = options.dataType || this.config.dataType
  31. options.url = options.baseUrl + options.url
  32. options.data = options.data || {}
  33. options.method = options.method || this.config.method
  34. return new Promise((resolve, reject) => {
  35. let _config = null
  36. const _this = this;
  37. options.complete = (response) => {
  38. let res = response;
  39. let statusCode = response.statusCode
  40. res.config = _config
  41. if (process.env.NODE_ENV === 'development') {
  42. if (statusCode === 200) {
  43. //console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(res.data))
  44. }
  45. }
  46. _reslog(response);
  47. if (this.interceptor.response && typeof this.interceptor.response === 'function') {
  48. let resInterceptors = this.interceptor.response(res);
  49. /* if (!resInterceptors) {
  50. reject('返回值已被您拦截!');
  51. return;
  52. } else */ if (Object.prototype.toString.call(resInterceptors) === "[object Promise]") {
  53. try {
  54. let promiseRes = resInterceptors;
  55. //console.log('请求拦截处理')
  56. promiseRes.then((res)=>{
  57. //console.log('请求数据没有问题视为成功')
  58. resolve(res)
  59. }).catch((ret)=>{
  60. //console.log('请求数据有问题视为失败')
  61. reject(ret)
  62. })
  63. } catch (error) {
  64. reject(error)
  65. }
  66. } else {
  67. res = resInterceptors;
  68. resolve(res);
  69. }
  70. }
  71. }
  72. _config = Object.assign({}, this.config, options)
  73. _config.requestId = new Date().getTime()
  74. if (this.interceptor.request) {
  75. this.interceptor.request(_config)
  76. }
  77. // 统一的请求日志记录
  78. _reqlog(_config)
  79. uni.request(_config);
  80. });
  81. },
  82. get(url, data, options) {
  83. if (!options) {
  84. options = {}
  85. }
  86. options.url = url
  87. options.data = data
  88. options.method = 'GET'
  89. return this.request(options)
  90. },
  91. post(url, data, options) {
  92. if (!options) {
  93. options = {}
  94. }
  95. options.url = url
  96. options.data = data
  97. options.method = 'POST'
  98. return this.request(options)
  99. },
  100. put(url, data, options) {
  101. if (!options) {
  102. options = {}
  103. }
  104. options.url = url
  105. options.data = data
  106. options.method = 'PUT'
  107. return this.request(options)
  108. },
  109. delete(url, data, options) {
  110. if (!options) {
  111. options = {}
  112. }
  113. options.url = url
  114. options.data = data
  115. options.method = 'DELETE'
  116. return this.request(options)
  117. }
  118. }
  119. /**
  120. * 请求接口日志记录
  121. */
  122. function _reqlog(req) {
  123. if (process.env.NODE_ENV === 'development') {
  124. //console.log("【" + req.requestId + "】 请求地址:" + req.url)
  125. if (req.data) {
  126. //console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
  127. //console.log("【" + req.requestId + "】 请求头:" + JSON.stringify(req.header))
  128. }
  129. }
  130. //TODO 调接口异步写入日志数据库
  131. }
  132. /**
  133. * 响应接口日志记录
  134. */
  135. function _reslog(res) {
  136. let _statusCode = res.statusCode;
  137. if (process.env.NODE_ENV === 'development') {
  138. //console.log("【" + res.config.requestId + "】 请求地址:" + res.config.url)
  139. if (res.config.data) {
  140. //console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
  141. }
  142. //console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
  143. }
  144. //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  145. switch(_statusCode){
  146. case 200:
  147. break;
  148. case 401:
  149. break;
  150. case 404:
  151. break;
  152. default:
  153. break;
  154. }
  155. }