123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import axios from '@/common/services/axios-instance'
- // eslint-disable-next-line no-restricted-imports
- import orginAxios from 'axios'
- import { sdLocalStorage } from '@/common/services/storage-service'
- class CubeServices {
- // 检查token是否过期
- checkCubeToken(scope) {
- // 检查是否存在sd-cube
- const cubeJson = sdLocalStorage.getItem('cube')
- // 没有获取过token
- if (cubeJson === null) {
- // 没有获取过token,直接获取
- return this.getCubeDict().then((res) => {
- const jsonObj = {}
- if (res.data.length === 0) {
- // 没有配置参数,直接返回
- return new Promise((resolve) => {
- resolve(false)
- })
- } else {
- // 配置了参数,写入
- jsonObj.is_open = res.data[0].param0
- jsonObj.api_url = res.data[0].param1
- sdLocalStorage.setItem('cube', JSON.stringify(jsonObj))
- if (jsonObj.is_open === 'YES') {
- // 后面要改成调用系统接口获取,因为后台还没开发,先写入固定值
- return this.setCubeToken(scope, jsonObj)
- }
- }
- })
- } else {
- // 不等于null,说明之前获取过,则先判断是否过期,如果过期,则刷新之后记录新的token,并返回,否则直接返回未过期token
- const cubeJsonObj = JSON.parse(cubeJson)
- const isOpen = cubeJsonObj.is_open
- // 未开启,则直接返回
- if (isOpen !== 'YES') {
- // 如果是NO,则删除,方便下次重新获取
- sdLocalStorage.removeItem('cube')
- return new Promise((resolve) => {
- resolve(false)
- })
- }
- const jsonObj = {
- ...cubeJsonObj,
- }
- // 开启了cube,先获取token以及刷新时间
- const cubeToken = cubeJsonObj[scope + '_token']
- // 没有获取到token,则直接获取
- if (!cubeToken) {
- return this.setCubeToken(scope, jsonObj)
- } else {
- // 获取到了token,则判断是否过期
- const cubeTokenRefresh = cubeJsonObj[scope + '_token_refresh']
- const nowTime = new Date().getTime()
- // 获取重新获取
- if (nowTime > parseFloat(cubeTokenRefresh)) {
- return this.setCubeToken(scope, jsonObj)
- } else {
- // 否则直接返回
- return new Promise((resolve) => {
- resolve(true)
- })
- }
- }
- }
- }
- // 获取cube字典
- getCubeDict(scope) {
- // 如果开启了参数
- return axios({
- url: `api/xcoa-mobile/v1/iam-common/common/dict-key-value?keyId=IS_OPEN_CUBE&dictType=value&valueId=IS_OPEN`,
- })
- }
- // 获取cubetoken
- getCubeToken(scope) {
- return axios({
- url: 'api/xcoa-mobile/v1/iamcubecontract/fineIntelAuditUrl?scope=' + scope,
- })
- }
- // 设置cubetoken
- setCubeToken(scope, jsonObj) {
- return this.getCubeToken(scope).then((res) => {
- // token获取失败,则认为未开启cube
- if (res.data === '') {
- jsonObj.is_open = 'NO'
- jsonObj.api_url = ''
- sdLocalStorage.setItem('cube', JSON.stringify(jsonObj))
- return new Promise((resolve) => {
- resolve(false)
- })
- }
- // token获取成功,记录token,并返回true
- jsonObj[scope + '_token'] = res.data.access_token
- jsonObj[scope + '_token_refresh'] = new Date().getTime() + res.data.expires_in
- // 写入
- sdLocalStorage.setItem('cube', JSON.stringify(jsonObj))
- return new Promise((resolve) => {
- resolve(true)
- })
- })
- }
- }
- export default new CubeServices()
|