interface.js 5.6 KB

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