pwish-cli 0.0.0-stage → 0.1.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/CHANGELOG.md +43 -0
- package/LICENSE +27 -0
- package/README.md +262 -2
- package/VERSIONING.md +33 -0
- package/bin/pwish.js +23 -0
- package/docs/screenshots/login-password.png +0 -0
- package/docs/screenshots/login-qr.png +0 -0
- package/package.json +51 -4
- package/scripts/check.js +72 -0
- package/scripts/format-check.js +46 -0
- package/skill/pwish/SKILL.md +146 -0
- package/src/api.js +110 -0
- package/src/args.js +102 -0
- package/src/auth-server.js +300 -0
- package/src/browser-token.js +348 -0
- package/src/cli.js +624 -0
- package/src/config.js +91 -0
- package/src/login-page.js +269 -0
- package/src/models.js +155 -0
- package/src/output.js +26 -0
- package/src/passport.js +129 -0
- package/src/suite.js +129 -0
- package/src/tasks.js +154 -0
- package/src/upload.js +112 -0
package/src/config.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
const HOME = os.homedir();
|
|
6
|
+
export const CONFIG_DIR = process.env.PWISH_CONFIG_DIR || path.join(HOME, ".config", "pwish");
|
|
7
|
+
export const CONFIG_PATH = path.join(CONFIG_DIR, "config.json");
|
|
8
|
+
|
|
9
|
+
const DEFAULTS = {
|
|
10
|
+
apiBase: "https://aw.aoscdn.com/app/picwish",
|
|
11
|
+
productId: 482,
|
|
12
|
+
language: "zh",
|
|
13
|
+
token: "",
|
|
14
|
+
outDir: "out",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function loadConfig() {
|
|
18
|
+
let file = {};
|
|
19
|
+
if (fs.existsSync(CONFIG_PATH)) {
|
|
20
|
+
try {
|
|
21
|
+
file = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
|
|
22
|
+
if (!file || typeof file !== "object" || Array.isArray(file)) throw new Error("格式不正确");
|
|
23
|
+
} catch (err) {
|
|
24
|
+
throw new UserError(
|
|
25
|
+
`配置文件损坏:${CONFIG_PATH}(${err.message})。请删除该文件后重新执行 pwish login。`,
|
|
26
|
+
"BAD_CONFIG",
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const cfg = { ...DEFAULTS, ...file };
|
|
31
|
+
if (process.env.PWISH_TOKEN) cfg.token = process.env.PWISH_TOKEN;
|
|
32
|
+
if (process.env.PWISH_API_BASE) cfg.apiBase = process.env.PWISH_API_BASE;
|
|
33
|
+
if (process.env.PWISH_OUT) cfg.outDir = process.env.PWISH_OUT;
|
|
34
|
+
return cfg;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function saveConfig(patch) {
|
|
38
|
+
const current = loadConfig();
|
|
39
|
+
const next = { ...current, ...patch };
|
|
40
|
+
delete next.tokenFix;
|
|
41
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
42
|
+
fs.writeFileSync(CONFIG_PATH, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
43
|
+
try {
|
|
44
|
+
fs.chmodSync(CONFIG_PATH, 0o600);
|
|
45
|
+
} catch {
|
|
46
|
+
/* best effort on exotic filesystems */
|
|
47
|
+
}
|
|
48
|
+
return next;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** passport token 形如 `v2,<user_id>,482,<secret>`;第二段是 user_id。 */
|
|
52
|
+
export function userIdOfToken(token) {
|
|
53
|
+
const parts = String(token || "").split(",");
|
|
54
|
+
const id = Number(parts[1]);
|
|
55
|
+
return Number.isFinite(id) && id > 0 ? id : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function resolveToken(cfg) {
|
|
59
|
+
const token = (cfg?.token || "").trim();
|
|
60
|
+
if (!token) {
|
|
61
|
+
throw new UserError(
|
|
62
|
+
"尚未登录。先执行 `pwish login`(会自动读取已登录浏览器),或设置环境变量 PWISH_TOKEN。",
|
|
63
|
+
"NO_TOKEN",
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return token;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class UserError extends Error {
|
|
70
|
+
constructor(message, code = "USER_ERROR", hint = "") {
|
|
71
|
+
super(message);
|
|
72
|
+
this.name = "UserError";
|
|
73
|
+
this.code = code;
|
|
74
|
+
this.hint = hint;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Parse `passport_user_info` JSON (optional, only for display). */
|
|
79
|
+
export function readCachedUser() {
|
|
80
|
+
try {
|
|
81
|
+
const raw = JSON.parse(fs.readFileSync(path.join(CONFIG_DIR, "user.json"), "utf8"));
|
|
82
|
+
return raw;
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function writeCachedUser(user) {
|
|
89
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
90
|
+
fs.writeFileSync(path.join(CONFIG_DIR, "user.json"), `${JSON.stringify(user, null, 2)}\n`, { mode: 0o600 });
|
|
91
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 授权登录页的 HTML。
|
|
3
|
+
* 视觉对齐佐糖官网登录页:浅蓝渐变背景 + 白色卡片 + 品牌紫蓝渐变主按钮。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const CSS = `
|
|
7
|
+
:root{
|
|
8
|
+
--brand:#5555FF; --brand-dark:#3D3DF6; --brand-soft:#EEEEFF;
|
|
9
|
+
--text:#333333; --text-sub:#666666; --text-mute:#999999;
|
|
10
|
+
--line:#E5E7EB; --field:#FAFAFA; --radius:6px;
|
|
11
|
+
}
|
|
12
|
+
*{box-sizing:border-box}
|
|
13
|
+
html,body{height:100%}
|
|
14
|
+
body{
|
|
15
|
+
margin:0;color:var(--text);
|
|
16
|
+
font:16px/1.6 "Microsoft YaHei","微软雅黑","PingFang SC","Hiragino Sans GB","Helvetica Neue",Arial,sans-serif;
|
|
17
|
+
background:
|
|
18
|
+
radial-gradient(1100px 620px at 82% 12%,#dbeafe 0%,rgba(219,234,254,0) 62%),
|
|
19
|
+
radial-gradient(900px 560px at 12% 88%,#e9e4ff 0%,rgba(233,228,255,0) 60%),
|
|
20
|
+
linear-gradient(160deg,#f4f7ff 0%,#eef4ff 45%,#eaf1ff 100%);
|
|
21
|
+
display:flex;align-items:center;justify-content:center;
|
|
22
|
+
}
|
|
23
|
+
.dots{position:fixed;top:72px;left:86px;width:180px;height:88px;pointer-events:none;
|
|
24
|
+
background-image:radial-gradient(circle,#a9c2f5 1.6px,transparent 1.7px);
|
|
25
|
+
background-size:26px 22px;opacity:.85}
|
|
26
|
+
.shell{display:flex;align-items:center;gap:clamp(40px,9vw,150px);padding:32px}
|
|
27
|
+
.brand{max-width:330px}
|
|
28
|
+
.mark{display:flex;align-items:center;gap:12px;margin-bottom:14px}
|
|
29
|
+
.mark svg{width:44px;height:44px}
|
|
30
|
+
.mark .name{font-size:30px;font-weight:700;letter-spacing:1px;
|
|
31
|
+
background:linear-gradient(270deg,var(--brand),var(--brand-dark));
|
|
32
|
+
-webkit-background-clip:text;background-clip:text;color:transparent}
|
|
33
|
+
.slogan{color:var(--brand);font-size:16px;letter-spacing:.4px}
|
|
34
|
+
.card{
|
|
35
|
+
width:374px;background:#fff;border-radius:12px;padding:40px 40px 28px;
|
|
36
|
+
box-shadow:0 18px 48px rgba(85,85,255,.12),0 2px 8px rgba(17,24,39,.04);
|
|
37
|
+
position:relative;
|
|
38
|
+
}
|
|
39
|
+
.corner{position:absolute;top:0;right:0;width:52px;height:46px;overflow:hidden;border-radius:0 12px 0 0;
|
|
40
|
+
background-image:radial-gradient(circle,rgba(85,85,255,.45) 1.5px,transparent 1.6px);background-size:9px 9px}
|
|
41
|
+
h1{font-size:20px;font-weight:600;text-align:center;margin:0 0 26px}
|
|
42
|
+
.tabs{display:flex;justify-content:center;gap:28px;margin-bottom:22px}
|
|
43
|
+
.tab{background:none;border:0;padding:6px 2px;font-size:15px;color:var(--text-sub);cursor:pointer;
|
|
44
|
+
border-bottom:2px solid transparent;font-family:inherit}
|
|
45
|
+
.tab[aria-selected=true]{color:var(--brand);border-bottom-color:var(--brand);font-weight:600}
|
|
46
|
+
.field{position:relative;margin-bottom:14px}
|
|
47
|
+
input{
|
|
48
|
+
width:100%;height:46px;padding:0 14px;font-size:16px;color:var(--text);font-family:inherit;
|
|
49
|
+
background:var(--field);border:1px solid var(--line);border-radius:var(--radius);outline:none;
|
|
50
|
+
transition:border-color .18s,background .18s;
|
|
51
|
+
}
|
|
52
|
+
input::placeholder{color:var(--text-mute)}
|
|
53
|
+
input:focus{border-color:var(--brand);background:#fff}
|
|
54
|
+
.submit{
|
|
55
|
+
width:100%;height:46px;margin-top:8px;border:0;border-radius:var(--radius);cursor:pointer;
|
|
56
|
+
color:#fff;font-size:16px;font-family:inherit;font-weight:500;
|
|
57
|
+
background:linear-gradient(270deg,var(--brand) 50%,var(--brand-dark));
|
|
58
|
+
transition:filter .18s,opacity .18s;
|
|
59
|
+
}
|
|
60
|
+
.submit:hover{filter:brightness(1.06)}
|
|
61
|
+
.submit:disabled{opacity:.6;cursor:default}
|
|
62
|
+
.hint{min-height:22px;margin-top:12px;font-size:13px;color:#e55757;text-align:center}
|
|
63
|
+
.hint.ok{color:#16a34a}
|
|
64
|
+
.qr-wrap{text-align:center}
|
|
65
|
+
.qr-wrap img{width:190px;height:190px;border:1px solid var(--line);border-radius:8px;background:#fff}
|
|
66
|
+
.qr-tip{margin-top:12px;font-size:14px;color:var(--text-sub)}
|
|
67
|
+
.qr-tip b{color:var(--brand)}
|
|
68
|
+
.foot{margin-top:22px;padding-top:16px;border-top:1px solid var(--line);
|
|
69
|
+
font-size:12.5px;color:var(--text-mute);text-align:center;line-height:1.7}
|
|
70
|
+
.foot code{background:var(--field);border:1px solid var(--line);border-radius:4px;padding:0 4px}
|
|
71
|
+
.switch{margin-top:14px;text-align:center;font-size:14px;color:var(--text-sub)}
|
|
72
|
+
.switch a{color:var(--brand);text-decoration:none}
|
|
73
|
+
.hidden{display:none !important}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
const LOGO_SVG = `<svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
|
77
|
+
<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
|
|
78
|
+
<stop offset="0" stop-color="#5555FF"/><stop offset="1" stop-color="#3D3DF6"/></linearGradient></defs>
|
|
79
|
+
<circle cx="15" cy="15" r="4.6" fill="url(#g)" opacity=".55"/>
|
|
80
|
+
<circle cx="33" cy="15" r="3.2" fill="url(#g)" opacity=".35"/>
|
|
81
|
+
<path d="M24 12c8 0 13 5.4 13 12s-5 12-13 12-13-5.4-13-12 5-12 13-12z" fill="url(#g)" opacity=".16"/>
|
|
82
|
+
<path d="M24 17c5.2 0 8.6 3.3 8.6 7.2S29.2 31.4 24 31.4 15.4 28.1 15.4 24.2 18.8 17 24 17z" fill="url(#g)"/>
|
|
83
|
+
</svg>`;
|
|
84
|
+
|
|
85
|
+
export function renderLoginPage({ origin, state, redirectUri }) {
|
|
86
|
+
return `<!doctype html>
|
|
87
|
+
<html lang="zh-CN">
|
|
88
|
+
<head>
|
|
89
|
+
<meta charset="utf-8">
|
|
90
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
91
|
+
<title>登录并授权 pwish</title>
|
|
92
|
+
<style>${CSS}</style>
|
|
93
|
+
</head>
|
|
94
|
+
<body>
|
|
95
|
+
<div class="dots"></div>
|
|
96
|
+
<div class="shell">
|
|
97
|
+
<div class="brand">
|
|
98
|
+
<div class="mark">${LOGO_SVG}<span class="name">佐糖</span></div>
|
|
99
|
+
<div class="slogan">用AI让图像处理更简单</div>
|
|
100
|
+
</div>
|
|
101
|
+
|
|
102
|
+
<div class="card">
|
|
103
|
+
<div class="corner"></div>
|
|
104
|
+
<h1>登录后授权 pwish</h1>
|
|
105
|
+
|
|
106
|
+
<div class="tabs" role="tablist">
|
|
107
|
+
<button class="tab" role="tab" data-tab="qr" aria-selected="true">微信扫码</button>
|
|
108
|
+
<button class="tab" role="tab" data-tab="pwd" aria-selected="false">密码登录</button>
|
|
109
|
+
</div>
|
|
110
|
+
|
|
111
|
+
<section id="pane-qr" class="qr-wrap">
|
|
112
|
+
<img id="qr" alt="微信登录二维码" width="190" height="190">
|
|
113
|
+
<div class="qr-tip">请使用 <b>微信</b> 扫码登录</div>
|
|
114
|
+
<div class="switch"><a href="#" id="qr-refresh">刷新二维码</a></div>
|
|
115
|
+
</section>
|
|
116
|
+
|
|
117
|
+
<section id="pane-pwd" class="hidden">
|
|
118
|
+
<div class="field"><input id="account" autocomplete="username" placeholder="手机号或邮箱"></div>
|
|
119
|
+
<div class="field"><input id="password" type="password" autocomplete="current-password" placeholder="密码"></div>
|
|
120
|
+
<button class="submit" id="do-login">登录</button>
|
|
121
|
+
</section>
|
|
122
|
+
|
|
123
|
+
<div class="hint" id="hint"></div>
|
|
124
|
+
|
|
125
|
+
<div class="foot">
|
|
126
|
+
凭据仅用于换取登录态,密码不会保存。<br>
|
|
127
|
+
授权后关闭本页即可,终端会自动继续。
|
|
128
|
+
</div>
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
131
|
+
|
|
132
|
+
<script>
|
|
133
|
+
(function () {
|
|
134
|
+
var ORIGIN = ${JSON.stringify(origin)};
|
|
135
|
+
var STATE = ${JSON.stringify(state)};
|
|
136
|
+
var hintEl = document.getElementById("hint");
|
|
137
|
+
var qrImg = document.getElementById("qr");
|
|
138
|
+
var pollTimer = null;
|
|
139
|
+
|
|
140
|
+
function hint(msg, ok) {
|
|
141
|
+
hintEl.textContent = msg || "";
|
|
142
|
+
hintEl.className = "hint" + (ok ? " ok" : "");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function finish(token) {
|
|
146
|
+
hint("登录成功,正在授权…", true);
|
|
147
|
+
fetch("/session", {
|
|
148
|
+
method: "POST",
|
|
149
|
+
headers: { "Content-Type": "application/json" },
|
|
150
|
+
body: JSON.stringify({ token: token, state: STATE })
|
|
151
|
+
})
|
|
152
|
+
.then(function (r) { return r.json(); })
|
|
153
|
+
.then(function (j) {
|
|
154
|
+
if (j.redirect) location.href = j.redirect;
|
|
155
|
+
else hint(j.message || "授权失败");
|
|
156
|
+
})
|
|
157
|
+
.catch(function () { hint("与本机服务的连接中断,请重新执行 pwish login"); });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/* ---------------- tabs ---------------- */
|
|
161
|
+
var tabs = document.querySelectorAll(".tab");
|
|
162
|
+
function show(name) {
|
|
163
|
+
tabs.forEach(function (t) { t.setAttribute("aria-selected", String(t.dataset.tab === name)); });
|
|
164
|
+
document.getElementById("pane-qr").classList.toggle("hidden", name !== "qr");
|
|
165
|
+
document.getElementById("pane-pwd").classList.toggle("hidden", name !== "pwd");
|
|
166
|
+
hint("");
|
|
167
|
+
}
|
|
168
|
+
tabs.forEach(function (t) { t.onclick = function () { show(t.dataset.tab); }; });
|
|
169
|
+
|
|
170
|
+
/* ---------------- 微信扫码 ---------------- */
|
|
171
|
+
function stopPolling() { if (pollTimer) { clearInterval(pollTimer); pollTimer = null; } }
|
|
172
|
+
|
|
173
|
+
function loadQr() {
|
|
174
|
+
stopPolling();
|
|
175
|
+
hint("正在获取二维码…", true);
|
|
176
|
+
fetch("/api/qrcode", { method: "POST" })
|
|
177
|
+
.then(function (r) { return r.json(); })
|
|
178
|
+
.then(function (j) {
|
|
179
|
+
if (!j.qrUrl) { hint(j.message || "获取二维码失败"); return; }
|
|
180
|
+
qrImg.src = j.qrUrl;
|
|
181
|
+
hint("");
|
|
182
|
+
pollTimer = setInterval(function () {
|
|
183
|
+
fetch("/api/qrcode/poll", {
|
|
184
|
+
method: "POST",
|
|
185
|
+
headers: { "Content-Type": "application/json" },
|
|
186
|
+
body: JSON.stringify({ hashKey: j.hashKey })
|
|
187
|
+
})
|
|
188
|
+
.then(function (r) { return r.json(); })
|
|
189
|
+
.then(function (p) {
|
|
190
|
+
if (p.status === "waiting") return;
|
|
191
|
+
stopPolling();
|
|
192
|
+
if (p.token) finish(p.token);
|
|
193
|
+
else { hint(p.message || "登录失败"); }
|
|
194
|
+
})
|
|
195
|
+
.catch(function () { stopPolling(); hint("轮询失败,请刷新二维码"); });
|
|
196
|
+
}, 2000);
|
|
197
|
+
})
|
|
198
|
+
.catch(function () { hint("无法连接本机服务"); });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
document.getElementById("qr-refresh").onclick = function (e) { e.preventDefault(); loadQr(); };
|
|
202
|
+
|
|
203
|
+
/* ---------------- 密码登录 ---------------- */
|
|
204
|
+
var btn = document.getElementById("do-login");
|
|
205
|
+
btn.onclick = function () {
|
|
206
|
+
var account = document.getElementById("account").value.trim();
|
|
207
|
+
var password = document.getElementById("password").value;
|
|
208
|
+
if (!account) { hint("请输入手机号或邮箱"); return; }
|
|
209
|
+
if (!password) { hint("请输入密码"); return; }
|
|
210
|
+
btn.disabled = true;
|
|
211
|
+
hint("正在登录…", true);
|
|
212
|
+
fetch("/api/login/password", {
|
|
213
|
+
method: "POST",
|
|
214
|
+
headers: { "Content-Type": "application/json" },
|
|
215
|
+
body: JSON.stringify({ account: account, password: password })
|
|
216
|
+
})
|
|
217
|
+
.then(function (r) { return r.json(); })
|
|
218
|
+
.then(function (j) {
|
|
219
|
+
btn.disabled = false;
|
|
220
|
+
if (j.token) finish(j.token);
|
|
221
|
+
else hint(j.message || "登录失败");
|
|
222
|
+
})
|
|
223
|
+
.catch(function () { btn.disabled = false; hint("无法连接本机服务"); });
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
document.getElementById("password").addEventListener("keydown", function (e) {
|
|
227
|
+
if (e.key === "Enter") btn.click();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
loadQr();
|
|
231
|
+
})();
|
|
232
|
+
</script>
|
|
233
|
+
</body>
|
|
234
|
+
</html>`;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function renderDonePage() {
|
|
238
|
+
return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8">
|
|
239
|
+
<meta name="viewport" content="width=device-width,initial-scale=1"><title>授权完成</title>
|
|
240
|
+
<style>${CSS}
|
|
241
|
+
body{flex-direction:column}
|
|
242
|
+
.tick{width:64px;height:64px;border-radius:50%;display:flex;align-items:center;justify-content:center;
|
|
243
|
+
background:linear-gradient(270deg,var(--brand),var(--brand-dark));margin:0 auto 18px}
|
|
244
|
+
.tick svg{width:30px;height:30px}
|
|
245
|
+
</style></head><body>
|
|
246
|
+
<div class="dots"></div>
|
|
247
|
+
<div class="card" style="text-align:center">
|
|
248
|
+
<div class="tick"><svg viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2.4"
|
|
249
|
+
stroke-linecap="round" stroke-linejoin="round"><path d="M20 6L9 17l-5-5"/></svg></div>
|
|
250
|
+
<h1 style="margin-bottom:10px">授权完成</h1>
|
|
251
|
+
<p style="color:var(--text-sub);font-size:14px;margin:0">pwish 已获取登录态,可以关闭本页了。</p>
|
|
252
|
+
<div class="foot">回到终端即可继续使用</div>
|
|
253
|
+
</div>
|
|
254
|
+
</body></html>`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function renderErrorPage(message) {
|
|
258
|
+
const safe = String(message).replace(/[<>&"]/g, (c) => ({ "<": "<", ">": ">", "&": "&", '"': """ })[c]);
|
|
259
|
+
return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8">
|
|
260
|
+
<meta name="viewport" content="width=device-width,initial-scale=1"><title>授权失败</title>
|
|
261
|
+
<style>${CSS}</style></head><body>
|
|
262
|
+
<div class="dots"></div>
|
|
263
|
+
<div class="card" style="text-align:center">
|
|
264
|
+
<h1>授权失败</h1>
|
|
265
|
+
<p style="color:#e55757;font-size:14px">${safe}</p>
|
|
266
|
+
<div class="foot">请回到终端重新执行 <code>pwish login</code></div>
|
|
267
|
+
</div>
|
|
268
|
+
</body></html>`;
|
|
269
|
+
}
|
package/src/models.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模型表来自站点前端 modelTypes 模块(picwish 前端 modelTypes.32ILtW8x.js)。
|
|
3
|
+
* payload 说明:
|
|
4
|
+
* - pattern : economy | stable(经济 / 稳定模式)
|
|
5
|
+
* - quality : GPT 系列取 high/default;GPT_IMAGE_2 系取 standard/high/super(由 1K/2K/2K+ 映射)
|
|
6
|
+
* - size : 比例,如 "1:1"、"9:16";"auto" 表示交给服务端按参考图判断
|
|
7
|
+
* - dimension: 仅 GPT_IMAGE_2 系需要,形如 "2048x2048"
|
|
8
|
+
*/
|
|
9
|
+
export const MODELS = {
|
|
10
|
+
"gemini-3": { id: 6, family: "nano", label: "电商模型 3.0(Gemini 3 / Nano Banana Pro)", credits: "10-20/张" },
|
|
11
|
+
"nano-banana-pro": { id: 72, family: "nano", label: "电商模型 2.0(Nano Banana 2 / Gemini 3.1)", credits: "10-20/张" },
|
|
12
|
+
"nano-banana-2-lite": { id: 61, family: "nano", label: "电商模型 1.0(Nano Banana 2 Lite)", credits: "5-10/张" },
|
|
13
|
+
"gpt-image-2": { id: 195, family: "gpt2", label: "GPT Image 2(补贴通用模型)", credits: "3-5/张" },
|
|
14
|
+
"gpt-image-2-flare": { id: 89, family: "gpt2", label: "GPT Image 2 Flare", credits: "3-5/张" },
|
|
15
|
+
"gpt-image-1.5": { id: 190, family: "gpt1", label: "GPT Image 1.5", credits: "5-15/张" },
|
|
16
|
+
"gpt-image-1": { id: 83, family: "gpt1", label: "GPT Image 1", credits: "15-60/张" },
|
|
17
|
+
"gemini-2.5": { id: 154, family: "nano", label: "Gemini 2.5 Flash Image", credits: "5-10/张" },
|
|
18
|
+
"seedream-5-pro": { id: 78, family: "seedream", label: "Seedream 5 Pro", credits: "按张计费" },
|
|
19
|
+
"seedream-5-flash": { id: 79, family: "seedream", label: "Seedream 5 Flash", credits: "按张计费" },
|
|
20
|
+
"seedream-5.5": { id: 94, family: "seedream", label: "Seedream 5.5", credits: "按张计费" },
|
|
21
|
+
"jimeng-4.5": { id: 41, family: "seedream", label: "即梦 4.5", credits: "按张计费" },
|
|
22
|
+
"jimeng-4.0": { id: 4, family: "seedream", label: "即梦 4.0", credits: "按张计费" },
|
|
23
|
+
flux: { id: 1, family: "flux", label: "FLUX", credits: "按张计费" },
|
|
24
|
+
google: { id: 5, family: "nano", label: "Google 原生生图", credits: "按张计费" },
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const MODEL_ALIASES = {
|
|
28
|
+
"电商模型3.0": "gemini-3",
|
|
29
|
+
"电商模型3": "gemini-3",
|
|
30
|
+
"gemini3": "gemini-3",
|
|
31
|
+
"电商模型2.0": "nano-banana-pro",
|
|
32
|
+
"nano-banana-2": "nano-banana-pro",
|
|
33
|
+
"banana": "nano-banana-pro",
|
|
34
|
+
"电商模型1.0": "nano-banana-2-lite",
|
|
35
|
+
"nano-banana": "nano-banana-2-lite",
|
|
36
|
+
"gpt2": "gpt-image-2",
|
|
37
|
+
"seedream-5": "seedream-5-pro",
|
|
38
|
+
"seedream": "seedream-5-pro",
|
|
39
|
+
"jimeng": "jimeng-4.5",
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const DEFAULT_MODEL = "gemini-3";
|
|
43
|
+
|
|
44
|
+
export function resolveModel(name) {
|
|
45
|
+
if (!name) return { key: DEFAULT_MODEL, ...MODELS[DEFAULT_MODEL] };
|
|
46
|
+
const raw = String(name).trim();
|
|
47
|
+
const key = MODELS[raw] ? raw : MODEL_ALIASES[raw.toLowerCase()] || MODEL_ALIASES[raw];
|
|
48
|
+
if (!key || !MODELS[key]) {
|
|
49
|
+
throw new Error(`未知模型:${name}。可用:${Object.keys(MODELS).join(", ")}`);
|
|
50
|
+
}
|
|
51
|
+
return { key, ...MODELS[key] };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 1K / 2K / 2K+ → standard / high / super */
|
|
55
|
+
export const SIZE_TO_QUALITY = { "1K": "standard", "2K": "high", "2K+": "super", "4k": "super", "4K": "super" };
|
|
56
|
+
|
|
57
|
+
/** 尺寸表(GPT_IMAGE_2 系需要显式 dimension) */
|
|
58
|
+
export const DIMENSIONS = {
|
|
59
|
+
"1:1": { standard: "1024x1024", high: "2048x2048", super: "2880x2880" },
|
|
60
|
+
"2:3": { standard: "1024x1536", high: "1360x2048", super: "2336x3504" },
|
|
61
|
+
"3:2": { standard: "1536x1024", high: "2048x1360", super: "3504x2336" },
|
|
62
|
+
"3:4": { standard: "1152x1536", high: "1536x2048", super: "2448x3264" },
|
|
63
|
+
"4:3": { standard: "1536x1152", high: "2048x1536", super: "3264x2448" },
|
|
64
|
+
"9:16": { standard: "864x1536", high: "1152x2048", super: "2160x3840" },
|
|
65
|
+
"16:9": { standard: "1536x864", high: "2048x1152", super: "3840x2160" },
|
|
66
|
+
"970:600": { standard: "1536x950", high: "2048x1268", super: "3072x1901" },
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const RATIOS = ["auto", ...Object.keys(DIMENSIONS)];
|
|
70
|
+
|
|
71
|
+
export function dimensionFor(ratio, sizeName) {
|
|
72
|
+
const table = DIMENSIONS[ratio] || DIMENSIONS["1:1"];
|
|
73
|
+
const level = SIZE_TO_QUALITY[sizeName] || "high";
|
|
74
|
+
return table[level] || table.high;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 电商套图场景:站点 UI 的 7 个选项里 5 个已确认可用。 */
|
|
78
|
+
export const SUITE_SCENES = {
|
|
79
|
+
main: "白底主图",
|
|
80
|
+
core: "核心卖点图",
|
|
81
|
+
selling: "卖点图",
|
|
82
|
+
material: "材质图",
|
|
83
|
+
person: "模特展示图",
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const SUITE_SITES = {
|
|
87
|
+
amazon: { country: "US", lang: "en", label: "亚马逊" },
|
|
88
|
+
taobao: { country: "CN", lang: "zh", label: "淘宝/天猫/1688" },
|
|
89
|
+
temu: { country: "US", lang: "en", label: "TEMU" },
|
|
90
|
+
pdd: { country: "CN", lang: "zh", label: "拼多多" },
|
|
91
|
+
shein: { country: "US", lang: "en", label: "SHEIN" },
|
|
92
|
+
ozon: { country: "RU", lang: "ru", label: "OZON" },
|
|
93
|
+
tiktok: { country: "US", lang: "en", label: "TikTok Shop" },
|
|
94
|
+
douyin: { country: "CN", lang: "zh", label: "抖音电商" },
|
|
95
|
+
shopee: { country: "WX_DNY", lang: "en", label: "Shopee" },
|
|
96
|
+
kuaishou: { country: "CN", lang: "zh", label: "快手电商" },
|
|
97
|
+
xiaohongshu: { country: "CN", lang: "zh", label: "小红书电商" },
|
|
98
|
+
ali: { country: "US", lang: "en", label: "阿里国际站" },
|
|
99
|
+
jd: { country: "CN", lang: "zh", label: "京东" },
|
|
100
|
+
walmart: { country: "US", lang: "en", label: "Walmart" },
|
|
101
|
+
coupang: { country: "KR", lang: "ko", label: "Coupang" },
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 组装 tasks/login/image-messages 的请求体。
|
|
106
|
+
* 不同模型族接受的字段不同,多余字段会被服务端忽略,缺失关键字段会报参数错误。
|
|
107
|
+
*/
|
|
108
|
+
export function buildImageMessagePayload({
|
|
109
|
+
prompt,
|
|
110
|
+
conversationId,
|
|
111
|
+
messageId,
|
|
112
|
+
modelId,
|
|
113
|
+
family,
|
|
114
|
+
files,
|
|
115
|
+
ratio = "auto",
|
|
116
|
+
sizeName = "2K",
|
|
117
|
+
pattern = "economy",
|
|
118
|
+
format = "png",
|
|
119
|
+
background = "auto",
|
|
120
|
+
isContinue = false,
|
|
121
|
+
}) {
|
|
122
|
+
const base = {
|
|
123
|
+
prompt,
|
|
124
|
+
...(files && files.length ? { files } : {}),
|
|
125
|
+
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
126
|
+
...(messageId ? { message_id: messageId } : {}),
|
|
127
|
+
model_type: modelId,
|
|
128
|
+
is_continue: isContinue,
|
|
129
|
+
};
|
|
130
|
+
const auto = !ratio || ratio === "auto";
|
|
131
|
+
const quality = SIZE_TO_QUALITY[sizeName] || "high";
|
|
132
|
+
|
|
133
|
+
switch (family) {
|
|
134
|
+
case "gpt2":
|
|
135
|
+
return {
|
|
136
|
+
...base,
|
|
137
|
+
pattern,
|
|
138
|
+
output_format: format,
|
|
139
|
+
background,
|
|
140
|
+
quality,
|
|
141
|
+
resolution: quality,
|
|
142
|
+
size: auto ? "" : ratio,
|
|
143
|
+
dimension: dimensionFor(auto ? "1:1" : ratio, sizeName),
|
|
144
|
+
};
|
|
145
|
+
case "gpt1":
|
|
146
|
+
return { ...base, output_format: format, quality: quality === "standard" ? "default" : "high", size: auto ? "" : ratio };
|
|
147
|
+
case "seedream":
|
|
148
|
+
return { ...base, output_format: format, quality, size: auto ? "" : ratio, quantity: -1 };
|
|
149
|
+
case "flux":
|
|
150
|
+
return { ...base, output_format: format, quality, size: auto ? "" : ratio };
|
|
151
|
+
case "nano":
|
|
152
|
+
default:
|
|
153
|
+
return { ...base, pattern, ...(quality === "high" || quality === "super" ? { quality } : {}), size: auto ? "" : ratio };
|
|
154
|
+
}
|
|
155
|
+
}
|
package/src/output.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** 终端输出层:stdout/stderr 与 ANSI 着色统一从这里走。 */
|
|
2
|
+
|
|
3
|
+
const tty = Boolean(process.stdout.isTTY);
|
|
4
|
+
|
|
5
|
+
/** 着色前缀(非 TTY 时为空字符串,保证管道输出干净)。 */
|
|
6
|
+
export const cyan = tty ? "\u001b[36m" : "";
|
|
7
|
+
export const green = tty ? "\u001b[32m" : "";
|
|
8
|
+
export const yellow = tty ? "\u001b[33m" : "";
|
|
9
|
+
export const red = tty ? "\u001b[31m" : "";
|
|
10
|
+
export const dim = tty ? "\u001b[2m" : "";
|
|
11
|
+
export const reset = tty ? "\u001b[0m" : "";
|
|
12
|
+
|
|
13
|
+
/** 进度/提示 → stderr,不污染可管道化的 stdout。 */
|
|
14
|
+
export function log(msg) {
|
|
15
|
+
process.stderr.write(`${msg}\n`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 结果 → stdout。 */
|
|
19
|
+
export function say(msg) {
|
|
20
|
+
process.stdout.write(`${msg}\n`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 机器可读输出。 */
|
|
24
|
+
export function json(obj) {
|
|
25
|
+
process.stdout.write(`${JSON.stringify(obj, null, 2)}\n`);
|
|
26
|
+
}
|
package/src/passport.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 佐糖账号登录接口客户端(自建登录页的后端)。
|
|
3
|
+
*
|
|
4
|
+
* 登录页本身由本机 loopback 服务提供,页面只与本机通信;
|
|
5
|
+
* 真正的账号校验由这里的函数转发到官方 passport 服务完成,
|
|
6
|
+
* 因此密码/扫码等敏感信息不经过任何第三方,也不落盘。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const PASSPORT_BASE = process.env.PWISH_PASSPORT_BASE || "https://gw.aoscdn.com/base/passport/v2";
|
|
10
|
+
|
|
11
|
+
/** 官方网页版登录使用的微信开放平台应用 id。 */
|
|
12
|
+
export const WECHAT_CLIENT_ID = "wxd6d7038d8f2449ea";
|
|
13
|
+
|
|
14
|
+
export class LoginError extends Error {
|
|
15
|
+
constructor(message, code = "LOGIN_FAILED") {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "LoginError";
|
|
18
|
+
this.code = code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 官方接口的状态码 → 用户可读文案。 */
|
|
23
|
+
const STATUS_MESSAGES = {
|
|
24
|
+
11000: "账号不存在",
|
|
25
|
+
11070: "验证码不正确",
|
|
26
|
+
11211: "邮箱未注册",
|
|
27
|
+
11212: "手机号未注册",
|
|
28
|
+
11216: "该账号未设置密码,请改用微信扫码登录",
|
|
29
|
+
11302: "微信应用配置缺失",
|
|
30
|
+
11308: "二维码已失效,请刷新",
|
|
31
|
+
11314: "等待扫码",
|
|
32
|
+
11317: "需要完成安全验证",
|
|
33
|
+
11320: "需要完成安全验证",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export function describeStatus(status, fallback = "") {
|
|
37
|
+
return STATUS_MESSAGES[status] || fallback || `登录失败(${status})`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function callPassport(path, { body, method = "POST", query = {} } = {}) {
|
|
41
|
+
const url = new URL(`${PASSPORT_BASE}/${path}`);
|
|
42
|
+
url.searchParams.set("product_id", "482");
|
|
43
|
+
url.searchParams.set("language", "zh");
|
|
44
|
+
url.searchParams.set("cli_os", "web");
|
|
45
|
+
for (const [k, v] of Object.entries(query)) url.searchParams.set(k, v);
|
|
46
|
+
|
|
47
|
+
const res = await fetch(url, {
|
|
48
|
+
method,
|
|
49
|
+
headers: { "Content-Type": "application/json", Origin: "https://picwish.cn", Referer: "https://picwish.cn/login" },
|
|
50
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
51
|
+
signal: AbortSignal.timeout(20_000),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
let payload;
|
|
55
|
+
try {
|
|
56
|
+
payload = await res.json();
|
|
57
|
+
} catch {
|
|
58
|
+
throw new LoginError(`登录服务返回了非 JSON 响应(HTTP ${res.status})`, "BAD_RESPONSE");
|
|
59
|
+
}
|
|
60
|
+
return { status: res.status, payload };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 统一把 {status,data} 响应转成 token;失败抛 LoginError。 */
|
|
64
|
+
function tokenFrom(payload) {
|
|
65
|
+
const data = payload?.data;
|
|
66
|
+
const token = data?.api_token;
|
|
67
|
+
if (payload?.status === 200 && token) {
|
|
68
|
+
return { token, userId: data.user_id ?? null, profile: data };
|
|
69
|
+
}
|
|
70
|
+
const status = payload?.status;
|
|
71
|
+
if (status && STATUS_MESSAGES[status]) {
|
|
72
|
+
throw new LoginError(describeStatus(status, payload?.message), String(status));
|
|
73
|
+
}
|
|
74
|
+
throw new LoginError(describeStatus(status, payload?.message || "登录失败"), "LOGIN_FAILED");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 手机号 / 邮箱 + 密码登录。 */
|
|
78
|
+
export async function loginWithPassword({ account, password }) {
|
|
79
|
+
const isEmail = String(account).includes("@");
|
|
80
|
+
if (!isEmail && !/^\d{6,15}$/.test(String(account))) {
|
|
81
|
+
throw new LoginError("请输入正确的手机号或邮箱", "INVALID_ACCOUNT");
|
|
82
|
+
}
|
|
83
|
+
if (!password) throw new LoginError("请输入密码", "INVALID_PASSWORD");
|
|
84
|
+
|
|
85
|
+
const path = isEmail ? "login/email" : "login/telephone";
|
|
86
|
+
const body = isEmail ? { account, password } : { telephone: account, password };
|
|
87
|
+
const { payload } = await callPassport(path, { body });
|
|
88
|
+
return tokenFrom(payload);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 申请微信扫码登录二维码。 */
|
|
92
|
+
export async function requestWechatQr() {
|
|
93
|
+
const { payload } = await callPassport("qrcode", { body: { client_id: WECHAT_CLIENT_ID } });
|
|
94
|
+
const data = payload?.data;
|
|
95
|
+
if (payload?.status !== 200 || !data?.hash_key || !data?.url) {
|
|
96
|
+
throw new LoginError(describeStatus(payload?.status, "无法获取微信二维码"), "QRCODE_FAILED");
|
|
97
|
+
}
|
|
98
|
+
return { hashKey: data.hash_key, qrUrl: data.url };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 轮询扫码结果。
|
|
103
|
+
* @returns {{status:"waiting"} | {status:"ok", token:string, userId:number|null}}
|
|
104
|
+
*/
|
|
105
|
+
export async function pollWechatQr(hashKey) {
|
|
106
|
+
const { payload } = await callPassport("login/qrcode", { body: { hash_key: hashKey } });
|
|
107
|
+
if (payload?.status === 11314) return { status: "waiting" };
|
|
108
|
+
if (payload?.status === 11308) throw new LoginError("二维码已失效,请刷新", "QRCODE_EXPIRED");
|
|
109
|
+
const result = tokenFrom(payload);
|
|
110
|
+
return { status: "ok", ...result };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 获取用户资料(登录成功后用于展示昵称/头像)。 */
|
|
114
|
+
export async function fetchProfile(token, userId) {
|
|
115
|
+
if (!userId) return null;
|
|
116
|
+
try {
|
|
117
|
+
const url = new URL(`${PASSPORT_BASE}/users/${userId}`);
|
|
118
|
+
url.searchParams.set("product_id", "482");
|
|
119
|
+
url.searchParams.set("language", "zh");
|
|
120
|
+
const res = await fetch(url, {
|
|
121
|
+
headers: { Authorization: `Bearer ${token}`, Origin: "https://picwish.cn" },
|
|
122
|
+
signal: AbortSignal.timeout(10_000),
|
|
123
|
+
});
|
|
124
|
+
const payload = await res.json();
|
|
125
|
+
return payload?.data ?? null;
|
|
126
|
+
} catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|