interface.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. BaseApi
  3. } from './baseApi.js'
  4. export default {
  5. config: {
  6. baseUrl: BaseApi,
  7. header: {
  8. 'Content-Type':'application/json;charset=UTF-8',
  9. 'Content-Type':'application/x-www-form-urlencoded'
  10. },
  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. request(options) {
  24. if (!options) {
  25. options = {}
  26. }
  27. options.baseUrl = options.baseUrl || this.config.baseUrl
  28. options.dataType = options.dataType || this.config.dataType
  29. options.url = options.baseUrl + options.url
  30. options.data = options.data || {}
  31. options.method = options.method || this.config.method
  32. const requestInterceptor = this.interceptor.request
  33. const responseInterceptor = this.interceptor.response
  34. return new Promise((resolve, reject) => {
  35. let _config = null
  36. options.complete = (response) => {
  37. let statusCode = response.statusCode
  38. response.config = _config
  39. if (process.env.NODE_ENV === 'development') {
  40. if (statusCode === 200) {
  41. ////console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
  42. }
  43. }
  44. if (responseInterceptor) {
  45. let newResponse = responseInterceptor(response)
  46. if (newResponse) {
  47. response = newResponse
  48. }
  49. }
  50. // 统一的响应日志记录
  51. //_reslog(response)
  52. if (statusCode === 200) { //成功
  53. resolve(response);
  54. } else {
  55. reject(response)
  56. }
  57. }
  58. _config = Object.assign({}, this.config, options)
  59. _config.requestId = new Date().getTime()
  60. if (requestInterceptor) {
  61. requestInterceptor(_config)
  62. }
  63. // 统一的请求日志记录
  64. //_reqlog(_config)
  65. if (process.env.NODE_ENV === 'development') {
  66. //console.log("【" + _config.requestId + "】 地址:" + _config.url)
  67. if (_config.data) {
  68. //console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
  69. }
  70. }
  71. uni.request(_config);
  72. });
  73. }
  74. }
  75. /**
  76. * 请求接口日志记录
  77. */
  78. function _reqlog(req) {
  79. if (process.env.NODE_ENV === 'development') {
  80. //console.log("【" + req.requestId + "】 地址:" + req.url)
  81. if (req.data) {
  82. //console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
  83. }
  84. }
  85. //TODO 调接口异步写入日志数据库
  86. }
  87. /**
  88. * 响应接口日志记录
  89. */
  90. function _reslog(res) {
  91. let _statusCode = res.statusCode;
  92. if (process.env.NODE_ENV === 'development') {
  93. //console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
  94. if (res.config.data) {
  95. //console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
  96. }
  97. //console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
  98. }
  99. //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  100. switch(_statusCode){
  101. case 200:
  102. break;
  103. case 401:
  104. break;
  105. case 404:
  106. break;
  107. default:
  108. break;
  109. }
  110. }