2026-05-30 19:55:48 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2026-08-17 14:12:30 +08:00
|
|
|
"apigo.cc/go/cast"
|
2026-05-30 19:55:48 +08:00
|
|
|
"apigo.cc/go/jsmod"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
jsmod.Register("api", map[string]any{
|
2026-06-10 12:09:04 +08:00
|
|
|
"Call": call,
|
|
|
|
|
"SetConfig": SetConfig,
|
|
|
|
|
"RegisterAction": RegisterAction,
|
|
|
|
|
"RegisterSigner": registerSigner,
|
2026-08-17 12:39:27 +08:00
|
|
|
"Encrypt": Encrypt,
|
2026-05-30 19:55:48 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 00:14:26 +08:00
|
|
|
// call 提供给 JS 的私有入口
|
2026-08-17 12:39:27 +08:00
|
|
|
func call(ctx context.Context, name string, payload any, options ...*CallOptions) (any, error) {
|
2026-06-10 12:09:04 +08:00
|
|
|
// 将 ctx 传入以透传追踪信息给 JS 签名器
|
2026-08-17 12:39:27 +08:00
|
|
|
opts := firstOptions(options)
|
|
|
|
|
opts.Context = ctx
|
|
|
|
|
res, err := CallBy(name, payload, opts)
|
2026-08-17 14:12:30 +08:00
|
|
|
if actionUsesDataResult(name) {
|
|
|
|
|
return projectDataResult(res), nil
|
|
|
|
|
}
|
2026-06-21 10:35:35 +08:00
|
|
|
if err != nil {
|
2026-08-17 12:39:27 +08:00
|
|
|
if res != nil {
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
2026-06-21 10:35:35 +08:00
|
|
|
return nil, jsmod.MakeError(err)
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
2026-05-30 19:55:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-17 14:12:30 +08:00
|
|
|
func actionUsesDataResult(name string) bool {
|
|
|
|
|
config, err := resolveGenericActionConfig(name, map[string]bool{})
|
|
|
|
|
return err == nil && cast.String(config["resultMode"]) == "data"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func projectDataResult(result *Result) any {
|
|
|
|
|
if result == nil {
|
|
|
|
|
return map[string]any{"ok": false, "code": "", "error": "empty API result"}
|
|
|
|
|
}
|
|
|
|
|
if data, ok := result.Data.(map[string]any); ok {
|
|
|
|
|
output := cloneMap(data)
|
|
|
|
|
if _, exists := output["ok"]; !exists {
|
|
|
|
|
output["ok"] = result.Ok
|
|
|
|
|
}
|
|
|
|
|
if _, exists := output["code"]; !exists {
|
|
|
|
|
output["code"] = result.Code
|
|
|
|
|
}
|
|
|
|
|
if _, exists := output["error"]; !exists {
|
|
|
|
|
output["error"] = result.Error
|
|
|
|
|
}
|
|
|
|
|
return output
|
|
|
|
|
}
|
|
|
|
|
return map[string]any{
|
|
|
|
|
"ok": result.Ok, "code": result.Code, "error": result.Error, "result": result.Data,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 00:14:26 +08:00
|
|
|
// registerSigner 允许从 JS 注册动态签名逻辑
|
|
|
|
|
func registerSigner(name string, code string) {
|
|
|
|
|
RegisterSigner(name, &jsSigner{code: code})
|
2026-05-30 19:55:48 +08:00
|
|
|
}
|