@smart100/spu-web-plugin 1.0.25-beta.5 → 1.0.25-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +15 -8
- package/dist/spu-web-plugin.mjs +4192 -403
- package/package.json +2 -2
- package/src/axios.ts +126 -106
- package/src/axiosCache.ts +290 -0
- package/src/components/expandexp/index.ts +6 -6
- package/src/core.js +29 -6
- package/src/envService.ts +2 -2
- package/src/globalConfig.ts +24 -4
- package/src/index.ts +18 -5
- package/src/login.ts +18 -8
- package/src/map/index.ts +3 -3
- package/src/oss/cache.ts +178 -0
- package/src/oss/downloadService.ts +115 -53
- package/src/oss/servtoken.ts +4 -2
- package/src/oss/uploadService.ts +72 -81
- package/src/spuConfig.ts +30 -7
- package/src/tenantSetting.ts +2 -2
- package/src/types/index.d.ts +15 -8
- package/src/utils.ts +38 -3
package/src/core.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { globalOptions } from './index'
|
|
2
|
-
import {
|
|
2
|
+
import { apaasAxios } from './axios'
|
|
3
|
+
import { axiosCache } from './axiosCache'
|
|
3
4
|
import { get, cloneDeep } from 'lodash-es'
|
|
4
5
|
import { urlquery } from './urlquery'
|
|
5
6
|
import { getToken, getUser, getRefreshToken, getTokenExpires } from './login'
|
|
@@ -83,16 +84,38 @@ class Core {
|
|
|
83
84
|
// debugger
|
|
84
85
|
let res
|
|
85
86
|
try {
|
|
86
|
-
res = await
|
|
87
|
-
|
|
87
|
+
// res = await apaasAxios.post(
|
|
88
|
+
// `${smartcenter}/api/smartcenter/biz/getTenantWebAndApiDefined`,
|
|
89
|
+
// {
|
|
90
|
+
// envid: envId,
|
|
91
|
+
// tenantcode: tenantCode
|
|
92
|
+
// },
|
|
93
|
+
// {
|
|
94
|
+
// isShowLoading: false
|
|
95
|
+
// }
|
|
96
|
+
// )
|
|
97
|
+
res = await axiosCache(
|
|
88
98
|
{
|
|
89
|
-
|
|
90
|
-
|
|
99
|
+
strategy: 'default',
|
|
100
|
+
async checkcachesave(res) {
|
|
101
|
+
// console.log(res)
|
|
102
|
+
// debugger
|
|
103
|
+
return Array.isArray(res?.data?.list)
|
|
104
|
+
}
|
|
91
105
|
},
|
|
106
|
+
apaasAxios,
|
|
92
107
|
{
|
|
93
|
-
|
|
108
|
+
url: `${smartcenter}/api/smartcenter/biz/getTenantWebAndApiDefined`,
|
|
109
|
+
method: 'post',
|
|
110
|
+
isShowLoading: false,
|
|
111
|
+
data: {
|
|
112
|
+
envid: envId,
|
|
113
|
+
tenantcode: tenantCode
|
|
114
|
+
}
|
|
94
115
|
}
|
|
95
116
|
)
|
|
117
|
+
// console.log(res)
|
|
118
|
+
// debugger
|
|
96
119
|
} catch (err) {
|
|
97
120
|
console.error(err)
|
|
98
121
|
}
|
package/src/envService.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { get, cloneDeep } from 'lodash-es'
|
|
2
2
|
import { lsProxy } from './storageProxy'
|
|
3
|
-
import {
|
|
3
|
+
import { apaasAxios } from './axios'
|
|
4
4
|
import { decrypt } from './crypt'
|
|
5
5
|
import { toggleHttpOrHttps } from './utils'
|
|
6
6
|
import { getData, setData, removeData } from './storageCache'
|
|
@@ -54,7 +54,7 @@ async function requestEnvdata(envName: string): Promise<IAny | null> {
|
|
|
54
54
|
? 'https://mconfig.xuantongkeji.com'
|
|
55
55
|
: 'http://mconfig.xuantongkeji.com:8015'
|
|
56
56
|
try {
|
|
57
|
-
const res = await
|
|
57
|
+
const res = await apaasAxios.get(`${hostsRoot}/multiplatconfig/env/${envName}`, {
|
|
58
58
|
isShowLoading: false,
|
|
59
59
|
isSendToken: false
|
|
60
60
|
})
|
package/src/globalConfig.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { apaasAxios } from './axios'
|
|
2
|
+
import { axiosCache } from './axiosCache'
|
|
2
3
|
import { cloneDeep } from 'lodash-es'
|
|
3
4
|
|
|
4
5
|
class GlobalConfig {
|
|
@@ -9,9 +10,28 @@ class GlobalConfig {
|
|
|
9
10
|
public async getFun(): Promise<any> {
|
|
10
11
|
if (!this.isload) {
|
|
11
12
|
try {
|
|
12
|
-
const res = await
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
// const res = await apaasAxios.post(
|
|
14
|
+
// '/api/pemission/rolepermission/globalconfig/getglobalconfigbytype',
|
|
15
|
+
// {
|
|
16
|
+
// configtype: '1'
|
|
17
|
+
// }
|
|
18
|
+
// )
|
|
19
|
+
const res = await axiosCache(
|
|
20
|
+
{
|
|
21
|
+
strategy: 'default',
|
|
22
|
+
async checkcachesave(res: any) {
|
|
23
|
+
return !!(res.code === 200 && res.data)
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
apaasAxios,
|
|
27
|
+
{
|
|
28
|
+
url: '/api/pemission/rolepermission/globalconfig/getglobalconfigbytype',
|
|
29
|
+
method: 'post',
|
|
30
|
+
data: {
|
|
31
|
+
configtype: '1'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
)
|
|
15
35
|
// console.log(res)
|
|
16
36
|
// debugger
|
|
17
37
|
if (res.code === 200) {
|
package/src/index.ts
CHANGED
|
@@ -3,10 +3,12 @@ import { merge } from 'lodash-es'
|
|
|
3
3
|
import { v4 as getUuid } from 'uuid'
|
|
4
4
|
import { installStorageProxy, lsProxy, ssProxy } from './storageProxy'
|
|
5
5
|
import { getLocation, getDistance, getAddress } from './map/index'
|
|
6
|
-
import { installAxios, spuAxios,
|
|
6
|
+
import { installAxios, spuAxios, apaasAxios, normalAxios, decryptAxiosResponseData } from './axios'
|
|
7
|
+
import { installAxiosCache, axiosCache } from './axiosCache'
|
|
7
8
|
import { installSpuConfig, spuConfig } from './spuConfig'
|
|
8
9
|
import { globalConfig } from './globalConfig'
|
|
9
10
|
import { downloadService, uploadService } from './oss'
|
|
11
|
+
import { installOssCache } from './oss/cache'
|
|
10
12
|
import { getServToken } from './oss/servtoken'
|
|
11
13
|
import { getCloudServ } from './oss/cloudServ'
|
|
12
14
|
import {
|
|
@@ -16,7 +18,8 @@ import {
|
|
|
16
18
|
isInApp,
|
|
17
19
|
isdebugger,
|
|
18
20
|
isvirtuallocation,
|
|
19
|
-
getServerTime
|
|
21
|
+
getServerTime,
|
|
22
|
+
isOnline
|
|
20
23
|
} from './utils'
|
|
21
24
|
import { encrypt, decrypt } from './crypt'
|
|
22
25
|
import { installUrlquery } from './urlquery'
|
|
@@ -59,6 +62,10 @@ const getDefaultGlobalOptions = () => {
|
|
|
59
62
|
storageproxyprefix: '',
|
|
60
63
|
isfixapptokenexpired: false, // 是否修复apptoken过期问题 目前版本暂时不默认修复 等门户引擎验证通过之后 再改成默认修复
|
|
61
64
|
isoptimizesingleloginlogic: false, // 是否优化判断不重复走单点登录的逻辑 暂时只在超表上优化
|
|
65
|
+
osscache: {
|
|
66
|
+
flag: false,
|
|
67
|
+
cachetime: 60 * 1000 * 60 * 2 // 默认缓存2小时
|
|
68
|
+
},
|
|
62
69
|
singleLoginCallback: null,
|
|
63
70
|
router: null
|
|
64
71
|
}
|
|
@@ -74,10 +81,13 @@ const install = (app: any, options: any) => {
|
|
|
74
81
|
installStorageProxy(globalOptions)
|
|
75
82
|
installUrlquery()
|
|
76
83
|
installAxios(globalOptions)
|
|
84
|
+
installAxiosCache()
|
|
77
85
|
installSpuConfig(globalOptions)
|
|
78
86
|
installWxworksuitePlugin() // 安装企微第三方应用插件
|
|
79
87
|
installAuth(globalOptions)
|
|
88
|
+
installOssCache(globalOptions)
|
|
80
89
|
installApaasSpuTrack()
|
|
90
|
+
|
|
81
91
|
installTest(globalOptions)
|
|
82
92
|
|
|
83
93
|
console.log(`@smart100/spu-web-plugin@${version} install!`)
|
|
@@ -87,7 +97,7 @@ const install = (app: any, options: any) => {
|
|
|
87
97
|
console.log(globalOptions)
|
|
88
98
|
|
|
89
99
|
// setTimeout(() => {
|
|
90
|
-
// console.error('
|
|
100
|
+
// console.error('8877778')
|
|
91
101
|
// }, 1000)
|
|
92
102
|
|
|
93
103
|
// if (install.installed) return
|
|
@@ -118,10 +128,12 @@ export {
|
|
|
118
128
|
getLocation,
|
|
119
129
|
getDistance,
|
|
120
130
|
getAddress,
|
|
131
|
+
normalAxios,
|
|
121
132
|
spuAxios,
|
|
122
|
-
|
|
123
|
-
|
|
133
|
+
apaasAxios,
|
|
134
|
+
apaasAxios as axios, // 兼容之前导出
|
|
124
135
|
decryptAxiosResponseData,
|
|
136
|
+
axiosCache,
|
|
125
137
|
spuConfig,
|
|
126
138
|
globalConfig,
|
|
127
139
|
downloadService,
|
|
@@ -132,6 +144,7 @@ export {
|
|
|
132
144
|
getUuid,
|
|
133
145
|
functionCheck,
|
|
134
146
|
getServerTime,
|
|
147
|
+
isOnline,
|
|
135
148
|
encrypt,
|
|
136
149
|
decrypt,
|
|
137
150
|
setTitle,
|
package/src/login.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { get, cloneDeep } from 'lodash-es'
|
|
2
2
|
import jwtDecode from 'jwt-decode'
|
|
3
3
|
import { lsProxy } from './storageProxy'
|
|
4
|
-
import {
|
|
4
|
+
import { apaasAxios } from './axios'
|
|
5
5
|
import cloudServ from './oss/cloudServ'
|
|
6
6
|
import core, { Module } from './core'
|
|
7
7
|
import { urlquery } from './urlquery'
|
|
@@ -283,7 +283,7 @@ function getLoginState() {
|
|
|
283
283
|
// 有可能是中心角色请求失败 兼容不报错
|
|
284
284
|
async function getAndSetUserInfo() {
|
|
285
285
|
try {
|
|
286
|
-
const accountinfo = await
|
|
286
|
+
const accountinfo = await apaasAxios
|
|
287
287
|
.post('/api/teapi/rolepermission/account/getaccountinfo', {
|
|
288
288
|
positionid: getUser('positioncode') || '',
|
|
289
289
|
deviceinfo: '',
|
|
@@ -333,7 +333,7 @@ function formatTenant(tenant: ITenantInfo) {
|
|
|
333
333
|
|
|
334
334
|
async function getAndSetTenant(tenantcode?: string) {
|
|
335
335
|
try {
|
|
336
|
-
const tenantsRes: null | ITenantInfo[] = await
|
|
336
|
+
const tenantsRes: null | ITenantInfo[] = await apaasAxios
|
|
337
337
|
.get('/api/auth/tenantlist', {})
|
|
338
338
|
.then((res: any) => {
|
|
339
339
|
return res?.data?.tenants
|
|
@@ -380,7 +380,7 @@ function updateToken() {
|
|
|
380
380
|
const token = getToken()
|
|
381
381
|
const refreshtoken = getRefreshToken()
|
|
382
382
|
const sendToken = checkLoginByToken(token) ? token : refreshtoken
|
|
383
|
-
return
|
|
383
|
+
return apaasAxios
|
|
384
384
|
.get('/api/auth/refreshtoken', {
|
|
385
385
|
params: {
|
|
386
386
|
refreshtoken: sendToken
|
|
@@ -647,12 +647,22 @@ async function singleLogin(query: IAny) {
|
|
|
647
647
|
// 旧:判断当前单点登录的 token/refreshtoken/tokenexpires 是否与本地缓存的一致
|
|
648
648
|
// 新:解析当前单点登录 token 中的 mbcode、tenantcode,如果与缓存的一致,说明本次登录与上次登录是同个人同个租户
|
|
649
649
|
// 新逻辑暂时只用于超表
|
|
650
|
-
if (
|
|
651
|
-
|
|
650
|
+
if (
|
|
651
|
+
globalOptions.modulekey === 'litheformspu' &&
|
|
652
|
+
globalOptions.isoptimizesingleloginlogic
|
|
653
|
+
) {
|
|
654
|
+
if (
|
|
655
|
+
curUser?.memberCode === cacheUser?.memberCode &&
|
|
656
|
+
curUser?.tenantCode === cacheUser?.tenantCode
|
|
657
|
+
) {
|
|
652
658
|
isneedlogin = false
|
|
653
659
|
}
|
|
654
660
|
} else {
|
|
655
|
-
if (
|
|
661
|
+
if (
|
|
662
|
+
token === getToken() &&
|
|
663
|
+
refreshtoken === getRefreshToken() &&
|
|
664
|
+
tokenexpires === getTokenExpires()
|
|
665
|
+
) {
|
|
656
666
|
isneedlogin = false
|
|
657
667
|
}
|
|
658
668
|
}
|
|
@@ -713,7 +723,7 @@ async function singleLogin(query: IAny) {
|
|
|
713
723
|
} else {
|
|
714
724
|
flag = false
|
|
715
725
|
setBaseInfo() // 传递的token过期依然写入 如果不写入的话 有可能之前的token本来没过期 页面依然可用
|
|
716
|
-
console.error('单点登录失败,请检查链接所传 token/refreshtoken/tokenexpires
|
|
726
|
+
console.error('单点登录失败,请检查链接所传 token/refreshtoken/tokenexpires 是否非法或过期。')
|
|
717
727
|
}
|
|
718
728
|
|
|
719
729
|
// 单点登录成功
|
package/src/map/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { urlquery } from '../urlquery'
|
|
|
2
2
|
import { cloneDeep, isObject } from 'lodash-es'
|
|
3
3
|
import { mapService } from './MapService'
|
|
4
4
|
import { getAMapKey } from './AMapKey'
|
|
5
|
-
import {
|
|
5
|
+
import { apaasAxios } from '../axios'
|
|
6
6
|
import { delay } from '../utils'
|
|
7
7
|
import { wgs84ToGcj02, BMapTransformBD09ToGCJ02Points, getPoiPhoneNumber } from './utils'
|
|
8
8
|
|
|
@@ -117,7 +117,7 @@ const getIPLocationByIpaas = async (ip?: string): Promise<Location> => {
|
|
|
117
117
|
return new Promise((resolve, reject) => {
|
|
118
118
|
const AMapKey = getAMapKey()
|
|
119
119
|
|
|
120
|
-
|
|
120
|
+
apaasAxios
|
|
121
121
|
.post(
|
|
122
122
|
'https://silkroad.wxchina.com/api/openapi/publishEvent?topic=xw-listener&subtopic=xw-listener&apicaseid=6684389338001815271',
|
|
123
123
|
{
|
|
@@ -167,7 +167,7 @@ const getAddressByIpaas = async (position: Location): Promise<string> => {
|
|
|
167
167
|
if (position) {
|
|
168
168
|
try {
|
|
169
169
|
const AMapKey = getAMapKey()
|
|
170
|
-
const result = await
|
|
170
|
+
const result = await apaasAxios.post(
|
|
171
171
|
'https://silkroad.wxchina.com/api/openapi/publishEvent?topic=xw-listener&subtopic=xw-listener&apicaseid=6684389338001809906',
|
|
172
172
|
{
|
|
173
173
|
longitude: position.longitude,
|
package/src/oss/cache.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import cloudServ from './cloudServ'
|
|
2
|
+
import localforage from 'localforage'
|
|
3
|
+
import type { IDownload } from './downloadService'
|
|
4
|
+
import type { IUpload } from './uploadService'
|
|
5
|
+
import { getAliyunCope, getCope } from './downloadService'
|
|
6
|
+
import { globalOptions } from '../index'
|
|
7
|
+
|
|
8
|
+
interface ICacheData {
|
|
9
|
+
blob: Blob
|
|
10
|
+
cachetime: number // 缓存过期时间
|
|
11
|
+
expiretime: number // 过期时间
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ICaches {
|
|
15
|
+
[propName: string]: ICacheData
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ICacheobj {
|
|
19
|
+
key: string
|
|
20
|
+
copekey: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let defaultCachetime = 60 * 1000 * 60 * 2 // 默认缓存2小时
|
|
24
|
+
|
|
25
|
+
const store = localforage.createInstance({
|
|
26
|
+
name: 'spu-db',
|
|
27
|
+
storeName: 'oss-cache'
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// 生成缓存键 缓存区分租户和人员
|
|
31
|
+
const generateCacheobj = ({
|
|
32
|
+
type = 'img',
|
|
33
|
+
source = '',
|
|
34
|
+
datetime = '',
|
|
35
|
+
storagetype = 'storage',
|
|
36
|
+
cope = ''
|
|
37
|
+
}: IDownload): ICacheobj => {
|
|
38
|
+
const provider = cloudServ.getProvider(storagetype)
|
|
39
|
+
const copeStr = provider?.isAliyun ? getAliyunCope(cope) : getCope(cope)
|
|
40
|
+
return {
|
|
41
|
+
key: `type=${type}&storagetype=${storagetype}&source=${source}&datetime=${datetime}`,
|
|
42
|
+
copekey: copeStr || 'default'
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const getAllCachesBykey = async (key: string): Promise<ICaches | null> => {
|
|
47
|
+
const allcaches: ICaches | null = await store.getItem(key)
|
|
48
|
+
return allcaches || null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const setCache = async (cacheobj: ICacheobj, cache: ICacheData) => {
|
|
52
|
+
let allcaches = await getAllCachesBykey(cacheobj.key)
|
|
53
|
+
if (allcaches) {
|
|
54
|
+
allcaches[cacheobj.copekey] = cache
|
|
55
|
+
} else {
|
|
56
|
+
allcaches = {
|
|
57
|
+
[cacheobj.copekey]: cache
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
await store.setItem(cacheobj.key, allcaches)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const setCacheByFile = ({
|
|
64
|
+
type = 'img',
|
|
65
|
+
file,
|
|
66
|
+
source = '',
|
|
67
|
+
datetime = '',
|
|
68
|
+
storagetype = 'storage'
|
|
69
|
+
}: IUpload) => {
|
|
70
|
+
// 暂时只作用于图片
|
|
71
|
+
if (!!globalOptions?.osscache?.flag && type === 'img') {
|
|
72
|
+
setCache(
|
|
73
|
+
generateCacheobj({
|
|
74
|
+
type,
|
|
75
|
+
source,
|
|
76
|
+
datetime,
|
|
77
|
+
storagetype,
|
|
78
|
+
cope: false
|
|
79
|
+
}),
|
|
80
|
+
{
|
|
81
|
+
cachetime: defaultCachetime,
|
|
82
|
+
expiretime: Date.now() + defaultCachetime,
|
|
83
|
+
blob: new Blob([file], {
|
|
84
|
+
type: file.type // 保留原 File 的 MIME 类型(如 image/png)
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const isExpireData = (cache: ICacheData) => {
|
|
92
|
+
if (Date.now() > cache.expiretime) {
|
|
93
|
+
return true
|
|
94
|
+
}
|
|
95
|
+
// return true
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const getCache = async (cacheobj: ICacheobj, whennomatchcopekeyreturnanyone: boolean = false) => {
|
|
100
|
+
let allcaches = await getAllCachesBykey(cacheobj.key)
|
|
101
|
+
let cache = null
|
|
102
|
+
if (allcaches) {
|
|
103
|
+
// 找出所有没过期的缓存
|
|
104
|
+
const noExpireData: ICaches = {}
|
|
105
|
+
for (const x in allcaches) {
|
|
106
|
+
const item = allcaches[x]
|
|
107
|
+
if (!isExpireData(item)) {
|
|
108
|
+
noExpireData[x] = item
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
cache = noExpireData[cacheobj.copekey] || null
|
|
113
|
+
// 当没命中copekey时,返回默认缓存 默认缓存不存在时返回第一个缓存
|
|
114
|
+
if (!cache && whennomatchcopekeyreturnanyone && Object.keys(noExpireData).length) {
|
|
115
|
+
cache = noExpireData['default'] || noExpireData[Object.keys(noExpireData)[0]]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return cache
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const removeCache = async (cacheobj: ICacheobj) => {
|
|
122
|
+
let allcaches = await getAllCachesBykey(cacheobj.key)
|
|
123
|
+
if (allcaches) {
|
|
124
|
+
delete allcaches[cacheobj.copekey]
|
|
125
|
+
if (Object.keys(allcaches).length) {
|
|
126
|
+
await store.setItem(cacheobj.key, allcaches)
|
|
127
|
+
} else {
|
|
128
|
+
await store.removeItem(cacheobj.key)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const removeExpireDataBykey = async (key: string) => {
|
|
134
|
+
let allcaches = await getAllCachesBykey(key)
|
|
135
|
+
if (allcaches) {
|
|
136
|
+
for (const x in allcaches) {
|
|
137
|
+
const cache: ICacheData | null = allcaches[x]
|
|
138
|
+
if (cache && isExpireData(cache)) {
|
|
139
|
+
delete allcaches[x]
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (Object.keys(allcaches).length) {
|
|
143
|
+
await store.setItem(key, allcaches)
|
|
144
|
+
} else {
|
|
145
|
+
await store.removeItem(key)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 清除过期无效数据
|
|
151
|
+
const removeExpireData = async () => {
|
|
152
|
+
store
|
|
153
|
+
.keys()
|
|
154
|
+
.then(async (keys) => {
|
|
155
|
+
for (let i = 0, len = keys.length; i < len; i++) {
|
|
156
|
+
const key = keys[i]
|
|
157
|
+
await removeExpireDataBykey(key)
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
.catch((err) => {
|
|
161
|
+
console.log(err)
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const installOssCache = (options: any) => {
|
|
166
|
+
defaultCachetime = options?.osscache?.cachetime || defaultCachetime
|
|
167
|
+
removeExpireData()
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export {
|
|
171
|
+
installOssCache,
|
|
172
|
+
defaultCachetime,
|
|
173
|
+
generateCacheobj,
|
|
174
|
+
setCache,
|
|
175
|
+
setCacheByFile,
|
|
176
|
+
getCache,
|
|
177
|
+
removeCache
|
|
178
|
+
}
|
|
@@ -2,18 +2,11 @@ import { AliClient, ObsClient, S3Client } from './OSSClient'
|
|
|
2
2
|
import dayjs from 'dayjs'
|
|
3
3
|
import cloudServ from './cloudServ'
|
|
4
4
|
import { getServToken } from './servtoken'
|
|
5
|
-
import {
|
|
5
|
+
import { normalAxios } from '../axios'
|
|
6
6
|
import { getUser } from '../login'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// function getUrlPaths(url: string): string[] {
|
|
11
|
-
// let path = url.split('?')[0]
|
|
12
|
-
// if (!path) return []
|
|
13
|
-
// path = path.replace('http://', '')
|
|
14
|
-
// path = path.replace('https://', '')
|
|
15
|
-
// return path.split('/')
|
|
16
|
-
// }
|
|
7
|
+
import { isOnline } from '../utils'
|
|
8
|
+
import { globalOptions } from '../index'
|
|
9
|
+
import { defaultCachetime, generateCacheobj, setCache, getCache } from './cache'
|
|
17
10
|
|
|
18
11
|
const getContentType = (suffix: string) => {
|
|
19
12
|
const map: IAny = {
|
|
@@ -33,32 +26,6 @@ const isIpUrl = (url: string) => {
|
|
|
33
26
|
return reg.test(url)
|
|
34
27
|
}
|
|
35
28
|
|
|
36
|
-
// interface UrlInfo {
|
|
37
|
-
// [propName: string]: {
|
|
38
|
-
// url: string
|
|
39
|
-
// outTime: number
|
|
40
|
-
// }
|
|
41
|
-
// }
|
|
42
|
-
|
|
43
|
-
// /**
|
|
44
|
-
// * @Description: 缓存DownloadUrl
|
|
45
|
-
// * @author: hyl
|
|
46
|
-
// */
|
|
47
|
-
// const cacheUrlInfo: UrlInfo = {}
|
|
48
|
-
|
|
49
|
-
// function setCacheUrl(key: string, url: string, expires: number) {
|
|
50
|
-
// const thisTime = new Date().getTime()
|
|
51
|
-
// let diff = expires * 1000 - thisTime
|
|
52
|
-
// if (diff < 0) return
|
|
53
|
-
// // 最多只缓存20分钟
|
|
54
|
-
// if (diff > 1000 * 60 * 20) diff = 1000 * 60 * 20
|
|
55
|
-
// const outTime = thisTime + diff
|
|
56
|
-
// cacheUrlInfo[key] = {
|
|
57
|
-
// url,
|
|
58
|
-
// outTime
|
|
59
|
-
// }
|
|
60
|
-
// }
|
|
61
|
-
|
|
62
29
|
type Cope = { width?: number | string; height?: number | string } | string | boolean
|
|
63
30
|
|
|
64
31
|
const getAliyunCope = (cope?: Cope) => {
|
|
@@ -140,10 +107,15 @@ const getUrl = async ({
|
|
|
140
107
|
filename = source
|
|
141
108
|
}
|
|
142
109
|
const isAbsoluteUrl =
|
|
143
|
-
!!source.match(/\/att\//) ||
|
|
110
|
+
!!source.match(/\/att\//) ||
|
|
111
|
+
!!source.match(/\/img\//) ||
|
|
112
|
+
!!source.match(/att\//) ||
|
|
113
|
+
!!source.match(/img\//)
|
|
144
114
|
const suffix = filename.slice(filename.lastIndexOf('.'))
|
|
145
115
|
const date = dayjs(+datetime).format('YYYYMMDD')
|
|
146
|
-
let objectKey = isAbsoluteUrl
|
|
116
|
+
let objectKey = isAbsoluteUrl
|
|
117
|
+
? source
|
|
118
|
+
: `${source.slice(0, 3)}/${type}/${date}/${tenantCode}/${source}`
|
|
147
119
|
const copeStr = provider?.isAliyun ? getAliyunCope(cope) : getCope(cope)
|
|
148
120
|
const contentType = getContentType(suffix)
|
|
149
121
|
|
|
@@ -319,24 +291,12 @@ const downloadFile = async ({
|
|
|
319
291
|
filename: realFilename,
|
|
320
292
|
cope
|
|
321
293
|
})
|
|
322
|
-
// axios.get(url, {
|
|
323
|
-
// responseType: 'blob',
|
|
324
|
-
// isSendToken: false,
|
|
325
|
-
// isShowErrorMessage: false
|
|
326
|
-
// }).then((response: any) => {
|
|
327
|
-
// // console.log(response)
|
|
328
|
-
// // debugger
|
|
329
|
-
// downloadFileByBlob(response.data, filename)
|
|
330
|
-
// }).catch((e) => {
|
|
331
|
-
// console.log(e)
|
|
332
|
-
// throw Error(e)
|
|
333
|
-
// })
|
|
334
294
|
|
|
335
295
|
const provider = cloudServ.getProvider(storagetype)
|
|
336
296
|
if (provider?.isAliyun) {
|
|
337
297
|
downloadFileByUrl(url, filename)
|
|
338
298
|
} else {
|
|
339
|
-
|
|
299
|
+
await normalAxios
|
|
340
300
|
.get(url, {
|
|
341
301
|
responseType: 'blob',
|
|
342
302
|
isSendToken: false,
|
|
@@ -345,7 +305,9 @@ const downloadFile = async ({
|
|
|
345
305
|
.then((response: any) => {
|
|
346
306
|
// console.log(response)
|
|
347
307
|
// debugger
|
|
348
|
-
|
|
308
|
+
if (Object.prototype.toString.call(response.data) === '[object Blob]') {
|
|
309
|
+
downloadFileByBlob(response.data, filename)
|
|
310
|
+
}
|
|
349
311
|
})
|
|
350
312
|
.catch((e: any) => {
|
|
351
313
|
console.log(e)
|
|
@@ -354,7 +316,107 @@ const downloadFile = async ({
|
|
|
354
316
|
}
|
|
355
317
|
}
|
|
356
318
|
|
|
319
|
+
// 根据文件信息生成blob
|
|
320
|
+
const getBlob = async ({
|
|
321
|
+
type = 'img',
|
|
322
|
+
source = '',
|
|
323
|
+
filename = '',
|
|
324
|
+
datetime = '',
|
|
325
|
+
storagetype = 'storage',
|
|
326
|
+
cope = ''
|
|
327
|
+
}: IDownload) => {
|
|
328
|
+
const cacheobj = generateCacheobj({
|
|
329
|
+
type,
|
|
330
|
+
source,
|
|
331
|
+
datetime,
|
|
332
|
+
storagetype,
|
|
333
|
+
cope
|
|
334
|
+
})
|
|
335
|
+
const online = await isOnline()
|
|
336
|
+
const requestBlob = async (issetcache = false): Promise<any> => {
|
|
337
|
+
let blob: any = null
|
|
338
|
+
if (online) {
|
|
339
|
+
try {
|
|
340
|
+
const url = await getUrl({
|
|
341
|
+
type,
|
|
342
|
+
source,
|
|
343
|
+
datetime,
|
|
344
|
+
storagetype,
|
|
345
|
+
filename,
|
|
346
|
+
cope
|
|
347
|
+
})
|
|
348
|
+
const response = await normalAxios.get(url, {
|
|
349
|
+
responseType: 'blob',
|
|
350
|
+
isSendToken: false,
|
|
351
|
+
isShowErrorMessage: false,
|
|
352
|
+
isShowLoading: false
|
|
353
|
+
})
|
|
354
|
+
// console.log(response)
|
|
355
|
+
if (Object.prototype.toString.call(response.data) === '[object Blob]') {
|
|
356
|
+
blob = response.data
|
|
357
|
+
if (issetcache) {
|
|
358
|
+
setCache(cacheobj, {
|
|
359
|
+
cachetime: defaultCachetime,
|
|
360
|
+
expiretime: Date.now() + defaultCachetime,
|
|
361
|
+
blob
|
|
362
|
+
})
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
} catch (err) {
|
|
366
|
+
console.error(err)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return blob
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// 暂时只作用于图片
|
|
373
|
+
if (!!globalOptions?.osscache?.flag && type === 'img') {
|
|
374
|
+
// 缓存策略:
|
|
375
|
+
// 1. 离线时,使用缓存,缓存不存在时,使用任一同key下的资源,再没有才返回null
|
|
376
|
+
// 2. 在线时,优先使用缓存,并请求最新数据存入,缓存不存在时,使用网络请求
|
|
377
|
+
if (online) {
|
|
378
|
+
const cache = await getCache(cacheobj)
|
|
379
|
+
if (cache) {
|
|
380
|
+
requestBlob(true)
|
|
381
|
+
return cache.blob
|
|
382
|
+
} else {
|
|
383
|
+
return await requestBlob(true)
|
|
384
|
+
}
|
|
385
|
+
} else {
|
|
386
|
+
const cache = await getCache(cacheobj, true)
|
|
387
|
+
return cache?.blob || null
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
return await requestBlob(false)
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const getObjectURL = async ({
|
|
395
|
+
type = 'img',
|
|
396
|
+
source = '',
|
|
397
|
+
filename = '',
|
|
398
|
+
datetime = '',
|
|
399
|
+
storagetype = 'storage',
|
|
400
|
+
cope = ''
|
|
401
|
+
}: IDownload) => {
|
|
402
|
+
const blob = await getBlob({
|
|
403
|
+
type,
|
|
404
|
+
source,
|
|
405
|
+
datetime,
|
|
406
|
+
storagetype,
|
|
407
|
+
filename,
|
|
408
|
+
cope
|
|
409
|
+
})
|
|
410
|
+
return blob ? URL.createObjectURL(blob) : ''
|
|
411
|
+
}
|
|
412
|
+
|
|
357
413
|
export default {
|
|
358
414
|
getUrl,
|
|
415
|
+
getBlob,
|
|
416
|
+
getObjectURL,
|
|
359
417
|
downloadFile
|
|
360
418
|
}
|
|
419
|
+
|
|
420
|
+
export { getAliyunCope, getCope }
|
|
421
|
+
|
|
422
|
+
export type { IDownload }
|
package/src/oss/servtoken.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { apaasAxios } from '../axios'
|
|
2
2
|
|
|
3
3
|
export type ServToken = {
|
|
4
4
|
accesskeyid: string
|
|
@@ -20,7 +20,9 @@ let xhr: Promise<ServToken> | null = null
|
|
|
20
20
|
|
|
21
21
|
const requestServToken = async () => {
|
|
22
22
|
try {
|
|
23
|
-
const response: ServToken = await
|
|
23
|
+
const response: ServToken = await apaasAxios
|
|
24
|
+
.get('/api/teapi/auth/servtoken')
|
|
25
|
+
.then((res: any) => res.data)
|
|
24
26
|
// debugger
|
|
25
27
|
return response
|
|
26
28
|
} catch (e) {
|