interface.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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://nmjt.nxjiewei.com:8011/api",
  31. // baseUrl: "http://colliery.nxjiewei.com/api",
  32. // baseUrl: "http://ningdongyunying.nxjiewei.com:8011/api",
  33. // baseUrl: "http://shicaocun.nxjiewei.com:8011/api",
  34. // baseUrl: "http://zaoquan.nxjiewei.com:8011/api",
  35. baseUrl: "http://qingshuiying.nxjiewei.com:8011/api",
  36. // baseUrl: "http://wuyegongsi.nxjiewei.com:8011/api",
  37. // baseUrl: "http://jinjiaqu.nxjiewei.com:8011/api",
  38. // 金家渠:内网
  39. // baseUrl: "http://n.jinjiaqu.nxjiewei.com:8011/api",
  40. // baseUrl: "http://yangchangwan.nxjiewei.com:8011/api",
  41. // baseUrl: "http://jinfeng.nxjiewei.com:8011/api",
  42. // 金凤:内网
  43. // baseUrl: "http://n.jinfeng.nxjiewei.com:8011/api",
  44. // baseUrl: "http://lingxin.nxjiewei.com:8011/api",
  45. // baseUrl: "http://renjiazhuang.nxjiewei.com:8011/api",
  46. // baseUrl: uni.getStorageSync('base_url'),
  47. header: {
  48. "Content-Type":"multipart/form-data",
  49. 'Content-Type':'application/json;charset=UTF-8',
  50. 'Content-Type':'application/x-www-form-urlencoded',
  51. 'Authorization' : uni.getStorageSync('token_type') +' '+uni.getStorageSync('Authorization') || {},
  52. 'accesskey': "b364b449a18af327867f7edc3431b541"
  53. },
  54. data: {},
  55. method: "GET",
  56. dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
  57. responseType: "text",
  58. success() {},
  59. fail() {},
  60. complete() {
  61. // uni.hideLoading()
  62. }
  63. },
  64. interceptor: {
  65. request: null,
  66. response: null
  67. },
  68. request(options) {
  69. if (!options) {
  70. options = {}
  71. }
  72. options.baseUrl = options.baseUrl || this.config.baseUrl
  73. options.dataType = options.dataType || this.config.dataType
  74. options.url = options.baseUrl + options.url
  75. options.data = options.data || {}
  76. options.method = options.method || this.config.method
  77. //TODO 加密数据
  78. //TODO 数据签名
  79. /*
  80. _token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
  81. _sign = {'sign': sign(JSON.stringify(options.data))}
  82. options.header = Object.assign({}, options.header, _token,_sign)
  83. */
  84. return new Promise((resolve, reject) => {
  85. let _config = null
  86. // uni.showLoading({
  87. // icon:"none",
  88. // title:"加载中...",
  89. // mask:true
  90. // })
  91. options.complete = (response) => {
  92. let statusCode = response.statusCode
  93. // console.log("【" + _config.requestId + "】 状态码:" + JSON.stringify(statusCode))
  94. response.config = _config
  95. if (process.env.NODE_ENV === 'development') {
  96. if (statusCode === 200) {
  97. // uni.hideLoading()
  98. // console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
  99. // if(response.data.code == 401){
  100. // uni.showToast({
  101. // icon:"none",
  102. // title:"登录失效、请重新登录"
  103. // })
  104. // setTimeout(function(){
  105. // uni.redirectTo({
  106. // url:"/my/login/login.vue"
  107. // })
  108. // },2000)
  109. // }
  110. }else if(statusCode === 500){
  111. uni.hideLoading()
  112. uni.showToast({
  113. icon:"none",
  114. title:"500"
  115. })
  116. // setTimeout(function(){
  117. // uni.navigateTo({
  118. // url:"/pages/login/login"
  119. // })
  120. // },1500)
  121. }
  122. }
  123. if (this.interceptor.response) {
  124. let newResponse = this.interceptor.response(response)
  125. if (newResponse) {
  126. response = newResponse
  127. }
  128. }
  129. // 统一的响应日志记录
  130. // _reslog(response)
  131. if (statusCode === 200) { //成功
  132. resolve(response);
  133. } else {
  134. reject(response)
  135. }
  136. }
  137. _config = Object.assign({}, this.config, options)
  138. _config.requestId = new Date().getTime()
  139. if (this.interceptor.request) {
  140. this.interceptor.request(_config)
  141. }
  142. // 统一的请求日志记录
  143. // _reqlog(_config)
  144. if (process.env.NODE_ENV === 'development') {
  145. // console.log("【" + _config.requestId + "】 地址:" + _config.url)
  146. if (_config.data) {
  147. // console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
  148. }
  149. }
  150. uni.request(_config);
  151. });
  152. },
  153. get(url, data, options) {
  154. if (!options) {
  155. options = {}
  156. }
  157. options.url = url
  158. options.data = data
  159. options.method = 'GET'
  160. return this.request(options)
  161. },
  162. post(url, data, options) {
  163. if (!options) {
  164. options = {}
  165. }
  166. options.url = url
  167. options.data = data
  168. options.method = 'POST'
  169. return this.request(options)
  170. },
  171. put(url, data, options) {
  172. if (!options) {
  173. options = {}
  174. }
  175. options.url = url
  176. options.data = data
  177. options.method = 'PUT'
  178. return this.request(options)
  179. },
  180. delete(url, data, options) {
  181. if (!options) {
  182. options = {}
  183. }
  184. options.url = url
  185. options.data = data
  186. options.method = 'DELETE'
  187. return this.request(options)
  188. }
  189. }
  190. /**
  191. * 请求接口日志记录
  192. */
  193. function _reqlog(req) {
  194. if (process.env.NODE_ENV === 'development') {
  195. console.log("【" + req.requestId + "】 地址:" + req.url)
  196. if (req.data) {
  197. console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
  198. }
  199. }
  200. //TODO 调接口异步写入日志数据库
  201. }
  202. /**
  203. * 响应接口日志记录
  204. */
  205. function _reslog(res) {
  206. let _statusCode = res.statusCode;
  207. if (process.env.NODE_ENV === 'development') {
  208. console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
  209. if (res.config.data) {
  210. console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
  211. }
  212. console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
  213. }
  214. //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  215. switch(_statusCode){
  216. case 200:
  217. break;
  218. case 401:
  219. break;
  220. case 404:
  221. break;
  222. default:
  223. break;
  224. }
  225. }