interface.js 6.1 KB

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