| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="UTF-8" /> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
| 6 | <title>Google Login</title> |
| 7 | <style> |
| 8 | .google-btn { |
| 9 | background-color: white; |
| 10 | color: #444; |
| 11 | border: 1px solid #ccc; |
| 12 | font-size: 16px; |
| 13 | padding: 10px 20px; |
| 14 | cursor: pointer; |
| 15 | border-radius: 4px; |
| 16 | } |
| 17 | .google-btn:hover { |
| 18 | background-color: #f7f7f7; |
| 19 | } |
| 20 | </style> |
| 21 | </head> |
| 22 | <body> |
| 23 | <h2>登录</h2> |
| 24 | <button class="google-btn" onclick="handleGoogleLogin()">使用 Google 登录</button> |
| 25 | |
| 26 | <script> |
| 27 | async function handleGoogleLogin() { |
| 28 | try { |
| 29 | const platform = 'google'; // 或 'android' / 'ios' |
| 30 | const url = `http://localhost:3000/api/plat/google/auth/login?platform=${encodeURIComponent(platform)}`; |
| 31 | |
| 32 | const response = await fetch(url, { |
| 33 | method: 'GET', |
| 34 | }); |
| 35 | |
| 36 | if (!response.ok) { |
| 37 | throw new Error('请求失败: ' + response.statusText); |
| 38 | } |
| 39 | |
| 40 | const data = await response.json(); |
| 41 | console.log(data); |
| 42 | if (data.data) { |
| 43 | window.location.href = data.url; // 跳转到 Google OAuth URL |
| 44 | } else { |
| 45 | alert('未收到跳转地址'); |
| 46 | } |
| 47 | } catch (error) { |
| 48 | console.error('登录出错:', error); |
| 49 | alert('登录失败,请重试'); |
| 50 | } |
| 51 | } |
| 52 | </script> |
| 53 | </body> |
| 54 | </html> |
| 55 |