interface.js 3.0 KB

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