interface.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * 通用uni-app网络请求
  3. * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
  4. */
  5. /*
  6. // 开放的接口
  7. import http from './interface'
  8. http.config.baseUrl = "http://localhost:8080/api/"
  9. http.request(url:'user/list',method:'GET').then((res)=>{
  10. console.log(JSON.stringify(res))
  11. })
  12. http.get('user/list').then((res)=>{
  13. console.log(JSON.stringify(res))
  14. })
  15. http.get('user/list', {status: 1}).then((res)=>{
  16. console.log(JSON.stringify(res))
  17. })
  18. http.post('user', {id:1, status: 1}).then((res)=>{
  19. console.log(JSON.stringify(res))
  20. })
  21. http.put('user/1', {status: 2}).then((res)=>{
  22. console.log(JSON.stringify(res))
  23. })
  24. http.delete('user/1').then((res)=>{
  25. console.log(JSON.stringify(res))
  26. })
  27. */
  28. export default {
  29. config: {
  30. baseUrl: "http://192.1.1.203:8086",
  31. header: {
  32. // "Content-Type": "multipart/form-data",
  33. "Content-Type": "application/x-www-form-urlencoded",
  34. "Content-Type": "text/plain",
  35. },
  36. data: {},
  37. method: "GET",
  38. dataType: "json",
  39. /* 如设为json,会对返回的数据做一次 JSON.parse */
  40. responseType: "text",
  41. success() {},
  42. fail() {},
  43. complete() {}
  44. },
  45. interceptor: {
  46. request: null,
  47. response: null
  48. },
  49. request(options) {
  50. if (!options) {
  51. options = {}
  52. }
  53. options.baseUrl = options.baseUrl || this.config.baseUrl
  54. options.dataType = options.dataType || this.config.dataType
  55. options.url = options.baseUrl + options.url
  56. options.data = options.data || {}
  57. options.method = options.method || this.config.method
  58. //TODO 加密数据
  59. //TODO 数据签名
  60. /*
  61. _token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
  62. _sign = {'sign': sign(JSON.stringify(options.data))}
  63. options.header = Object.assign({}, options.header, _token,_sign)
  64. */
  65. return new Promise((resolve, reject) => {
  66. let _config = null
  67. options.complete = (response) => {
  68. let statusCode = response.statusCode
  69. response.config = _config
  70. // if (process.env.NODE_ENV === 'development') {
  71. // if (statusCode === 200) {
  72. // }
  73. // }
  74. if (this.interceptor.response) {
  75. let newResponse = this.interceptor.response(response)
  76. if (newResponse) {
  77. response = newResponse
  78. }
  79. }
  80. // 统一的响应日志记录
  81. // _reslog(response)
  82. if (statusCode === 200) { //成功
  83. resolve(response);
  84. } else {
  85. reject(response)
  86. }
  87. }
  88. _config = Object.assign({}, this.config, options)
  89. _config.requestId = new Date().getTime()
  90. if (this.interceptor.request) {
  91. this.interceptor.request(_config)
  92. }
  93. // 统一的请求日志记录
  94. // _reqlog(_config)
  95. // if (process.env.NODE_ENV === 'development') {
  96. // if (_config.data) {}
  97. // }
  98. uni.request(_config);
  99. });
  100. },
  101. get(url, data, options) {
  102. if (!options) {
  103. options = {}
  104. }
  105. options.url = url
  106. options.data = data
  107. options.method = 'GET'
  108. return this.request(options)
  109. },
  110. post(url, data, options) {
  111. if (!options) {
  112. options = {}
  113. }
  114. options.url = url
  115. options.data = data
  116. options.method = 'POST'
  117. return this.request(options)
  118. },
  119. put(url, data, options) {
  120. if (!options) {
  121. options = {}
  122. }
  123. options.url = url
  124. options.data = data
  125. options.method = 'PUT'
  126. return this.request(options)
  127. },
  128. delete(url, data, options) {
  129. if (!options) {
  130. options = {}
  131. }
  132. options.url = url
  133. options.data = data
  134. options.method = 'DELETE'
  135. return this.request(options)
  136. }
  137. }