koishi-plugin-monetary-bank 1.1.1 → 1.2.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.
package/lib/index.d.ts CHANGED
@@ -27,6 +27,63 @@ declare module 'koishi' {
27
27
  interface Tables {
28
28
  monetary_bank_int: MonetaryBankInterest;
29
29
  }
30
+ interface Context {
31
+ monetaryBank: MonetaryBankAPI;
32
+ }
33
+ }
34
+ /**
35
+ * 银行API接口类
36
+ * 提供给其他插件调用的银行操作接口
37
+ */
38
+ export declare class MonetaryBankAPI {
39
+ private ctx;
40
+ private config;
41
+ constructor(ctx: Context, config: Config);
42
+ /**
43
+ * 查询用户银行余额
44
+ * @param uid 用户ID
45
+ * @param currency 货币类型
46
+ * @returns 包含总资产、活期、定期的余额信息
47
+ */
48
+ getBalance(uid: number, currency: string): Promise<{
49
+ total: number;
50
+ demand: number;
51
+ fixed: number;
52
+ }>;
53
+ /**
54
+ * 存款(从用户现金存入银行,自动转为活期)
55
+ * @param uid 用户ID
56
+ * @param currency 货币类型
57
+ * @param amount 存款金额
58
+ * @returns 成功返回 { success: true, newCash, newBalance },失败返回 { success: false, error }
59
+ */
60
+ deposit(uid: number, currency: string, amount: number): Promise<{
61
+ success: boolean;
62
+ newCash?: number;
63
+ newBalance?: {
64
+ total: number;
65
+ demand: number;
66
+ fixed: number;
67
+ };
68
+ error?: string;
69
+ }>;
70
+ /**
71
+ * 取款(从银行取出到用户现金,仅可取活期)
72
+ * @param uid 用户ID
73
+ * @param currency 货币类型
74
+ * @param amount 取款金额
75
+ * @returns 成功返回 { success: true, newCash, newBalance },失败返回 { success: false, error }
76
+ */
77
+ withdraw(uid: number, currency: string, amount: number): Promise<{
78
+ success: boolean;
79
+ newCash?: number;
80
+ newBalance?: {
81
+ total: number;
82
+ demand: number;
83
+ fixed: number;
84
+ };
85
+ error?: string;
86
+ }>;
30
87
  }
31
88
  /**
32
89
  * 利息记录表结构
package/lib/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  Config: () => Config,
24
+ MonetaryBankAPI: () => MonetaryBankAPI,
24
25
  apply: () => apply,
25
26
  inject: () => inject,
26
27
  name: () => name
@@ -65,6 +66,110 @@ function logSuccess(...args) {
65
66
  else logger.info && logger.info.apply(logger, args);
66
67
  }
67
68
  __name(logSuccess, "logSuccess");
69
+ var MonetaryBankAPI = class {
70
+ constructor(ctx, config) {
71
+ this.ctx = ctx;
72
+ this.config = config;
73
+ }
74
+ static {
75
+ __name(this, "MonetaryBankAPI");
76
+ }
77
+ /**
78
+ * 查询用户银行余额
79
+ * @param uid 用户ID
80
+ * @param currency 货币类型
81
+ * @returns 包含总资产、活期、定期的余额信息
82
+ */
83
+ async getBalance(uid, currency) {
84
+ return await getBankBalance(this.ctx, uid, currency);
85
+ }
86
+ /**
87
+ * 存款(从用户现金存入银行,自动转为活期)
88
+ * @param uid 用户ID
89
+ * @param currency 货币类型
90
+ * @param amount 存款金额
91
+ * @returns 成功返回 { success: true, newCash, newBalance },失败返回 { success: false, error }
92
+ */
93
+ async deposit(uid, currency, amount) {
94
+ try {
95
+ if (!amount || amount <= 0) {
96
+ return { success: false, error: "金额必须大于0" };
97
+ }
98
+ let cash = await getMonetaryBalance(this.ctx, uid, currency);
99
+ if (cash === null) {
100
+ const created = await createMonetaryUser(this.ctx, uid, currency);
101
+ if (!created) return { success: false, error: "无法验证/创建主货币账户" };
102
+ cash = 0;
103
+ }
104
+ if (cash < amount) {
105
+ return { success: false, error: `现金不足,当前现金:${cash} ${currency}` };
106
+ }
107
+ const newCash = await changeMonetary(this.ctx, uid, currency, -amount);
108
+ if (newCash === null) {
109
+ return { success: false, error: "扣除现金失败" };
110
+ }
111
+ const demandConfig = this.config.demandInterest || { enabled: true, rate: 0.25, cycle: "day" };
112
+ const settlementDate = calculateNextSettlementDate(demandConfig.cycle, true);
113
+ await this.ctx.database.create("monetary_bank_int", {
114
+ uid,
115
+ currency,
116
+ amount,
117
+ type: "demand",
118
+ rate: demandConfig.rate || 0.25,
119
+ cycle: demandConfig.cycle || "day",
120
+ settlementDate,
121
+ extendRequested: false
122
+ });
123
+ const newBalance = await getBankBalance(this.ctx, uid, currency);
124
+ logInfo(`API存款: uid=${uid}, amount=${amount}`);
125
+ return { success: true, newCash, newBalance };
126
+ } catch (error) {
127
+ logger.error("API存款失败:", error);
128
+ return { success: false, error: "存款操作失败" };
129
+ }
130
+ }
131
+ /**
132
+ * 取款(从银行取出到用户现金,仅可取活期)
133
+ * @param uid 用户ID
134
+ * @param currency 货币类型
135
+ * @param amount 取款金额
136
+ * @returns 成功返回 { success: true, newCash, newBalance },失败返回 { success: false, error }
137
+ */
138
+ async withdraw(uid, currency, amount) {
139
+ try {
140
+ if (!amount || amount <= 0) {
141
+ return { success: false, error: "金额必须大于0" };
142
+ }
143
+ const balance = await getBankBalance(this.ctx, uid, currency);
144
+ if (balance.demand < amount) {
145
+ return { success: false, error: `可用余额不足,当前活期:${balance.demand} ${currency}` };
146
+ }
147
+ let remaining = amount;
148
+ const demandRecords = await this.ctx.database.select("monetary_bank_int").where({ uid, currency, type: "demand" }).orderBy("settlementDate", "asc").execute();
149
+ for (const record of demandRecords) {
150
+ if (remaining <= 0) break;
151
+ if (record.amount <= remaining) {
152
+ remaining -= record.amount;
153
+ await this.ctx.database.remove("monetary_bank_int", { id: record.id });
154
+ } else {
155
+ const newAmount = record.amount - remaining;
156
+ await this.ctx.database.set("monetary_bank_int", { id: record.id }, { amount: newAmount });
157
+ remaining = 0;
158
+ }
159
+ }
160
+ const newCash = await changeMonetary(this.ctx, uid, currency, amount);
161
+ if (newCash === null) {
162
+ return { success: false, error: "转账到现金失败" };
163
+ }
164
+ const newBalance = await getBankBalance(this.ctx, uid, currency);
165
+ logInfo(`API取款: uid=${uid}, amount=${amount}`);
166
+ return { success: true, newCash, newBalance };
167
+ } catch (error) {
168
+ logger.error("API取款失败:", error);
169
+ return { success: false, error: "取款操作失败" };
170
+ }
171
+ }
172
+ };
68
173
  async function initDatabase(ctx) {
69
174
  try {
70
175
  const tables = await ctx.database.tables;
@@ -239,6 +344,7 @@ async function scheduleInterestSettlement(ctx, config) {
239
344
  await settleInterest(ctx, config, record);
240
345
  }
241
346
  }
347
+ await mergeDemandRecords(ctx);
242
348
  logSuccess("利息结算任务执行完成");
243
349
  } catch (error) {
244
350
  logger.error("利息结算任务执行失败:", error);
@@ -318,6 +424,51 @@ async function getBankBalance(ctx, uid, currency) {
318
424
  return { total: demand + fixed, demand, fixed };
319
425
  }
320
426
  __name(getBankBalance, "getBankBalance");
427
+ async function mergeDemandRecords(ctx) {
428
+ try {
429
+ const records = await ctx.database.select("monetary_bank_int").where({ type: "demand" }).execute();
430
+ const groups = {};
431
+ for (const r of records) {
432
+ const sd = new Date(r.settlementDate);
433
+ sd.setHours(0, 0, 0, 0);
434
+ const key = `${r.uid}|${r.currency}|${sd.getTime()}|${r.rate}|${r.cycle}`;
435
+ if (!groups[key]) {
436
+ groups[key] = { ids: [], uid: r.uid, currency: r.currency, settlementDate: sd, rate: r.rate, cycle: r.cycle, total: 0 };
437
+ }
438
+ groups[key].ids.push(r.id);
439
+ groups[key].total += Number(r.amount || 0);
440
+ }
441
+ for (const key of Object.keys(groups)) {
442
+ const g = groups[key];
443
+ if (g.ids.length <= 1) continue;
444
+ for (const id of g.ids) {
445
+ try {
446
+ await ctx.database.remove("monetary_bank_int", { id });
447
+ } catch (e) {
448
+ logger.warn(`合并活期记录时删除 id=${id} 失败:`, e);
449
+ }
450
+ }
451
+ try {
452
+ await ctx.database.create("monetary_bank_int", {
453
+ uid: g.uid,
454
+ currency: g.currency,
455
+ amount: g.total,
456
+ type: "demand",
457
+ rate: g.rate,
458
+ cycle: g.cycle,
459
+ settlementDate: g.settlementDate,
460
+ extendRequested: false
461
+ });
462
+ logInfo(`合并活期记录: uid=${g.uid}, currency=${g.currency}, settlementDate=${g.settlementDate.toISOString()}, 合并后金额=${g.total}`);
463
+ } catch (e) {
464
+ logger.error("创建合并后活期记录失败:", e);
465
+ }
466
+ }
467
+ } catch (err) {
468
+ logger.error("合并活期记录失败:", err);
469
+ }
470
+ }
471
+ __name(mergeDemandRecords, "mergeDemandRecords");
321
472
  function generateDepositConfirmMessage(amount, currency, cash) {
322
473
  return `您将存入 ${amount} ${currency} 到银行,当前现金:${cash} ${currency}。
323
474
  确认操作请回复 yes 或 y,取消请回复其他内容。`;
@@ -336,27 +487,34 @@ async function apply(ctx, config) {
336
487
  return;
337
488
  }
338
489
  logSuccess("✓ monetary-bank 插件加载成功");
339
- ctx.command("bank.bal", "查询银行存款").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").action(async ({ session, options }) => {
490
+ ctx.monetaryBank = new MonetaryBankAPI(ctx, config);
491
+ ctx.command("bank.bal [currency:string]", "查询银行存款").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").action(async ({ session, options }, currencyArg) => {
340
492
  const uid = session.user.id;
341
- const currency = options?.currency || config.defaultCurrency || "coin";
493
+ const currency = options?.currency || currencyArg || config.defaultCurrency || "coin";
342
494
  try {
343
- const balance = await getBankBalance(ctx, uid, currency);
495
+ const balance = await ctx.monetaryBank.getBalance(uid, currency);
344
496
  if (balance.total === 0) {
345
497
  return `您在银行中还没有 ${currency} 存款。`;
346
498
  }
347
499
  return `您的银行资产:
348
500
  总资产:${balance.total} ${currency}
349
- 可用资产:${balance.demand} ${currency}
350
- 不可用资产:${balance.fixed} ${currency}`;
501
+ 活期资产(可用):${balance.demand} ${currency}
502
+ 不可用资产(定期):${balance.fixed} ${currency}`;
351
503
  } catch (error) {
352
504
  logger.error("查询存款失败:", error);
353
505
  return "查询失败,请稍后再试。";
354
506
  }
355
507
  });
356
- ctx.command("bank.in <amount:string>", "存入货币到银行(自动转为活期)").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").option("yes", "-y 跳过确认直接执行").action(async ({ session, options }, amount) => {
508
+ ctx.command("bank.in [amount:string]", "存入货币到银行活期").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").option("yes", "-y 跳过确认直接执行").action(async ({ session, options }, amount) => {
357
509
  const uid = session.user.id;
358
510
  const currency = options?.currency || config.defaultCurrency || "coin";
359
511
  try {
512
+ if (!amount) {
513
+ await session.send(`请输入存款金额(正整数或 all):`);
514
+ const amountInput2 = await session.prompt(3e4);
515
+ if (!amountInput2) return "操作超时,已取消存款。";
516
+ amount = amountInput2.trim();
517
+ }
360
518
  let cash = await getMonetaryBalance(ctx, uid, currency);
361
519
  if (cash === null) {
362
520
  const created = await createMonetaryUser(ctx, uid, currency);
@@ -374,7 +532,6 @@ async function apply(ctx, config) {
374
532
  return "请输入有效的存款金额(正整数或 all)。";
375
533
  }
376
534
  }
377
- if (cash < amountNum) return `现金不足!当前现金:${cash} ${currency}`;
378
535
  if (!options?.yes) {
379
536
  const confirmMsg = generateDepositConfirmMessage(amountNum, currency, cash);
380
537
  await session.send(confirmMsg);
@@ -385,37 +542,32 @@ async function apply(ctx, config) {
385
542
  return "已取消存款操作。";
386
543
  }
387
544
  }
388
- const newCash = await changeMonetary(ctx, uid, currency, -amountNum);
389
- if (newCash === null) return "扣除现金失败,操作已中止。";
390
- const demandConfig = config.demandInterest || { enabled: true, rate: 0.25, cycle: "day" };
391
- const settlementDate = calculateNextSettlementDate(demandConfig.cycle, true);
392
- await ctx.database.create("monetary_bank_int", {
393
- uid,
394
- currency,
395
- amount: amountNum,
396
- type: "demand",
397
- rate: demandConfig.rate || 0.25,
398
- cycle: demandConfig.cycle || "day",
399
- settlementDate,
400
- extendRequested: false
401
- });
402
- const balance = await getBankBalance(ctx, uid, currency);
403
- logInfo(`创建活期记录 uid=${uid}, amount=${amountNum}`);
545
+ const result = await ctx.monetaryBank.deposit(uid, currency, amountNum);
546
+ if (!result.success) {
547
+ return result.error || "存款失败";
548
+ }
404
549
  return `成功存入 ${amountNum} ${currency}(活期)!
405
- 现金余额:${newCash} ${currency}
406
- 银行总资产:${balance.total} ${currency}`;
550
+ 现金余额:${result.newCash} ${currency}
551
+ 银行总资产:${result.newBalance.total} ${currency}`;
407
552
  } catch (error) {
408
553
  logger.error("存款失败:", error);
409
554
  return "存款失败,请稍后再试。";
410
555
  }
411
556
  });
412
- ctx.command("bank.out <amount:string>", "从银行取出货币(仅可取活期)").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").option("yes", "-y 跳过确认直接执行").action(async ({ session, options }, amount) => {
557
+ ctx.command("bank.out [amount:string]", "从银行取出货币").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").option("yes", "-y 跳过确认直接执行").action(async ({ session, options }, amount) => {
413
558
  const uid = session.user.id;
414
559
  const currency = options?.currency || config.defaultCurrency || "coin";
415
560
  try {
416
- const balance = await getBankBalance(ctx, uid, currency);
561
+ const balance = await ctx.monetaryBank.getBalance(uid, currency);
417
562
  if (balance.demand === 0) {
418
- return `没有可取出的活期存款。当前活期:${balance.demand} ${currency}`;
563
+ return `没有可取出的存款。当前活期:${balance.demand} ${currency}`;
564
+ }
565
+ if (!amount) {
566
+ await session.send(`请输入取款金额(正整数或 all):
567
+ 当前可用余额:${balance.demand} ${currency}`);
568
+ const amountInput2 = await session.prompt(3e4);
569
+ if (!amountInput2) return "操作超时,已取消取款。";
570
+ amount = amountInput2.trim();
419
571
  }
420
572
  const amountInput = String(amount || "").trim().toLowerCase();
421
573
  let amountNum;
@@ -427,9 +579,6 @@ async function apply(ctx, config) {
427
579
  return "请输入有效的取款金额(正整数或 all)。";
428
580
  }
429
581
  }
430
- if (balance.demand < amountNum) {
431
- return `活期余额不足!当前活期:${balance.demand} ${currency}`;
432
- }
433
582
  if (!options?.yes) {
434
583
  const confirmMsg = generateWithdrawConfirmMessage(amountNum, currency, balance.demand);
435
584
  await session.send(confirmMsg);
@@ -440,29 +589,13 @@ async function apply(ctx, config) {
440
589
  return "已取消取款操作。";
441
590
  }
442
591
  }
443
- let remaining = amountNum;
444
- const demandRecords = await ctx.database.select("monetary_bank_int").where({ uid, currency, type: "demand" }).orderBy("settlementDate", "asc").execute();
445
- for (const record of demandRecords) {
446
- if (remaining <= 0) break;
447
- if (record.amount <= remaining) {
448
- remaining -= record.amount;
449
- await ctx.database.remove("monetary_bank_int", { id: record.id });
450
- logInfo(`完全扣除活期记录 id=${record.id}, amount=${record.amount}`);
451
- } else {
452
- const newAmount = record.amount - remaining;
453
- await ctx.database.set("monetary_bank_int", { id: record.id }, { amount: newAmount });
454
- logInfo(`部分扣除活期记录 id=${record.id}, 扣除=${remaining}, 剩余=${newAmount}`);
455
- remaining = 0;
456
- }
457
- }
458
- const newCash = await changeMonetary(ctx, uid, currency, amountNum);
459
- if (newCash === null) {
460
- return "转账到现金失败,请联系管理员。";
592
+ const result = await ctx.monetaryBank.withdraw(uid, currency, amountNum);
593
+ if (!result.success) {
594
+ return result.error || "取款失败";
461
595
  }
462
- const newBalance = await getBankBalance(ctx, uid, currency);
463
596
  return `成功取出 ${amountNum} ${currency}!
464
- 现金余额:${newCash} ${currency}
465
- 银行总资产:${newBalance.total} ${currency}`;
597
+ 现金余额:${result.newCash} ${currency}
598
+ 银行总资产:${result.newBalance.total} ${currency}`;
466
599
  } catch (error) {
467
600
  logger.error("取款失败:", error);
468
601
  return "取款失败,请稍后再试。";
@@ -492,7 +625,7 @@ async function apply(ctx, config) {
492
625
  if (planIndex < 0) return "已取消定期存款申请。";
493
626
  if (planIndex >= plans.length) return "无效的方案编号。";
494
627
  const selectedPlan = plans[planIndex];
495
- await session.send(`请输入存入金额(可使用现金和活期存款):`);
628
+ await session.send(`请输入存入金额(优先使用现金,不足时使用银行活期存款):`);
496
629
  const amountInput = await session.prompt(3e4);
497
630
  if (!amountInput) return "操作超时,已取消。";
498
631
  const amount = parseInt(amountInput.trim());
@@ -639,6 +772,7 @@ __name(apply, "apply");
639
772
  // Annotate the CommonJS export names for ESM import in node:
640
773
  0 && (module.exports = {
641
774
  Config,
775
+ MonetaryBankAPI,
642
776
  apply,
643
777
  inject,
644
778
  name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-monetary-bank",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
6
6
  "files": [
@@ -25,21 +25,21 @@
25
25
  "chatbot",
26
26
  "koishi",
27
27
  "plugin",
28
- "bank",
29
- "银行",
30
- "金融",
31
- "monetary",
32
- "货币"
28
+ "bank",
29
+ "银行",
30
+ "金融",
31
+ "monetary",
32
+ "货币"
33
33
  ],
34
34
  "author": {
35
35
  "name": "BYWled",
36
36
  "email": "bywled@qq.com",
37
37
  "url": "https://github.com/BYWled"
38
38
  },
39
- "homepage": "https://github.com/BYWled/jrys-fix-ranks",
39
+ "homepage": "https://github.com/BYWled/monetary-bank",
40
40
  "repository": {
41
41
  "type": "git",
42
- "url": "https://github.com/BYWled/jrys-fix-ranks"
42
+ "url": "https://github.com/BYWled/monetary-bank"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "koishi": "4.18.9",
package/readme.md CHANGED
@@ -4,11 +4,11 @@
4
4
 
5
5
  `Provides banking related services for monetary money`
6
6
 
7
- ---
8
-
9
7
  `为 monetary 插件(或外部项目中的主货币表)提供轻量级银行功能,支持活期/定期存款与利息结算。`
10
8
 
11
- **版本**: v1.1.0
9
+ ---
10
+
11
+ **版本**: v1.2.1
12
12
 
13
13
  **特点**
14
14
  - **查询/存取**: 提供银行资产查询、存入(存为活期)、取出(仅活期)。
@@ -21,6 +21,7 @@
21
21
 
22
22
  **数据库表**
23
23
  - **表名**: `monetary_bank_int` — 用于记录所有银行存款(活期与定期)。
24
+ - **早期表名**:`monetary_bank` — 用于存放用户和银行存款。
24
25
 
25
26
  **快速安装**
26
27
  - 在 Koishi 项目中安装并启用本插件(示例:`package.json` 或 Koishi 配置中添加插件)
@@ -71,7 +72,7 @@ plugins: {
71
72
  - 支持从现金和活期组合扣款(优先扣现金)
72
73
  - **`bank.fixed.manage`**: 管理已申请的定期(申请延期或取消延期)
73
74
 
74
- 示例:
75
+ 命令示例:
75
76
 
76
77
  ```
77
78
  bank.bal -c coin
@@ -81,6 +82,43 @@ bank.fixed -c coin
81
82
  bank.fixed.manage -c coin
82
83
  ```
83
84
 
85
+ **API**
86
+
87
+ 本插件提供可被其他插件直接调用的编程接口 `MonetaryBankAPI`,并在 `apply` 中注册为 `ctx.monetaryBank`。该 API 将业务逻辑与命令交互分离,便于测试和复用。
88
+
89
+ - `getBalance(uid: number, currency: string): Promise<{ total: number; demand: number; fixed: number }>`
90
+ - 说明:查询指定用户在银行的总资产、活期(可用)与定期(不可用)余额。
91
+
92
+ - `deposit(uid: number, currency: string, amount: number): Promise<{ success: boolean; newCash?: number; newBalance?: { total:number; demand:number; fixed:number }; error?: string }>`
93
+ - 说明:将用户的现金存入银行并创建活期记录(会自动尝试创建主货币账户)。返回操作结果与更新后的现金/银行余额。
94
+
95
+ - `withdraw(uid: number, currency: string, amount: number): Promise<{ success: boolean; newCash?: number; newBalance?: { total:number; demand:number; fixed:number }; error?: string }>`
96
+ - 说明:从活期中按时间顺序扣除金额并转回用户现金。仅能提取活期部分,返回操作结果与更新后的现金/银行余额。
97
+
98
+ 返回格式说明(统一格式)
99
+ - `success`: 操作是否成功(布尔)
100
+ - `newCash`(可选):操作后用户主货币表中的现金余额
101
+ - `newBalance`(可选):操作后银行中归集的余额信息(`{ total, demand, fixed }`)
102
+ - `error`(可选):失败时的错误消息
103
+
104
+ API示例(在其他插件中调用):
105
+
106
+ ```
107
+ // 查询余额
108
+ const balance = await ctx.monetaryBank.getBalance(uid, 'coin')
109
+ console.log(`总资产:${balance.total}, 活期:${balance.demand}, 定期:${balance.fixed}`)
110
+
111
+ // 存款
112
+ const res = await ctx.monetaryBank.deposit(uid, 'coin', 100)
113
+ if (res.success) console.log(`存款成功,新现金:${res.newCash}`)
114
+ else console.error(`存款失败:${res.error}`)
115
+
116
+ // 取款
117
+ const res2 = await ctx.monetaryBank.withdraw(uid, 'coin', 50)
118
+ if (res2.success) console.log(`取款成功,新现金:${res2.newCash}`)
119
+ else console.error(`取款失败:${res2.error}`)
120
+ ```
121
+
84
122
  **利息结算**
85
123
  - 若在配置中启用 `enableInterest`,插件会在后台启动定时任务,每日0点检查并结算到期记录(对 `day/week/month` 周期分别按设定处理)。
86
124
  - 到期的定期记录可以:延期(若用户已申请)或转为活期并结算利息。
@@ -93,15 +131,30 @@ bank.fixed.manage -c coin
93
131
  - 插件作者: BYWled
94
132
  - 仓库: `koishi-plugin-monetary-bank`
95
133
 
134
+
96
135
  **更新日志**
97
136
 
98
- - 1.1.1
99
- - 修复了代码未构建的问题。~~有个神人忘了构建源代码了~~
137
+ - **1.2.1**
138
+ - 修复插件主页异常的问题
139
+
140
+ - **1.2.0**
141
+ - 新增:`MonetaryBankAPI` 类并导出为 `ctx.monetaryBank`(接口方法:`getBalance` / `deposit` / `withdraw`),所有方法返回统一格式 `{ success, newCash?, newBalance?, error? }`,便于其他插件以编程方式调用。
142
+ - 新增:在 `apply` 向 `Context` 注册 `monetaryBank`(`ctx.monetaryBank = new MonetaryBankAPI(ctx, config)`),并在类型声明中扩展 `Context`。
143
+ - 重构:命令接口使用新的 API:
144
+ - `bank.bal [currency]` 使用 `ctx.monetaryBank.getBalance()`,支持可选参数输入货币并改进显示为“活期(可用)/定期(不可用)”。
145
+ - `bank.in [amount]` 支持可选参数;无参数时交互式提示 `请输入存款金额(正整数或 all):`,支持 `all` 将全部现金存入;使用 `ctx.monetaryBank.deposit()` 执行业务逻辑。
146
+ - `bank.out [amount]` 支持可选参数;无参数时交互式提示并展示当前活期余额,支持 `all` 将全部活期取出;使用 `ctx.monetaryBank.withdraw()`。
147
+ - 改进:利息结算流程中在结算完成后合并相同组别的活期记录(`mergeDemandRecords`),减少碎片小额记录,提升性能与可读性。
148
+ - 改进:交互文本与超时处理更友好(30s 超时提示取消)。
149
+
150
+ - **1.1.1**
151
+ - 修复了代码未构建的问题。
100
152
 
101
153
  - **1.1.0**
102
154
  - 增加:命令 `bank.fixed`, `bank.fixed.manage` 与更完善的交互流程
103
155
  - 增加:活期/定期利息配置与结算调度(可配置开启)
104
156
  - 改进:兼容更多 `monetary` 表字段,增强容错与自动创建最小记录的能力
157
+ - **注意**:本版本重构了数据库,因此旧的数据库中的**数据将丢失**,请在更新前做好**数据备份**!
105
158
  - **1.0.1**
106
159
  - 修复依赖版本问题
107
160
  - **1.0.0**