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