添加 IntelliJ IDEA 项目配置文件

This commit is contained in:
ylweng
2025-09-02 21:59:27 +08:00
parent 59cfe620fe
commit 501c218a83
56 changed files with 11886 additions and 126 deletions

View File

@@ -0,0 +1,203 @@
# 开发环境配置指南
## 🖥️ 系统要求
### 基础环境
- **操作系统**Windows 10+, macOS 10.15+, Ubuntu 18.04+
- **Node.js**>= 18.0.0
- **npm**>= 8.0.0
- **Git**>= 2.20.0
### 数据库环境
- **MySQL**>= 8.0
- **Redis**>= 7.0
### 开发工具
- **IDE**VS Code推荐/ WebStorm / HBuilderX
- **浏览器**Chrome 90+ (开发调试)
- **微信开发者工具**:最新版本(小程序开发)
## 🔧 环境安装
### 1. Node.js 安装
```bash
# 使用 nvm 管理版本(推荐)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install 18
nvm use 18
# 或直接下载安装
# https://nodejs.org/
```
### 2. 数据库配置
```bash
# MySQL 8.0
# 连接信息:
HOST: 129.211.213.226
PORT: 9527
USERNAME: root
PASSWORD: aiotAiot123!
DATABASE: jiebandata
# Redis本地开发
HOST: localhost
PORT: 6379
```
### 3. 开发工具安装
```bash
# VS Code 推荐插件
ext install ms-vscode.vscode-typescript-next
ext install Vue.volar
ext install bradlc.vscode-tailwindcss
ext install esbenp.prettier-vscode
ext install ms-vscode.vscode-eslint
```
## 🚀 项目启动
### 全局依赖安装
```bash
# 安装全局工具
npm install -g @vue/cli
npm install -g serve
npm install -g pm2
npm install -g sequelize-cli
```
### 项目克隆和初始化
```bash
# 克隆项目
git clone <repository-url>
cd niumall
# 安装各模块依赖
cd admin-system && npm install
cd ../backend && npm install
cd ../mini_program && npm install
```
### 启动服务
#### 1. 后端服务
```bash
cd backend
cp .env.example .env.development
# 编辑环境变量
npm run dev
# 服务运行在 http://localhost:3001
```
#### 2. 管理后台
```bash
cd admin-system
npm run dev
# 服务运行在 http://localhost:3000
```
#### 3. 官网
```bash
cd website
python -m http.server 8080
# 或 npx serve . -p 8080
# 访问 http://localhost:8080
```
#### 4. 小程序
```bash
cd mini_program/client-mp
npm run dev:mp-weixin
# 使用微信开发者工具打开 dist/dev/mp-weixin
```
## 🔗 开发服务地址
| 服务 | 地址 | 说明 |
|------|------|------|
| 后端API | http://localhost:3001 | Express服务 |
| 管理后台 | http://localhost:3000 | Vue3应用 |
| 企业官网 | http://localhost:8080 | 静态网站 |
| 小程序 | 微信开发者工具 | uni-app应用 |
## 🛠️ 开发工具配置
### VS Code 配置
```json
// .vscode/settings.json
{
"typescript.preferences.importModuleSpecifier": "relative",
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"eslint.format.enable": true,
"vetur.validation.template": false
}
```
### Git 配置
```bash
# 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 配置提交模板
git config --global commit.template .gitmessage
```
## 🔧 常见问题解决
### Node.js 版本问题
```bash
# 切换Node版本
nvm use 18
npm install
```
### 端口冲突
```bash
# 检查端口占用
netstat -ano | findstr :3000
# 杀死进程
taskkill /PID <PID> /F
```
### 数据库连接失败
1. 检查数据库服务是否启动
2. 验证连接参数
3. 检查防火墙设置
4. 确认网络连通性
### 依赖安装失败
```bash
# 清除缓存重新安装
npm cache clean --force
rm -rf node_modules package-lock.json
npm install
```
## 📝 开发规范
### 代码提交规范
```bash
# 提交格式
feat: 新功能
fix: 修复bug
docs: 文档更新
style: 代码格式调整
refactor: 代码重构
test: 测试相关
chore: 其他修改
```
### 分支管理
- `main`: 主分支,生产环境代码
- `develop`: 开发分支,集成测试
- `feature/*`: 功能分支
- `hotfix/*`: 紧急修复分支
## 📧 技术支持
- **开发环境问题**dev@niumall.com
- **数据库相关**db@niumall.com
- **部署相关**ops@niumall.com
- **技术交流群**:微信群-前端技术交流

226
docs/测试规范文档.md Normal file
View File

@@ -0,0 +1,226 @@
# 测试规范文档
## 📋 测试策略
### 测试层次
- **单元测试**: 覆盖率 >= 80%
- **集成测试**: 覆盖核心业务流程
- **接口测试**: 覆盖所有API端点
- **端到端测试**: 覆盖用户关键路径
### 测试框架
- **后端**: Jest + Supertest
- **前端**: Vitest + Vue Test Utils
- **接口**: Postman + Newman
- **性能**: JMeter
## 🧪 单元测试规范
### 测试文件命名
```
src/services/user.service.js -> tests/unit/services/user.service.test.js
src/utils/validator.js -> tests/unit/utils/validator.test.js
src/components/UserForm.vue -> tests/unit/components/UserForm.spec.ts
```
### 测试结构
```javascript
describe('模块名称', () => {
beforeEach(() => {
// 测试前置操作
});
describe('方法名称', () => {
it('should 预期行为 when 条件', () => {
// Arrange - 准备
// Act - 执行
// Assert - 断言
});
});
});
```
### 示例代码
```javascript
// 后端单元测试
describe('UserService', () => {
describe('createUser', () => {
it('should create user successfully', async () => {
const userData = { phone: '13800138000' };
const mockUser = { id: 1, ...userData };
User.create = jest.fn().mockResolvedValue(mockUser);
const result = await UserService.createUser(userData);
expect(User.create).toHaveBeenCalledWith(userData);
expect(result).toEqual(mockUser);
});
});
});
// 前端组件测试
describe('UserForm', () => {
it('validates required fields', async () => {
const wrapper = mount(UserForm);
await wrapper.find('form').trigger('submit');
expect(wrapper.text()).toContain('请输入真实姓名');
});
});
```
## 🔗 接口测试规范
### Postman测试集合
```javascript
// 认证接口测试
pm.test("登录成功", function () {
pm.response.to.have.status(200);
const response = pm.response.json();
pm.expect(response.code).to.eql(200);
pm.expect(response.data.token).to.not.be.empty;
// 保存token用于后续测试
pm.environment.set("authToken", response.data.token);
});
// 业务接口测试
pm.test("创建订单成功", function () {
pm.response.to.have.status(200);
const response = pm.response.json();
pm.expect(response.data.id).to.be.a('number');
pm.environment.set("orderId", response.data.id);
});
```
### 自动化测试脚本
```bash
# 运行Postman测试
newman run tests/api/niumall-api.postman_collection.json \
-e tests/api/test-environment.json \
--reporters cli,html \
--reporter-html-export reports/api-test-report.html
```
## 🎯 端到端测试
### 关键用户路径
1. **用户注册登录流程**
2. **订单创建到完成流程**
3. **运输跟踪流程**
4. **支付结算流程**
### Cypress测试示例
```javascript
describe('订单管理流程', () => {
beforeEach(() => {
cy.login('client'); // 自定义命令
});
it('客户可以创建订单', () => {
cy.visit('/orders/create');
cy.get('[data-cy=supplier-select]').select('供应商A');
cy.get('[data-cy=cattle-type]').type('肉牛');
cy.get('[data-cy=quantity]').type('10');
cy.get('[data-cy=submit-btn]').click();
cy.url().should('include', '/orders/');
cy.contains('订单创建成功');
});
});
```
## 📊 性能测试
### JMeter测试计划
```xml
<!-- 并发用户登录测试 -->
<ThreadGroup>
<elementProp name="ThreadGroup.arguments" elementType="Arguments"/>
<stringProp name="ThreadGroup.num_threads">100</stringProp>
<stringProp name="ThreadGroup.ramp_time">60</stringProp>
<stringProp name="ThreadGroup.duration">300</stringProp>
</ThreadGroup>
```
### 性能指标要求
- **响应时间**: API < 200ms, 页面 < 3s
- **并发用户**: 支持1000并发
- **错误率**: < 0.1%
- **TPS**: >= 500
## 🔍 质量检查
### 代码覆盖率
```bash
# 后端覆盖率
npm run test:coverage
# 前端覆盖率
npm run test:unit -- --coverage
```
### 测试执行
```bash
# 运行所有测试
npm test
# 运行单元测试
npm run test:unit
# 运行集成测试
npm run test:integration
# 运行E2E测试
npm run test:e2e
```
### CI/CD集成
```yaml
# .github/workflows/test.yml
name: Test Pipeline
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run unit tests
run: npm run test:unit
- name: Run integration tests
run: npm run test:integration
- name: Upload coverage
uses: codecov/codecov-action@v2
```
## 📋 测试检查清单
### 提交前检查
- [ ] 所有单元测试通过
- [ ] 代码覆盖率 >= 80%
- [ ] 集成测试通过
- [ ] API测试通过
- [ ] 无ESLint错误
### 发布前检查
- [ ] 端到端测试通过
- [ ] 性能测试达标
- [ ] 安全扫描通过
- [ ] 浏览器兼容性测试
- [ ] 移动端适配测试
## 📞 测试支持
- **测试负责人**: test@niumall.com
- **质量保证**: qa@niumall.com
- **性能测试**: perf@niumall.com

View File

@@ -0,0 +1,518 @@
# 部署和运维文档
## 🏗️ 部署架构
### 生产环境架构
```
┌─────────────┐
│ 用户访问 │
└─────────────┘
┌─────────────┐
│ CDN/负载均衡 │
└─────────────┘
┌──────────────┼──────────────┐
│ │ │
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ 官网服务 │ │ 管理后台 │ │ 小程序 │
│ (Nginx) │ │ (Nginx) │ │ (CDN) │
└─────────────┘ └─────────────┘ └─────────────┘
┌─────────────┐
│ API网关 │
│ (Nginx) │
└─────────────┘
┌──────────────┼──────────────┐
│ │ │
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ 后端服务1 │ │ 后端服务2 │ │ 后端服务N │
│ (PM2) │ │ (PM2) │ │ (PM2) │
└─────────────┘ └─────────────┘ └─────────────┘
┌──────────────┼──────────────┐
│ │ │
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ MySQL │ │ Redis │ │ 文件存储 │
│ (主从复制) │ │ (集群) │ │ (MinIO) │
└─────────────┘ └─────────────┘ └─────────────┘
```
## 🚀 部署流程
### 1. 服务器准备
#### 基础环境
```bash
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装基础软件
sudo apt install -y nginx nodejs npm mysql-server redis-server git
# 安装Node.js 18
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# 安装PM2
sudo npm install -g pm2
```
#### 目录结构
```bash
# 创建项目目录
sudo mkdir -p /var/www/niumall
sudo mkdir -p /var/www/niumall/website
sudo mkdir -p /var/www/niumall/admin
sudo mkdir -p /var/www/niumall/backend
sudo mkdir -p /var/www/niumall/logs
sudo mkdir -p /var/www/niumall/uploads
# 设置权限
sudo chown -R www-data:www-data /var/www/niumall
sudo chmod -R 755 /var/www/niumall
```
### 2. 数据库部署
#### MySQL配置
```bash
# 安全配置
sudo mysql_secure_installation
# 创建数据库和用户
mysql -u root -p
```
```sql
-- 创建数据库
CREATE DATABASE jiebandata CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 创建用户
CREATE USER 'niumall'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT ALL PRIVILEGES ON jiebandata.* TO 'niumall'@'localhost';
FLUSH PRIVILEGES;
```
#### Redis配置
```bash
# 编辑Redis配置
sudo vim /etc/redis/redis.conf
# 关键配置项
bind 127.0.0.1
port 6379
requirepass your_redis_password
maxmemory 2gb
maxmemory-policy allkeys-lru
# 重启Redis
sudo systemctl restart redis-server
sudo systemctl enable redis-server
```
### 3. 后端服务部署
#### 代码部署
```bash
# 克隆代码
cd /var/www/niumall
sudo git clone <repository-url> .
# 安装后端依赖
cd backend
sudo npm install --production
# 复制环境配置
sudo cp .env.example .env.production
sudo vim .env.production
```
#### 环境配置
```bash
# .env.production
NODE_ENV=production
PORT=3001
DB_HOST=localhost
DB_PORT=3306
DB_NAME=jiebandata
DB_USER=niumall
DB_PASSWORD=your_secure_password
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
JWT_SECRET=your_jwt_secret_key
```
#### PM2配置
```javascript
// ecosystem.config.js
module.exports = {
apps: [{
name: 'niumall-backend',
script: 'src/app.js',
cwd: '/var/www/niumall/backend',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3001
},
error_file: '/var/www/niumall/logs/backend-error.log',
out_file: '/var/www/niumall/logs/backend-out.log',
log_file: '/var/www/niumall/logs/backend.log',
time: true
}]
}
```
#### 启动后端服务
```bash
# 数据库迁移
cd /var/www/niumall/backend
sudo npm run db:migrate
sudo npm run db:seed
# 启动服务
sudo pm2 start ecosystem.config.js
sudo pm2 save
sudo pm2 startup
```
### 4. 前端部署
#### 管理后台构建
```bash
cd /var/www/niumall/admin-system
sudo npm install
sudo npm run build:prod
# 复制构建文件
sudo cp -r dist/* /var/www/niumall/admin/
```
#### 官网部署
```bash
# 直接复制静态文件
sudo cp -r website/* /var/www/niumall/website/
```
### 5. Nginx配置
#### 主配置文件
```nginx
# /etc/nginx/sites-available/niumall
server {
listen 80;
server_name niumall.com www.niumall.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name niumall.com www.niumall.com;
ssl_certificate /path/to/ssl/certificate.crt;
ssl_certificate_key /path/to/ssl/private.key;
# 官网
location / {
root /var/www/niumall/website;
index index.html;
try_files $uri $uri/ /index.html;
}
# 管理后台
location /admin {
alias /var/www/niumall/admin;
index index.html;
try_files $uri $uri/ /admin/index.html;
}
# API接口
location /api {
proxy_pass http://127.0.0.1:3001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 文件上传
location /uploads {
alias /var/www/niumall/uploads;
expires 1M;
add_header Cache-Control "public, immutable";
}
}
```
#### 启用配置
```bash
# 创建软链接
sudo ln -s /etc/nginx/sites-available/niumall /etc/nginx/sites-enabled/
# 测试配置
sudo nginx -t
# 重启Nginx
sudo systemctl restart nginx
sudo systemctl enable nginx
```
## 🔧 运维管理
### 1. 监控配置
#### 系统监控
```bash
# 安装监控工具
sudo npm install -g pm2-logrotate
sudo pm2 install pm2-server-monit
# 配置日志轮转
sudo pm2 set pm2-logrotate:max_size 10M
sudo pm2 set pm2-logrotate:retain 30
```
#### 健康检查脚本
```bash
#!/bin/bash
# health-check.sh
# 检查后端服务
if curl -f http://localhost:3001/api/health > /dev/null 2>&1; then
echo "✓ Backend service is healthy"
else
echo "✗ Backend service is down"
# 重启服务
pm2 restart niumall-backend
fi
# 检查数据库
if mysqladmin ping -h localhost -u niumall -p'password' --silent; then
echo "✓ MySQL is healthy"
else
echo "✗ MySQL is down"
fi
# 检查Redis
if redis-cli ping > /dev/null 2>&1; then
echo "✓ Redis is healthy"
else
echo "✗ Redis is down"
fi
```
### 2. 备份策略
#### 数据库备份
```bash
#!/bin/bash
# backup.sh
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/var/backups/niumall"
# 创建备份目录
mkdir -p $BACKUP_DIR
# MySQL备份
mysqldump -u niumall -p'password' jiebandata > $BACKUP_DIR/mysql_$DATE.sql
# 压缩备份
gzip $BACKUP_DIR/mysql_$DATE.sql
# 文件备份
tar -czf $BACKUP_DIR/uploads_$DATE.tar.gz /var/www/niumall/uploads
# 清理老备份保留30天
find $BACKUP_DIR -name "*.gz" -mtime +30 -delete
echo "Backup completed: $DATE"
```
#### 定时任务
```bash
# 编辑crontab
sudo crontab -e
# 添加任务
# 每日凌晨2点备份
0 2 * * * /path/to/backup.sh
# 每小时健康检查
0 * * * * /path/to/health-check.sh
# 每日凌晨重启PM2可选
0 3 * * 0 pm2 restart all
```
### 3. 日志管理
#### 日志配置
```bash
# 创建日志目录
sudo mkdir -p /var/log/niumall
# 配置logrotate
sudo vim /etc/logrotate.d/niumall
```
```
/var/log/niumall/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 644 www-data www-data
}
```
#### 日志查看命令
```bash
# 查看后端日志
sudo pm2 logs niumall-backend
# 查看Nginx日志
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
# 查看系统日志
sudo journalctl -u nginx
sudo journalctl -u mysql
```
### 4. 性能优化
#### 数据库优化
```sql
-- 查看慢查询
SHOW VARIABLES LIKE 'slow_query_log';
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
-- 分析查询性能
EXPLAIN SELECT * FROM orders WHERE status = 'pending';
-- 添加索引
CREATE INDEX idx_orders_status ON orders(status);
CREATE INDEX idx_orders_created_at ON orders(created_at);
```
#### Redis优化
```bash
# 监控Redis性能
redis-cli --latency-history -i 1
# 查看内存使用
redis-cli info memory
# 清理过期key
redis-cli --scan --pattern "expired:*" | xargs redis-cli del
```
## 🚨 故障处理
### 常见问题排查
#### 服务无法启动
```bash
# 检查端口占用
sudo netstat -tlnp | grep :3001
# 检查进程状态
sudo pm2 status
# 查看错误日志
sudo pm2 logs niumall-backend --err
# 重启服务
sudo pm2 restart niumall-backend
```
#### 数据库连接失败
```bash
# 检查MySQL状态
sudo systemctl status mysql
# 检查连接数
mysql -u root -p -e "SHOW PROCESSLIST;"
# 重启MySQL
sudo systemctl restart mysql
```
#### 内存不足
```bash
# 查看内存使用
free -h
sudo ps aux --sort=-%mem | head
# 清理缓存
sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
# 重启占用内存大的进程
sudo pm2 restart all
```
### 紧急恢复流程
#### 数据库恢复
```bash
# 停止应用
sudo pm2 stop all
# 恢复数据库
mysql -u root -p jiebandata < /var/backups/niumall/mysql_20240120.sql
# 重启应用
sudo pm2 start all
```
#### 代码回滚
```bash
# 查看提交历史
cd /var/www/niumall
sudo git log --oneline -10
# 回滚到指定版本
sudo git reset --hard <commit-hash>
# 重新部署
cd backend && sudo npm run build
sudo pm2 restart all
```
## 📊 监控指标
### 关键指标
- **服务可用性**: > 99.9%
- **响应时间**: < 200ms (API), < 3s (页面)
- **错误率**: < 0.1%
- **CPU使用率**: < 70%
- **内存使用率**: < 80%
- **磁盘使用率**: < 85%
### 告警配置
```bash
# CPU使用率告警
if [ $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1) > 70 ]; then
echo "High CPU usage detected" | mail -s "Server Alert" admin@niumall.com
fi
# 磁盘空间告警
if [ $(df / | tail -1 | awk '{print $5}' | cut -d'%' -f1) > 85 ]; then
echo "Low disk space" | mail -s "Storage Alert" admin@niumall.com
fi
```
## 📞 运维联系方式
- **运维负责人**: ops@niumall.com
- **紧急联系**: +86 138-xxxx-xxxx
- **技术支持**: tech@niumall.com
- **监控告警**: alert@niumall.com