zmdms-utils 0.0.65 → 0.0.67

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.
@@ -25,5 +25,7 @@ var TOKEN_KEY = window.__TOKEN_KEY__ || "Cicoms-Auth";
25
25
  // 基础前缀
26
26
  // export const BASIC_KEY = "zmdms";
27
27
  var BASIC_KEY = window.__BASIC_KEY__ || "cicoms";
28
+ // 附件相关服务
29
+ var FILE_SERVICE = window.__FILE_SERVICE__ || "cicoms";
28
30
 
29
- export { BASIC_KEY, EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE, PERSONNEL_CENTER_APPNAME, SALARY_CENTER_APPNAME, TOKEN_KEY };
31
+ export { BASIC_KEY, EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE, FILE_SERVICE, PERSONNEL_CENTER_APPNAME, SALARY_CENTER_APPNAME, TOKEN_KEY };
package/dist/es/file.js CHANGED
@@ -1,10 +1,13 @@
1
1
  import { getToken } from './auth.js';
2
- import { dangerouslyXss, specialString, getFileExtension } from './common.js';
3
- import { BASIC_KEY, TOKEN_KEY } from './constants.js';
2
+ import { dangerouslyXss, specialString, getFileExtension, isImageExtension } from './common.js';
3
+ import { BASIC_KEY, TOKEN_KEY, FILE_SERVICE } from './constants.js';
4
+ import { Crypto } from './crypto.js';
4
5
 
5
6
  /**
6
7
  * 附件相关接口
7
8
  */
9
+ // 是否是中拓的附件服务
10
+ var isZmdmsFileService = FILE_SERVICE === "zmdms";
8
11
  /**
9
12
  * 附件下载方法
10
13
  * @param fileId 附件id
@@ -17,6 +20,9 @@ import { BASIC_KEY, TOKEN_KEY } from './constants.js';
17
20
  */
18
21
  function downloadFile(fileId, fileName, options) {
19
22
  var API_BASEURL = options.API_BASEURL, authToken = options.authToken, _a = options.waterMark, waterMark = _a === void 0 ? true : _a, open = options.open;
23
+ if (isZmdmsFileService) {
24
+ fileId = encodeFileId(fileId, 2);
25
+ }
20
26
  return new Promise(function (resolve, reject) {
21
27
  var xhr = new XMLHttpRequest();
22
28
  // 获取请求基础数据
@@ -95,6 +101,9 @@ function batchDownloadFiles(fileList, options) {
95
101
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
96
102
  var fileIds = (_c = (_b = (_a = fileList === null || fileList === void 0 ? void 0 : fileList.map) === null || _a === void 0 ? void 0 : _a.call(fileList, function (item) { return item.attachId; })) === null || _b === void 0 ? void 0 : _b.join) === null || _c === void 0 ? void 0 : _c.call(_b, ",");
97
103
  var fileNames = (_f = (_e = (_d = fileList === null || fileList === void 0 ? void 0 : fileList.map) === null || _d === void 0 ? void 0 : _d.call(fileList, function (item) { return item.attachName; })) === null || _e === void 0 ? void 0 : _e.join) === null || _f === void 0 ? void 0 : _f.call(_e, ",");
104
+ if (isZmdmsFileService) {
105
+ fileIds = encodeFileId(fileIds, 2);
106
+ }
98
107
  xhr.send("attachIds=".concat(fileIds, "&fileNames=").concat(fileNames, "&zipName=").concat(zipName, "&addWaterMark=").concat(waterMark));
99
108
  });
100
109
  }
@@ -109,7 +118,7 @@ function batchDownloadFiles(fileList, options) {
109
118
  * @param options.fileList { attachId: string, attachName: string ... }[] 附件列表。批量预览时有用
110
119
  */
111
120
  function previewFile(fileId, fileName, options) {
112
- var API_BASEURL = options.API_BASEURL, authToken = options.authToken; options.fileList;
121
+ var API_BASEURL = options.API_BASEURL, authToken = options.authToken, _a = options.fileList, fileList = _a === void 0 ? [] : _a;
113
122
  // 获取请求基础数据
114
123
  var _b = getBasicInfo(API_BASEURL, authToken), token = _b.token, apiBaseURL = _b.apiBaseURL;
115
124
  // 处理附件名中的一些特殊字符
@@ -126,34 +135,39 @@ function previewFile(fileId, fileName, options) {
126
135
  // 如果文件名没变,文件内容改变。这样就会导致再次预览时 内容不会更新
127
136
  // 所以将文件名 与 文件id组合来生成不同的文件名
128
137
  previewFileName = "".concat(filePreName, "-").concat(fileId, ".").concat(fileExtension);
129
- // 2023-12-30 修改成数字中心预览
130
- window.open("".concat(apiBaseURL, "/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=").concat(fileId, "&").concat(TOKEN_KEY, "=").concat(token));
138
+ if (!isZmdmsFileService) {
139
+ // 2023-12-30 修改成数字中心预览
140
+ window.open("".concat(apiBaseURL, "/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=").concat(fileId, "&").concat(TOKEN_KEY, "=").concat(token));
141
+ return;
142
+ }
131
143
  // 如果是图片 走批量预览接口
132
- // const isImage = isImageExtension(fileExtension);
133
- // if (isImage) {
134
- // const imgFiles = fileList.filter((item) => {
135
- // const { attachName, attachId } = item;
136
- // const [, fileExtension] = getFileExtension(attachName);
137
- // if (isImageExtension(fileExtension) && attachId !== fileId) {
138
- // return true;
139
- // }
140
- // return false;
141
- // });
142
- // let attachIds = imgFiles.map((item) => item.attachId).join(",");
143
- // // 批量图片预览
144
- // window.open(
145
- // `${apiBaseURL}/picturesAttchPreview?attachIds=${
146
- // attachIds ? `${fileId},${attachIds}` : fileId
147
- // }&${TOKEN_KEY}=${token}` +
148
- // (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
149
- // );
150
- // } else {
151
- // // 普通预览
152
- // window.open(
153
- // `${apiBaseURL}/attchPreview?attachId=${fileId}&${TOKEN_KEY}=${token}&officePreviewType=pdf` +
154
- // (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
155
- // );
156
- // }
144
+ var isImage = isImageExtension(fileExtension);
145
+ if (isImage) {
146
+ var imgFiles = fileList.filter(function (item) {
147
+ var attachName = item.attachName, attachId = item.attachId;
148
+ var _a = getFileExtension(attachName), fileExtension = _a[1];
149
+ if (isImageExtension(fileExtension) && attachId !== fileId) {
150
+ return true;
151
+ }
152
+ return false;
153
+ });
154
+ var attachIds = imgFiles.map(function (item) { return item.attachId; }).join(",");
155
+ var fileIds = attachIds ? "".concat(fileId, ",").concat(attachIds) : fileId;
156
+ if (isZmdmsFileService) {
157
+ fileIds = encodeFileId(fileIds, 1);
158
+ }
159
+ // 批量图片预览
160
+ window.open("".concat(apiBaseURL, "/picturesAttchPreview?attachIds=").concat(fileIds, "&").concat(TOKEN_KEY, "=").concat(token) +
161
+ (fileName ? "&titleName=".concat(encodeURIComponent(previewFileName)) : ""));
162
+ }
163
+ else {
164
+ if (isZmdmsFileService) {
165
+ fileId = encodeFileId(fileId, 1);
166
+ }
167
+ // 普通预览
168
+ window.open("".concat(apiBaseURL, "/attchPreview?attachId=").concat(fileId, "&").concat(TOKEN_KEY, "=").concat(token, "&officePreviewType=pdf") +
169
+ (fileName ? "&titleName=".concat(encodeURIComponent(previewFileName)) : ""));
170
+ }
157
171
  }
158
172
  /**
159
173
  * 导出一个文件流
@@ -238,6 +252,9 @@ function createDownloadLink(fileId, options) {
238
252
  if (options === void 0) { options = {}; }
239
253
  var API_BASEURL = options.API_BASEURL, authToken = options.authToken, open = options.open;
240
254
  var _a = getBasicInfo(API_BASEURL || "", authToken), token = _a.token, apiBaseURL = _a.apiBaseURL;
255
+ if (isZmdmsFileService) {
256
+ fileId = encodeFileId(fileId, 2);
257
+ }
241
258
  if (open) {
242
259
  return "".concat(apiBaseURL, "/api/").concat(BASIC_KEY, "-resource/oss/endpoint/public/download-file/").concat(fileId);
243
260
  }
@@ -272,11 +289,14 @@ function createOriginalLink(fileId, fileName, options) {
272
289
  // 所以将文件名 与 文件id组合来生成不同的文件名
273
290
  previewFileName = "".concat(filePreName, "-").concat(fileId, ".").concat(fileExtension);
274
291
  // 2023-12-30 替换成数字中心的 预览接口
275
- return "".concat(apiBaseURL, "/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=").concat(fileId, "&").concat(TOKEN_KEY, "=").concat(token);
276
- // return (
277
- // `${apiBaseURL}/attchPreview?attachId=${fileId}&${TOKEN_KEY}=${token}&officePreviewType=pdf` +
278
- // (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
279
- // );
292
+ if (!isZmdmsFileService) {
293
+ return "".concat(apiBaseURL, "/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=").concat(fileId, "&").concat(TOKEN_KEY, "=").concat(token);
294
+ }
295
+ if (isZmdmsFileService) {
296
+ fileId = encodeFileId(fileId, 2);
297
+ }
298
+ return ("".concat(apiBaseURL, "/attchPreview?attachId=").concat(fileId, "&").concat(TOKEN_KEY, "=").concat(token, "&officePreviewType=pdf") +
299
+ (fileName ? "&titleName=".concat(encodeURIComponent(previewFileName)) : ""));
280
300
  }
281
301
  /**
282
302
  * 生成缩略图路径
@@ -333,5 +353,21 @@ function getBasicInfo(baseURL, token) {
333
353
  : baseURL,
334
354
  };
335
355
  }
356
+ /**
357
+ * 加密附件ID
358
+ * @param fileId 附件ID
359
+ * @description 加密方式
360
+ * 1. 都会通过encrypt加密一次
361
+ * 2. 如果参数是通过地址栏传输的,附件预览:encode一次;附件下载:encode两次;
362
+ * 3. 如果参数是通过body传输的,只需要encode一次
363
+ */
364
+ function encodeFileId(fileId, encodeNum) {
365
+ if (encodeNum === void 0) { encodeNum = 1; }
366
+ var crypto = new Crypto(window.__CRYPTO_AES_KEY__, window.__CRYPTO_DES_KEY__);
367
+ if (encodeNum === 1) {
368
+ return encodeURIComponent(crypto.encrypt(fileId));
369
+ }
370
+ return encodeURIComponent(encodeURIComponent(crypto.encrypt(fileId)));
371
+ }
336
372
 
337
- export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile };
373
+ export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, encodeFileId, exportBolb, previewFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zmdms-utils",
3
- "version": "0.0.65",
3
+ "version": "0.0.67",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
package/src/constants.ts CHANGED
@@ -31,3 +31,6 @@ export const TOKEN_KEY = (window as any).__TOKEN_KEY__ || "Cicoms-Auth";
31
31
  // 基础前缀
32
32
  // export const BASIC_KEY = "zmdms";
33
33
  export const BASIC_KEY = (window as any).__BASIC_KEY__ || "cicoms";
34
+
35
+ // 附件相关服务
36
+ export const FILE_SERVICE = (window as any).__FILE_SERVICE__ || "cicoms";
package/src/file.ts CHANGED
@@ -8,8 +8,12 @@ import {
8
8
  getFileExtension,
9
9
  isImageExtension,
10
10
  } from "./common";
11
- import { TOKEN_KEY, BASIC_KEY } from "./constants";
11
+ import { TOKEN_KEY, BASIC_KEY, FILE_SERVICE } from "./constants";
12
12
  import { AxiosResponse } from "axios";
13
+ import { Crypto } from "./crypto";
14
+
15
+ // 是否是中拓的附件服务
16
+ const isZmdmsFileService = FILE_SERVICE === "zmdms";
13
17
 
14
18
  /**
15
19
  * 附件下载方法
@@ -27,6 +31,9 @@ export function downloadFile(
27
31
  options: IDownloadOptions
28
32
  ) {
29
33
  const { API_BASEURL, authToken, waterMark = true, open } = options;
34
+ if (isZmdmsFileService) {
35
+ fileId = encodeFileId(fileId, 2);
36
+ }
30
37
  return new Promise((resolve, reject) => {
31
38
  const xhr = new XMLHttpRequest();
32
39
  // 获取请求基础数据
@@ -129,8 +136,11 @@ export function batchDownloadFiles(
129
136
  };
130
137
  xhr.setRequestHeader(TOKEN_KEY, token || "");
131
138
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
132
- const fileIds = fileList?.map?.((item) => item.attachId)?.join?.(",");
139
+ let fileIds = fileList?.map?.((item) => item.attachId)?.join?.(",");
133
140
  const fileNames = fileList?.map?.((item) => item.attachName)?.join?.(",");
141
+ if (isZmdmsFileService) {
142
+ fileIds = encodeFileId(fileIds, 2);
143
+ }
134
144
  xhr.send(
135
145
  `attachIds=${fileIds}&fileNames=${fileNames}&zipName=${zipName}&addWaterMark=${waterMark}`
136
146
  );
@@ -169,37 +179,46 @@ export function previewFile(
169
179
  // 所以将文件名 与 文件id组合来生成不同的文件名
170
180
  previewFileName = `${filePreName}-${fileId}.${fileExtension}`;
171
181
 
172
- // 2023-12-30 修改成数字中心预览
173
- window.open(
174
- `${apiBaseURL}/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=${fileId}&${TOKEN_KEY}=${token}`
175
- );
182
+ if (!isZmdmsFileService) {
183
+ // 2023-12-30 修改成数字中心预览
184
+ window.open(
185
+ `${apiBaseURL}/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=${fileId}&${TOKEN_KEY}=${token}`
186
+ );
187
+ return;
188
+ }
176
189
 
177
190
  // 如果是图片 走批量预览接口
178
- // const isImage = isImageExtension(fileExtension);
179
- // if (isImage) {
180
- // const imgFiles = fileList.filter((item) => {
181
- // const { attachName, attachId } = item;
182
- // const [, fileExtension] = getFileExtension(attachName);
183
- // if (isImageExtension(fileExtension) && attachId !== fileId) {
184
- // return true;
185
- // }
186
- // return false;
187
- // });
188
- // let attachIds = imgFiles.map((item) => item.attachId).join(",");
189
- // // 批量图片预览
190
- // window.open(
191
- // `${apiBaseURL}/picturesAttchPreview?attachIds=${
192
- // attachIds ? `${fileId},${attachIds}` : fileId
193
- // }&${TOKEN_KEY}=${token}` +
194
- // (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
195
- // );
196
- // } else {
197
- // // 普通预览
198
- // window.open(
199
- // `${apiBaseURL}/attchPreview?attachId=${fileId}&${TOKEN_KEY}=${token}&officePreviewType=pdf` +
200
- // (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
201
- // );
202
- // }
191
+
192
+ const isImage = isImageExtension(fileExtension);
193
+ if (isImage) {
194
+ const imgFiles = fileList.filter((item) => {
195
+ const { attachName, attachId } = item;
196
+ const [, fileExtension] = getFileExtension(attachName);
197
+ if (isImageExtension(fileExtension) && attachId !== fileId) {
198
+ return true;
199
+ }
200
+ return false;
201
+ });
202
+ let attachIds = imgFiles.map((item) => item.attachId).join(",");
203
+ let fileIds = attachIds ? `${fileId},${attachIds}` : fileId;
204
+ if (isZmdmsFileService) {
205
+ fileIds = encodeFileId(fileIds, 1);
206
+ }
207
+ // 批量图片预览
208
+ window.open(
209
+ `${apiBaseURL}/picturesAttchPreview?attachIds=${fileIds}&${TOKEN_KEY}=${token}` +
210
+ (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
211
+ );
212
+ } else {
213
+ if (isZmdmsFileService) {
214
+ fileId = encodeFileId(fileId, 1);
215
+ }
216
+ // 普通预览
217
+ window.open(
218
+ `${apiBaseURL}/attchPreview?attachId=${fileId}&${TOKEN_KEY}=${token}&officePreviewType=pdf` +
219
+ (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
220
+ );
221
+ }
203
222
  }
204
223
 
205
224
  /**
@@ -286,6 +305,9 @@ export function createDownloadLink(
286
305
  ) {
287
306
  const { API_BASEURL, authToken, open } = options;
288
307
  const { token, apiBaseURL } = getBasicInfo(API_BASEURL || "", authToken);
308
+ if (isZmdmsFileService) {
309
+ fileId = encodeFileId(fileId, 2);
310
+ }
289
311
  if (open) {
290
312
  return `${apiBaseURL}/api/${BASIC_KEY}-resource/oss/endpoint/public/download-file/${fileId}`;
291
313
  }
@@ -323,11 +345,16 @@ export function createOriginalLink(
323
345
  // 所以将文件名 与 文件id组合来生成不同的文件名
324
346
  previewFileName = `${filePreName}-${fileId}.${fileExtension}`;
325
347
  // 2023-12-30 替换成数字中心的 预览接口
326
- return `${apiBaseURL}/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=${fileId}&${TOKEN_KEY}=${token}`;
327
- // return (
328
- // `${apiBaseURL}/attchPreview?attachId=${fileId}&${TOKEN_KEY}=${token}&officePreviewType=pdf` +
329
- // (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
330
- // );
348
+ if (!isZmdmsFileService) {
349
+ return `${apiBaseURL}/api/cicoms-cicohr-org/cicomscicohr/v1/common/filePreview?attachId=${fileId}&${TOKEN_KEY}=${token}`;
350
+ }
351
+ if (isZmdmsFileService) {
352
+ fileId = encodeFileId(fileId, 2);
353
+ }
354
+ return (
355
+ `${apiBaseURL}/attchPreview?attachId=${fileId}&${TOKEN_KEY}=${token}&officePreviewType=pdf` +
356
+ (fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
357
+ );
331
358
  }
332
359
 
333
360
  /**
@@ -423,3 +450,22 @@ interface IThumbnailLinkOptions extends Partial<IOptions> {
423
450
  /** 缩放比例 */
424
451
  scale?: number;
425
452
  }
453
+
454
+ /**
455
+ * 加密附件ID
456
+ * @param fileId 附件ID
457
+ * @description 加密方式
458
+ * 1. 都会通过encrypt加密一次
459
+ * 2. 如果参数是通过地址栏传输的,附件预览:encode一次;附件下载:encode两次;
460
+ * 3. 如果参数是通过body传输的,只需要encode一次
461
+ */
462
+ export function encodeFileId(fileId: string, encodeNum: number = 1) {
463
+ const crypto = new Crypto(
464
+ (window as any).__CRYPTO_AES_KEY__,
465
+ (window as any).__CRYPTO_DES_KEY__
466
+ );
467
+ if (encodeNum === 1) {
468
+ return encodeURIComponent(crypto.encrypt(fileId));
469
+ }
470
+ return encodeURIComponent(encodeURIComponent(crypto.encrypt(fileId)));
471
+ }