cube-services.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import axios from '@/common/services/axios-instance'
  2. // eslint-disable-next-line no-restricted-imports
  3. import orginAxios from 'axios'
  4. import { sdLocalStorage } from '@/common/services/storage-service'
  5. class CubeServices {
  6. // 检查token是否过期
  7. checkCubeToken(scope) {
  8. // 检查是否存在sd-cube
  9. const cubeJson = sdLocalStorage.getItem('cube')
  10. // 没有获取过token
  11. if (cubeJson === null) {
  12. // 没有获取过token,直接获取
  13. return this.getCubeDict().then((res) => {
  14. const jsonObj = {}
  15. if (res.data.length === 0) {
  16. // 没有配置参数,直接返回
  17. return new Promise((resolve) => {
  18. resolve(false)
  19. })
  20. } else {
  21. // 配置了参数,写入
  22. jsonObj.is_open = res.data[0].param0
  23. jsonObj.api_url = res.data[0].param1
  24. sdLocalStorage.setItem('cube', JSON.stringify(jsonObj))
  25. if (jsonObj.is_open === 'YES') {
  26. // 后面要改成调用系统接口获取,因为后台还没开发,先写入固定值
  27. return this.setCubeToken(scope, jsonObj)
  28. }
  29. }
  30. })
  31. } else {
  32. // 不等于null,说明之前获取过,则先判断是否过期,如果过期,则刷新之后记录新的token,并返回,否则直接返回未过期token
  33. const cubeJsonObj = JSON.parse(cubeJson)
  34. const isOpen = cubeJsonObj.is_open
  35. // 未开启,则直接返回
  36. if (isOpen !== 'YES') {
  37. // 如果是NO,则删除,方便下次重新获取
  38. sdLocalStorage.removeItem('cube')
  39. return new Promise((resolve) => {
  40. resolve(false)
  41. })
  42. }
  43. const jsonObj = {
  44. ...cubeJsonObj,
  45. }
  46. // 开启了cube,先获取token以及刷新时间
  47. const cubeToken = cubeJsonObj[scope + '_token']
  48. // 没有获取到token,则直接获取
  49. if (!cubeToken) {
  50. return this.setCubeToken(scope, jsonObj)
  51. } else {
  52. // 获取到了token,则判断是否过期
  53. const cubeTokenRefresh = cubeJsonObj[scope + '_token_refresh']
  54. const nowTime = new Date().getTime()
  55. // 获取重新获取
  56. if (nowTime > parseFloat(cubeTokenRefresh)) {
  57. return this.setCubeToken(scope, jsonObj)
  58. } else {
  59. // 否则直接返回
  60. return new Promise((resolve) => {
  61. resolve(true)
  62. })
  63. }
  64. }
  65. }
  66. }
  67. // 获取cube字典
  68. getCubeDict(scope) {
  69. // 如果开启了参数
  70. return axios({
  71. url: `api/xcoa-mobile/v1/iam-common/common/dict-key-value?keyId=IS_OPEN_CUBE&dictType=value&valueId=IS_OPEN`,
  72. })
  73. }
  74. // 获取cubetoken
  75. getCubeToken(scope) {
  76. return axios({
  77. url: 'api/xcoa-mobile/v1/iamcubecontract/fineIntelAuditUrl?scope=' + scope,
  78. })
  79. }
  80. // 设置cubetoken
  81. setCubeToken(scope, jsonObj) {
  82. return this.getCubeToken(scope).then((res) => {
  83. // token获取失败,则认为未开启cube
  84. if (res.data === '') {
  85. jsonObj.is_open = 'NO'
  86. jsonObj.api_url = ''
  87. sdLocalStorage.setItem('cube', JSON.stringify(jsonObj))
  88. return new Promise((resolve) => {
  89. resolve(false)
  90. })
  91. }
  92. // token获取成功,记录token,并返回true
  93. jsonObj[scope + '_token'] = res.data.access_token
  94. jsonObj[scope + '_token_refresh'] = new Date().getTime() + res.data.expires_in
  95. // 写入
  96. sdLocalStorage.setItem('cube', JSON.stringify(jsonObj))
  97. return new Promise((resolve) => {
  98. resolve(true)
  99. })
  100. })
  101. }
  102. }
  103. export default new CubeServices()