From 9ff0e7aa2df15502863b5ca6447dee51d73abe99 Mon Sep 17 00:00:00 2001 From: zyh Date: Fri, 29 Aug 2025 23:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=A7=AF=E5=88=86=E4=BD=99?= =?UTF-8?q?=E9=A2=9D=E6=98=BE=E7=A4=BA=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=A7=AF=E5=88=86=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=AE=8C=E5=96=84=E7=A7=AF=E5=88=86=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/代理商积分获取接口.md | 155 ++++++++++++++++++++++++++++++++++++ src/api/points.js | 15 ++++ src/layouts/AdminLayout.vue | 54 ++++++++++++- src/utils/points.js | 18 +++++ 4 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 docs/代理商积分获取接口.md create mode 100644 src/api/points.js create mode 100644 src/utils/points.js diff --git a/docs/代理商积分获取接口.md b/docs/代理商积分获取接口.md new file mode 100644 index 0000000..14135f3 --- /dev/null +++ b/docs/代理商积分获取接口.md @@ -0,0 +1,155 @@ +# 用户积分余额接口文档 + +## 接口概述 + +获取当前登录用户的积分余额信息,通过JWT token自动识别用户身份。 + +--- + +## 接口详情 + +### 基本信息 +- **接口路径**: `/api/admin/accounts/me/points-balance` +- **请求方法**: `GET` +- **接口描述**: 根据token解析用户ID并获取当前用户的积分余额 +- **认证方式**: JWT Bearer Token + +--- + +## 请求参数 + +### Headers +| 参数名 | 类型 | 必填 | 说明 | +|--------|------|------|------| +| Authorization | string | 是 | Bearer {token},JWT认证令牌 | + +### 请求示例 +```http +GET /api/admin/accounts/me/points-balance HTTP/1.1 +Host: localhost:8080 +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c +Content-Type: application/json +``` + +--- + +## 响应结果 + +### 成功响应 (200 OK) + +```json +{ + "userId": 12345, + "username": "agent001", + "userType": "AGENT", + "pointsBalance": 15000 +} +``` + +### 响应字段说明 +| 字段名 | 类型 | 说明 | +|--------|------|------| +| userId | Long | 用户ID | +| username | String | 用户名 | +| userType | String | 用户类型,ADMIN(管理员)或 AGENT(代理) | +| pointsBalance | Long | 积分余额(单位:积分点数) | + +--- + +## 错误响应 + +### 401 未授权 +```json +{ + "error": "Unauthorized", + "message": "Authorization header is required" +} +``` + +### 400 请求错误 +```json +{ + "error": "Bad Request", + "message": "Invalid token: userId not found" +} +``` + +### 404 用户不存在 +```json +{ + "error": "Not Found", + "message": "用户不存在" +} +``` + +--- + +## 调用示例 + +### JavaScript (fetch) +```javascript +const token = 'your-jwt-token-here'; + +fetch('/api/admin/accounts/me/points-balance', { + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + } +}) +.then(response => response.json()) +.then(data => { + console.log('用户积分余额:', data.pointsBalance); + console.log('用户信息:', data); +}) +.catch(error => { + console.error('获取积分余额失败:', error); +}); +``` + +### curl +```bash +curl -X GET "http://localhost:8080/api/admin/accounts/me/points-balance" \ + -H "Authorization: Bearer your-jwt-token-here" \ + -H "Content-Type: application/json" +``` + +### Java (Spring WebFlux) +```java +WebClient webClient = WebClient.create("http://localhost:8080"); + +Mono response = webClient + .get() + .uri("/api/admin/accounts/me/points-balance") + .header("Authorization", "Bearer " + jwtToken) + .retrieve() + .bodyToMono(PointsBalanceResponse.class); + +response.subscribe( + pointsBalance -> System.out.println("积分余额: " + pointsBalance.getPointsBalance()), + error -> System.err.println("请求失败: " + error.getMessage()) +); +``` + +--- + +## 注意事项 + +1. **Token有效性**: JWT token必须有效且未过期 +2. **用户类型**: + - ADMIN用户的积分余额通常为0 + - AGENT用户才有实际的积分余额 +3. **权限控制**: 只能查询当前登录用户自己的积分余额 +4. **数据格式**: 积分余额以长整型返回,单位为积分点数 + +--- + +## 状态码说明 + +| 状态码 | 说明 | +|--------|------| +| 200 | 请求成功,返回积分余额信息 | +| 400 | 请求参数错误或token无效 | +| 401 | 未提供认证信息或认证失败 | +| 404 | 用户不存在 | +| 500 | 服务器内部错误 | \ No newline at end of file diff --git a/src/api/points.js b/src/api/points.js new file mode 100644 index 0000000..6f619e9 --- /dev/null +++ b/src/api/points.js @@ -0,0 +1,15 @@ +import http from '@/plugins/http' + +/** + * 获取当前用户的积分余额 + * @returns {Promise} 包含用户信息和积分余额的对象 + */ +export async function getPointsBalance() { + try { + const response = await http.get('/admin/accounts/me/points-balance') + return response.data + } catch (error) { + console.error('获取积分余额失败:', error) + throw error + } +} \ No newline at end of file diff --git a/src/layouts/AdminLayout.vue b/src/layouts/AdminLayout.vue index 78a2936..390e253 100644 --- a/src/layouts/AdminLayout.vue +++ b/src/layouts/AdminLayout.vue @@ -60,9 +60,13 @@ {{ currentPageTitle }}
+ + + 积分: {{ formatPoints(pointsBalance) }} + - {{ currentUser?.username || '用户' }} + {{ currentUser?.username || '用户' }}