uniapp 数据网络请求封装以及通用使用

缺乏、安全感 2021-09-19 13:48 869阅读 0赞

index.js

  1. import http from './interface'
  2. /**
  3. * 将业务所有接口统一起来便于维护
  4. * 如果项目很大可以将 url 独立成文件,接口分成不同的模块
  5. *
  6. */
  7. // 单独导出(测试接口) import {test} from '@/common/vmeitime-http/'
  8. export const test = (data) => {
  9. /* http.config.baseUrl = "http://localhost:8080/api/"
  10. //设置请求前拦截器
  11. http.interceptor.request = (config) => {
  12. config.header = {
  13. "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  14. }
  15. } */
  16. //设置请求结束后拦截器
  17. http.interceptor.response = (response) => {
  18. console.log('个性化response....')
  19. //判断返回状态 执行相应操作
  20. return response;
  21. }
  22. return http.request({
  23. baseUrl: 'https://unidemo.dcloud.net.cn/',
  24. url: 'ajax/echo/text?name=uni-app',
  25. dataType: 'text',
  26. data,
  27. })
  28. }
  29. // 轮播图
  30. export const banner = (data) => {
  31. return http.request({
  32. url: '/banner/36kr',
  33. method: 'GET',
  34. data,
  35. // handle:true
  36. })
  37. }
  38. // 默认全部导出 import api from '@/common/vmeitime-http/'
  39. export default {
  40. test,
  41. banner
  42. }

interface.js

  1. export default {
  2. config: {
  3. baseUrl: "https:",
  4. header: {
  5. 'Content-Type':'application/json;charset=UTF-8',
  6. 'Content-Type':'application/x-www-form-urlencoded'
  7. },
  8. data: {},
  9. method: "GET",
  10. dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
  11. responseType: "text",
  12. success() {},
  13. fail() {},
  14. complete() {}
  15. },
  16. interceptor: {
  17. request: null,
  18. response: null
  19. },
  20. request(options) {
  21. if (!options) {
  22. options = {}
  23. }
  24. options.baseUrl = options.baseUrl || this.config.baseUrl
  25. options.dataType = options.dataType || this.config.dataType
  26. options.url = options.baseUrl + options.url
  27. options.data = options.data || {}
  28. options.method = options.method || this.config.method
  29. //TODO 加密数据
  30. //TODO 数据签名
  31. /*
  32. _token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
  33. _sign = {'sign': sign(JSON.stringify(options.data))}
  34. options.header = Object.assign({}, options.header, _token,_sign)
  35. */
  36. return new Promise((resolve, reject) => {
  37. let _config = null
  38. options.complete = (response) => {
  39. let statusCode = response.statusCode
  40. response.config = _config
  41. if (process.env.NODE_ENV === 'development') {
  42. if (statusCode === 200) {
  43. console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
  44. }
  45. }
  46. if (this.interceptor.response) {
  47. let newResponse = this.interceptor.response(response)
  48. if (newResponse) {
  49. response = newResponse
  50. }
  51. }
  52. // 统一的响应日志记录
  53. _reslog(response)
  54. if (statusCode === 200) { //成功
  55. resolve(response);
  56. } else {
  57. reject(response)
  58. }
  59. }
  60. _config = Object.assign({}, this.config, options)
  61. _config.requestId = new Date().getTime()
  62. if (this.interceptor.request) {
  63. this.interceptor.request(_config)
  64. }
  65. // 统一的请求日志记录
  66. _reqlog(_config)
  67. if (process.env.NODE_ENV === 'development') {
  68. console.log("【" + _config.requestId + "】 地址:" + _config.url)
  69. if (_config.data) {
  70. console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
  71. }
  72. }
  73. uni.request(_config);
  74. });
  75. },
  76. get(url, data, options) {
  77. if (!options) {
  78. options = {}
  79. }
  80. options.url = url
  81. options.data = data
  82. options.method = 'GET'
  83. return this.request(options)
  84. },
  85. post(url, data, options) {
  86. if (!options) {
  87. options = {}
  88. }
  89. options.url = url
  90. options.data = data
  91. options.method = 'POST'
  92. return this.request(options)
  93. },
  94. put(url, data, options) {
  95. if (!options) {
  96. options = {}
  97. }
  98. options.url = url
  99. options.data = data
  100. options.method = 'PUT'
  101. return this.request(options)
  102. },
  103. delete(url, data, options) {
  104. if (!options) {
  105. options = {}
  106. }
  107. options.url = url
  108. options.data = data
  109. options.method = 'DELETE'
  110. return this.request(options)
  111. }
  112. }
  113. /**
  114. * 请求接口日志记录
  115. */
  116. function _reqlog(req) {
  117. if (process.env.NODE_ENV === 'development') {
  118. console.log("【" + req.requestId + "】 地址:" + req.url)
  119. if (req.data) {
  120. console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
  121. }
  122. }
  123. //TODO 调接口异步写入日志数据库
  124. }
  125. /**
  126. * 响应接口日志记录
  127. */
  128. function _reslog(res) {
  129. let _statusCode = res.statusCode;
  130. if (process.env.NODE_ENV === 'development') {
  131. console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
  132. if (res.config.data) {
  133. console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
  134. }
  135. console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
  136. }
  137. //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  138. switch(_statusCode){
  139. case 200:
  140. break;
  141. case 401:
  142. break;
  143. case 404:
  144. break;
  145. default:
  146. break;
  147. }
  148. }

发表评论

表情:
评论列表 (有 0 条评论,869人围观)

还没有评论,来说两句吧...

相关阅读