interface.js 5.6 KB

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