普兆健康管家后端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
7.2 KiB

  1. # 小程序登录API接口文档
  2. ## 概述
  3. 本文档描述小程序登录模块的API接口,包括登录、用户管理、token管理等功能。
  4. ## 基础信息
  5. - **基础URL**: `http://your-domain/applet/login`
  6. - **请求方式**: GET/POST
  7. - **数据格式**: JSON
  8. - **字符编码**: UTF-8
  9. ## 通用响应格式
  10. ```json
  11. {
  12. "success": true,
  13. "message": "操作成功",
  14. "code": 200,
  15. "result": {}
  16. }
  17. ```
  18. ## 接口列表
  19. ### 1. 微信小程序登录
  20. **接口地址**: `POST /wxLogin`
  21. **请求参数**:
  22. | 参数名 | 类型 | 必填 | 说明 |
  23. |--------|------|------|------|
  24. | code | String | 是 | 微信登录code |
  25. **请求示例**:
  26. ```bash
  27. curl -X POST "http://your-domain/applet/login/wxLogin" \
  28. -H "Content-Type: application/x-www-form-urlencoded" \
  29. -d "code=wx_login_code_here"
  30. ```
  31. **响应示例**:
  32. ```json
  33. {
  34. "success": true,
  35. "message": "登录成功",
  36. "code": 200,
  37. "result": {
  38. "token": "eyJhbGciOiJIUzI1NiJ9...",
  39. "userInfo": {
  40. "id": "applet_user_123",
  41. "name": "微信用户12345678",
  42. "openid": "wx_openid_123",
  43. "phone": "13800138000",
  44. "avatar": "https://example.com/avatar.jpg",
  45. "bmi": 22.5,
  46. "fat": 15.2
  47. },
  48. "openid": "wx_openid_123",
  49. "sessionKey": "session_key_123"
  50. }
  51. }
  52. ```
  53. ### 2. 获取用户手机号
  54. **接口地址**: `POST /getPhoneNumber`
  55. **请求参数**:
  56. | 参数名 | 类型 | 必填 | 说明 |
  57. |--------|------|------|------|
  58. | code | String | 是 | 手机号获取code |
  59. **请求示例**:
  60. ```bash
  61. curl -X POST "http://your-domain/applet/login/getPhoneNumber" \
  62. -H "Content-Type: application/x-www-form-urlencoded" \
  63. -d "code=phone_code_here"
  64. ```
  65. **响应示例**:
  66. ```json
  67. {
  68. "success": true,
  69. "message": "获取成功",
  70. "code": 200,
  71. "result": "13800138000"
  72. }
  73. ```
  74. ### 3. 绑定手机号到用户
  75. **接口地址**: `POST /bindPhoneNumber`
  76. **请求参数**:
  77. | 参数名 | 类型 | 必填 | 说明 |
  78. |--------|------|------|------|
  79. | token | String | 是 | 用户token |
  80. | phoneCode | String | 是 | 手机号获取code |
  81. **请求示例**:
  82. ```bash
  83. curl -X POST "http://your-domain/applet/login/bindPhoneNumber" \
  84. -H "Content-Type: application/x-www-form-urlencoded" \
  85. -d "token=user_token_here&phoneCode=phone_code_here"
  86. ```
  87. **响应示例**:
  88. ```json
  89. {
  90. "success": true,
  91. "message": "绑定成功",
  92. "code": 200,
  93. "result": "绑定成功"
  94. }
  95. ```
  96. ### 4. 刷新token
  97. **接口地址**: `POST /refreshToken`
  98. **请求参数**:
  99. | 参数名 | 类型 | 必填 | 说明 |
  100. |--------|------|------|------|
  101. | token | String | 是 | 原token |
  102. **请求示例**:
  103. ```bash
  104. curl -X POST "http://your-domain/applet/login/refreshToken" \
  105. -H "Content-Type: application/x-www-form-urlencoded" \
  106. -d "token=old_token_here"
  107. ```
  108. **响应示例**:
  109. ```json
  110. {
  111. "success": true,
  112. "message": "刷新成功",
  113. "code": 200,
  114. "result": "eyJhbGciOiJIUzI1NiJ9..."
  115. }
  116. ```
  117. ### 5. 退出登录
  118. **接口地址**: `POST /logout`
  119. **请求参数**:
  120. | 参数名 | 类型 | 必填 | 说明 |
  121. |--------|------|------|------|
  122. | token | String | 是 | 用户token |
  123. **请求示例**:
  124. ```bash
  125. curl -X POST "http://your-domain/applet/login/logout" \
  126. -H "Content-Type: application/x-www-form-urlencoded" \
  127. -d "token=user_token_here"
  128. ```
  129. **响应示例**:
  130. ```json
  131. {
  132. "success": true,
  133. "message": "退出成功",
  134. "code": 200,
  135. "result": "退出成功"
  136. }
  137. ```
  138. ### 6. 检查登录状态
  139. **接口地址**: `GET /checkLogin`
  140. **请求参数**:
  141. | 参数名 | 类型 | 必填 | 说明 |
  142. |--------|------|------|------|
  143. | token | String | 是 | 用户token |
  144. **请求示例**:
  145. ```bash
  146. curl -X GET "http://your-domain/applet/login/checkLogin?token=user_token_here"
  147. ```
  148. **响应示例**:
  149. ```json
  150. {
  151. "success": true,
  152. "message": "登录有效",
  153. "code": 200,
  154. "result": {
  155. "id": "applet_user_123",
  156. "name": "微信用户12345678",
  157. "openid": "wx_openid_123",
  158. "phone": "13800138000",
  159. "avatar": "https://example.com/avatar.jpg",
  160. "bmi": 22.5,
  161. "fat": 15.2
  162. }
  163. }
  164. ```
  165. ### 7. 获取用户信息
  166. **接口地址**: `GET /getUserInfo`
  167. **请求参数**:
  168. | 参数名 | 类型 | 必填 | 说明 |
  169. |--------|------|------|------|
  170. | token | String | 是 | 用户token |
  171. **请求示例**:
  172. ```bash
  173. curl -X GET "http://your-domain/applet/login/getUserInfo?token=user_token_here"
  174. ```
  175. **响应示例**:
  176. ```json
  177. {
  178. "success": true,
  179. "message": "获取成功",
  180. "code": 200,
  181. "result": {
  182. "id": "applet_user_123",
  183. "name": "微信用户12345678",
  184. "openid": "wx_openid_123",
  185. "phone": "13800138000",
  186. "avatar": "https://example.com/avatar.jpg",
  187. "bmi": 22.5,
  188. "fat": 15.2
  189. }
  190. }
  191. ```
  192. ### 8. 更新用户信息
  193. **接口地址**: `POST /updateUserInfo`
  194. **请求参数**:
  195. | 参数名 | 类型 | 必填 | 说明 |
  196. |--------|------|------|------|
  197. | token | String | 是 | 用户token |
  198. | userInfo | Object | 是 | 用户信息对象 |
  199. **请求示例**:
  200. ```bash
  201. curl -X POST "http://your-domain/applet/login/updateUserInfo" \
  202. -H "Content-Type: application/json" \
  203. -d '{
  204. "token": "user_token_here",
  205. "userInfo": {
  206. "name": "新昵称",
  207. "avatar": "https://example.com/new_avatar.jpg",
  208. "bmi": 23.0,
  209. "fat": 16.0
  210. }
  211. }'
  212. ```
  213. **响应示例**:
  214. ```json
  215. {
  216. "success": true,
  217. "message": "更新成功",
  218. "code": 200,
  219. "result": "更新成功"
  220. }
  221. ```
  222. ## 错误码说明
  223. | 错误码 | 说明 |
  224. |--------|------|
  225. | 200 | 成功 |
  226. | 400 | 请求参数错误 |
  227. | 401 | 未授权/token无效 |
  228. | 403 | 禁止访问 |
  229. | 404 | 资源不存在 |
  230. | 500 | 服务器内部错误 |
  231. ## 常见错误响应
  232. ### 1. token无效
  233. ```json
  234. {
  235. "success": false,
  236. "message": "token无效",
  237. "code": 401,
  238. "result": null
  239. }
  240. ```
  241. ### 2. 用户不存在
  242. ```json
  243. {
  244. "success": false,
  245. "message": "用户不存在",
  246. "code": 404,
  247. "result": null
  248. }
  249. ```
  250. ### 3. 微信登录失败
  251. ```json
  252. {
  253. "success": false,
  254. "message": "微信登录失败: code无效",
  255. "code": 400,
  256. "result": null
  257. }
  258. ```
  259. ### 4. 手机号已被绑定
  260. ```json
  261. {
  262. "success": false,
  263. "message": "该手机号已被其他用户绑定",
  264. "code": 400,
  265. "result": null
  266. }
  267. ```
  268. ## 使用流程
  269. ### 1. 小程序登录流程
  270. 1. 小程序端调用 `wx.login()` 获取code
  271. 2. 调用 `/wxLogin` 接口进行登录
  272. 3. 保存返回的token用于后续请求
  273. ### 2. 手机号绑定流程
  274. 1. 小程序端调用 `wx.getPhoneNumber()` 获取手机号code
  275. 2. 调用 `/getPhoneNumber` 接口获取手机号
  276. 3. 调用 `/bindPhoneNumber` 接口绑定手机号到用户
  277. ### 3. token管理流程
  278. 1. 定期调用 `/refreshToken` 刷新token
  279. 2. 退出时调用 `/logout` 接口
  280. 3. 使用 `/checkLogin` 检查登录状态
  281. ## 安全注意事项
  282. 1. **token安全**: token应该安全存储,不要暴露给第三方
  283. 2. **HTTPS**: 生产环境必须使用HTTPS
  284. 3. **参数验证**: 客户端应该验证所有参数
  285. 4. **错误处理**: 客户端应该妥善处理各种错误情况
  286. 5. **日志记录**: 重要操作应该记录日志
  287. ## 性能优化建议
  288. 1. **缓存**: 用户信息可以适当缓存
  289. 2. **连接池**: 数据库和Redis连接池配置合理
  290. 3. **异步处理**: 非关键操作可以使用异步处理
  291. 4. **监控**: 添加接口调用监控和性能指标
  292. ## 测试建议
  293. 1. **单元测试**: 为每个接口编写单元测试
  294. 2. **集成测试**: 测试完整的登录流程
  295. 3. **压力测试**: 测试高并发场景
  296. 4. **安全测试**: 测试各种异常情况