@hzab/utils 1.0.11 → 1.0.13

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 CHANGED
@@ -1,3 +1,11 @@
1
+ # @hzab/utils@1.0.13
2
+
3
+ feat: upload axios ts
4
+
5
+ # @hzab/utils@1.0.12
6
+
7
+ feat: 去除省市区行政校验
8
+
1
9
  # @hzab/utils@1.0.11
2
10
 
3
11
  feat: 身份证校验合法性
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@hzab/utils",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "utils",
5
5
  "main": "src",
6
6
  "scripts": {
7
7
  "dev": "webpack serve -c ./config/webpack.config.js --env local",
8
8
  "build": "webpack -c ./config/webpack.config.js --env production",
9
- "publish-beta": "npm publish --beta",
10
- "publish-patch": "npm version patch && npm publish --access public",
11
- "publish-minor": "npm version minor && npm publish --access public",
12
- "publish-major": "npm version major && npm publish --access public",
9
+ "version:alpha": "npm version prerelease --preid=alpha",
10
+ "publish:alpha": "npm publish --tag alpha",
11
+ "version:beta": "npm version prerelease --preid=beta",
12
+ "publish:beta": "npm publish --tag beta",
13
+ "version:patch": "npm version patch",
14
+ "version:minior": "npm version minor",
15
+ "version:major": "npm version major",
16
+ "publish:stable": "npm publish --access public",
13
17
  "prepare": "husky install",
14
18
  "mac-chmod": "chmod +x .husky && chmod +x .husky/pre-commit",
15
19
  "docs": "typedoc"
@@ -32,7 +32,7 @@ export function isValidIdCard(idCard, opt?) {
32
32
  }
33
33
 
34
34
  // 校验地址码(省级+市级)
35
- if (!isValidAddressCode(id18)) return false;
35
+ // if (!isValidAddressCode(id18)) return false;
36
36
 
37
37
  // 校验出生日期是否在合理范围内(1900年-当前年份)
38
38
  if (opt?.validBirthDate && !isValidBirthDate(id18)) return false;
@@ -45,20 +45,20 @@ export function isValidIdCard(idCard, opt?) {
45
45
  * 校验地址码(前6位)
46
46
  * 规则校验:省级代码合法 + 市级代码格式合法
47
47
  */
48
- export function isValidAddressCode(id18) {
49
- const addrCode = id18.substring(0, 6);
48
+ // export function isValidAddressCode(id18) {
49
+ // const addrCode = id18.substring(0, 6);
50
50
 
51
- const provinceCode = addrCode.substring(0, 2);
52
- const cityCode = addrCode.substring(2, 4);
51
+ // const provinceCode = addrCode.substring(0, 2);
52
+ // const cityCode = addrCode.substring(2, 4);
53
53
 
54
- // 1. 校验省级代码(第1-2位)
55
- if (!isValidProvinceCode(provinceCode)) return false;
54
+ // // 1. 校验省级代码(第1-2位)
55
+ // if (!isValidProvinceCode(provinceCode)) return false;
56
56
 
57
- // 2. 校验市级代码(第3-4位)
58
- if (!isValidCityCode(cityCode)) return false;
57
+ // // 2. 校验市级代码(第3-4位)
58
+ // if (!isValidCityCode(cityCode)) return false;
59
59
 
60
- return true;
61
- }
60
+ // return true;
61
+ // }
62
62
 
63
63
  /**
64
64
  * 校验省级代码(第1-2位)
@@ -1,4 +1,5 @@
1
1
  import dayjs from "dayjs";
2
+ import { Axios, AxiosRequestConfig } from "axios";
2
3
 
3
4
  import { axios } from "@hzab/data-model";
4
5
 
@@ -15,11 +16,11 @@ export interface IGetSignatureOpt {
15
16
  /**
16
17
  * axios 实例
17
18
  */
18
- axios?: Object;
19
+ axios?: Axios;
19
20
  /**
20
21
  * axios 配置
21
22
  */
22
- axiosConf?: Object;
23
+ axiosConf?: AxiosRequestConfig;
23
24
  /**
24
25
  * 请求入参
25
26
  */
@@ -32,11 +33,11 @@ export interface IOfflineUploadProps {
32
33
  /**
33
34
  * axios 实例
34
35
  */
35
- axios?: Object;
36
+ axios?: Axios;
36
37
  /**
37
38
  * axios 配置
38
39
  */
39
- axiosConf?: Object;
40
+ axiosConf?: AxiosRequestConfig;
40
41
  /**
41
42
  * 配置接口地址
42
43
  */
@@ -84,11 +85,11 @@ export interface IUploadOpt {
84
85
  /**
85
86
  * axios 实例
86
87
  */
87
- axios?: Object;
88
+ axios?: Axios;
88
89
  /**
89
90
  * axios 配置
90
91
  */
91
- axiosConf?: Object;
92
+ axiosConf?: AxiosRequestConfig;
92
93
  }
93
94
 
94
95
  /**
@@ -141,8 +142,8 @@ export function getSignature(opt: IGetSignatureOpt = {}) {
141
142
  export interface IGetPreviewUrlsOpt {
142
143
  previewUrl?: string;
143
144
  params?: Object;
144
- axios?: Object;
145
- axiosConf?: Object;
145
+ axios?: Axios;
146
+ axiosConf?: AxiosRequestConfig;
146
147
  }
147
148
 
148
149
  /**
@@ -1,206 +1,214 @@
1
- import dayjs from "dayjs";
2
-
3
- import { axios } from "@hzab/data-model";
4
- import { formatDirStr } from "./uploadUtils";
5
- import { getGlobalServeConfig } from "../globalConfig"
6
- const isPublicMap = {
7
- 0: "&",
8
- 1: "?"
9
- }
10
- /**
11
- * getSignature 配置
12
- */
13
- export interface IGetSignatureOpt {
14
- /** 获取配置的接口地址 */
15
- serverUrl?: string;
16
- /** axios 实例 */
17
- axios?: typeof axios;
18
- /** axios 配置参数 */
19
- axiosConf?: Object;
20
- /** 获取配置的接口自定义入参 */
21
- params?: {
22
- /** 文件路径 */
23
- dir?: string;
24
- /** 是否是公开库 */
25
- isPublic?: number;
26
- };
27
- }
28
-
29
- /**
30
- * OssUpload props 参数
31
- */
32
- export interface IOssUploadProps {
33
- /** axios 实例 */
34
- axios?: typeof axios;
35
- /** axios 配置参数 */
36
- axiosConf?: Object;
37
- /** 获取配置的接口地址 */
38
- serverUrl?: string;
39
- /** 获取配置的接口自定义入参 */
40
- signatureParams?: {
41
- /** 文件路径 */
42
- dir?: string;
43
- /** 是否是公开库 */
44
- isPublic?: number;
45
- };
46
- }
47
-
48
- export interface IUploadOpt {
49
- fileName?: string;
50
- /** axios 实例 */
51
- axios?: typeof axios;
52
- /** axios 配置参数 */
53
- axiosConf?: Object;
54
- /** 获取配置的接口地址 */
55
- serverUrl?: string;
56
- /** 获取配置的接口自定义入参 */
57
- params?: {
58
- /** 文件路径 */
59
- dir?: string;
60
- /** 是否是公开库 */
61
- isPublic?: number;
62
- };
63
- /**
64
- * 获取配置接口请求入参
65
- */
66
- signatureParams?: {
67
- /** 公开还是私有 */
68
- isPublic?: number;
69
- };
70
- /** 文件上传的接口自定义入参 */
71
- ossParams?: {};
72
- /** 是否使用 hash 文件名 */
73
- useHashName?: boolean;
74
- /**缩略图参数 */
75
- thumbnailParams?: string;
76
- }
77
-
78
- export function getSignature(opt: IGetSignatureOpt = {}) {
79
- const { serverUrl = getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig" } = opt;
80
- // 减 10 秒,避免发起请求时 刚好过期的情况
81
- if (
82
- window._$ossSignatureRes &&
83
- serverUrl === window._$ossSignatureRes.serverUrl &&
84
- dayjs().valueOf() - window._$ossSignatureRes.__saveTime < window._$ossSignatureRes.expireTimeMilles - 10000
85
- ) {
86
- return Promise.resolve(window._$ossSignatureRes);
87
- }
88
- const { axios: _ax = axios, params = {}, axiosConf } = opt;
89
- // 处理 dir 格式,必须为非 / 开头, / 结尾。如: test/
90
- params.dir = formatDirStr(params.dir);
91
-
92
- return _ax
93
- .get(serverUrl, {
94
- ...axiosConf,
95
- params: {
96
- isPublic: 1,
97
- ...params,
98
- },
99
- })
100
- .then((res) => {
101
- window._$ossSignatureRes = res?.data?.data ?? res?.data ?? res;
102
- if (window._$ossSignatureRes) {
103
- window._$ossSignatureRes.__saveTime = dayjs().valueOf();
104
- window._$ossSignatureRes.serverUrl = serverUrl;
105
- }
106
- return window._$ossSignatureRes;
107
- });
108
- }
109
-
110
- /**
111
- * oss 上传工具类
112
- */
113
- class OssUploadUtil {
114
- /** axios 实例 */
115
- axios: typeof axios;
116
- /** axios 配置参数 */
117
- axiosConf: Object;
118
- /** 获取配置的接口地址 */
119
- serverUrl: string;
120
- /** 获取配置的接口自定义入参 */
121
- signatureParams?: {
122
- /** 文件路径 */
123
- dir?: string;
124
- /** 是否是公开库 */
125
- isPublic?: number;
126
- };
127
-
128
- constructor(props: IOssUploadProps = {}) {
129
- this.axios = props.axios || axios;
130
- this.axiosConf = props.axiosConf || {};
131
- this.serverUrl = props.serverUrl || getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig";
132
- this.signatureParams = props.signatureParams || {};
133
- }
134
-
135
- getSignature(serverUrl = this.serverUrl, opt) {
136
- return this._getSignature(serverUrl, opt);
137
- }
138
-
139
- upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
140
- return this._upload(file, { ...opt });
141
- }
142
-
143
- _getSignature(serverUrl = this.serverUrl, opt) {
144
- return getSignature({
145
- ...opt,
146
- serverUrl,
147
- axios: opt?.axios || this.axios,
148
- axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
149
- });
150
- }
151
-
152
- _upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
153
- return new Promise(async (resolve, reject) => {
154
- const ossParams = await this._getSignature(opt.serverUrl || this.serverUrl, {
155
- ...opt,
156
- params: { ...this.signatureParams, ...opt.params },
157
- });
158
-
159
- const { ossParams: propOssParams, fileName } = opt || {};
160
- const formData = new FormData();
161
- // key 表示上传到 Bucket 内的 Object 的完整路径,例如 exampledir/exampleobject.txtObject,完整路径中不能包含 Bucket 名称。
162
- // filename 表示待上传的本地文件名称。
163
- const filename = fileName || file.name;
164
- const key = `${ossParams?.dir}${filename}`;
165
- formData.set("key", key);
166
- formData.set("OSSAccessKeyId", ossParams.accessid);
167
- formData.set("policy", ossParams.policy);
168
- formData.set("Signature", ossParams.signature);
169
- if (ossParams.callback) {
170
- formData.set("callback", ossParams.callback);
171
- }
172
- // @ts-ignore
173
- formData.set("success_action_status", 200);
174
- formData.set("file", file);
175
-
176
- if (propOssParams) {
177
- for (const key in propOssParams) {
178
- if (Object.hasOwnProperty.call(propOssParams, key)) {
179
- formData.set(key, propOssParams[key]);
180
- }
181
- }
182
- }
183
-
184
- const _axios = opt?.axios || this.axios;
185
-
186
- return _axios
187
- .post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
188
- .then((res) => {
189
- if (getGlobalServeConfig()?.showThumbnail) {
190
- res.data.data.thumbnailUrl = `${res?.data?.data?.fileUrl}${opt?.thumbnailParams ? isPublicMap[opt?.signatureParams?.isPublic] + opt?.thumbnailParams : ""}`;
191
- }
192
- resolve(res);
193
- return res;
194
- })
195
- .catch((err) => {
196
- console.error("oss upload err", err);
197
- reject(err);
198
- return Promise.reject(err);
199
- });
200
- });
201
- }
202
- }
203
-
204
- export { axios };
205
-
206
- export default OssUploadUtil;
1
+ import dayjs from "dayjs";
2
+ import { Axios, AxiosResponse, AxiosRequestConfig } from "axios";
3
+
4
+ import { axios } from "@hzab/data-model";
5
+ import { formatDirStr } from "./uploadUtils";
6
+ import { getGlobalServeConfig } from "../globalConfig";
7
+ const isPublicMap = {
8
+ 0: "&",
9
+ 1: "?",
10
+ };
11
+ /**
12
+ * getSignature 配置
13
+ */
14
+ export interface IGetSignatureOpt {
15
+ /** 获取配置的接口地址 */
16
+ serverUrl?: string;
17
+ /** axios 实例 */
18
+ axios?: Axios;
19
+ /** axios 配置参数 */
20
+ axiosConf?: AxiosRequestConfig;
21
+ /** 获取配置的接口自定义入参 */
22
+ params?: {
23
+ /** 文件路径 */
24
+ dir?: string;
25
+ /** 是否是公开库 */
26
+ isPublic?: number;
27
+ };
28
+ }
29
+
30
+ /**
31
+ * OssUpload props 参数
32
+ */
33
+ export interface IOssUploadProps {
34
+ /** axios 实例 */
35
+ axios?: Axios;
36
+ /** axios 配置参数 */
37
+ axiosConf?: AxiosRequestConfig;
38
+ /** 获取配置的接口地址 */
39
+ serverUrl?: string;
40
+ /** 获取配置的接口自定义入参 */
41
+ signatureParams?: {
42
+ /** 文件路径 */
43
+ dir?: string;
44
+ /** 是否是公开库 */
45
+ isPublic?: number;
46
+ };
47
+ }
48
+
49
+ export interface IUploadOpt {
50
+ fileName?: string;
51
+ /** axios 实例 */
52
+ axios?: Axios;
53
+ /** axios 配置参数 */
54
+ axiosConf?: AxiosRequestConfig;
55
+ /** 获取配置的接口地址 */
56
+ serverUrl?: string;
57
+ /** 获取配置的接口自定义入参 */
58
+ params?: {
59
+ /** 文件路径 */
60
+ dir?: string;
61
+ /** 是否是公开库 */
62
+ isPublic?: number;
63
+ };
64
+ /**
65
+ * 获取配置接口请求入参
66
+ */
67
+ signatureParams?: {
68
+ /** 公开还是私有 */
69
+ isPublic?: number;
70
+ };
71
+ /** 文件上传的接口自定义入参 */
72
+ ossParams?: {};
73
+ /** 是否使用 hash 文件名 */
74
+ useHashName?: boolean;
75
+ /**缩略图参数 */
76
+ thumbnailParams?: string;
77
+ }
78
+
79
+ export interface ResponseData extends AxiosResponse {
80
+ fileUrl: string;
81
+ thumbnailUrl?: string;
82
+ }
83
+
84
+ export function getSignature(opt: IGetSignatureOpt = {}) {
85
+ const { serverUrl = getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig" } = opt;
86
+ // 减 10 秒,避免发起请求时 刚好过期的情况
87
+ if (
88
+ window._$ossSignatureRes &&
89
+ serverUrl === window._$ossSignatureRes.serverUrl &&
90
+ dayjs().valueOf() - window._$ossSignatureRes.__saveTime < window._$ossSignatureRes.expireTimeMilles - 10000
91
+ ) {
92
+ return Promise.resolve(window._$ossSignatureRes);
93
+ }
94
+ const { axios: _ax = axios, params = {}, axiosConf } = opt;
95
+ // 处理 dir 格式,必须为非 / 开头, / 结尾。如: test/
96
+ params.dir = formatDirStr(params.dir);
97
+
98
+ return _ax
99
+ .get(serverUrl, {
100
+ ...axiosConf,
101
+ params: {
102
+ isPublic: 1,
103
+ ...params,
104
+ },
105
+ })
106
+ .then((res) => {
107
+ window._$ossSignatureRes = res?.data?.data ?? res?.data ?? res;
108
+ if (window._$ossSignatureRes) {
109
+ window._$ossSignatureRes.__saveTime = dayjs().valueOf();
110
+ window._$ossSignatureRes.serverUrl = serverUrl;
111
+ }
112
+ return window._$ossSignatureRes;
113
+ });
114
+ }
115
+
116
+ /**
117
+ * oss 上传工具类
118
+ */
119
+ class OssUploadUtil {
120
+ /** axios 实例 */
121
+ axios: Axios;
122
+ /** axios 配置参数 */
123
+ axiosConf: AxiosRequestConfig;
124
+ /** 获取配置的接口地址 */
125
+ serverUrl: string;
126
+ /** 获取配置的接口自定义入参 */
127
+ signatureParams?: {
128
+ /** 文件路径 */
129
+ dir?: string;
130
+ /** 是否是公开库 */
131
+ isPublic?: number;
132
+ };
133
+
134
+ constructor(props: IOssUploadProps = {}) {
135
+ this.axios = props.axios || axios;
136
+ this.axiosConf = props.axiosConf || {};
137
+ this.serverUrl = props.serverUrl || getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig";
138
+ this.signatureParams = props.signatureParams || {};
139
+ }
140
+
141
+ getSignature(serverUrl = this.serverUrl, opt) {
142
+ return this._getSignature(serverUrl, opt);
143
+ }
144
+
145
+ upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
146
+ return this._upload(file, { ...opt });
147
+ }
148
+
149
+ _getSignature(serverUrl = this.serverUrl, opt) {
150
+ return getSignature({
151
+ ...opt,
152
+ serverUrl,
153
+ axios: opt?.axios || this.axios,
154
+ axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
155
+ });
156
+ }
157
+
158
+ _upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
159
+ return new Promise(async (resolve, reject) => {
160
+ const ossParams = await this._getSignature(opt.serverUrl || this.serverUrl, {
161
+ ...opt,
162
+ params: { ...this.signatureParams, ...opt.params },
163
+ });
164
+
165
+ const { ossParams: propOssParams, fileName } = opt || {};
166
+ const formData = new FormData();
167
+ // key 表示上传到 Bucket 内的 Object 的完整路径,例如 exampledir/exampleobject.txtObject,完整路径中不能包含 Bucket 名称。
168
+ // filename 表示待上传的本地文件名称。
169
+ const filename = fileName || file.name;
170
+ const key = `${ossParams?.dir}${filename}`;
171
+ formData.set("key", key);
172
+ formData.set("OSSAccessKeyId", ossParams.accessid);
173
+ formData.set("policy", ossParams.policy);
174
+ formData.set("Signature", ossParams.signature);
175
+ if (ossParams.callback) {
176
+ formData.set("callback", ossParams.callback);
177
+ }
178
+ // @ts-ignore
179
+ formData.set("success_action_status", 200);
180
+ formData.set("file", file);
181
+
182
+ if (propOssParams) {
183
+ for (const key in propOssParams) {
184
+ if (Object.hasOwnProperty.call(propOssParams, key)) {
185
+ formData.set(key, propOssParams[key]);
186
+ }
187
+ }
188
+ }
189
+
190
+ const _axios = opt?.axios || this.axios;
191
+
192
+ return _axios
193
+ .post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
194
+ .then((res: ResponseData) => {
195
+ if (getGlobalServeConfig()?.showThumbnail) {
196
+ res.data.data.thumbnailUrl = `${res?.data?.data?.fileUrl}${
197
+ opt?.thumbnailParams ? isPublicMap[opt?.signatureParams?.isPublic] + opt?.thumbnailParams : ""
198
+ }`;
199
+ }
200
+ resolve(res);
201
+ return res;
202
+ })
203
+ .catch((err) => {
204
+ console.error("oss upload err", err);
205
+ reject(err);
206
+ return Promise.reject(err);
207
+ });
208
+ });
209
+ }
210
+ }
211
+
212
+ export { axios };
213
+
214
+ export default OssUploadUtil;