优先选择Ubuntu22.04服务器,配置最低2核4G、带宽5M起,按以下命令安装所有依赖:
``` 更新系统源 sudo apt update && sudo apt upgrade -y 安装基础软件 sudo apt install nginx=1.18. mysql-server-8.0 redis-server openjdk-17-jdk python3-pip -y 安装Python依赖 sudo pip3 install pymysql ```必填操作:开放服务器80、443端口,3306、6379端口仅允许本地访问,关闭防火墙不必要的规则。
直接使用开源RuoYi-Vue框架改造,下载地址:https://gitee.com/y_project/RuoYi-Vue/archive/refs/tags/v3.8.8.zip
``` 解压包 unzip RuoYi-Vue-3.8.8.zip 后端配置:修改ruoyi-admin/src/main/resources/application.yml中的数据库、Redis连接地址为本地地址 后端打包 cd RuoYi-Vue-3.8.8/ruoyi-admin && mvn clean package 启动后端 nohup java -jar target/ruoyi-admin.jar & 前端打包 cd ../ruoyi-ui && npm install --registry=https://registry.npmmirror.com && npm run build 把打包后的dist目录内容放到Nginx的/var/www/html目录下 ```仅统计用户上传、下载档案文件产生的公网流量,过滤系统接口请求流量,编辑/etc/nginx/nginx.conf,添加以下配置:
``` http { log_format archive_flow '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" $cookie_token'; server { listen 80; server_name 你的正式域名; root /var/www/html; index index.html; 档案文件存储路径,所有上传的档案都存在该目录 location /archive/ { alias /data/archive/files/; access_log /var/log/nginx/archive_flow.log archive_flow; 拦截未登录用户访问 if ($cookie_token = '') { return 403; } } 系统接口路径,不统计流量 location /prod-api/ { proxy_pass http://127.0.0.1:8080/; access_log off; } } } ```必填操作:执行`sudo nginx -t`验证配置无误后,执行`sudo nginx -s reload`生效。提前创建/data/archive/files目录,设置755权限。

先在MySQL中创建账单表,执行以下SQL:
``` CREATE TABLE `user_flow_bill` ( `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `user_id` int NOT NULL COMMENT '用户ID', `flow_gb` decimal(10,4) NOT NULL COMMENT '当日使用流量/GB', `amount` decimal(10,2) NOT NULL COMMENT '应付金额/元', `bill_date` date NOT NULL COMMENT '账单日期', `status` tinyint NOT NULL DEFAULT '0' COMMENT '0待支付 1已支付 2逾期' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ```创建/opt/calc_flow.py脚本,内容如下,直接修改配置项即可使用:
```python import re import pymysql from datetime import datetime, timedelta 改为自己的数据库配置 DB_CONFIG = { "host": "127.0.0.1", "user": "root", "password": "你的MySQL密码", "database": "ry-vue", "port": 3306 } 流量单价,默认1元/GB,可自行调整 PRICE_PER_GB = 1 LOG_PATH = "/var/log/nginx/archive_flow.log" def calc_daily_flow(): yesterday = (datetime.now() - timedelta(days=1)).strftime("%d/%b/%Y") user_flow = {} 匹配日志中的流量、用户token pattern = re.compile(r'\[('+yesterday+'.?)\] ".?/archive/.?" \d+ (\d+) .? ([a-f0-9]{32})') with open(LOG_PATH, 'r') as f: for line in f: res = pattern.search(line) if not res: continue token = res.group(3) bytes_num = int(res.group(2)) user_flow[token] = user_flow.get(token, 0) + bytes_num 写入账单表 conn = pymysql.connect(DB_CONFIG) cursor = conn.cursor() for token, total_bytes in user_flow.items(): 匹配用户ID cursor.execute("SELECT user_id FROM sys_user WHERE token = %s", (token,)) user_res = cursor.fetchone() if not user_res: continue user_id = user_res[0] flow_gb = round(total_bytes / 1024 / 1024 / 1024, 4) amount = round(flow_gb PRICE_PER_GB, 2) cursor.execute("INSERT INTO user_flow_bill(user_id, flow_gb, amount, bill_date) VALUES (%s,%s,%s,%s)", (user_id, flow_gb, amount, (datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d"))) 标记超7天未支付的账单为逾期 cursor.execute("UPDATE user_flow_bill SET status = 2 WHERE status = 0 AND bill_date < DATE_SUB(NOW(), INTERVAL 7 DAY)") conn.commit() cursor.close() conn.close() if __name__ == "__main__": calc_daily_flow() ```必填操作:执行`crontab -e`添加定时任务:`0 1 /usr/bin/python3 /opt/calc_flow.py`,每天凌晨1点自动统计前一天流量生成账单。
在SpringBoot后端添加流量拦截器,仅拦截文件上传下载接口,代码如下:
```java @Component public class FlowArrearsInterceptor implements HandlerInterceptor { @Autowired private UserBillMapper userBillMapper; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String token = request.getHeader("Authorization").replace("Bearer ", ""); // 查询用户是否有逾期账单 Integer arrearsCount = userBillMapper.selectCount(new LambdaQueryWrapper必填操作:在SpringMvc配置类中注册拦截器,仅映射/archive/upload、/archive/download接口路径。
直接对接微信支付JSAPI/Native支付,官方文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_7_2.shtml,支付成功后回调接口修改对应账单的status为1即可。前端新增「我的账单」页面,直接查询user_flow_bill表展示用户流量使用记录、待支付金额。