login-modal.component.ts 13 KB

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