interface.js 6.1 KB

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