interface.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. http://qb30408096.qicp.vip/doc.html
  28. */
  29. export default {
  30. config: {
  31. baseUrl: "https://aixiangcheng.qicaihong.info/api/", //"http://xpp.58bmsh.com/app/index.php?i=2&t=0&v=1.1.19&from=wxapp&c=entry&a=wxapp&m=yb_shop&do=",
  32. header: {
  33. 'Content-Type': 'application/json;charset=UTF-8',
  34. // 'Content-Type':'application/x-www-form-urlencoded'
  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. // let _token = {'Authorization': uni.getStorageSync("_token") || 'undefined'}
  62. // let _sign = {'sign': sign(JSON.stringify(options.data))}
  63. options.header = Object.assign({
  64. shopId: 327
  65. }, options.header)
  66. if (options.url != 'exchange_auth/ua/login' && options.url.indexOf('exchange_multishop/ua/index_img/list') <
  67. 0) {
  68. options.header = Object.assign({}, options.header)
  69. }
  70. return new Promise((resolve, reject) => {
  71. let _config = null
  72. options.complete = (response) => {
  73. let statusCode = response.statusCode
  74. response.config = _config
  75. if (process.env.NODE_ENV === 'development-') {
  76. if (statusCode === 200) {
  77. //// console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
  78. }
  79. }
  80. if (this.interceptor.response) {
  81. let newResponse = this.interceptor.response(response)
  82. if (newResponse) {
  83. response = newResponse
  84. }
  85. }
  86. // 统一的响应日志记录
  87. _reslog(response)
  88. if (statusCode === 200) { //成功
  89. resolve(response);
  90. } else {
  91. reject(response)
  92. }
  93. }
  94. _config = Object.assign({}, this.config, options)
  95. _config.requestId = new Date().getTime()
  96. if (this.interceptor.request) {
  97. this.interceptor.request(_config)
  98. }
  99. // 统一的请求日志记录
  100. _reqlog(_config)
  101. if (process.env.NODE_ENV === 'development-') {
  102. // console.log("【" + _config.requestId + "】 地址:" + _config.url)
  103. if (_config.data) {
  104. // console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
  105. }
  106. }
  107. uni.request(_config);
  108. });
  109. },
  110. get(url, data, options) {
  111. if (!options) {
  112. options = {}
  113. }
  114. options.url = url
  115. options.data = data
  116. options.method = 'GET'
  117. return this.request(options)
  118. },
  119. post(url, data, options) {
  120. if (!options) {
  121. options = {}
  122. }
  123. options.url = url
  124. options.data = data
  125. options.method = 'POST'
  126. return this.request(options)
  127. },
  128. put(url, data, options) {
  129. if (!options) {
  130. options = {}
  131. }
  132. options.url = url
  133. options.data = data
  134. options.method = 'PUT'
  135. return this.request(options)
  136. },
  137. delete(url, data, options) {
  138. if (!options) {
  139. options = {}
  140. }
  141. options.url = url
  142. options.data = data
  143. options.method = 'DELETE'
  144. return this.request(options)
  145. }
  146. }
  147. /**
  148. * 请求接口日志记录
  149. */
  150. function _reqlog(req) {
  151. if (process.env.NODE_ENV === 'development-') {
  152. // console.log("【" + req.requestId + "】 地址:" + req.url)
  153. if (req.data) {
  154. // console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
  155. }
  156. }
  157. //TODO 调接口异步写入日志数据库
  158. }
  159. /**
  160. * 响应接口日志记录
  161. */
  162. function _reslog(res) {
  163. let _statusCode = res.statusCode;
  164. if (process.env.NODE_ENV === 'development-') {
  165. // console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
  166. if (res.config.data) {
  167. // console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
  168. }
  169. // console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
  170. }
  171. //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  172. switch (_statusCode) {
  173. case 200:
  174. break;
  175. case 401:
  176. break;
  177. case 404:
  178. break;
  179. default:
  180. break;
  181. }
  182. }