interface.js 4.5 KB

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