├── 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 |
4 | 5 | [![JavaScript](https://img.shields.io/badge/JavaScript-F7DF1E?style=for-the-badge&logo=javascript&logoColor=black)](https://developer.mozilla.org/en-US/docs/Web/JavaScript) 6 | [![Node.js](https://img.shields.io/badge/Node.js-43853D?style=for-the-badge&logo=node.js&logoColor=white)](https://nodejs.org/) 7 | [![Express](https://img.shields.io/badge/Express-000000?style=for-the-badge&logo=express&logoColor=white)](https://expressjs.com/) 8 | [![Vercel](https://img.shields.io/badge/Vercel-000000?style=for-the-badge&logo=vercel&logoColor=white)](https://vercel.com/) 9 | 10 | [![Deployed on Vercel](https://img.shields.io/badge/Deployed%20on-Vercel-brightgreen?style=flat-square)](https://xmsportcode.vercel.app) 11 | [![Version](https://img.shields.io/badge/version-1.0.0-blue?style=flat-square)](https://github.com/chiupam/xmSportCode) 12 | [![Maintenance](https://img.shields.io/badge/Maintained-yes-green.svg?style=flat-square)](https://github.com/chiupam/xmSportCode/commits/main) 13 | [![License](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT) 14 | 15 |
16 | 17 | 这是一个运动相关的项目仓库,提供华米运动API访问代理服务。 18 | 19 | [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fchiupam%2FxmSportCode) 20 | 21 | ## 📝 简介 22 | 该项目包含一个简单的API代理服务,用于与华米运动API进行交互,部署到Vercel云平台。使用Node.js实现。 23 | 24 | ## ✨ 功能特点 25 | - 📡 提供POST接口代理请求到华米API 26 | - 🔑 自动提取access token并返回 27 | - 💻 使用Node.js + Express框架开发 28 | - 🚀 专为Vercel云平台设计 29 | - 🔄 享受Vercel的每月可重置免费请求次数 30 | 31 | ## 🤖 自动化选项 32 | 33 | 如果不想手动部署和调用API,可以访问 [github.com/chiupam/xmSport](https://github.com/chiupam/xmSport) 仓库,该仓库使用GitHub Actions自动执行完整流程。 34 | 35 | ## 🚀 Vercel部署教程 36 | 37 | ### 快速部署 38 | 点击上方的 "Deploy with Vercel" 按钮,一键部署到您的Vercel账户。 39 | 40 | ### 方法一:通过网页界面部署 41 | 1. 创建Vercel账号 42 | - 访问 [Vercel官网](https://vercel.com/) 43 | - 使用GitHub账号直接登录 44 | 45 | 2. 从GitHub仓库导入项目 46 | - 首先确保您的代码已推送到GitHub仓库 47 | ``` 48 | git add . 49 | git commit -m "配置Vercel部署" 50 | git push origin main 51 | ``` 52 | - 在Vercel控制台中点击"Add New Project" 53 | - 选择"Import Git Repository"并选择您的xmSportCode仓库 54 | - Vercel会自动识别Node.js项目并使用适当的构建设置 55 | 56 | 3. 部署设置 57 | - 在部署设置页面,保持默认设置即可 58 | - 点击"Deploy"按钮开始部署 59 | 60 | 4. 获取公共URL 61 | - 部署完成后,Vercel会自动为您的应用生成一个公共URL 62 | - 通常格式为:https://your-project-name.vercel.app 63 | 64 | ### 方法二:通过CLI命令行部署 65 | 1. 安装Vercel CLI 66 | ``` 67 | npm i -g vercel 68 | ``` 69 | 70 | 2. 登录到Vercel 71 | ``` 72 | vercel login 73 | ``` 74 | 75 | 3. 部署项目 76 | ``` 77 | cd xmSportCode 78 | vercel 79 | ``` 80 | 按照命令行提示进行配置 81 | 82 | 4. 如需更新部署 83 | ``` 84 | vercel --prod 85 | ``` 86 | 87 | ## 💻 本地开发 88 | 1. 安装依赖 89 | ``` 90 | npm install 91 | ``` 92 | 93 | 2. 启动开发服务器 94 | ``` 95 | npm run dev 96 | ``` 97 | 或 98 | ``` 99 | npm start 100 | ``` 101 | 102 | 3. 本地测试 103 | ``` 104 | curl -X POST -d "phoneNumber=123456789&password=yourpassword" http://localhost:3000/api 105 | ``` 106 | 107 | ## 📋 使用方法 108 | 向API发送POST请求: 109 | 110 | ``` 111 | POST https://your-project-name.vercel.app/api 112 | Content-Type: application/x-www-form-urlencoded 113 | 114 | phoneNumber=123456789&password=yourpassword 115 | ``` 116 | 117 | 请求参数: 118 | - phoneNumber: 手机号码(不需要+86前缀) 119 | - password: 密码 120 | 121 | 响应: 122 | - 成功: 返回JSON格式 `{"status": true, "code": "access_token"}` 123 | - 失败: 返回JSON格式 `{"status": false, "code": "错误信息"}` 124 | 125 | ## 🔧 故障排除 126 | - 如果部署失败,检查Vercel控制台中的构建日志 127 | - 如果需要查看应用日志,可以在Vercel控制台中的"Logs"标签查看 128 | - 确保您的GitHub仓库包含了所有必要的文件,包括vercel.json和package.json 129 | 130 | ## 📜 许可证 131 | 本项目采用MIT许可证 132 | 133 | --------------------------------------------------------------------------------