koishi-plugin-http-monetary 0.0.2
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/LICENSE.txt +201 -0
- package/lib/config.d.ts +7 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +168 -0
- package/lib/utils.d.ts +2 -0
- package/package.json +54 -0
- package/readme.md +172 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/lib/config.d.ts
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Context } from 'koishi';
|
|
2
|
+
import { Config } from './config';
|
|
3
|
+
export * from './config';
|
|
4
|
+
export declare const name = "plugin-http-monetary";
|
|
5
|
+
export declare const inject: string[];
|
|
6
|
+
export declare const usage = "\n<hr>\n<h2>\u6D45\u6D45\u7684\u7528http\u5C01\u88C5\u4E00\u4E0Bwww\uFF08\u5E26\u9274\u6743\u7248\uFF09</h2>\n<hr>\n";
|
|
7
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Config: () => Config,
|
|
24
|
+
apply: () => apply,
|
|
25
|
+
inject: () => inject,
|
|
26
|
+
name: () => name,
|
|
27
|
+
usage: () => usage
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
var import_koishi2 = require("koishi");
|
|
31
|
+
|
|
32
|
+
// src/utils.ts
|
|
33
|
+
function logger_status(logger2, enable) {
|
|
34
|
+
if (enable) {
|
|
35
|
+
return logger2;
|
|
36
|
+
} else {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
__name(logger_status, "logger_status");
|
|
41
|
+
|
|
42
|
+
// src/config.ts
|
|
43
|
+
var import_koishi = require("koishi");
|
|
44
|
+
var Config = import_koishi.Schema.intersect([
|
|
45
|
+
import_koishi.Schema.object({
|
|
46
|
+
apiPrefix: import_koishi.Schema.string().default("/monetary").description("API 前缀"),
|
|
47
|
+
apiKey: import_koishi.Schema.string().default("").description("API 鉴权密钥,留空则不启用鉴权"),
|
|
48
|
+
loggerinfo: import_koishi.Schema.boolean().default(false).description("日志调试模式").experimental()
|
|
49
|
+
}).description("调试设置")
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
// src/index.ts
|
|
53
|
+
var name = "plugin-http-monetary";
|
|
54
|
+
var inject = ["monetary", "server", "logger"];
|
|
55
|
+
var usage = `
|
|
56
|
+
<hr>
|
|
57
|
+
<h2>浅浅的用http封装一下www(带鉴权版)</h2>
|
|
58
|
+
<hr>
|
|
59
|
+
`;
|
|
60
|
+
var logger = new import_koishi2.Logger("plugin-http-monetary");
|
|
61
|
+
function apply(ctx, config) {
|
|
62
|
+
const prefix = config.apiPrefix;
|
|
63
|
+
const apiKey = config.apiKey;
|
|
64
|
+
const checkAuth = /* @__PURE__ */ __name((koaCtx) => {
|
|
65
|
+
if (!apiKey) return true;
|
|
66
|
+
const authHeader = koaCtx.headers.authorization;
|
|
67
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
const token = authHeader.substring(7);
|
|
71
|
+
return token === apiKey;
|
|
72
|
+
}, "checkAuth");
|
|
73
|
+
const handleUnauthorized = /* @__PURE__ */ __name((koaCtx) => {
|
|
74
|
+
koaCtx.status = 401;
|
|
75
|
+
koaCtx.body = { error: "unauthorized" };
|
|
76
|
+
logger_status(logger, config.loggerinfo)?.warn(`鉴权失败: IP=${koaCtx.ip} 路径=${koaCtx.path}`);
|
|
77
|
+
}, "handleUnauthorized");
|
|
78
|
+
ctx.server.get(prefix + "/balance/:uid", async (koaCtx) => {
|
|
79
|
+
if (!checkAuth(koaCtx)) {
|
|
80
|
+
handleUnauthorized(koaCtx);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const koishiCtx = koaCtx.ctx;
|
|
84
|
+
const uid = parseInt(koaCtx.params.uid);
|
|
85
|
+
if (isNaN(uid)) {
|
|
86
|
+
koaCtx.status = 400;
|
|
87
|
+
koaCtx.body = { error: "invalid uid" };
|
|
88
|
+
logger_status(logger, config.loggerinfo)?.warn(`参数错误: uid=${koaCtx.params.uid} IP=${koaCtx.ip}`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const currency = koaCtx.query.currency || "default";
|
|
92
|
+
try {
|
|
93
|
+
const [data] = await koishiCtx.database.get("monetary", { uid, currency }, ["value"]);
|
|
94
|
+
koaCtx.body = {
|
|
95
|
+
uid,
|
|
96
|
+
currency,
|
|
97
|
+
balance: data?.value ?? 0
|
|
98
|
+
};
|
|
99
|
+
logger.info(`查询余额成功: uid=${uid} currency=${currency} balance=${data?.value ?? 0}`);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
koaCtx.status = 500;
|
|
102
|
+
koaCtx.body = { error: e.message };
|
|
103
|
+
logger_status(logger, config.loggerinfo)?.error(`查询余额失败: uid=${uid} error=${e.message}`);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
ctx.server.post(prefix + "/gain", async (koaCtx) => {
|
|
107
|
+
if (!checkAuth(koaCtx)) {
|
|
108
|
+
handleUnauthorized(koaCtx);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const koishiCtx = koaCtx.ctx;
|
|
112
|
+
const { uid, amount, currency } = koaCtx.request.body;
|
|
113
|
+
if (typeof uid !== "number" || typeof amount !== "number" || amount <= 0) {
|
|
114
|
+
koaCtx.status = 400;
|
|
115
|
+
koaCtx.body = { error: "invalid parameters: uid and amount must be positive numbers" };
|
|
116
|
+
logger_status(logger, config.loggerinfo)?.warn(`增加点数参数错误: body=${JSON.stringify(koaCtx.request.body)} IP=${koaCtx.ip}`);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
await koishiCtx.monetary.gain(uid, amount, currency || "default");
|
|
121
|
+
koaCtx.body = { success: true };
|
|
122
|
+
logger_status(logger, config.loggerinfo)?.info(`增加点数成功: uid=${uid} amount=${amount} currency=${currency || "default"}`);
|
|
123
|
+
} catch (e) {
|
|
124
|
+
koaCtx.status = 500;
|
|
125
|
+
koaCtx.body = { error: e.message };
|
|
126
|
+
logger_status(logger, config.loggerinfo)?.error(`增加点数失败: uid=${uid} error=${e.message}`);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
ctx.server.post(prefix + "/cost", async (koaCtx) => {
|
|
130
|
+
if (!checkAuth(koaCtx)) {
|
|
131
|
+
handleUnauthorized(koaCtx);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const koishiCtx = koaCtx.ctx;
|
|
135
|
+
const { uid, amount, currency } = koaCtx.request.body;
|
|
136
|
+
if (typeof uid !== "number" || typeof amount !== "number" || amount <= 0) {
|
|
137
|
+
koaCtx.status = 400;
|
|
138
|
+
koaCtx.body = { error: "invalid parameters: uid and amount must be positive numbers" };
|
|
139
|
+
logger_status(logger, config.loggerinfo)?.warn(`扣除点数参数错误: body=${JSON.stringify(koaCtx.request.body)} IP=${koaCtx.ip}`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
await koishiCtx.monetary.cost(uid, amount, currency || "default");
|
|
144
|
+
koaCtx.body = { success: true };
|
|
145
|
+
logger_status(logger, config.loggerinfo)?.info(`扣除点数成功: uid=${uid} amount=${amount} currency=${currency || "default"}`);
|
|
146
|
+
} catch (e) {
|
|
147
|
+
if (e.message === "insufficient balance.") {
|
|
148
|
+
koaCtx.status = 400;
|
|
149
|
+
koaCtx.body = { error: "insufficient balance" };
|
|
150
|
+
logger_status(logger, config.loggerinfo)?.warn(`扣除点数余额不足: uid=${uid} amount=${amount}`);
|
|
151
|
+
} else {
|
|
152
|
+
koaCtx.status = 500;
|
|
153
|
+
koaCtx.body = { error: e.message };
|
|
154
|
+
logger_status(logger, config.loggerinfo)?.error(`扣除点数失败: uid=${uid} error=${e.message}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
logger_status(logger, config.loggerinfo)?.info(`HTTP API 已挂载在 ${prefix},鉴权状态:${apiKey ? "已启用" : "未启用"}`);
|
|
159
|
+
}
|
|
160
|
+
__name(apply, "apply");
|
|
161
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
162
|
+
0 && (module.exports = {
|
|
163
|
+
Config,
|
|
164
|
+
apply,
|
|
165
|
+
inject,
|
|
166
|
+
name,
|
|
167
|
+
usage
|
|
168
|
+
});
|
package/lib/utils.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-http-monetary",
|
|
3
|
+
"description": "HTTP API for Koishi Monetary Plugin",
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"contributors": [
|
|
8
|
+
"FLY1919 <qw687255511@163.com>"
|
|
9
|
+
],
|
|
10
|
+
"files": [
|
|
11
|
+
"lib",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/FLY1919/plugin-http-monetary.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/FLY1919/plugin-http-monetary/issues"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc",
|
|
23
|
+
"pub": "npm run build && npm publish"
|
|
24
|
+
},
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"homepage": "https://github.com/FLY1919/plugin-http-monetary",
|
|
27
|
+
"koishi": {
|
|
28
|
+
"description": {
|
|
29
|
+
"zh": "Koishi Monetary 插件的 HTTP API 插件",
|
|
30
|
+
"en": "HTTP API plugin for Koishi Monetary plugin",
|
|
31
|
+
"ja": "Koishi Monetary プラグインの HTTP API プラグイン",
|
|
32
|
+
"ko": "Koishi Monetary 플러그인의 HTTP API 플러그인",
|
|
33
|
+
"zh-TW": "Koishi Monetary 插件的 HTTP API 插件",
|
|
34
|
+
"fr": "Plugin HTTP API pour le plugin Koishi Monetary",
|
|
35
|
+
"ru": "HTTP API плагин для плагина Koishi Monetary",
|
|
36
|
+
"de": "HTTP-API-Plugin für das Koishi Monetary Plugin",
|
|
37
|
+
"es": "Plugin HTTP API para el plugin Koishi Monetary"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"chatbot",
|
|
42
|
+
"koishi",
|
|
43
|
+
"plugin",
|
|
44
|
+
"monetary",
|
|
45
|
+
"http",
|
|
46
|
+
"api"
|
|
47
|
+
],
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"koishi": "4.18.10"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"koishi": "4.18.10"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# 🐱 喵点通 · Koishi HTTP Monetary 插件
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/koishi-plugin-plugin-http-monetary)
|
|
4
|
+
|
|
5
|
+
✨ **给 Koishi 的 monetary 服务加上萌萌的 HTTP 小爪子~**
|
|
6
|
+
主人可以通过 REST API 查询余额、增加/扣除点数,还能用暗号保护咱的小金库哦。
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 📦 安装
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install koishi-plugin-plugin-http-monetary
|
|
14
|
+
# 或
|
|
15
|
+
yarn add koishi-plugin-plugin-http-monetary
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
> 🚨 **需要一起玩耍的小伙伴**:
|
|
19
|
+
> - [`koishi-plugin-monetary`](https://www.npmjs.com/package/koishi-plugin-monetary) —— 点数系统的家
|
|
20
|
+
> - [`@koishijs/plugin-server`](https://www.npmjs.com/package/@koishijs/plugin-server) —— 开门迎客的 HTTP 服务
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## ⚙️ 配置项
|
|
25
|
+
|
|
26
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
27
|
+
|------|------|--------|------|
|
|
28
|
+
| `apiPrefix` | `string` | `'/monetary'` | API 路由前缀,不喜欢可以改喵 |
|
|
29
|
+
| `apiKey` | `string` | `''` | 访问密钥,留空就是公开访问(慎用哦) |
|
|
30
|
+
| `loggerinfo` | `boolean` | `false` | 调试模式,打开后咱会多说点话 |
|
|
31
|
+
|
|
32
|
+
**配置示例**(在 `koishi.config.yml` 里):
|
|
33
|
+
```yaml
|
|
34
|
+
plugin-http-monetary:
|
|
35
|
+
apiPrefix: '/money' # 改成 /money 也认得
|
|
36
|
+
apiKey: 'meow-secret-key' # 只有知道暗号的人才能调用
|
|
37
|
+
loggerinfo: true # 想听咱碎碎念就打开吧
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 🛣️ API 路由
|
|
43
|
+
|
|
44
|
+
所有接口都返回 JSON。如果设置了 `apiKey`,请求头必须包含:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
Authorization: Bearer <你的密钥>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
### 🔍 查询余额
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
GET /monetary/balance/:uid?currency=<币种>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**路径参数**
|
|
59
|
+
- `uid`:用户 ID(数字)
|
|
60
|
+
|
|
61
|
+
**查询参数**
|
|
62
|
+
- `currency`:币种,默认 `'default'`
|
|
63
|
+
|
|
64
|
+
**示例**
|
|
65
|
+
```bash
|
|
66
|
+
curl "http://localhost:8080/monetary/balance/12345?currency=gold"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**成功响应**
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"uid": 12345,
|
|
73
|
+
"currency": "gold",
|
|
74
|
+
"balance": 100
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**错误响应**
|
|
79
|
+
- `400`:uid 格式不对
|
|
80
|
+
- `401`:未授权(密钥错误或缺失)
|
|
81
|
+
- `500`:服务器内部错误
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
### ➕ 增加点数
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
POST /monetary/gain
|
|
89
|
+
Content-Type: application/json
|
|
90
|
+
|
|
91
|
+
{
|
|
92
|
+
"uid": 12345,
|
|
93
|
+
"amount": 50,
|
|
94
|
+
"currency": "gold" // 可选,默认为 default
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**示例**
|
|
99
|
+
```bash
|
|
100
|
+
curl -X POST http://localhost:8080/monetary/gain \
|
|
101
|
+
-H "Authorization: Bearer meow-secret-key" \
|
|
102
|
+
-H "Content-Type: application/json" \
|
|
103
|
+
-d '{"uid": 12345, "amount": 50}'
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**成功响应**
|
|
107
|
+
```json
|
|
108
|
+
{ "success": true }
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**错误响应**
|
|
112
|
+
- `400`:参数无效(如 amount ≤ 0)
|
|
113
|
+
- `401`:未授权
|
|
114
|
+
- `500`:内部错误
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
### ➖ 扣除点数
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
POST /monetary/cost
|
|
122
|
+
Content-Type: application/json
|
|
123
|
+
|
|
124
|
+
{
|
|
125
|
+
"uid": 12345,
|
|
126
|
+
"amount": 30,
|
|
127
|
+
"currency": "gold"
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**示例**
|
|
132
|
+
```bash
|
|
133
|
+
curl -X POST http://localhost:8080/monetary/cost \
|
|
134
|
+
-H "Authorization: Bearer meow-secret-key" \
|
|
135
|
+
-H "Content-Type: application/json" \
|
|
136
|
+
-d '{"uid": 12345, "amount": 30}'
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**成功响应**
|
|
140
|
+
```json
|
|
141
|
+
{ "success": true }
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**余额不足时**(HTTP 400)
|
|
145
|
+
```json
|
|
146
|
+
{ "error": "insufficient balance" }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**其他错误**
|
|
150
|
+
- `400`:参数无效
|
|
151
|
+
- `401`:未授权
|
|
152
|
+
- `500`:内部错误
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## 🐾 调试模式
|
|
157
|
+
|
|
158
|
+
将 `loggerinfo` 设为 `true`,咱就会在控制台里轻声细语地记录每次请求,帮主人揪出捣蛋鬼。
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## 💌 来自猫娘的叮嘱
|
|
163
|
+
|
|
164
|
+
- 如果 `apiKey` 留空,任何人都能调用 API,请务必在安全网络下使用哦。
|
|
165
|
+
- 记得先让 `@koishijs/plugin-server` 起床,不然咱开不了门。
|
|
166
|
+
- 数据库表由 `monetary` 自动创建,咱只负责跑腿。
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## 📄 许可证
|
|
171
|
+
|
|
172
|
+
Apache-2.0 — 想怎么玩就怎么玩,不过要是把咱弄哭了,可是要负责的哦 (,,•́ . •̀,,)
|