request-gat.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * @Author: your name
  3. * @Date: 2022-01-06 09:45:17
  4. * @LastEditTime: 2022-04-21 17:36:42
  5. * @LastEditors: your name
  6. * @Description: axios封装
  7. */
  8. import axios from 'axios'
  9. import { MessageBox, Message } from 'element-ui'
  10. import store from '@/store'
  11. import { getToken, getUserId, getCodeToken, getLogId, getUserName } from '@/utils/auth'
  12. import { parseTime } from '@/utils'
  13. import { Loading } from 'element-ui'
  14. // create an axios instance
  15. const service = axios.create({
  16. //baseURL: baseURL, // url = base url + request url
  17. baseURL: PLATFROM_CONFIG.gatewayUrl,
  18. // withCredentials: true, // send cookies when cross-domain requests
  19. timeout: 30000, // request timeout
  20. headers: {
  21. 'Content-Type': 'application/json'
  22. }
  23. })
  24. let loadingInstance,
  25. flag = false
  26. // request interceptor
  27. service.interceptors.request.use(
  28. config => {
  29. if (config.istoken) {
  30. config.headers['appSecret'] = PLATFROM_CONFIG.appSecret
  31. } else if (getCodeToken() && !config.istoken && config.islogin) {
  32. config.headers['token'] = getCodeToken()
  33. } else if (store.getters.token) {
  34. // let each request carry token
  35. // ['X-Token'] is a custom headers key
  36. // please modify it according to the actual situation
  37. config.headers['Token'] = getToken()
  38. }
  39. if (config.data && !config.url.includes('/api/fs4a/sendLog')) {
  40. config.data['OperatorId'] = getUserId()
  41. }
  42. flag = true
  43. if (flag) {
  44. loadingInstance = Loading.service({
  45. text: '数据加载中'
  46. })
  47. }
  48. if (config.url.includes('/api/fs4a/sendLog') && config.data.Data[0]?.OperateResult?.Token) {
  49. config.headers['token'] = config.data.Data[0].OperateResult.Token
  50. }
  51. config.metaData = {
  52. BeginTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
  53. }
  54. return config
  55. },
  56. error => {
  57. // do something with request error
  58. console.log(error) // for debug
  59. return Promise.reject(error)
  60. }
  61. )
  62. // response interceptor
  63. service.interceptors.response.use(
  64. /**
  65. * If you want to get http information such as headers or status
  66. * Please return response => response
  67. */
  68. /**
  69. * Determine the request status by custom code
  70. * Here is just an example
  71. * You can also judge the status by HTTP Status Code
  72. */
  73. response => {
  74. const res = response.data
  75. // 特定请求完成后发送日志
  76. const { metaData, url, Ident, data, msg, islogin } = response.config
  77. if (!url.includes('/api/fs4a/sendLog') && (Ident || msg)) {
  78. Object.assign(metaData, {
  79. LogInfo: typeof data === 'string' ? JSON.parse(data) : data,
  80. EndTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
  81. LogType: msg ?? '未定义操作',
  82. OperateResult: res.message,
  83. Ident: Ident ?? ''
  84. })
  85. const { BeginTime, EndTime, OperateResult } = metaData
  86. const config = {
  87. url: '/api/fs4a/sendLog',
  88. method: 'post',
  89. data: {
  90. Data: [metaData],
  91. BrowserTag: navigator.userAgent,
  92. OperateIP: returnCitySN.cip,
  93. Msg: msg ?? '未定义操作',
  94. Type: '2',
  95. BeginTime,
  96. BeginTime2: BeginTime,
  97. EndTime,
  98. OperateResult,
  99. Ident: Ident ?? '',
  100. AppId: '9'
  101. }
  102. }
  103. if (islogin) {
  104. if (res.code === 0) {
  105. config.data.OperatorId = (res.returnData.UserId ?? '').toString()
  106. config.data.LogId = (res.returnData.LogId ?? '').toString()
  107. config.data.UserName = (res.returnData.UserName ?? '').toString()
  108. } else {
  109. const loginData = typeof data === 'string' ? JSON.parse(data) : data
  110. config.data.UserName = loginData.LoginName.toString()
  111. }
  112. } else {
  113. config.data.OperatorId = getUserId()
  114. config.data.LogId = getLogId()
  115. config.data.UserName = getUserName()
  116. }
  117. service(config)
  118. }
  119. if (res.success) {
  120. flag = false
  121. loadingInstance.close()
  122. return res
  123. } else {
  124. if (res.code == 500) {
  125. Message({
  126. message: '身份令牌过期或失效,即将重新登录',
  127. type: 'error',
  128. duration: 5 * 1000,
  129. onClose: () => {
  130. store.dispatch('tagsView/delAllViews').then(() => { })
  131. store.dispatch('user/resetToken').then(() => {
  132. location.reload()
  133. })
  134. }
  135. })
  136. }
  137. flag = false
  138. loadingInstance.close()
  139. }
  140. // if the custom code is not 20000, it is judged as an error.
  141. // if (res.code != 0 || res.code == -1) {
  142. // Message({
  143. // message: res.message || 'Error',
  144. // type: 'error',
  145. // duration: 5 * 1000
  146. // })
  147. // // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  148. // if (res.code == 500) {
  149. // // to re-login
  150. // Message({
  151. // message: '身份令牌过期或失效,即将重新登录',
  152. // type: 'error',
  153. // duration: 5 * 1000,
  154. // onClose: () => {
  155. // store.dispatch('tagsView/delAllViews').then(() => { })
  156. // store.dispatch('user/resetToken').then(() => {
  157. // location.reload()
  158. // })
  159. // }
  160. // })
  161. // }
  162. // // loadingInstance.close()
  163. // return Promise.reject(new Error(res.message || 'Error'))
  164. // } else {
  165. // // loadingInstance.close()
  166. // return res
  167. // }
  168. },
  169. error => {
  170. // console.log('err', error) // for debug
  171. const des = `${error}`.split(' ').includes('500')
  172. if (des) {
  173. Message({
  174. message: '身份令牌过期或失效,即将重新登录',
  175. type: 'error',
  176. duration: 5 * 1000,
  177. onClose: () => {
  178. store.dispatch('app/toggleDialog', false)
  179. store.dispatch('tagsView/delAllViews').then(() => { })
  180. store.dispatch('user/resetToken').then(() => {
  181. location.reload()
  182. })
  183. }
  184. })
  185. // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  186. // confirmButtonText: 'Re-Login',
  187. // cancelButtonText: 'Cancel',
  188. // type: 'warning'
  189. // }).then(() => {
  190. // store.dispatch('user/resetToken').then(() => {
  191. // location.reload()
  192. // })
  193. // })
  194. } else {
  195. Message({
  196. message: error.message,
  197. type: 'error',
  198. duration: 5 * 1000
  199. })
  200. }
  201. flag = false
  202. loadingInstance.close()
  203. return Promise.reject(error)
  204. }
  205. )
  206. export default service