integrated_crm_app/DEPLOY.md

251 lines
7.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Integrated CRM 部署指南
## 1. 环境准备
### 1.1 系统要求
- Python 3.10+
- MySQL 5.7+
- Git
### 1.2 克隆所有仓库
```bash
mkdir -p ~/repos && cd ~/repos
# 引用模块5个绝对不能修改
git clone git@git.opencomputing.cn:yumoqing/apppublic.git
git clone git@git.opencomputing.cn:yumoqing/sqlor.git
git clone git@git.opencomputing.cn:yumoqing/ahserver.git
git clone git@git.opencomputing.cn:yumoqing/appbase.git
git clone git@git.opencomputing.cn:yumoqing/rbac.git
# CRM业务模块6个
git clone git@git.opencomputing.cn:yumoqing/customer_management.git
git clone git@git.opencomputing.cn:yumoqing/opportunity_management.git
git clone git@git.opencomputing.cn:yumoqing/contract_management.git
git clone git@git.opencomputing.cn:yumoqing/financial_management.git
git clone git@git.opencomputing.cn:yumoqing/workflow_approval.git
git clone git@git.opencomputing.cn:yumoqing/unified_dashboard.git
# 主应用
git clone git@git.opencomputing.cn:yumoqing/integrated_crm_app.git
```
## 2. 构建应用
```bash
cd ~/repos/integrated_crm_app
chmod +x build.sh
./build.sh
```
构建脚本会完成:
1. 创建虚拟环境py3/
2. 安装核心依赖apppublic、sqlor、ahserver
3. 链接本地业务模块到 pkgs/
4. 生成数据库 DDLintegrated_crm_app_schema.sql
5. 创建 wwwroot 符号链接
6. 生成 start.sh / stop.sh
## 3. 数据库初始化
### 3.1 创建数据库
```bash
mysql -u root -p -e "CREATE DATABASE crm_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -u root -p -e "CREATE DATABASE rbac CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
```
### 3.2 导入表结构
```bash
# CRM业务表
mysql -u root -p crm_db < ~/repos/integrated_crm_app/integrated_crm_app_schema.sql
# RBAC表rbac模块自带
cd ~/repos/rbac
source ~/repos/integrated_crm_app/py3/bin/activate
python -c "
from appPublic.jsonConfig import getConfig
from appPublic.dictObject import DictObject
from sqlor.dbpools import DBPools
from rbac import load_rbac
import os
os.chdir(os.path.expanduser('~/repos/rbac'))
config = getConfig('.', {'workdir': '.'})
db_config = {}
for k, v in config.databases.items():
db_config[k] = DictObject(driver=v['driver'], kwargs=DictObject(**v['kwargs']))
DBPools(db_config)
from rbac.rbac_tables import init_database
init_database()
"
```
### 3.3 创建RBAC默认数据
RBAC模块需要在数据库中初始化
- 一个超级管理员用户
- 初始机构orgtype
可通过 rbac 自带的注册功能完成,或运行 RBAC 的初始化脚本。
## 4. 配置数据库连接
编辑 `~/repos/integrated_crm_app/conf/config.json`
```json
{
"password_key": "!@#$%^&*(*&^%$QWERTYUIqwertyui234567",
"databases": {
"crm_db": {
"driver": "mysql",
"kwargs": {
"host": "localhost",
"port": 3306,
"user": "你的MySQL用户名",
"password": "加密后的密码",
"db": "crm_db",
"charset": "utf8mb4"
}
}
}
}
```
> 注意password 需要使用 apppublic 的加密工具加密,格式为 `apppublic.aes.encrypt(明文密码)`
## 5. 权限初始化
### 5.1 角色定义
CRM是**单业主机构**系统,所有角色属于同一机构内部:
| 角色ID | 名称 | 说明 |
|--------|------|------|
| any | 任何人 | 未登录用户固定ID不可改 |
| logined | 已登录 | 任意登录用户固定ID不可改 |
| sales_manager | 销售经理 | 团队管理、客户分配、审批 |
| sales_rep | 销售代表 | 客户跟进、商机推进 |
| finance_admin | 财务管理员 | 所有财务操作、报表 |
| finance_clerk | 财务出纳 | 收款登记、付款处理 |
| admin_superuser | 超级用户 | 全部权限 |
### 5.2 执行权限初始化
```bash
cd ~/repos/integrated_crm_app
source py3/bin/activate
python app/init_permissions.py
```
该脚本会:
1. 创建约定角色any、logined
2. 创建定义的角色sales_manager 等)
3. 扫描各模块 wwwroot 注册路径权限
4. 根据 `app/perm_config.py` 中的 PERMISSION_MATRIX 授予角色权限
5. 注册 CRUD 路径
### 5.3 约定角色说明
rbac 模块的 `userperm.py` 硬编码检查特殊角色 ID
```python
if r.id == 'anonymous': k = 'anonymous'
elif r.id == 'any': k = 'any'
elif r.id == 'logined': k = 'logined'
```
因此 `any``logined` 必须使用固定字符串作为 ID**不能用 getID() 随机生成**。
### 5.4 业务角色ID命名
业务角色 ID 使用**下划线**(如 `sales_manager`**不能用点号**,因为前端显示格式为 `机构类型.角色名称`(如 `sales.manager`)。
## 6. 创建初始用户
通过 rbac 自带的注册功能创建用户:
```
访问 http://localhost:8080/user/register.ui
```
创建第一个用户后,通过数据库或 rbac 管理界面为其分配角色:
```sql
-- 将用户分配给 admin_superuser 角色
INSERT INTO userroles (userid, roleid)
VALUES ('你的用户ID', 'admin_superuser');
```
## 7. 启动应用
```bash
cd ~/repos/integrated_crm_app
./start.sh
```
默认端口 8080可通过参数修改
```bash
python app/integrated_crm_app.py --port 9999 --root wwwroot/
```
访问:`http://localhost:8080/main/login.ui`
## 8. 停止应用
```bash
./stop.sh
# 或
pkill -f "integrated_crm_app.py"
```
## 9. 模块引用规则
| 类别 | 模块 | 规则 |
|------|------|------|
| 引用模块 | apppublic, sqlor, ahserver, appbase, rbac | **绝对不能修改** |
| 业务模块 | customer_management 等 6 个 | 遵循 module-development-spec |
rbac 自带完整的登录注册功能(`rbac/wwwroot/user/`),开发时直接引用,**不要创建冗余的登录注册文件**。
## 10. 目录结构
```
integrated_crm_app/
├── app/
│ ├── integrated_crm_app.py # 主入口
│ ├── perm_config.py # 权限配置(角色+权限矩阵)
│ └── init_permissions.py # 权限初始化脚本
├── conf/
│ └── config.json # 应用配置
├── wwwroot/
│ ├── main/ # 主页面
│ ├── rbac/ # 用户管理symlink
│ ├── customer_management/ # 客户管理symlink
│ ├── opportunity_management/ # 商机管理symlink
│ ├── contract_management/ # 合同管理symlink
│ ├── financial_management/ # 财务管理symlink
│ ├── workflow_approval/ # 审批管理symlink
│ ├── unified_dashboard/ # 仪表盘symlink
│ └── bricks/ # 前端框架symlink
├── pkgs/ # 模块链接
├── py3/ # 虚拟环境
├── logs/ # 日志
├── files/ # 文件存储
└── build.sh # 构建脚本
```
## 11. 故障排查
### 端口被占用
```bash
lsof -i :8080
kill -9 <PID>
```
### 数据库连接失败
```bash
cd ~/repos/integrated_crm_app
python test_db_conn.py
```
### 权限初始化失败
检查 rbac 数据库中的 role、permission、rolepermission 表是否正确创建。