interface.js 6.2 KB

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