├── vercel.json ├── .gitignore ├── package.json ├── LICENSE ├── index.js └── README.md /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "builds": [ 4 | { 5 | "src": "index.js", 6 | "use": "@vercel/node" 7 | } 8 | ], 9 | "routes": [ 10 | { 11 | "src": "/(.*)", 12 | "dest": "index.js" 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Node.js 依赖目录 2 | node_modules/ 3 | npm-debug.log 4 | yarn-debug.log 5 | yarn-error.log 6 | 7 | # 环境配置文件 8 | .env 9 | .env.local 10 | .env.development.local 11 | .env.test.local 12 | .env.production.local 13 | 14 | # 日志文件 15 | logs 16 | *.log 17 | 18 | # 运行时数据 19 | pids 20 | *.pid 21 | *.seed 22 | *.pid.lock 23 | 24 | # 编辑器设置目录 25 | .idea/ 26 | .vscode/ 27 | *.swp 28 | *.swo 29 | 30 | # 操作系统文件 31 | .DS_Store 32 | Thumbs.db 33 | 34 | # Vercel 35 | .vercel 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xmsportcode-js", 3 | "version": "1.0.0", 4 | "description": "华米运动API代理服务,Node.js版本", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "dev": "nodemon index.js" 9 | }, 10 | "dependencies": { 11 | "axios": "^0.27.2", 12 | "body-parser": "^1.20.0", 13 | "express": "^4.18.1" 14 | }, 15 | "devDependencies": { 16 | "nodemon": "^2.0.19" 17 | }, 18 | "engines": { 19 | "node": ">=14.x" 20 | } 21 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 chiupam 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const axios = require('axios'); 3 | const bodyParser = require('body-parser'); 4 | 5 | const app = express(); 6 | const port = process.env.PORT || 3000; 7 | 8 | // 中间件配置 9 | app.use(bodyParser.urlencoded({ extended: true })); 10 | 11 | // 健康检查端点 12 | app.get('/', (req, res) => { 13 | res.send('API服务正常运行中'); 14 | }); 15 | 16 | // API代理端点 17 | app.post('/', async (req, res) => { 18 | try { 19 | const { phoneNumber, password } = req.body; 20 | 21 | // 验证参数 22 | if (!phoneNumber || !password) { 23 | return res.status(400).json({ status: false, code: '手机号码和密码不能为空' }); 24 | } 25 | 26 | // 构建请求URL和头部 27 | const url = `https://api-user.huami.com/registrations/+86${phoneNumber}/tokens`; 28 | const headers = { 29 | 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 30 | 'User-Agent': 'MiFit/4.6.0 (iPhone; iOS 14.0.1; Scale/2.00)' 31 | }; 32 | 33 | // 构建请求数据 34 | const data = new URLSearchParams(); 35 | data.append('client_id', 'HuaMi'); 36 | data.append('password', password); 37 | data.append('redirect_uri', 'https://s3-us-west-2.amazonaws.com/hm-registration/successsignin.html'); 38 | data.append('token', 'access'); 39 | 40 | // 发送请求到华米API,禁止重定向 41 | const response = await axios({ 42 | method: 'post', 43 | url: url, 44 | headers: headers, 45 | data: data.toString(), 46 | maxRedirects: 0, // 禁止重定向 47 | validateStatus: status => true // 允许所有状态码 48 | }); 49 | 50 | // 从Location头部提取access token 51 | if (response.headers.location) { 52 | const match = /(?<=access=).*?(?=&)/.exec(response.headers.location); 53 | if (match) { 54 | return res.json({ status: true, code: match[0] }); 55 | } else { 56 | return res.status(500).json({ status: false, code: '无法提取access token' }); 57 | } 58 | } else { 59 | return res.status(500).json({ 60 | status: false, 61 | code: '响应中没有Location头部', 62 | response: response.data 63 | }); 64 | } 65 | } catch (error) { 66 | // 处理axios错误,区分重定向错误和其他错误 67 | if (error.response && error.response.status >= 300 && error.response.status < 400) { 68 | // 重定向错误,尝试从Location头部提取access token 69 | if (error.response.headers.location) { 70 | const match = /(?<=access=).*?(?=&)/.exec(error.response.headers.location); 71 | if (match) { 72 | return res.json({ status: true, code: match[0] }); 73 | } 74 | } 75 | } 76 | 77 | // 其他错误 78 | return res.status(500).json({ 79 | status: false, 80 | code: error.message || '请求处理失败', 81 | details: error.response ? error.response.data : null 82 | }); 83 | } 84 | }); 85 | 86 | // 启动服务器 87 | app.listen(port, () => { 88 | console.log(`API服务运行在端口 ${port}`); 89 | }); 90 | 91 | // 导出Express应用供Vercel使用 92 | module.exports = app; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xmSportCode 2 | 3 |