login-modal.component.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import { Component, Input, Output, EventEmitter, signal } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { FormsModule } from '@angular/forms';
  4. const API_HOST = 'https://server.fmode.cn';
  5. const APP_ID = 'ncloudmaster';
  6. @Component({
  7. selector: 'app-login-modal',
  8. standalone: true,
  9. imports: [CommonModule, FormsModule],
  10. template: `
  11. <div class="login-overlay" *ngIf="visible">
  12. <div class="login-modal">
  13. <!-- Logo -->
  14. <div class="logo-section">
  15. <div class="logo-icon">
  16. <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  17. <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
  18. </svg>
  19. </div>
  20. <h1 class="logo-title">BRAIN<span>HACK</span></h1>
  21. <p class="logo-subtitle">{{ qiweiTrial ? '登录后领取 7 天企业微服务试用' : qiweiSkill ? '登录后领取 7 天企微数字员工试用' : skillInstall ? '登录后开启 VOC 数据洞察' : '登录后继续' }}</p>
  22. </div>
  23. <div
  24. class="purchase-summary"
  25. *ngIf="skillInstall"
  26. [attr.aria-label]="qiweiTrial ? '企业微服务试用' : qiweiSkill ? '企业微信智能助手价格' : 'VOC 数据洞察体验价格'"
  27. >
  28. <div>
  29. <span>{{ qiweiTrial ? '企业微服务' : qiweiSkill ? '企业微信智能助手' : 'VOC 数据洞察体验' }}</span>
  30. <p>{{ qiweiTrial ? '手机号登录后填写姓名,自动开通 7 天试用' : qiweiSkill ? '手机号登录后接收验证码,领取 1 个试用席位' : '支付金额全部成为可用数据额度' }}</p>
  31. </div>
  32. <strong>7 天免费</strong>
  33. </div>
  34. <!-- 登录表单 -->
  35. <form class="login-form" (ngSubmit)="handleSubmit()">
  36. <!-- 手机号 -->
  37. <div class="form-group">
  38. <label for="phone">手机号</label>
  39. <div class="input-wrapper">
  40. <svg class="input-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
  41. <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/>
  42. </svg>
  43. <input
  44. id="phone"
  45. name="phone"
  46. type="tel"
  47. class="form-input"
  48. placeholder="请输入手机号"
  49. [(ngModel)]="phone"
  50. (ngModelChange)="validatePhone()"
  51. maxlength="11"
  52. [disabled]="isLoading()"
  53. />
  54. </div>
  55. </div>
  56. <!-- 验证码 -->
  57. <div class="form-group">
  58. <label for="code">验证码</label>
  59. <div class="code-input-wrapper">
  60. <div class="input-wrapper">
  61. <svg class="input-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
  62. <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
  63. </svg>
  64. <input
  65. id="code"
  66. name="code"
  67. type="text"
  68. class="form-input code-input"
  69. placeholder="请输入验证码"
  70. [(ngModel)]="code"
  71. maxlength="6"
  72. [disabled]="isLoading()"
  73. />
  74. </div>
  75. <button
  76. type="button"
  77. class="btn-send-code"
  78. (click)="sendCode()"
  79. [disabled]="!canSendCode() || isLoading() || countdown() > 0"
  80. >
  81. {{ countdown() > 0 ? countdown() + 's' : '发送验证码' }}
  82. </button>
  83. </div>
  84. </div>
  85. <!-- 错误提示 -->
  86. <div class="error-msg" *ngIf="errorMsg">{{errorMsg}}</div>
  87. <!-- 提交 -->
  88. <button type="submit" class="btn-submit" [disabled]="!canSubmit() || isLoading()">
  89. <span *ngIf="isLoading()" class="loading-spinner"></span>
  90. <span *ngIf="isLoading()">登录中...</span>
  91. <span *ngIf="!isLoading()">{{ qiweiTrial ? '登录并领取试用' : qiweiSkill ? '登录并领取试用' : skillInstall ? '登录并继续开通' : '立即登录' }}</span>
  92. </button>
  93. </form>
  94. <p class="footer-hint">登录即表示同意《用户协议》和《隐私政策》</p>
  95. </div>
  96. </div>
  97. `,
  98. styles: [`
  99. .login-overlay {
  100. position: fixed;
  101. top: 0; left: 0; right: 0; bottom: 0;
  102. background: rgba(0,0,0,0.85);
  103. backdrop-filter: blur(12px);
  104. display: flex;
  105. align-items: center;
  106. justify-content: center;
  107. padding: 24px 0;
  108. box-sizing: border-box;
  109. overflow-y: auto;
  110. z-index: 200;
  111. animation: fadeIn 0.3s ease-out;
  112. }
  113. @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
  114. .login-modal {
  115. position: relative;
  116. background: #0A0A0A;
  117. border: 1px solid rgba(0,212,255,0.15);
  118. border-radius: 20px;
  119. padding: 40px 36px;
  120. width: 90%;
  121. max-width: 420px;
  122. box-shadow: 0 25px 50px rgba(0,0,0,0.5), 0 0 30px rgba(0,212,255,0.08);
  123. animation: slideUp 0.4s ease-out;
  124. }
  125. @keyframes slideUp {
  126. from { opacity: 0; transform: translateY(30px); }
  127. to { opacity: 1; transform: translateY(0); }
  128. }
  129. .logo-section { text-align: center; margin-bottom: 32px; }
  130. .logo-icon {
  131. width: 64px; height: 64px;
  132. margin: 0 auto 16px;
  133. background: rgba(0,212,255,0.1);
  134. border: 1px solid rgba(0,212,255,0.3);
  135. border-radius: 16px;
  136. display: flex; align-items: center; justify-content: center;
  137. color: #00D4FF;
  138. animation: pulse 2s ease-in-out infinite;
  139. }
  140. @keyframes pulse {
  141. 0%,100% { box-shadow: 0 0 0 0 rgba(0,212,255,0.4); }
  142. 50% { box-shadow: 0 0 0 12px rgba(0,212,255,0); }
  143. }
  144. .logo-icon svg { width: 32px; height: 32px; }
  145. .logo-title {
  146. margin: 0 0 8px 0; font-size: 24px; font-weight: 900;
  147. color: #fff; letter-spacing: 2px;
  148. }
  149. .logo-title span { color: #00D4FF; }
  150. .logo-subtitle { margin: 0; font-size: 14px; color: #888; }
  151. .purchase-summary {
  152. display: flex; align-items: center; justify-content: space-between; gap: 20px;
  153. padding: 16px 18px;
  154. background: rgba(0,212,255,0.08);
  155. border: 1px solid rgba(0,212,255,0.22);
  156. border-radius: 12px;
  157. }
  158. .purchase-summary span { color: #fff; font-size: 14px; font-weight: 800; }
  159. .purchase-summary p { margin: 5px 0 0; color: rgba(224,224,224,0.58); font-size: 12px; }
  160. .purchase-summary strong { color: #00D4FF; font-size: 26px; white-space: nowrap; }
  161. .login-form { margin-top: 28px; }
  162. .form-group { margin-bottom: 20px; }
  163. .form-group label {
  164. display: block; margin-bottom: 8px;
  165. font-weight: 700; font-size: 12px;
  166. color: rgba(224,224,224,0.9);
  167. text-transform: uppercase; letter-spacing: 0.5px;
  168. }
  169. .input-wrapper { position: relative; display: flex; align-items: center; }
  170. .input-icon {
  171. position: absolute; left: 14px; width: 18px; height: 18px;
  172. color: rgba(224,224,224,0.4); pointer-events: none;
  173. }
  174. .form-input {
  175. width: 100%; padding: 12px 14px 12px 44px;
  176. background: rgba(20,20,20,0.8);
  177. border: 1px solid rgba(255,255,255,0.1);
  178. border-radius: 10px; font-size: 14px; color: #E0E0E0;
  179. transition: all 0.2s; box-sizing: border-box;
  180. }
  181. .form-input:focus {
  182. outline: none;
  183. border-color: rgba(0,212,255,0.5);
  184. box-shadow: 0 0 0 3px rgba(0,212,255,0.1);
  185. }
  186. .form-input::placeholder { color: rgba(224,224,224,0.4); }
  187. .form-input:disabled { opacity: 0.6; cursor: not-allowed; }
  188. .code-input-wrapper { display: flex; gap: 10px; }
  189. .code-input-wrapper .input-wrapper { flex: 1; }
  190. .code-input { letter-spacing: 4px; font-weight: 700; text-align: center; }
  191. .btn-send-code {
  192. padding: 12px 16px;
  193. background: rgba(0,212,255,0.1);
  194. border: 1px solid rgba(0,212,255,0.3);
  195. border-radius: 10px; color: #00D4FF;
  196. font-size: 13px; font-weight: 700;
  197. cursor: pointer; transition: all 0.2s;
  198. white-space: nowrap; min-width: 100px;
  199. }
  200. .btn-send-code:hover:not(:disabled) {
  201. background: rgba(0,212,255,0.2);
  202. border-color: rgba(0,212,255,0.5);
  203. }
  204. .btn-send-code:disabled { opacity: 0.5; cursor: not-allowed; }
  205. .btn-submit {
  206. width: 100%; display: flex; align-items: center; justify-content: center;
  207. gap: 8px; padding: 14px 20px; margin-top: 8px;
  208. background: linear-gradient(145deg, rgba(0,212,255,0.15), rgba(0,0,0,0.3));
  209. border: 1px solid rgba(0,212,255,0.4);
  210. border-radius: 12px; color: #00D4FF;
  211. font-size: 15px; font-weight: 800;
  212. cursor: pointer; transition: all 0.2s;
  213. }
  214. .btn-submit:hover:not(:disabled) {
  215. background: linear-gradient(145deg, rgba(0,212,255,0.25), rgba(0,0,0,0.2));
  216. border-color: rgba(0,212,255,0.6);
  217. transform: translateY(-2px);
  218. box-shadow: 0 8px 20px rgba(0,212,255,0.2);
  219. }
  220. .btn-submit:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }
  221. .loading-spinner {
  222. width: 16px; height: 16px;
  223. border: 2px solid rgba(0,212,255,0.3);
  224. border-top-color: #00D4FF;
  225. border-radius: 50%;
  226. animation: spin 0.8s linear infinite;
  227. }
  228. @keyframes spin { to { transform: rotate(360deg); } }
  229. .error-msg {
  230. padding: 10px 14px; margin-bottom: 12px;
  231. background: rgba(255,77,106,0.1);
  232. border: 1px solid rgba(255,77,106,0.3);
  233. border-radius: 8px; color: #FF4D6A; font-size: 13px;
  234. }
  235. .footer-hint {
  236. margin: 20px 0 0 0; text-align: center;
  237. font-size: 12px; color: rgba(224,224,224,0.4); line-height: 1.5;
  238. }
  239. @media (max-width: 480px) {
  240. .login-overlay { align-items: flex-start; padding: 16px 0; }
  241. .login-modal { padding: 28px 20px; margin: auto 0; }
  242. .logo-section { margin-bottom: 22px; }
  243. .purchase-summary { padding: 14px; }
  244. }
  245. `]
  246. })
  247. export class LoginModalComponent {
  248. @Input() visible = false;
  249. @Input() skillInstall = false;
  250. @Input() qiweiSkill = false;
  251. @Input() qiweiTrial = false;
  252. @Output() loginSuccess = new EventEmitter<{ userId: string; sessionToken: string; displayName: string }>();
  253. phone = '';
  254. code = '';
  255. errorMsg = '';
  256. isLoading = signal(false);
  257. countdown = signal(0);
  258. countdownTimer: any = null;
  259. show(): void { this.visible = true; }
  260. hide(): void { this.visible = false; }
  261. validatePhone(): void {
  262. this.phone = this.phone.replace(/\D/g, '').substring(0, 11);
  263. }
  264. canSendCode(): boolean {
  265. return /^1[3-9]\d{9}$/.test(this.phone);
  266. }
  267. canSubmit(): boolean {
  268. return this.canSendCode() && this.code.length >= 4;
  269. }
  270. async sendCode(): Promise<void> {
  271. if (!this.canSendCode() || this.countdown() > 0) return;
  272. this.isLoading.set(true);
  273. this.errorMsg = '';
  274. try {
  275. const resp = await fetch(`${API_HOST}/api/apig/message`, {
  276. method: 'POST',
  277. headers: {
  278. 'Content-Type': 'application/json',
  279. 'X-Parse-Application-Id': APP_ID
  280. },
  281. body: JSON.stringify({ mobile: this.phone, company: 'E4KpGvTEto' })
  282. });
  283. const result = await resp.json();
  284. if (result.code !== 1) {
  285. throw new Error(result.data?.msg || result.error || '发送失败');
  286. }
  287. // 开始倒计时
  288. this.countdown.set(60);
  289. this.countdownTimer = setInterval(() => {
  290. const cur = this.countdown();
  291. if (cur <= 0) {
  292. clearInterval(this.countdownTimer);
  293. } else {
  294. this.countdown.set(cur - 1);
  295. }
  296. }, 1000);
  297. console.log('[Login] 验证码已发送');
  298. } catch (e: any) {
  299. console.error('[Login] 发送验证码失败:', e);
  300. this.errorMsg = '发送失败: ' + (e.message || '未知错误');
  301. } finally {
  302. this.isLoading.set(false);
  303. }
  304. }
  305. async handleSubmit(): Promise<void> {
  306. if (!this.canSubmit() || this.isLoading()) return;
  307. this.isLoading.set(true);
  308. this.errorMsg = '';
  309. try {
  310. // 1. 调用登录接口获取 session token
  311. const loginUrl = `${API_HOST}/api/fmode/mobile?mobile=${this.phone}&code=${this.code}`;
  312. const resp = await fetch(loginUrl, {
  313. method: 'GET',
  314. headers: { 'X-Parse-Application-Id': APP_ID }
  315. });
  316. const result = await resp.json();
  317. if (result.code !== 200 || !result.data?.token) {
  318. throw new Error(result.mess || result.message || result.error || '验证码错误或已过期');
  319. }
  320. const sessionToken = result.data.token;
  321. // 2. 用 session token 获取用户信息
  322. const meResp = await fetch(`${API_HOST}/parse/users/me`, {
  323. method: 'GET',
  324. headers: {
  325. 'X-Parse-Application-Id': APP_ID,
  326. 'X-Parse-Session-Token': sessionToken
  327. }
  328. });
  329. const userData = await meResp.json();
  330. if (!userData.objectId) {
  331. throw new Error('登录成功但无法获取用户信息');
  332. }
  333. // 3. 缓存登录态(company 不再是必须的,后端已改为 user+APIGAuth 扣费)
  334. const displayName = this.phone ? this.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : (userData.username || userData.objectId);
  335. localStorage.setItem('apig_session_token', sessionToken);
  336. localStorage.setItem('apig_user_id', userData.objectId);
  337. localStorage.setItem('apig_display_name', displayName);
  338. localStorage.setItem('apig_phone', String(result.data.mobile || userData.mobile || userData.mobilePhoneNumber || this.phone));
  339. console.log('[Login] userId:', userData.objectId, 'display:', displayName);
  340. this.loginSuccess.emit({ userId: userData.objectId, sessionToken, displayName });
  341. this.hide();
  342. } catch (e: any) {
  343. console.error('[Login] 登录失败:', e);
  344. this.errorMsg = '登录失败: ' + (e.message || '未知错误');
  345. } finally {
  346. this.isLoading.set(false);
  347. }
  348. }
  349. // 检查是否已登录(从 localStorage 恢复)
  350. async checkExistingLogin(): Promise<{ userId: string; sessionToken: string } | null> {
  351. try {
  352. const token = localStorage.getItem('apig_session_token');
  353. const cachedUserId = localStorage.getItem('apig_user_id');
  354. if (!token) return null;
  355. // 验证 token 是否仍然有效
  356. const resp = await fetch(`${API_HOST}/parse/users/me`, {
  357. method: 'GET',
  358. headers: {
  359. 'X-Parse-Application-Id': APP_ID,
  360. 'X-Parse-Session-Token': token
  361. }
  362. });
  363. const userData = await resp.json();
  364. if (userData.objectId) {
  365. console.log('[Login] 已有登录用户:', userData.objectId);
  366. const cachedPhone = String(userData.mobile || userData.mobilePhoneNumber || '').trim();
  367. if (cachedPhone) localStorage.setItem('apig_phone', cachedPhone);
  368. return { userId: userData.objectId, sessionToken: token };
  369. }
  370. // token 失效,清除缓存
  371. localStorage.removeItem('apig_session_token');
  372. localStorage.removeItem('apig_user_id');
  373. } catch (e) {
  374. console.warn('[Login] 检查登录态失败:', e);
  375. }
  376. return null;
  377. }
  378. ngOnDestroy(): void {
  379. if (this.countdownTimer) clearInterval(this.countdownTimer);
  380. }
  381. }