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