interface.js 6.4 KB

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