interface.js 6.5 KB

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