interface.js 5.6 KB

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