@ray-js/lock-sdk 1.0.0-beta-1

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.
Files changed (95) hide show
  1. package/lib/api/index.d.ts +5 -0
  2. package/lib/api/index.js +18 -0
  3. package/lib/api/linkage.d.ts +161 -0
  4. package/lib/api/linkage.js +74 -0
  5. package/lib/api/lock.d.ts +195 -0
  6. package/lib/api/lock.js +129 -0
  7. package/lib/api/log.d.ts +166 -0
  8. package/lib/api/log.js +44 -0
  9. package/lib/api/setting.d.ts +14 -0
  10. package/lib/api/setting.js +36 -0
  11. package/lib/api/temp.d.ts +211 -0
  12. package/lib/api/temp.js +130 -0
  13. package/lib/api/user.d.ts +154 -0
  14. package/lib/api/user.js +59 -0
  15. package/lib/api/video.d.ts +9 -0
  16. package/lib/api/video.js +17 -0
  17. package/lib/config/dp-code/index.d.ts +175 -0
  18. package/lib/config/dp-code/index.js +224 -0
  19. package/lib/config/dp-map/common.d.ts +102 -0
  20. package/lib/config/dp-map/common.js +120 -0
  21. package/lib/config/dp-map/normal.d.ts +6 -0
  22. package/lib/config/dp-map/normal.js +51 -0
  23. package/lib/config/dp-map/open.d.ts +21 -0
  24. package/lib/config/dp-map/open.js +62 -0
  25. package/lib/config/dp-map/unlock-method-big.d.ts +559 -0
  26. package/lib/config/dp-map/unlock-method-big.js +237 -0
  27. package/lib/config/dp-map/unlock-method.d.ts +551 -0
  28. package/lib/config/dp-map/unlock-method.js +231 -0
  29. package/lib/config/index.d.ts +46 -0
  30. package/lib/config/index.js +56 -0
  31. package/lib/constant.d.ts +101 -0
  32. package/lib/constant.js +136 -0
  33. package/lib/dp-interface.d.ts +7 -0
  34. package/lib/dp-interface.js +1 -0
  35. package/lib/event.d.ts +8 -0
  36. package/lib/event.js +9 -0
  37. package/lib/index.d.ts +25 -0
  38. package/lib/index.js +92 -0
  39. package/lib/interface.d.ts +807 -0
  40. package/lib/interface.js +1 -0
  41. package/lib/linkage.d.ts +26 -0
  42. package/lib/linkage.js +175 -0
  43. package/lib/log.d.ts +91 -0
  44. package/lib/log.js +314 -0
  45. package/lib/media.d.ts +43 -0
  46. package/lib/media.js +80 -0
  47. package/lib/open.d.ts +48 -0
  48. package/lib/open.js +247 -0
  49. package/lib/other.d.ts +36 -0
  50. package/lib/other.js +178 -0
  51. package/lib/parse/index.d.ts +6 -0
  52. package/lib/parse/index.js +22 -0
  53. package/lib/signal.d.ts +26 -0
  54. package/lib/signal.js +38 -0
  55. package/lib/sleep.d.ts +61 -0
  56. package/lib/sleep.js +121 -0
  57. package/lib/state.d.ts +54 -0
  58. package/lib/state.js +429 -0
  59. package/lib/sync/remote-serect-key.d.ts +5 -0
  60. package/lib/sync/remote-serect-key.js +60 -0
  61. package/lib/sync/t0.d.ts +5 -0
  62. package/lib/sync/t0.js +33 -0
  63. package/lib/sync/temp.d.ts +7 -0
  64. package/lib/sync/temp.js +88 -0
  65. package/lib/sync/unlock-mothod.d.ts +5 -0
  66. package/lib/sync/unlock-mothod.js +54 -0
  67. package/lib/temporary.d.ts +226 -0
  68. package/lib/temporary.js +637 -0
  69. package/lib/unlock-method.d.ts +269 -0
  70. package/lib/unlock-method.js +723 -0
  71. package/lib/user.d.ts +108 -0
  72. package/lib/user.js +361 -0
  73. package/lib/utils/base64-to-hex.d.ts +1 -0
  74. package/lib/utils/base64-to-hex.js +12 -0
  75. package/lib/utils/byte.d.ts +19 -0
  76. package/lib/utils/byte.js +74 -0
  77. package/lib/utils/constant.d.ts +11 -0
  78. package/lib/utils/constant.js +17 -0
  79. package/lib/utils/device.d.ts +207 -0
  80. package/lib/utils/device.js +353 -0
  81. package/lib/utils/errors.d.ts +2 -0
  82. package/lib/utils/errors.js +125 -0
  83. package/lib/utils/event.d.ts +23 -0
  84. package/lib/utils/event.js +144 -0
  85. package/lib/utils/hex-to-base64.d.ts +1 -0
  86. package/lib/utils/hex-to-base64.js +8 -0
  87. package/lib/utils/hex-to-bytes.d.ts +6 -0
  88. package/lib/utils/hex-to-bytes.js +16 -0
  89. package/lib/utils/index.d.ts +169 -0
  90. package/lib/utils/index.js +419 -0
  91. package/lib/utils/log.d.ts +5 -0
  92. package/lib/utils/log.js +78 -0
  93. package/lib/utils/publishDps.d.ts +11 -0
  94. package/lib/utils/publishDps.js +91 -0
  95. package/package.json +37 -0
@@ -0,0 +1,637 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import "core-js/modules/esnext.iterator.constructor.js";
3
+ import "core-js/modules/esnext.iterator.find.js";
4
+ import "core-js/modules/esnext.iterator.map.js";
5
+ import { clearInvalidTempList, createOfflinePassword, createTemporaryPassword, deleteTempPassword, fetchEffectiveList, fetchInvalidList, getDynamicPassword, reNameOffline, reNameTemporary, revokeOfflinePassword, updateTemporaryPassword, validateTempPwd } from "./api/temp";
6
+ import config from "./config";
7
+ import { formatWeek, isUseNearChannel, parallelOnly, parseOfflinePassword, parseWeek, validateEffectiveConfig } from "./utils";
8
+ import { LoopTypes } from "./utils/constant";
9
+ import { encrypt } from "./utils/device";
10
+ import { getError } from "./utils/errors";
11
+ import { addTempPwd as addTempPwdDpMap, reportAddTempPwd as reportAddTempPwdDpMap } from "./config/dp-map/unlock-method";
12
+ import { addTempPwd as addTempPwdBigDpMap, reportAddTempPwd as reportAddTempPwdDBigpMap } from "./config/dp-map/unlock-method-big";
13
+ import { publishDps } from "./utils/publishDps";
14
+ import dpCodes from "./config/dp-code";
15
+ import dpUtils from "@ray-js/tuya-dp-transform";
16
+ const PASSWORD_REGEX = /^[0-9]+$/;
17
+ const handleError = cloudError => {
18
+ switch (cloudError.errorCode) {
19
+ case "USER_PWD_ALREADY_EXIST":
20
+ throw getError(1037);
21
+ case "LOCK_PWD_NAME_REPEAT":
22
+ throw getError(1038);
23
+ case "START_END_DATE_NOT_RIGHT":
24
+ throw getError(1039);
25
+ case "RECORD_NOT_EXIST":
26
+ throw getError(1040);
27
+ default:
28
+ throw cloudError;
29
+ }
30
+ };
31
+ const isValidPassword = async params => {
32
+ // 首先验证密码有效性
33
+ const validateResult = await validateTempPwd(params);
34
+ if (!validateResult.valid) {
35
+ handleError(validateResult);
36
+ }
37
+ return true;
38
+ };
39
+
40
+ /**
41
+ * 创建自定义临时密码
42
+ * @param {CreateCustomParams} params - 创建临时密码的参数
43
+ * @returns {Promise<boolean>} - 返回创建结果
44
+ * @throws {ErrorData} - 如果创建失败,将抛出错误
45
+ */
46
+ export const createTempCustom = parallelOnly(async params => {
47
+ var _effective$repeat, _effective$weeks;
48
+ const {
49
+ password,
50
+ name,
51
+ effective
52
+ } = params;
53
+ // 密码的校验,仅做简单的数字校验
54
+ if (!PASSWORD_REGEX.test(password)) {
55
+ throw getError(1030);
56
+ }
57
+ // 名称的校验,长度不能超过20个字符
58
+ if (name && name.length > 20) {
59
+ throw getError(1031);
60
+ }
61
+
62
+ // 时效校验
63
+ validateEffectiveConfig(effective);
64
+ const {
65
+ effectiveTime = 0,
66
+ expiredTime = 1439
67
+ } = effective;
68
+ const useNear = isUseNearChannel();
69
+ const aesPwd = await encrypt(config.devInfo.devId, password);
70
+ // 循环方式
71
+ const loopType = effective !== null && effective !== void 0 && effective.repeat ?
72
+ // @ts-ignore
73
+ LoopTypes[effective === null || effective === void 0 || (_effective$repeat = effective.repeat) === null || _effective$repeat === void 0 ? void 0 : _effective$repeat.toUpperCase()] : LoopTypes.NONE;
74
+ const schedule = [{
75
+ allDay: loopType === LoopTypes.NONE,
76
+ effectiveTime,
77
+ invalidTime: expiredTime,
78
+ workingDay: formatWeek((_effective$weeks = effective === null || effective === void 0 ? void 0 : effective.weeks) !== null && _effective$weeks !== void 0 ? _effective$weeks : [0])
79
+ }];
80
+ const baseData = {
81
+ devId: config.devInfo.devId,
82
+ effectiveTime: effective.effectiveDate,
83
+ invalidTime: effective.expiredDate,
84
+ name: name,
85
+ password: aesPwd,
86
+ schedule
87
+ };
88
+ if (useNear) {
89
+ var _effective$weeks2;
90
+ // 走蓝牙通道
91
+ await isValidPassword(baseData);
92
+ const {
93
+ tyNum,
94
+ name
95
+ } = await createTemporaryPassword(_objectSpread(_objectSpread({}, baseData), {}, {
96
+ sn: 0,
97
+ availTime: 0,
98
+ symbolic: false,
99
+ dpTunnel: 2
100
+ }));
101
+ if (!tyNum) {
102
+ throw getError(1043);
103
+ }
104
+
105
+ // 下发dp
106
+ const dpData = {
107
+ cloudNo: tyNum,
108
+ valid: true,
109
+ validConfig: {
110
+ startTime: effective.effectiveDate,
111
+ endTime: effective.expiredDate,
112
+ loop: loopType,
113
+ loopConfig: 0,
114
+ weeks: (_effective$weeks2 = effective.weeks) !== null && _effective$weeks2 !== void 0 ? _effective$weeks2 : [0, 0, 0, 0, 0, 0, 0],
115
+ days: [],
116
+ startHour: Math.floor(effectiveTime / 60),
117
+ startMinute: effectiveTime % 60,
118
+ endHour: Math.floor(expiredTime / 60),
119
+ endMinute: expiredTime % 60
120
+ },
121
+ validNum: 0,
122
+ pwdLength: params.password.length,
123
+ pwd: params.password.split("").map(Number)
124
+ };
125
+ const targetDpCode = config.supportBigData ? dpCodes.tempPwdCreateW : dpCodes.tempPwdCreate;
126
+ const result = await publishDps({
127
+ [targetDpCode]: dpUtils.format(dpData, config.supportBigData ? addTempPwdBigDpMap : addTempPwdDpMap)
128
+ }, {
129
+ checkReport: dpData => {
130
+ const reportData = dpData[targetDpCode];
131
+ if (reportData) {
132
+ const data = dpUtils.parse(dpData[targetDpCode], config.supportBigData ? reportAddTempPwdDBigpMap : reportAddTempPwdDpMap);
133
+ if (data.cloudNo === tyNum) {
134
+ return data;
135
+ }
136
+ }
137
+ return false;
138
+ }
139
+ });
140
+ if (result.status === 0) {
141
+ return {
142
+ type: "custom",
143
+ name: name,
144
+ password: password,
145
+ unlockBindingId: result.unlockBindingId,
146
+ effectiveConfig: effective
147
+ };
148
+ }
149
+ throw getError(result.status === 2 ? 1041 : 1042);
150
+ } else {
151
+ // 走云端
152
+ try {
153
+ const res = await createTemporaryPassword(_objectSpread(_objectSpread({}, baseData), {}, {
154
+ sn: 0,
155
+ availTime: 0,
156
+ symbolic: false,
157
+ dpTunnel: 2
158
+ }));
159
+ return {
160
+ type: "custom",
161
+ name: res.name,
162
+ password: password,
163
+ unlockBindingId: res.unlockBindingId,
164
+ effectiveConfig: effective
165
+ };
166
+ } catch (error) {
167
+ throw handleError(error);
168
+ }
169
+ }
170
+ });
171
+ /**
172
+ * 删除自定义密码
173
+ * @param {RemoveCustomProps} params
174
+ * @returns
175
+ */
176
+ export const removeTempCustom = async params => {
177
+ try {
178
+ return deleteTempPassword({
179
+ devId: config.devInfo.devId,
180
+ unlockBindingId: params.unlockBindingId,
181
+ symbolic: false,
182
+ dpTunnel: 2
183
+ });
184
+ } catch (e) {
185
+ handleError(e);
186
+ }
187
+ };
188
+ /**
189
+ * 更新自定义临时密码
190
+ * 注意,当前只支持更新名称和时效配置,且只支持走云端更新。
191
+ * @param {UpdateTempPasswordParams} params 更新临时密码的参数
192
+ * @returns {Promise<void>} - 返回更新结果
193
+ * @throws {ErrorData} - 如果更新失败,将抛出错误
194
+ */
195
+ export const updateTempCustom = async params => {
196
+ var _effective$repeat2, _effective$effectiveT, _effective$expiredTim, _effective$weeks3;
197
+ const {
198
+ unlockBindingId,
199
+ name,
200
+ effective
201
+ } = params;
202
+
203
+ // 名称的校验,长度不能超过20个字符
204
+ if (name && name.length > 20) {
205
+ throw getError(1031);
206
+ }
207
+
208
+ // 时效校验
209
+ validateEffectiveConfig(effective);
210
+ // 循环方式
211
+ const loopType = effective !== null && effective !== void 0 && effective.repeat ?
212
+ // @ts-ignore
213
+ LoopTypes[effective === null || effective === void 0 || (_effective$repeat2 = effective.repeat) === null || _effective$repeat2 === void 0 ? void 0 : _effective$repeat2.toUpperCase()] : LoopTypes.NONE;
214
+ const schedule = [{
215
+ allDay: loopType === LoopTypes.NONE,
216
+ effectiveTime: (_effective$effectiveT = effective === null || effective === void 0 ? void 0 : effective.effectiveTime) !== null && _effective$effectiveT !== void 0 ? _effective$effectiveT : 0,
217
+ invalidTime: (_effective$expiredTim = effective === null || effective === void 0 ? void 0 : effective.expiredTime) !== null && _effective$expiredTim !== void 0 ? _effective$expiredTim : 0,
218
+ workingDay: formatWeek((_effective$weeks3 = effective === null || effective === void 0 ? void 0 : effective.weeks) !== null && _effective$weeks3 !== void 0 ? _effective$weeks3 : [0])
219
+ }];
220
+ try {
221
+ return updateTemporaryPassword({
222
+ devId: config.devInfo.devId,
223
+ effectiveTime: effective.effectiveDate,
224
+ invalidTime: effective.expiredDate,
225
+ name: name,
226
+ schedule,
227
+ unlockBindingId,
228
+ symbolic: false,
229
+ dpTunnel: 2
230
+ });
231
+ } catch (error) {
232
+ handleError(error);
233
+ }
234
+ };
235
+ const getOfflinePassword = async params => {
236
+ try {
237
+ // 获取密码
238
+ const res = await createOfflinePassword(params);
239
+ return _objectSpread(_objectSpread({}, res), {}, {
240
+ pwd: parseOfflinePassword(res.pwd)
241
+ });
242
+ } catch (error) {
243
+ throw handleError(error);
244
+ }
245
+ };
246
+
247
+ /**
248
+ * 创建临时限时密码
249
+ */
250
+ export const createTempLimit = async params => {
251
+ const {
252
+ name,
253
+ effectiveTime,
254
+ invalidTime
255
+ } = params;
256
+ // 名称的校验,长度不能超过20个字符
257
+ if (name && name.length > 20) {
258
+ throw getError(1031);
259
+ }
260
+ // 生效时间校验
261
+ if (effectiveTime >= invalidTime) {
262
+ throw getError(1010);
263
+ }
264
+ const res = await getOfflinePassword({
265
+ devId: config.devInfo.devId,
266
+ pwdType: "0",
267
+ pwdName: name,
268
+ gmtStart: effectiveTime,
269
+ gmtExpired: invalidTime
270
+ });
271
+ return {
272
+ type: "limit",
273
+ name: res.pwdName,
274
+ password: parseOfflinePassword(res.pwd),
275
+ unlockBindingId: res.unlockBindingId,
276
+ effectiveConfig: {
277
+ effectiveDate: res.gmtStart,
278
+ expiredDate: res.gmtExpired
279
+ }
280
+ };
281
+ };
282
+ /**
283
+ * 创建一次性密码
284
+ * @param {CreateOnceParams} params - 创建一次性密码的参数
285
+ * @returns {Promise<OfflinePwdResult>} - 返回创建结果
286
+ * @throws {ErrorData} - 如果创建失败,将抛出错误
287
+ */
288
+ export const createTempOnce = async params => {
289
+ const {
290
+ name
291
+ } = params;
292
+ // 名称的校验,长度不能超过20个字符
293
+ if (name && name.length > 20) {
294
+ throw getError(1031);
295
+ }
296
+ const res = await getOfflinePassword({
297
+ devId: config.devInfo.devId,
298
+ pwdType: "1",
299
+ pwdName: name,
300
+ gmtStart: 0,
301
+ gmtExpired: 0
302
+ });
303
+ return {
304
+ type: "once",
305
+ name: res.pwdName,
306
+ password: parseOfflinePassword(res.pwd),
307
+ unlockBindingId: res.unlockBindingId,
308
+ effectiveConfig: {
309
+ effectiveDate: res.gmtStart,
310
+ expiredDate: res.gmtExpired
311
+ }
312
+ };
313
+ };
314
+
315
+ /**
316
+ * 创建动态密码
317
+ */
318
+ export const createTempDynamic = async () => {
319
+ const {
320
+ dynamicPassword
321
+ } = await getDynamicPassword({
322
+ devId: config.devInfo.devId
323
+ });
324
+ const now = Math.floor(Date.now() / 1000);
325
+ return {
326
+ type: "dynamic",
327
+ name: "",
328
+ // 动态密码无名称
329
+ password: parseOfflinePassword(dynamicPassword),
330
+ unlockBindingId: "",
331
+ // 动态密码无解锁绑定 ID
332
+ effectiveConfig: {
333
+ effectiveDate: now,
334
+ // 动态密码生效时间为当前时间
335
+ expiredDate: now + 5 * 60 // 动态密码有效期为5分钟
336
+ }
337
+ };
338
+ };
339
+ /**
340
+ * 生成清除密码
341
+ * 可支持清除所有或某个限时密码
342
+ * @param {CreateClearParams} params
343
+ * @returns {Promise<OfflinePwdResult>} - 返回清除密码结果
344
+ * @throws {ErrorData} - 如果生成清除密码失败,将抛出错误
345
+ */
346
+ export const createTempClear = async params => {
347
+ const {
348
+ name,
349
+ unlockBindingId,
350
+ clearAll
351
+ } = params;
352
+ if (name && name.length > 20) {
353
+ throw getError(1031);
354
+ }
355
+ if (!clearAll && !unlockBindingId) {
356
+ throw getError(1044);
357
+ }
358
+ let res;
359
+ if (clearAll) {
360
+ res = await getOfflinePassword({
361
+ devId: config.devInfo.devId,
362
+ pwdType: "9",
363
+ pwdName: name,
364
+ gmtStart: 0,
365
+ gmtExpired: 0
366
+ });
367
+ } else {
368
+ res = await revokeOfflinePassword({
369
+ devId: config.devInfo.devId,
370
+ name,
371
+ unlockBindingId: unlockBindingId
372
+ });
373
+ }
374
+ return {
375
+ type: clearAll ? "clear" : "clearOne",
376
+ name: res.pwdName,
377
+ password: parseOfflinePassword(res.pwd),
378
+ unlockBindingId: res.unlockBindingId,
379
+ effectiveConfig: {
380
+ effectiveDate: res.gmtStart,
381
+ expiredDate: res.gmtExpired
382
+ }
383
+ };
384
+ };
385
+
386
+ /**
387
+ * 临时密码的状态
388
+ * effective - 生效
389
+ * toBeEffective - 待生效
390
+ * expired - 已失效
391
+ * frozen - 冻结
392
+ * freezing - 冻结中
393
+ * synchronizing - 同步中
394
+ * synchronized - 已同步
395
+ * deleted - 已删除
396
+ * toBeDeleted - 待删除
397
+ * synchronizationFailed - 同步失败
398
+ */
399
+
400
+ /**
401
+ *密码的无效原因
402
+ * synchronizationFailed - 同步失败
403
+ * passwordFull - 密码已满
404
+ * deleted - 已删除
405
+ */
406
+
407
+ const passwordTypeMap = {
408
+ "1-0": "custom",
409
+ "3-1": "once",
410
+ "3-0": "limit",
411
+ "3-8": "clearOne",
412
+ "3-9": "clear"
413
+ };
414
+
415
+ /**
416
+ * 获取有效的临时密码列表
417
+ */
418
+ export const getTempEffectiveList = async () => {
419
+ const res = await fetchEffectiveList({
420
+ devId: config.devInfo.devId,
421
+ authTypes: ["LOCK_OFFLINE_TEMP_PWD", "LOCK_TEMP_PWD"]
422
+ });
423
+ return res.map(item => {
424
+ var _item$scheduleDetails, _item$opModeInfo;
425
+ // 类型
426
+ const type = passwordTypeMap[`${item.opModeType}-${item.opModeSubType}`] || "custom";
427
+ // 状态
428
+ let status = "toBeEffective"; // 默认状态为未生效
429
+ if (item.effective !== 1) {
430
+ switch (item.phase) {
431
+ case 1:
432
+ status = "effective"; // 生效中
433
+ break;
434
+ case 2:
435
+ status = "synchronizing"; // 同步中
436
+ break;
437
+ case 3:
438
+ status = "frozen"; // 已冻结
439
+ break;
440
+ case 4:
441
+ status = "freezing"; // 冻结中
442
+ break;
443
+ case 9:
444
+ status = "toBeDeleted"; // 待删除
445
+ break;
446
+ case 10:
447
+ status = "synchronizing"; // 同步中
448
+ break;
449
+ }
450
+ }
451
+ const effectiveConfig = {
452
+ effectiveDate: item.effectiveTime,
453
+ expiredDate: item.invalidTime
454
+ };
455
+ if ((_item$scheduleDetails = item.scheduleDetails) !== null && _item$scheduleDetails !== void 0 && _item$scheduleDetails.length) {
456
+ const scheduleDetail = item.scheduleDetails[0];
457
+ effectiveConfig.repeat = scheduleDetail.allDay === false ? "week" : "none";
458
+ if (effectiveConfig.repeat === "week") {
459
+ effectiveConfig.weeks = parseWeek(scheduleDetail.workingDay);
460
+ effectiveConfig.effectiveTime = scheduleDetail.effectiveTime;
461
+ effectiveConfig.expiredTime = scheduleDetail.invalidTime;
462
+ }
463
+ }
464
+ const data = {
465
+ unlockBindingId: item.unlockBindingId,
466
+ name: item.name,
467
+ status,
468
+ type,
469
+ sn: item.sn,
470
+ hasClearPwd: ((_item$opModeInfo = item.opModeInfo) === null || _item$opModeInfo === void 0 ? void 0 : _item$opModeInfo.hasClearPwd) === true,
471
+ effectiveConfig
472
+ };
473
+ if (data.type === "clearOne") {
474
+ var _item$opModeInfo2, _item$opModeInfo3, _item$opModeInfo4;
475
+ data.clearInfo = {
476
+ name: ((_item$opModeInfo2 = item.opModeInfo) === null || _item$opModeInfo2 === void 0 ? void 0 : _item$opModeInfo2.revokedPwdName) || "",
477
+ effectiveConfig: {
478
+ effectiveDate: ((_item$opModeInfo3 = item.opModeInfo) === null || _item$opModeInfo3 === void 0 ? void 0 : _item$opModeInfo3.revokedPwdEffectiveTime) || 0,
479
+ expiredDate: ((_item$opModeInfo4 = item.opModeInfo) === null || _item$opModeInfo4 === void 0 ? void 0 : _item$opModeInfo4.revokedPwdInvalidTime) || 0
480
+ }
481
+ };
482
+ }
483
+ return data;
484
+ });
485
+ };
486
+ export const getTempInvalidList = async params => {
487
+ const res = await fetchInvalidList({
488
+ devId: config.devInfo.devId,
489
+ offset: ((params === null || params === void 0 ? void 0 : params.page) || 1) - 1,
490
+ limit: (params === null || params === void 0 ? void 0 : params.pageSize) || 200
491
+ });
492
+ const list = res.datas.map(item => {
493
+ var _item$scheduleDetails2, _item$opModeInfo5;
494
+ // 类型
495
+ const type = passwordTypeMap[`${item.opModeType}-${item.opModeSubType}`] || "custom";
496
+ // 状态
497
+
498
+ // 默认状态为未生效
499
+ let invalidReason = "synchronizationFailed"; // 默认失败原因为已删除
500
+ if (item.phase === 8) {
501
+ invalidReason = "deleted"; // 已删除
502
+ }
503
+ if (item.failReason) {
504
+ switch (item.phase) {
505
+ case 1:
506
+ invalidReason = "synchronizationFailed"; // 生效中
507
+ break;
508
+ case 2:
509
+ invalidReason = "passwordFull"; // 密码已满
510
+ break;
511
+ }
512
+ }
513
+ const effectiveConfig = {
514
+ effectiveDate: item.effectiveTime,
515
+ expiredDate: item.invalidTime
516
+ };
517
+ if ((_item$scheduleDetails2 = item.scheduleDetails) !== null && _item$scheduleDetails2 !== void 0 && _item$scheduleDetails2.length) {
518
+ const scheduleDetail = item.scheduleDetails[0];
519
+ effectiveConfig.repeat = scheduleDetail.allDay === false ? "week" : "none";
520
+ if (effectiveConfig.repeat === "week") {
521
+ effectiveConfig.weeks = parseWeek(scheduleDetail.workingDay);
522
+ effectiveConfig.effectiveTime = scheduleDetail.effectiveTime;
523
+ effectiveConfig.expiredTime = scheduleDetail.invalidTime;
524
+ }
525
+ }
526
+ const data = {
527
+ unlockBindingId: item.unlockBindingId,
528
+ name: item.name,
529
+ invalidReason,
530
+ status: "expired",
531
+ type,
532
+ sn: item.sn,
533
+ hasClearPwd: ((_item$opModeInfo5 = item.opModeInfo) === null || _item$opModeInfo5 === void 0 ? void 0 : _item$opModeInfo5.hasClearPwd) === true,
534
+ effectiveConfig
535
+ };
536
+ if (data.type === "clearOne") {
537
+ var _item$opModeInfo6, _item$opModeInfo7, _item$opModeInfo8;
538
+ data.clearInfo = {
539
+ name: ((_item$opModeInfo6 = item.opModeInfo) === null || _item$opModeInfo6 === void 0 ? void 0 : _item$opModeInfo6.revokedPwdName) || "",
540
+ effectiveConfig: {
541
+ effectiveDate: ((_item$opModeInfo7 = item.opModeInfo) === null || _item$opModeInfo7 === void 0 ? void 0 : _item$opModeInfo7.revokedPwdEffectiveTime) || 0,
542
+ expiredDate: ((_item$opModeInfo8 = item.opModeInfo) === null || _item$opModeInfo8 === void 0 ? void 0 : _item$opModeInfo8.revokedPwdInvalidTime) || 0
543
+ }
544
+ };
545
+ }
546
+ return data;
547
+ });
548
+ return {
549
+ list,
550
+ hasMore: res.hasNext,
551
+ total: res.totalCount
552
+ };
553
+ };
554
+
555
+ /**
556
+ * 清空无效的临时密码列表
557
+ */
558
+ export const clearTempInvalidList = async () => {
559
+ return clearInvalidTempList({
560
+ devId: config.devInfo.devId
561
+ });
562
+ };
563
+ const changePasswordPhase = async (unlockBindingId, phase) => {
564
+ const list = await fetchEffectiveList({
565
+ devId: config.devInfo.devId,
566
+ authTypes: ["LOCK_OFFLINE_TEMP_PWD", "LOCK_TEMP_PWD"]
567
+ });
568
+ const password = list.find(item => item.unlockBindingId === unlockBindingId);
569
+ if (!password) {
570
+ throw getError(1051);
571
+ }
572
+ try {
573
+ await updateTemporaryPassword({
574
+ unlockBindingId: unlockBindingId,
575
+ schedule: password.scheduleDetails,
576
+ effectiveTime: password.effectiveTime,
577
+ invalidTime: password.invalidTime,
578
+ name: password.name,
579
+ phase,
580
+ symbolic: false,
581
+ dpTunnel: 2,
582
+ devId: config.devInfo.devId
583
+ });
584
+ } catch (e) {
585
+ handleError(e);
586
+ }
587
+ return true;
588
+ };
589
+
590
+ /**
591
+ * 冻结临时密码
592
+ * @param params
593
+ * @returns
594
+ */
595
+ export const freezeTemp = async params => {
596
+ return changePasswordPhase(params.unlockBindingId, 4);
597
+ };
598
+
599
+ /**
600
+ * 解冻临时密码
601
+ * @param params
602
+ * @returns
603
+ */
604
+ export const unfreezeTemp = async params => {
605
+ return changePasswordPhase(params.unlockBindingId, 2);
606
+ };
607
+ /**
608
+ * 重命名密码名称
609
+ * @param {RenameParams} params
610
+ */
611
+ export const renameTemp = async params => {
612
+ const list = await fetchEffectiveList({
613
+ devId: config.devInfo.devId,
614
+ authTypes: ["LOCK_OFFLINE_TEMP_PWD", "LOCK_TEMP_PWD"]
615
+ });
616
+ const password = list.find(item => item.unlockBindingId === params.unlockBindingId);
617
+ if (!password) {
618
+ throw getError(1051);
619
+ }
620
+ try {
621
+ if (password.opModeType === 1) {
622
+ await reNameTemporary({
623
+ devId: config.devInfo.devId,
624
+ unlockBindingId: params.unlockBindingId,
625
+ name: params.name
626
+ });
627
+ } else {
628
+ await reNameOffline({
629
+ devId: config.devInfo.devId,
630
+ unlockBindingId: params.unlockBindingId,
631
+ pwdName: params.name
632
+ });
633
+ }
634
+ } catch (e) {
635
+ handleError(e);
636
+ }
637
+ };