koishi-plugin-prism-neo 0.0.18 → 0.0.20

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/lib/index.d.ts CHANGED
@@ -4,6 +4,8 @@ export interface Config {
4
4
  url: string;
5
5
  admin: string;
6
6
  currency: string;
7
+ autoLockOnLogin: boolean;
8
+ logoutConfirmation: boolean;
7
9
  }
8
10
  export declare const Config: Schema<Config>;
9
11
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -183,7 +183,9 @@ var kv = /* @__PURE__ */ new Map();
183
183
  var Config = import_koishi.Schema.object({
184
184
  url: import_koishi.Schema.string().required(),
185
185
  admin: import_koishi.Schema.string().default("authority:3"),
186
- currency: import_koishi.Schema.string().default("月饼").description("货币名称")
186
+ currency: import_koishi.Schema.string().default("月饼").description("货币名称"),
187
+ autoLockOnLogin: import_koishi.Schema.boolean().default(true).description("登录时自动获取门锁密码"),
188
+ logoutConfirmation: import_koishi.Schema.boolean().default(true).description("登出时需要二次确认")
187
189
  });
188
190
  async function getUserName(session, userId) {
189
191
  if (userId) {
@@ -301,6 +303,34 @@ var formatBilling = /* @__PURE__ */ __name((res, currency) => {
301
303
  }
302
304
  return message.join("\n");
303
305
  }, "formatBilling");
306
+ async function executeWithAutoRegister(context, userArg, userId, action, formatter) {
307
+ try {
308
+ const res = await action();
309
+ return await formatter(res);
310
+ } catch (e) {
311
+ const code = e?.response?.data?.errcode;
312
+ if (code === "USER_NOT_FOUND") {
313
+ let message = "用户不存在,尝试注册\n";
314
+ message += await handleRegisterCmd(context, userArg);
315
+ message += "\n";
316
+ try {
317
+ const res = await action();
318
+ message += await formatter(res);
319
+ return message;
320
+ } catch (retryError) {
321
+ const apiMessage = retryError?.response?.data?.message;
322
+ if (apiMessage) {
323
+ message += apiMessage;
324
+ } else {
325
+ message += "操作失败。";
326
+ }
327
+ return message;
328
+ }
329
+ }
330
+ throw e;
331
+ }
332
+ }
333
+ __name(executeWithAutoRegister, "executeWithAutoRegister");
304
334
  async function getTargetUserId(context, user) {
305
335
  if (user) {
306
336
  if (!await context.ctx.permissions.check(context.config.admin, context.session)) {
@@ -322,14 +352,23 @@ async function handleLoginCmd(context, user) {
322
352
  const { error, userId } = await getTargetUserId(context, user);
323
353
  if (error) return error;
324
354
  await login(context, userId);
325
- let lockMessage = await handleLockCmd(context);
326
- let message = "✅ 入场成功\n\n" + lockMessage;
355
+ let message = "✅ 入场成功";
356
+ if (context.config.autoLockOnLogin) {
357
+ let lockMessage = await handleLockCmd(context);
358
+ message += "\n\n" + lockMessage;
359
+ }
327
360
  return message;
328
361
  }
329
362
  __name(handleLoginCmd, "handleLoginCmd");
330
363
  async function handleLogoutCmd(context, user) {
331
364
  const { error, userId: targetUserId } = await getTargetUserId(context, user);
332
365
  if (error) return error;
366
+ if (!context.config.logoutConfirmation) {
367
+ const res = await logout(context, targetUserId);
368
+ let message = user ? `✅ 已为用户 ${await getUserName(context.session, targetUserId)} 退场` : "✅ 退场成功";
369
+ message += "\n";
370
+ message += formatBilling(res, context.config.currency);
371
+ }
333
372
  const pendingLogout = kv.get(targetUserId);
334
373
  const now = Date.now();
335
374
  if (pendingLogout && now - pendingLogout < 60 * 1e3) {
@@ -504,53 +543,79 @@ async function handleWalletAdd(context, user, amount) {
504
543
  const { error, userId } = await getTargetUserId(context, user);
505
544
  if (error) return error;
506
545
  if (!amount) return "请输入数量";
507
- const res = await walletAdd(context, parseInt(amount), userId);
508
- return [
509
- `为用户 ${await getUserName(context.session, userId)} 增加${context.config.currency}成功`,
510
- `增加前: ${res.originalBalance}`,
511
- `增加后: ${res.finalBalance}`
512
- ].join("\n");
546
+ return executeWithAutoRegister(
547
+ context,
548
+ user,
549
+ userId,
550
+ () => walletAdd(context, parseInt(amount), userId),
551
+ async (res) => {
552
+ return [
553
+ `为用户 ${await getUserName(context.session, userId)} 增加${context.config.currency}成功`,
554
+ `增加前: ${res.originalBalance}`,
555
+ `增加后: ${res.finalBalance}`
556
+ ].join("\n");
557
+ }
558
+ );
513
559
  }
514
560
  __name(handleWalletAdd, "handleWalletAdd");
515
561
  async function handleWalletDeduct(context, user, amount) {
516
562
  const { error, userId } = await getTargetUserId(context, user);
517
563
  if (error) return error;
518
564
  if (!amount) return "请输入数量";
519
- const res = await walletDel(context, parseInt(amount), userId);
520
- return [
521
- `为用户 ${userId} 扣除${context.config.currency}成功`,
522
- `扣款前: ${res.originalBalance}`,
523
- `扣款后: ${res.finalBalance}`
524
- ].join("\n");
565
+ return executeWithAutoRegister(
566
+ context,
567
+ user,
568
+ userId,
569
+ () => walletDel(context, parseInt(amount), userId),
570
+ (res) => {
571
+ return [
572
+ `为用户 ${userId} 扣除${context.config.currency}成功`,
573
+ `扣款前: ${res.originalBalance}`,
574
+ `扣款后: ${res.finalBalance}`
575
+ ].join("\n");
576
+ }
577
+ );
525
578
  }
526
579
  __name(handleWalletDeduct, "handleWalletDeduct");
527
580
  async function handleCostOverwrite(context, user, amount) {
528
581
  const { error, userId } = await getTargetUserId(context, user);
529
582
  if (error) return error;
530
583
  if (!amount) return "请输入数量";
531
- await costOverwrite(context, amount, userId);
532
- return `为用户 ${userId} 调价成功`;
584
+ return executeWithAutoRegister(
585
+ context,
586
+ user,
587
+ userId,
588
+ () => costOverwrite(context, amount, userId),
589
+ () => `为用户 ${userId} 调价成功`
590
+ );
533
591
  }
534
592
  __name(handleCostOverwrite, "handleCostOverwrite");
535
593
  async function handleRedeem(context, code) {
536
594
  const { error, userId } = await getTargetUserId(context, null);
537
595
  if (error) return error;
538
596
  if (!code) return "请输入兑换码";
539
- const res = await redeem(context, code, userId);
540
- const items = res;
541
- if (!items || items.length === 0) {
542
- return "兑换成功,但没有获得任何物品。";
543
- }
544
- const message = ["✅ 兑换成功!您获得了以下物品:"];
545
- items.forEach((item) => {
546
- let itemName = item.name;
547
- if (item.assetType === "PASS" && item.durationMs) {
548
- const days = Math.floor(item.durationMs / (1e3 * 60 * 60 * 24));
549
- if (days > 0) itemName += ` (${days}天)`;
597
+ return executeWithAutoRegister(
598
+ context,
599
+ void 0,
600
+ userId,
601
+ () => redeem(context, code, userId),
602
+ (res) => {
603
+ const items = res;
604
+ if (!items || items.length === 0) {
605
+ return "兑换成功,但没有获得任何物品。";
606
+ }
607
+ const message = ["✅ 兑换成功!您获得了以下物品:"];
608
+ items.forEach((item) => {
609
+ let itemName = item.name;
610
+ if (item.assetType === "PASS" && item.durationMs) {
611
+ const days = Math.floor(item.durationMs / (1e3 * 60 * 60 * 24));
612
+ if (days > 0) itemName += ` (${days}天)`;
613
+ }
614
+ message.push(`- ${itemName} x${item.count}`);
615
+ });
616
+ return message.join("\n");
550
617
  }
551
- message.push(`- ${itemName} x${item.count}`);
552
- });
553
- return message.join("\n");
618
+ );
554
619
  }
555
620
  __name(handleRedeem, "handleRedeem");
556
621
  async function handleCoin(context, alias) {
package/lib/model.d.ts CHANGED
@@ -119,6 +119,7 @@ export interface BillingResponse {
119
119
  }
120
120
  export interface ApiErrorData {
121
121
  message: string;
122
+ errcode: string;
122
123
  }
123
124
  export interface ApiError {
124
125
  response: {
package/lib/service.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ActionContext } from "./types";
2
- import { BillingResponse, ListResponse, LogoutResponse, UserAsset, Wallet } from "./model";
2
+ import { BillingResponse, ListResponse, UserAsset, Wallet } from "./model";
3
3
  export declare function register({ ctx, config }: ActionContext, userId: string): Promise<unknown>;
4
4
  export declare function login({ ctx, config }: ActionContext, userId: string): Promise<unknown>;
5
- export declare function logout({ ctx, config }: ActionContext, userId: string): Promise<LogoutResponse>;
5
+ export declare function logout({ ctx, config }: ActionContext, userId: string): Promise<BillingResponse>;
6
6
  export declare function billing({ ctx, config }: ActionContext, userId: string): Promise<BillingResponse>;
7
7
  export declare function list({ ctx, config }: ActionContext): Promise<ListResponse>;
8
8
  export declare function wallet({ ctx, config }: ActionContext, userId: string): Promise<Wallet>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism-neo",
3
3
  "description": "prism koishi前端",
4
- "version": "0.0.18",
4
+ "version": "0.0.20",
5
5
  "main": "lib/index.js",
6
6
  "files": [
7
7
  "lib",