| 119 | } |
| 120 | |
| 121 | async function getWxCode(account) { |
| 122 | const wxServerUrl = process.env.wx_server_url; |
| 123 | const wxAuth = process.env.wx_auth; |
| 124 | if (!wxServerUrl || !wxAuth) { |
| 125 | throw new Error("未配置 wx_server_url 或 wx_auth,无法获取code登录"); |
| 126 | } |
| 127 | |
| 128 | const res = await axios.post( |
| 129 | `${wxServerUrl.replace(/\/$/, "")}/wx/getuserinfo`, |
| 130 | { |
| 131 | appid: MINI_APPID, |
| 132 | openid: account, |
| 133 | }, |
| 134 | { |
| 135 | headers: { |
| 136 | auth: wxAuth, |
| 137 | "content-type": "application/json", |
| 138 | }, |
| 139 | timeout: 15000, |
| 140 | validateStatus: () => true, |
| 141 | } |
| 142 | ); |
| 143 | |
| 144 | const code = res.data?.code || res.data?.data?.code; |
| 145 | if (res.status !== 200 || !code) { |
| 146 | throw new Error(`获取code失败: HTTP ${res.status} ${JSON.stringify(res.data)}`); |
| 147 | } |
| 148 | |
| 149 | return code; |
| 150 | } |
| 151 | |
| 152 | async function loginByCode(account) { |
| 153 | $.log("🔐 正在获取code并登录..."); |