request.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import axios from 'axios'
  2. import { MessageBox, Message } from 'element-ui'
  3. import store from '@/store'
  4. import { geteSessionStorage } from '@/utils/auth'
  5. import qs from 'qs'
  6. import router from '@/router'
  7. import { pipeConfigInfo } from 'staticPub/config'
  8. // 创建axios实例
  9. // export const IP = 'http://36.138.232.124:10085/'
  10. // export const IP = 'http://221.182.8.141:10085'
  11. //公司环境
  12. let tempIP = "";
  13. if (pipeConfigInfo.currentType == pipeConfigInfo.type.company) {
  14. tempIP = 'http://221.182.8.141:7077';
  15. } else {
  16. tempIP = 'http://172.16.0.4:10077';
  17. // tempIP = 'http://172.16.0.2:11020';
  18. }
  19. export const IP = tempIP;
  20. //客户环境
  21. // export const IP=tempIP
  22. // export const NewIp = 'http://58.17.241.6:1212'
  23. const service = axios.create({
  24. // baseURL: tempIP,
  25. // baseURL:"",
  26. withCredentials: true, // 跨域请求时发送cookiesokies
  27. timeout: 60000 // 请求超时
  28. })
  29. let requestTimes = 0
  30. // 请求拦截器
  31. service.interceptors.request.use(
  32. config => {
  33. if (config.method === 'post' && config.url === '/auth/oauth/login') { // 请求为post 使用qs转换数据
  34. config.data = qs.stringify(config.data)
  35. config.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  36. } else if (config.method === 'put' && config.url === '/dc/interfacemanage/updateByStatus') {
  37. config.data = qs.stringify(config.data)
  38. config.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  39. } else {
  40. config.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
  41. }
  42. if (config.file) {
  43. config.headers.post['Content-Type'] = 'multipart/form-data'
  44. }
  45. if (config.url === '/dc/interfacemanage/callInterface') {
  46. const formData = new FormData()
  47. formData.append('code', config.data.code)
  48. const parmasType = Object.prototype.toString.call(config.data.parameter)
  49. if (parmasType == '[object FormData]') {
  50. const paramsAry = {};
  51. const isAppend = [];//防止重复加入
  52. for (var key of config.data.parameter.keys()) {
  53. const formDataType = Object.prototype.toString.call(config.data.parameter.get(key))
  54. if (formDataType == '[object File]') {
  55. if (isAppend.findIndex(e => { return e == key }) == -1) {//防止重复加入
  56. isAppend.push(key)
  57. config.data.parameter.getAll(key).forEach(item => {
  58. formData.append(key, item)
  59. })
  60. }
  61. } else {
  62. paramsAry[key] = config.data.parameter.get(key)
  63. }
  64. }
  65. formData.append('parameter', JSON.stringify(paramsAry))
  66. } else {
  67. formData.append('parameter', JSON.stringify(config.data.parameter))
  68. }
  69. // if (config.data.templateServiceType) {
  70. // formData.append('templateServiceType', config.data.templateServiceType)
  71. // }
  72. config.data = formData
  73. // formData.append('parameter', JSON.stringify(config.data.parameter))
  74. // config.data = formData
  75. }
  76. // if (config.url === '/dc/interfacemanage/callInterface' && config.data.file) {
  77. // const formData = new FormData()
  78. // formData.append('code', config.data.code)
  79. // formData.append('parameter', config.data.parameter)
  80. // config.data = formData
  81. // } else if (config.url === '/dc/interfacemanage/callInterface') {
  82. // const formData = new FormData()
  83. // formData.append('code', config.data.code)
  84. // formData.append('parameter', JSON.stringify(config.data.parameter))
  85. // config.data = formData
  86. // }
  87. // 在发出请求前做点什么
  88. if (store.getters.token && config.url.indexOf('mbData') == "-1") {
  89. config.headers['Authorization'] = 'bearer ' + geteSessionStorage('token')
  90. }
  91. return config
  92. },
  93. error => {
  94. // 处理请求错误
  95. console.log(error)
  96. return Promise.reject(error)
  97. }
  98. )
  99. // 响应拦截器
  100. service.interceptors.response.use(
  101. /**
  102. * 如果您想要获取诸如头或状态之类的http信息
  103. * return response => response
  104. */
  105. /**
  106. * 通过自定义代码确定请求状态
  107. */
  108. response => {
  109. const res = response.data
  110. // 如果直接返回字符串(请求的图片 直接返回base64
  111. if (typeof res === 'string') return res
  112. // 下载文件 后端返回二进制流 直接返回data
  113. if (res instanceof ArrayBuffer) return res
  114. // 下载文件 后端返回二进制流 直接返回data
  115. if (res instanceof Blob) return res
  116. // 判断是否从中台返回接口
  117. if (response.config.url.includes('/dc/interfacemanage/callInterface')) {
  118. // 如果自定义代码是-1,则判断为错误。
  119. if (res.code === -1) {
  120. if (!res.message || res.message.length < 100) {
  121. Message({
  122. message: res.message || 'Error',
  123. type: 'error',
  124. duration: 2 * 1000
  125. })
  126. } else {
  127. Message({
  128. message: '系统错误',
  129. type: 'error',
  130. duration: 2 * 1000
  131. })
  132. }
  133. } else if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  134. // to re-login
  135. MessageBox.confirm('您已注销,您可以取消以停留在此页,或重新登录', '确认注销', {
  136. confirmButtonText: '确定',
  137. cancelButtonText: '取消',
  138. type: 'warning'
  139. }).then(() => {
  140. store.dispatch('user/resetToken').then(() => {
  141. location.reload()
  142. })
  143. })
  144. } else if (res.code === 1) {
  145. requestTimes = 0
  146. return res.result
  147. } else {
  148. Message({
  149. message: res.message || 'Error',
  150. type: 'error',
  151. duration: 2 * 1000
  152. })
  153. }
  154. return Promise.reject(new Error(res.message || 'Error')).catch(err => { console.log(err) })
  155. } else {
  156. // 如果自定义代码是-1,则判断为错误。
  157. if (res.code === -1) {
  158. if (!res.message || res.message.length < 100) {
  159. Message({
  160. message: res.message || 'Error',
  161. type: 'error',
  162. duration: 2 * 1000
  163. })
  164. } else {
  165. Message({
  166. message: '系统错误',
  167. type: 'error',
  168. duration: 2 * 1000
  169. })
  170. }
  171. } else if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  172. // to re-login
  173. MessageBox.confirm('您已注销,您可以取消以停留在此页,或重新登录', '确认注销', {
  174. confirmButtonText: '确定',
  175. cancelButtonText: '取消',
  176. type: 'warning'
  177. }).then(() => {
  178. store.dispatch('user/resetToken').then(() => {
  179. location.reload()
  180. })
  181. })
  182. } else if (res.code === 1 || res.Code === 200) {
  183. requestTimes = 0;
  184. //将所有的null转换为'';
  185. // const temp=JSON.parse(JSON.stringify(res).replace(/null/g,'""'));
  186. // debugger
  187. // res=temp;
  188. return res
  189. } else {
  190. Message({
  191. message: res.message || 'Error',
  192. type: 'error',
  193. duration: 2 * 1000
  194. })
  195. }
  196. return Promise.reject(new Error(res.message || 'Error')).catch(err => { console.log(err) })
  197. }
  198. },
  199. error => {
  200. function clearSession() {
  201. return new Promise(resolve => {
  202. sessionStorage.clear()
  203. resolve(1)
  204. })
  205. }
  206. if (error && error.response && error.response.data && error.response.data.code === 401) {
  207. if (requestTimes === 0) {
  208. requestTimes = 1
  209. MessageBox.confirm('登录失效,请重新登录!', '提示', {
  210. confirmButtonText: '确定',
  211. showCancelButton: false,
  212. showClose: false,
  213. closeOnPressEscape: false,
  214. closeOnClickModal: false
  215. }).then(async () => {
  216. const result = await clearSession()
  217. if (result === 1) {
  218. requestTimes = 0
  219. router.replace('/login')
  220. }
  221. })
  222. }
  223. } else {
  224. Message({
  225. message: error.message,
  226. type: 'error',
  227. duration: 5 * 1000
  228. })
  229. }
  230. }
  231. )
  232. // 森鑫炬接口
  233. export function getSXJList(data) {
  234. return service({
  235. url: '/gis/sxjDocking/send',
  236. method: 'post',
  237. data
  238. })
  239. }
  240. export default service