@zfqh/uniapp-x 0.0.0-stage → 0.0.2
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/CHANGELOG.md +44 -0
- package/LICENSE +21 -0
- package/README.md +61 -3
- package/lib/apis/core.d.ts +18 -0
- package/lib/apis/core.js +19 -0
- package/lib/apis/index.d.ts +2 -0
- package/lib/apis/index.js +2 -0
- package/lib/apis/request.d.ts +4 -0
- package/lib/apis/request.js +57 -0
- package/lib/configs/api.d.ts +7 -0
- package/lib/configs/api.js +6 -0
- package/lib/configs/app.d.ts +20 -0
- package/lib/configs/app.js +18 -0
- package/lib/configs/cdn.d.ts +15 -0
- package/lib/configs/cdn.js +17 -0
- package/lib/configs/env.d.ts +37 -0
- package/lib/configs/env.js +71 -0
- package/lib/configs/index.d.ts +130 -0
- package/lib/configs/index.js +76 -0
- package/lib/configs/link.d.ts +11 -0
- package/lib/configs/link.js +3 -0
- package/lib/configs/log.d.ts +9 -0
- package/lib/configs/log.js +8 -0
- package/lib/configs/mp.d.ts +15 -0
- package/lib/configs/mp.js +9 -0
- package/lib/configs/navigate.d.ts +17 -0
- package/lib/configs/navigate.js +5 -0
- package/lib/configs/page.d.ts +18 -0
- package/lib/configs/page.js +14 -0
- package/lib/configs/path.d.ts +11 -0
- package/lib/configs/path.js +10 -0
- package/lib/configs/storage.d.ts +7 -0
- package/lib/configs/storage.js +6 -0
- package/lib/configs/tabbar.d.ts +7 -0
- package/lib/configs/tabbar.js +6 -0
- package/lib/configs/toast.d.ts +18 -0
- package/lib/configs/toast.js +29 -0
- package/lib/configs/updater.d.ts +15 -0
- package/lib/configs/updater.js +33 -0
- package/lib/configs/upload.d.ts +30 -0
- package/lib/configs/upload.js +19 -0
- package/lib/configs/ver.d.ts +8 -0
- package/lib/configs/ver.js +28 -0
- package/lib/configs/web.d.ts +5 -0
- package/lib/configs/web.js +2 -0
- package/lib/hooks/index.d.ts +11 -0
- package/lib/hooks/index.js +11 -0
- package/lib/hooks/useAppLifecycle.d.ts +8 -0
- package/lib/hooks/useAppLifecycle.js +87 -0
- package/lib/hooks/useBadge.d.ts +8 -0
- package/lib/hooks/useBadge.js +47 -0
- package/lib/hooks/useNeedLogin.d.ts +1 -0
- package/lib/hooks/useNeedLogin.js +27 -0
- package/lib/hooks/useOverlay.d.ts +1 -0
- package/lib/hooks/useOverlay.js +17 -0
- package/lib/hooks/usePageShow.d.ts +2 -0
- package/lib/hooks/usePageShow.js +16 -0
- package/lib/hooks/usePopup.d.ts +1 -0
- package/lib/hooks/usePopup.js +14 -0
- package/lib/hooks/useRedirect.d.ts +5 -0
- package/lib/hooks/useRedirect.js +92 -0
- package/lib/hooks/useReferrer.d.ts +1 -0
- package/lib/hooks/useReferrer.js +11 -0
- package/lib/hooks/useShare.d.ts +12 -0
- package/lib/hooks/useShare.js +49 -0
- package/lib/hooks/useVersion.d.ts +6 -0
- package/lib/hooks/useVersion.js +21 -0
- package/lib/hooks/useWebview.d.ts +14 -0
- package/lib/hooks/useWebview.js +294 -0
- package/lib/index.d.ts +27 -0
- package/lib/index.js +26 -0
- package/lib/stores/app.d.ts +51 -0
- package/lib/stores/app.js +57 -0
- package/lib/stores/dict.d.ts +35 -0
- package/lib/stores/dict.js +42 -0
- package/lib/stores/im.d.ts +57 -0
- package/lib/stores/im.js +102 -0
- package/lib/stores/index.d.ts +12 -0
- package/lib/stores/index.js +17 -0
- package/lib/stores/launch.d.ts +42 -0
- package/lib/stores/launch.js +39 -0
- package/lib/stores/location.d.ts +35 -0
- package/lib/stores/location.js +58 -0
- package/lib/stores/login.d.ts +80 -0
- package/lib/stores/login.js +91 -0
- package/lib/stores/network.d.ts +26 -0
- package/lib/stores/network.js +45 -0
- package/lib/stores/pinia.d.ts +2 -0
- package/lib/stores/pinia.js +3 -0
- package/lib/stores/system.d.ts +39 -0
- package/lib/stores/system.js +36 -0
- package/lib/styles/index.scss +9 -0
- package/lib/styles/reset-vue.scss +15 -0
- package/lib/utils/audio.d.ts +14 -0
- package/lib/utils/audio.js +43 -0
- package/lib/utils/consts.d.ts +74 -0
- package/lib/utils/consts.js +110 -0
- package/lib/utils/cos.d.ts +17 -0
- package/lib/utils/cos.js +141 -0
- package/lib/utils/file.d.ts +28 -0
- package/lib/utils/file.js +292 -0
- package/lib/utils/index.d.ts +166 -0
- package/lib/utils/index.js +486 -0
- package/lib/utils/message.d.ts +1 -0
- package/lib/utils/message.js +55 -0
- package/lib/utils/navigate.d.ts +28 -0
- package/lib/utils/navigate.js +192 -0
- package/lib/utils/pages.d.ts +21 -0
- package/lib/utils/pages.js +83 -0
- package/lib/utils/permission.d.ts +17 -0
- package/lib/utils/permission.js +278 -0
- package/lib/utils/query.d.ts +20 -0
- package/lib/utils/query.js +51 -0
- package/lib/utils/request.d.ts +8 -0
- package/lib/utils/request.js +139 -0
- package/lib/utils/storage.d.ts +40 -0
- package/lib/utils/storage.js +128 -0
- package/lib/utils/subscribe.d.ts +7 -0
- package/lib/utils/subscribe.js +94 -0
- package/lib/utils/toast.d.ts +17 -0
- package/lib/utils/toast.js +66 -0
- package/lib/utils/uni.d.ts +1 -0
- package/lib/utils/uni.js +141 -0
- package/lib/utils/update.d.ts +41 -0
- package/lib/utils/update.js +322 -0
- package/lib/utils/url.d.ts +14 -0
- package/lib/utils/url.js +40 -0
- package/package.json +38 -4
- package/types/index.d.ts +78 -0
- package/types/virtual.d.ts +1 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface SystemInfo {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
deviceOrientation: string;
|
|
4
|
+
titleHeight: number;
|
|
5
|
+
topStatusBarHeight: number;
|
|
6
|
+
topBarTitleHeight: number;
|
|
7
|
+
}
|
|
8
|
+
/** 系统信息 */
|
|
9
|
+
export declare const useSystemStore: import("pinia").StoreDefinition<"system", import("pinia")._UnwrapAll<Pick<{
|
|
10
|
+
state: import("vue").Ref<{
|
|
11
|
+
[x: string]: any;
|
|
12
|
+
deviceOrientation: string;
|
|
13
|
+
titleHeight: number;
|
|
14
|
+
topStatusBarHeight: number;
|
|
15
|
+
topBarTitleHeight: number;
|
|
16
|
+
}>;
|
|
17
|
+
setSystemInfo: (data: Partial<SystemInfo>) => void;
|
|
18
|
+
getSystemInfo: () => Promise<void>;
|
|
19
|
+
}, "state">>, Pick<{
|
|
20
|
+
state: import("vue").Ref<{
|
|
21
|
+
[x: string]: any;
|
|
22
|
+
deviceOrientation: string;
|
|
23
|
+
titleHeight: number;
|
|
24
|
+
topStatusBarHeight: number;
|
|
25
|
+
topBarTitleHeight: number;
|
|
26
|
+
}>;
|
|
27
|
+
setSystemInfo: (data: Partial<SystemInfo>) => void;
|
|
28
|
+
getSystemInfo: () => Promise<void>;
|
|
29
|
+
}, never>, Pick<{
|
|
30
|
+
state: import("vue").Ref<{
|
|
31
|
+
[x: string]: any;
|
|
32
|
+
deviceOrientation: string;
|
|
33
|
+
titleHeight: number;
|
|
34
|
+
topStatusBarHeight: number;
|
|
35
|
+
topBarTitleHeight: number;
|
|
36
|
+
}>;
|
|
37
|
+
setSystemInfo: (data: Partial<SystemInfo>) => void;
|
|
38
|
+
getSystemInfo: () => Promise<void>;
|
|
39
|
+
}, "setSystemInfo" | "getSystemInfo">>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ref } from 'vue';
|
|
2
|
+
import { defineStore } from 'pinia';
|
|
3
|
+
import { MENU_BUTTON } from '../utils/consts';
|
|
4
|
+
function getInfo() {
|
|
5
|
+
const info = uni.getWindowInfo();
|
|
6
|
+
let titleHeight = 44;
|
|
7
|
+
let topStatusBarHeight = info.statusBarHeight;
|
|
8
|
+
let topBarTitleHeight = 0;
|
|
9
|
+
// #ifdef MP
|
|
10
|
+
const { height, top } = MENU_BUTTON;
|
|
11
|
+
titleHeight = height + 16;
|
|
12
|
+
topStatusBarHeight = top - 8;
|
|
13
|
+
// #endif
|
|
14
|
+
topBarTitleHeight = topStatusBarHeight + titleHeight;
|
|
15
|
+
return Object.assign({}, info, {
|
|
16
|
+
deviceOrientation: info.windowWidth > info.windowHeight ? 'landscape' : 'portrait',
|
|
17
|
+
titleHeight,
|
|
18
|
+
topStatusBarHeight,
|
|
19
|
+
topBarTitleHeight,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/** 系统信息 */
|
|
23
|
+
export const useSystemStore = defineStore('system', () => {
|
|
24
|
+
const state = ref(getInfo());
|
|
25
|
+
function setSystemInfo(data) {
|
|
26
|
+
Object.assign(state.value, data);
|
|
27
|
+
}
|
|
28
|
+
async function getSystemInfo() {
|
|
29
|
+
setSystemInfo(getInfo());
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
state,
|
|
33
|
+
setSystemInfo,
|
|
34
|
+
getSystemInfo,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface AudioOptions {
|
|
2
|
+
url: string;
|
|
3
|
+
autoplay: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare class Audio {
|
|
6
|
+
static instance: Audio | null;
|
|
7
|
+
private options;
|
|
8
|
+
context: UniNamespace.InnerAudioContext | null;
|
|
9
|
+
constructor(opts: AudioOptions);
|
|
10
|
+
private createContext;
|
|
11
|
+
play(): void;
|
|
12
|
+
stop(): void;
|
|
13
|
+
destroy(): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export class Audio {
|
|
2
|
+
static instance = null;
|
|
3
|
+
options = {
|
|
4
|
+
url: '',
|
|
5
|
+
autoplay: false,
|
|
6
|
+
};
|
|
7
|
+
context = null;
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
Object.assign(this.options, opts);
|
|
10
|
+
this.createContext();
|
|
11
|
+
}
|
|
12
|
+
createContext() {
|
|
13
|
+
this.context = uni.createInnerAudioContext();
|
|
14
|
+
this.context.src = this.options.url;
|
|
15
|
+
this.context.autoplay = this.options.autoplay;
|
|
16
|
+
}
|
|
17
|
+
play() {
|
|
18
|
+
if (this.context) {
|
|
19
|
+
this.context.play();
|
|
20
|
+
if (Audio.instance !== this) {
|
|
21
|
+
if (Audio.instance) {
|
|
22
|
+
Audio.instance.stop();
|
|
23
|
+
}
|
|
24
|
+
Audio.instance = this;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
stop() {
|
|
29
|
+
if (this.context) {
|
|
30
|
+
this.context.pause();
|
|
31
|
+
this.context.seek(0);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
destroy() {
|
|
35
|
+
if (Audio.instance === this) {
|
|
36
|
+
Audio.instance = null;
|
|
37
|
+
}
|
|
38
|
+
if (this.context) {
|
|
39
|
+
this.context.destroy();
|
|
40
|
+
this.context = null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 公共常量
|
|
3
|
+
*/
|
|
4
|
+
export declare const MENU_BUTTON: {
|
|
5
|
+
height: number;
|
|
6
|
+
width: number;
|
|
7
|
+
top: number;
|
|
8
|
+
bottom: number;
|
|
9
|
+
left: number;
|
|
10
|
+
};
|
|
11
|
+
export declare const SYSTEM_INFO: UniApp.GetWindowInfoResult & UniApp.GetDeviceInfoResult & UniApp.GetAppBaseInfoResult;
|
|
12
|
+
export declare const WEBVIEW_ENV: {
|
|
13
|
+
h5: boolean;
|
|
14
|
+
app: boolean;
|
|
15
|
+
mp: boolean;
|
|
16
|
+
};
|
|
17
|
+
export declare const IS_WEBVIEW_ENV: boolean;
|
|
18
|
+
export declare const PLATFORM_CODE: string;
|
|
19
|
+
export declare enum MP_ENV_VERSION {
|
|
20
|
+
release = 0,
|
|
21
|
+
develop = 1,
|
|
22
|
+
trial = 2
|
|
23
|
+
}
|
|
24
|
+
export declare enum CHANNEL {
|
|
25
|
+
患者端 = 1,
|
|
26
|
+
医生端 = 2,
|
|
27
|
+
门店端 = 3
|
|
28
|
+
}
|
|
29
|
+
export declare const LANGUAGE: {
|
|
30
|
+
'zh-Hans': string;
|
|
31
|
+
'zh-Hant': string;
|
|
32
|
+
ja: string;
|
|
33
|
+
ko: string;
|
|
34
|
+
ar: string;
|
|
35
|
+
fa: string;
|
|
36
|
+
th: string;
|
|
37
|
+
vi: string;
|
|
38
|
+
my: string;
|
|
39
|
+
km: string;
|
|
40
|
+
en: string;
|
|
41
|
+
fr: string;
|
|
42
|
+
es: string;
|
|
43
|
+
ru: string;
|
|
44
|
+
pt: string;
|
|
45
|
+
de: string;
|
|
46
|
+
it: string;
|
|
47
|
+
el: string;
|
|
48
|
+
pl: string;
|
|
49
|
+
};
|
|
50
|
+
export declare const TYPES: {
|
|
51
|
+
readonly MESSAGE: {
|
|
52
|
+
readonly LOGIN: "LOGIN";
|
|
53
|
+
readonly SHARE_PAGE: "SHARE_PAGE";
|
|
54
|
+
readonly GET_LOCATION: "GET_LOCATION";
|
|
55
|
+
readonly OPEN_LOCATION: "OPEN_LOCATION";
|
|
56
|
+
readonly CHNAGE_TITLE: "CHNAGE_TITLE";
|
|
57
|
+
readonly SAVE_IMAGE: "SAVE_IMAGE";
|
|
58
|
+
readonly HIDE_LOADING: "HIDE_LOADING";
|
|
59
|
+
readonly WEBVIEW_MESSAGE: "WEBVIEW_MESSAGE";
|
|
60
|
+
readonly APP_RESTART: "APP_RESTART";
|
|
61
|
+
readonly APP_INSTALL: "APP_INSTALL";
|
|
62
|
+
readonly PERM_LOCATION: "PERM_LOCATION";
|
|
63
|
+
readonly PERM_BLUETOOTH: "PERM_BLUETOOTH";
|
|
64
|
+
};
|
|
65
|
+
readonly STORAGE: {
|
|
66
|
+
readonly APP: "app";
|
|
67
|
+
readonly QUERY: "query";
|
|
68
|
+
readonly LOGIN: "login";
|
|
69
|
+
readonly USER: "user";
|
|
70
|
+
readonly OPEN: "open";
|
|
71
|
+
readonly LAUNCH: "launch";
|
|
72
|
+
readonly LOCATION: "location";
|
|
73
|
+
};
|
|
74
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 公共常量
|
|
3
|
+
*/
|
|
4
|
+
import query from './query';
|
|
5
|
+
// 小程序胶囊
|
|
6
|
+
export const MENU_BUTTON = { height: 0, width: 0, top: 0, bottom: 0, left: 0 };
|
|
7
|
+
// #ifdef MP
|
|
8
|
+
Object.assign(MENU_BUTTON, uni.getMenuButtonBoundingClientRect());
|
|
9
|
+
// #endif
|
|
10
|
+
export const SYSTEM_INFO = Object.assign({}, uni.getWindowInfo(), uni.getDeviceInfo(), uni.getAppBaseInfo());
|
|
11
|
+
// #ifdef H5
|
|
12
|
+
const safeTop = Number(query.safeTop);
|
|
13
|
+
const safeBottom = Number(query.safeBottom);
|
|
14
|
+
if (safeTop > 0) {
|
|
15
|
+
SYSTEM_INFO.statusBarHeight = safeTop;
|
|
16
|
+
SYSTEM_INFO.safeAreaInsets.top = safeTop;
|
|
17
|
+
}
|
|
18
|
+
if (safeBottom) {
|
|
19
|
+
SYSTEM_INFO.safeAreaInsets.bottom = safeBottom;
|
|
20
|
+
}
|
|
21
|
+
// #endif
|
|
22
|
+
// webview宿主环境
|
|
23
|
+
export const WEBVIEW_ENV = {
|
|
24
|
+
h5: false,
|
|
25
|
+
app: false,
|
|
26
|
+
mp: false,
|
|
27
|
+
};
|
|
28
|
+
// #ifdef H5
|
|
29
|
+
Object.assign(WEBVIEW_ENV, {
|
|
30
|
+
h5: (query.platform === 'h5' || query.platform === 'web') && window !== parent,
|
|
31
|
+
app: query.platform === 'app',
|
|
32
|
+
mp: window.__wxjs_environment === 'miniprogram' || query.platform === 'mp',
|
|
33
|
+
});
|
|
34
|
+
// #endif
|
|
35
|
+
export const IS_WEBVIEW_ENV = Object.values(WEBVIEW_ENV).some((ele) => ele === true);
|
|
36
|
+
// 平台来源
|
|
37
|
+
export const PLATFORM_CODE = (() => {
|
|
38
|
+
let code = '';
|
|
39
|
+
// #ifdef MP
|
|
40
|
+
code = 'mp';
|
|
41
|
+
// #endif
|
|
42
|
+
// #ifdef H5
|
|
43
|
+
code = window['electron'] ? 'web' : 'h5';
|
|
44
|
+
// #endif
|
|
45
|
+
// #ifdef APP-PLUS
|
|
46
|
+
code = 'app';
|
|
47
|
+
// #endif
|
|
48
|
+
return code;
|
|
49
|
+
})();
|
|
50
|
+
export var MP_ENV_VERSION;
|
|
51
|
+
(function (MP_ENV_VERSION) {
|
|
52
|
+
MP_ENV_VERSION[MP_ENV_VERSION["release"] = 0] = "release";
|
|
53
|
+
MP_ENV_VERSION[MP_ENV_VERSION["develop"] = 1] = "develop";
|
|
54
|
+
MP_ENV_VERSION[MP_ENV_VERSION["trial"] = 2] = "trial";
|
|
55
|
+
})(MP_ENV_VERSION || (MP_ENV_VERSION = {}));
|
|
56
|
+
export var CHANNEL;
|
|
57
|
+
(function (CHANNEL) {
|
|
58
|
+
CHANNEL[CHANNEL["\u60A3\u8005\u7AEF"] = 1] = "\u60A3\u8005\u7AEF";
|
|
59
|
+
CHANNEL[CHANNEL["\u533B\u751F\u7AEF"] = 2] = "\u533B\u751F\u7AEF";
|
|
60
|
+
CHANNEL[CHANNEL["\u95E8\u5E97\u7AEF"] = 3] = "\u95E8\u5E97\u7AEF";
|
|
61
|
+
})(CHANNEL || (CHANNEL = {}));
|
|
62
|
+
export const LANGUAGE = {
|
|
63
|
+
// 亚洲语言
|
|
64
|
+
'zh-Hans': 'zh-CN', // 简体中文
|
|
65
|
+
'zh-Hant': 'zh-TW', // 繁体中文
|
|
66
|
+
ja: 'ja-JP', // 日语
|
|
67
|
+
ko: 'ko-KR', // 韩语
|
|
68
|
+
ar: 'ar-SA', // 阿拉伯语
|
|
69
|
+
fa: 'fa-IR', // 伊朗语(波斯语)
|
|
70
|
+
th: 'th-TH', // 泰语
|
|
71
|
+
vi: 'vi-VN', // 越南语
|
|
72
|
+
my: 'my-MM', // 缅甸语
|
|
73
|
+
km: 'km-KH', // 柬埔寨语
|
|
74
|
+
// 欧洲语言
|
|
75
|
+
en: 'en-US', // 英语
|
|
76
|
+
fr: 'fr-FR', // 法语
|
|
77
|
+
es: 'es-ES', // 西班牙语
|
|
78
|
+
ru: 'ru-RU', // 俄语
|
|
79
|
+
pt: 'pt-PT', // 葡萄牙语
|
|
80
|
+
de: 'de-DE', // 德语
|
|
81
|
+
it: 'it-IT', // 意大利语
|
|
82
|
+
el: 'el-GR', // 希腊语
|
|
83
|
+
pl: 'pl-PL', // 波兰语
|
|
84
|
+
};
|
|
85
|
+
// webview 与宿主环境通信的消息类型
|
|
86
|
+
export const TYPES = {
|
|
87
|
+
MESSAGE: {
|
|
88
|
+
LOGIN: 'LOGIN',
|
|
89
|
+
SHARE_PAGE: 'SHARE_PAGE',
|
|
90
|
+
GET_LOCATION: 'GET_LOCATION',
|
|
91
|
+
OPEN_LOCATION: 'OPEN_LOCATION',
|
|
92
|
+
CHNAGE_TITLE: 'CHNAGE_TITLE',
|
|
93
|
+
SAVE_IMAGE: 'SAVE_IMAGE',
|
|
94
|
+
HIDE_LOADING: 'HIDE_LOADING',
|
|
95
|
+
WEBVIEW_MESSAGE: 'WEBVIEW_MESSAGE',
|
|
96
|
+
APP_RESTART: 'APP_RESTART',
|
|
97
|
+
APP_INSTALL: 'APP_INSTALL',
|
|
98
|
+
PERM_LOCATION: 'PERM_LOCATION',
|
|
99
|
+
PERM_BLUETOOTH: 'PERM_BLUETOOTH',
|
|
100
|
+
},
|
|
101
|
+
STORAGE: {
|
|
102
|
+
APP: 'app',
|
|
103
|
+
QUERY: 'query',
|
|
104
|
+
LOGIN: 'login',
|
|
105
|
+
USER: 'user',
|
|
106
|
+
OPEN: 'open',
|
|
107
|
+
LAUNCH: 'launch',
|
|
108
|
+
LOCATION: 'location',
|
|
109
|
+
},
|
|
110
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { cosCompress } from './index';
|
|
2
|
+
export declare function checkRefreshToken(): Promise<boolean>;
|
|
3
|
+
export interface UploadFileOptions {
|
|
4
|
+
key?: string;
|
|
5
|
+
path?: string;
|
|
6
|
+
/** 获取上传任务实例,可用于 task.abort() 取消上传;token 过期重试时会再次回调 */
|
|
7
|
+
getTask?: ((task: UniNamespace.UploadTask) => void) | ((task: any) => void);
|
|
8
|
+
}
|
|
9
|
+
export declare function uploadFile(file: any, opts?: UploadFileOptions): Promise<{
|
|
10
|
+
url: string;
|
|
11
|
+
}>;
|
|
12
|
+
declare const _default: {
|
|
13
|
+
checkRefreshToken: typeof checkRefreshToken;
|
|
14
|
+
uploadFile: typeof uploadFile;
|
|
15
|
+
compress: typeof cosCompress;
|
|
16
|
+
};
|
|
17
|
+
export default _default;
|
package/lib/utils/cos.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Base64, Hex, HmacSHA1, SHA1, Utf8 } from 'crypto-es';
|
|
2
|
+
import { guid, timeFormat } from '@zfqh/uview-x/utils/function';
|
|
3
|
+
import upload from '../configs/upload';
|
|
4
|
+
import { cosCompress } from './index';
|
|
5
|
+
const data = {};
|
|
6
|
+
let promise = null;
|
|
7
|
+
export async function checkRefreshToken() {
|
|
8
|
+
if (!promise) {
|
|
9
|
+
// 每次上传都会生成token
|
|
10
|
+
// 批量上传一次多张使用一个token
|
|
11
|
+
promise = upload.request({ mode: 'cos' }).then((e) => {
|
|
12
|
+
Object.assign(data, e);
|
|
13
|
+
return true;
|
|
14
|
+
});
|
|
15
|
+
setTimeout(() => {
|
|
16
|
+
promise = null;
|
|
17
|
+
}, 1000);
|
|
18
|
+
}
|
|
19
|
+
return promise;
|
|
20
|
+
}
|
|
21
|
+
class COS {
|
|
22
|
+
async uploadFile(params) {
|
|
23
|
+
const { Bucket, Region, Key, FilePath, File } = params;
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const cosHost = `${Bucket}.cos.${Region}.myqcloud.com`;
|
|
26
|
+
const now = data.create_time || Math.round(Date.now() / 1000);
|
|
27
|
+
const exp = now + 1800; // 30分钟过期
|
|
28
|
+
const qKeyTime = now + ';' + exp;
|
|
29
|
+
const qSignAlgorithm = 'sha1';
|
|
30
|
+
const qAk = data.secret_id;
|
|
31
|
+
const policyData = {
|
|
32
|
+
expiration: new Date(exp * 1000).toISOString(),
|
|
33
|
+
conditions: [
|
|
34
|
+
{ 'q-sign-algorithm': qSignAlgorithm },
|
|
35
|
+
{ 'q-ak': qAk },
|
|
36
|
+
{ 'q-sign-time': qKeyTime },
|
|
37
|
+
{ bucket: Bucket },
|
|
38
|
+
{ key: Key },
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
const policy = JSON.stringify(policyData);
|
|
42
|
+
const signKey = HmacSHA1(qKeyTime, data.secret_key).toString(Hex);
|
|
43
|
+
const stringToSign = SHA1(policy).toString(Hex);
|
|
44
|
+
const qSignature = HmacSHA1(stringToSign, signKey).toString(Hex);
|
|
45
|
+
const FormData = {
|
|
46
|
+
key: Key,
|
|
47
|
+
policy: Base64.stringify(Utf8.parse(policy)),
|
|
48
|
+
success_action_status: 200,
|
|
49
|
+
'x-cos-security-token': data.session_token,
|
|
50
|
+
'q-sign-algorithm': qSignAlgorithm,
|
|
51
|
+
'q-ak': qAk,
|
|
52
|
+
'q-key-time': qKeyTime,
|
|
53
|
+
'q-signature': qSignature,
|
|
54
|
+
};
|
|
55
|
+
const task = uni.uploadFile({
|
|
56
|
+
url: `https://${cosHost}`,
|
|
57
|
+
filePath: FilePath,
|
|
58
|
+
file: File,
|
|
59
|
+
name: 'file',
|
|
60
|
+
formData: FormData,
|
|
61
|
+
success: (res) => {
|
|
62
|
+
if (res.statusCode === 200) {
|
|
63
|
+
resolve({ Location: `${cosHost}/${Key}` });
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const { data: e, statusCode } = res;
|
|
67
|
+
const codeMatch = e.match(/<Code>([^<>]+)<\/Code>/);
|
|
68
|
+
if (codeMatch && codeMatch[1] === 'ExpiredToken' && data.expire_time !== 0) {
|
|
69
|
+
data.expire_time = 0;
|
|
70
|
+
checkRefreshToken()
|
|
71
|
+
.then(() => {
|
|
72
|
+
this.uploadFile(params)
|
|
73
|
+
.then((e) => {
|
|
74
|
+
resolve(e);
|
|
75
|
+
})
|
|
76
|
+
.catch((e) => {
|
|
77
|
+
reject(e);
|
|
78
|
+
});
|
|
79
|
+
})
|
|
80
|
+
.catch((e) => {
|
|
81
|
+
reject(e);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const err = {
|
|
86
|
+
code: statusCode,
|
|
87
|
+
msg: 'Upload Fail',
|
|
88
|
+
};
|
|
89
|
+
const msgMatch = e.match(/<Message>([^<>]+)<\/Message>/);
|
|
90
|
+
if (msgMatch) {
|
|
91
|
+
err.msg = msgMatch[1];
|
|
92
|
+
}
|
|
93
|
+
reject(err);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
fail: (err) => {
|
|
98
|
+
reject({
|
|
99
|
+
msg: err.errMsg || err.message || err.msg,
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
params.getTask?.(task);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const cos = new COS();
|
|
108
|
+
export async function uploadFile(file, opts = {}) {
|
|
109
|
+
let ext = '.png';
|
|
110
|
+
if (file.name) {
|
|
111
|
+
const match = file.name.match(/\.\w+$/);
|
|
112
|
+
if (match) {
|
|
113
|
+
ext = match[0];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const [day, clock] = timeFormat(new Date().getTime(), 'yyyymmdd hhMMss').split(' ');
|
|
117
|
+
const key = `${opts.path || ''}${day}/${clock}${guid(30)}${ext}`;
|
|
118
|
+
await checkRefreshToken();
|
|
119
|
+
const params = {
|
|
120
|
+
Bucket: data.bucket,
|
|
121
|
+
Region: data.region,
|
|
122
|
+
Key: opts.key || key,
|
|
123
|
+
getTask: opts.getTask,
|
|
124
|
+
};
|
|
125
|
+
if (file.path) {
|
|
126
|
+
params.FilePath = file.path;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
params.File = file;
|
|
130
|
+
}
|
|
131
|
+
const e = await cos.uploadFile(params);
|
|
132
|
+
const { Location } = e;
|
|
133
|
+
return {
|
|
134
|
+
url: `https://${Location}`,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
export default {
|
|
138
|
+
checkRefreshToken,
|
|
139
|
+
uploadFile,
|
|
140
|
+
compress: cosCompress,
|
|
141
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 图片地址转换为base64格式图片
|
|
3
|
+
* @param {string} url 图片地址 网络地址 本地相对路径
|
|
4
|
+
* @param {string} type base64图片类型 默认webp
|
|
5
|
+
*/
|
|
6
|
+
export declare function urlToBase64(url: any, type?: string): any;
|
|
7
|
+
/**
|
|
8
|
+
* dataUrl转base64
|
|
9
|
+
* @param {String} url url值
|
|
10
|
+
*/
|
|
11
|
+
export declare function dataUrlToBase64(url?: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* base64转文件对象
|
|
14
|
+
* @param {String} base64 base64值
|
|
15
|
+
* @param {String} filename 文件名
|
|
16
|
+
*/
|
|
17
|
+
export declare function base64ToFile(base64?: string, filename?: string): File;
|
|
18
|
+
/**
|
|
19
|
+
* path转base64
|
|
20
|
+
* @param {String} path 文件本地路径
|
|
21
|
+
*/
|
|
22
|
+
export declare function pathToBase64(path: string): Promise<unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* base64转本地path
|
|
25
|
+
* @param {String} base64 base64值
|
|
26
|
+
* @param {String} filename 文件名
|
|
27
|
+
*/
|
|
28
|
+
export declare function base64ToPath(base64?: string, filename?: string): Promise<string>;
|