interface.js 5.8 KB

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