interface.js 5.5 KB

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