158 lines
6.0 KiB
TypeScript
158 lines
6.0 KiB
TypeScript
import { useEffect } from "react";
|
|
import { Alert, Button, Form, Input, Space } from "antd";
|
|
import type { MiniAppReleaseSettings } from "@personal-brand/api-client";
|
|
import { AdminSection } from "../../components/AdminSection.js";
|
|
import { MutationError } from "../../components/QueryState.js";
|
|
import { StatusTag } from "../../components/StatusTag.js";
|
|
|
|
export type MiniAppSettingsValues = {
|
|
appId: string;
|
|
apiBaseUrl: string;
|
|
privateKey?: string;
|
|
appSecret?: string;
|
|
};
|
|
|
|
interface MiniAppSettingsTabProps {
|
|
settings: MiniAppReleaseSettings | undefined;
|
|
saving: boolean;
|
|
error?: unknown;
|
|
onSave: (values: MiniAppSettingsValues) => void;
|
|
}
|
|
|
|
export function MiniAppSettingsTab({ settings, saving, error, onSave }: MiniAppSettingsTabProps) {
|
|
const [form] = Form.useForm<MiniAppSettingsValues>();
|
|
|
|
useEffect(() => {
|
|
if (!settings) return;
|
|
form.setFieldsValue({
|
|
appId: settings.appId,
|
|
apiBaseUrl: settings.apiBaseUrl,
|
|
privateKey: "",
|
|
appSecret: "",
|
|
});
|
|
}, [form, settings]);
|
|
|
|
return (
|
|
<div className="admin-stack admin-stack-lg">
|
|
<AdminSection
|
|
title="小程序配置"
|
|
description="AppID 同时用于代码上传和小程序登录;AppSecret 只用于 wx.login 换取 OpenID;上传私钥和 AppSecret 均使用 AES-256-GCM 加密保存。"
|
|
>
|
|
{settings?.buildError ? (
|
|
<Alert
|
|
type="warning"
|
|
showIcon
|
|
className="admin-offset-bottom"
|
|
title="小程序构建参数未就绪"
|
|
description={settings.buildError}
|
|
/>
|
|
) : (
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
className="admin-offset-bottom"
|
|
title={`请求域名 ${settings?.requestDomain ?? "未确认"}`}
|
|
description="该域名在构建时写入小程序包,必须与微信后台“开发设置 → 服务器域名 → request 合法域名”一致;不一致会导致小程序打开即加载失败。"
|
|
/>
|
|
)}
|
|
<Form form={form} layout="vertical" onFinish={onSave}>
|
|
<Form.Item
|
|
name="appId"
|
|
label="微信 AppID"
|
|
rules={[{ required: true, message: "请输入微信 AppID" }]}
|
|
>
|
|
<Input maxLength={100} autoComplete="off" />
|
|
</Form.Item>
|
|
<Form.Item
|
|
name="appSecret"
|
|
label="微信登录 AppSecret"
|
|
help={
|
|
settings?.hasStoredAppSecret
|
|
? "已加密保存。留空保持现有 AppSecret;修改 AppID 时必须同时填写新 AppID 对应的 AppSecret。"
|
|
: "从微信公众平台“开发管理 → 开发设置”复制。保存后立即生效,不需要重新上传小程序代码。"
|
|
}
|
|
rules={[
|
|
{
|
|
validator: (_rule, value: string) =>
|
|
!value || /^[A-Za-z0-9]{16,64}$/.test(value.trim())
|
|
? Promise.resolve()
|
|
: Promise.reject(new Error("AppSecret 应为 16-64 位字母或数字,请复制完整值")),
|
|
},
|
|
]}
|
|
>
|
|
<Input.Password
|
|
maxLength={64}
|
|
autoComplete="new-password"
|
|
placeholder={
|
|
settings?.hasStoredAppSecret ? "留空保持现有 AppSecret" : "填写该小程序的 AppSecret"
|
|
}
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item
|
|
name="apiBaseUrl"
|
|
label="小程序请求地址"
|
|
rules={[
|
|
{ required: true, message: "请输入小程序请求地址" },
|
|
{
|
|
validator: (_rule, value: string) =>
|
|
!value || /^https:\/\/[^/]+/.test(value)
|
|
? Promise.resolve()
|
|
: Promise.reject(
|
|
new Error(
|
|
"公网地址必须以 https:// 开头,例如 https://wechat.app.que01.top/api",
|
|
),
|
|
),
|
|
},
|
|
]}
|
|
extra="构建时写入小程序包,必须与微信后台“开发设置 → 服务器域名 → request 合法域名”一致。修改后需重新构建并上传代码才生效。"
|
|
>
|
|
<Input maxLength={200} placeholder="https://wechat.app.que01.top/api" />
|
|
</Form.Item>
|
|
<Form.Item
|
|
name="privateKey"
|
|
label="上传私钥"
|
|
help={
|
|
settings?.hasStoredCredentials
|
|
? "已加密保存。留空保持现有私钥,输入新值可替换。"
|
|
: "粘贴微信开发者工具导出的 private.<appid>.key 全文,必须保留换行。"
|
|
}
|
|
>
|
|
<Input.TextArea
|
|
rows={5}
|
|
autoComplete="off"
|
|
spellCheck={false}
|
|
placeholder={"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"}
|
|
className="admin-code-input"
|
|
/>
|
|
</Form.Item>
|
|
{!settings?.loginConfigured ? (
|
|
<Alert
|
|
type="warning"
|
|
showIcon
|
|
className="admin-offset-bottom"
|
|
title="微信登录尚不可用"
|
|
description="该品牌需要同时保存微信 AppID 和 AppSecret,体验版才能通过 wx.login 换取 OpenID。"
|
|
/>
|
|
) : null}
|
|
<Space wrap>
|
|
<Button type="primary" loading={saving} htmlType="submit">
|
|
保存小程序配置
|
|
</Button>
|
|
{settings?.hasStoredCredentials ? (
|
|
<StatusTag tone="success" label="已加密保存上传凭据" />
|
|
) : (
|
|
<StatusTag tone="warning" label="未配置上传私钥" />
|
|
)}
|
|
{settings?.hasStoredAppSecret ? (
|
|
<StatusTag tone="success" label="已加密保存登录凭据" />
|
|
) : (
|
|
<StatusTag tone="warning" label="未配置登录 AppSecret" />
|
|
)}
|
|
</Space>
|
|
<MutationError error={error} />
|
|
</Form>
|
|
</AdminSection>
|
|
</div>
|
|
);
|
|
}
|