188 lines
4.3 KiB
Markdown
188 lines
4.3 KiB
Markdown
# 微信联系人祝福管理
|
|
|
|
一个用于管理微信联系人并发送节日祝福的 Web 应用。
|
|
|
|
## 功能特性
|
|
|
|
- 联系人管理:添加、编辑、删除联系人
|
|
- 分类标签:支持同事、好友、同学、老师、亲戚、客户、供应商等分类
|
|
- 自定义内容:为每个联系人设置自定义备注
|
|
- 祝福语管理:编辑和保存个性化祝福语
|
|
- 批量操作:批量选择、批量删除
|
|
- 搜索筛选:按姓名或分类快速查找
|
|
|
|
## 技术栈
|
|
|
|
- **后端**: Python Flask
|
|
- **前端**: HTML + Bootstrap 5 + JavaScript
|
|
- **数据库**: SQLite
|
|
|
|
## 快速开始
|
|
|
|
### 安装依赖
|
|
|
|
```bash
|
|
pip install flask pyautogui pyperclip
|
|
```
|
|
|
|
### 启动服务
|
|
|
|
```bash
|
|
python app.py
|
|
```
|
|
|
|
### 访问页面
|
|
|
|
打开浏览器访问: http://localhost:5000/static/contacts_manager.html
|
|
|
|
## 项目结构
|
|
|
|
```
|
|
├── app.py # Flask 后端 API
|
|
├── init_db.py # 数据库初始化脚本
|
|
├── static/
|
|
│ └── contacts_manager.html # 前端页面
|
|
└── contacts.db # SQLite 数据库(不包含在仓库中)
|
|
```
|
|
|
|
## API 接口
|
|
|
|
| 方法 | 路径 | 说明 |
|
|
|------|------|------|
|
|
| GET | /api/contacts | 获取联系人列表 |
|
|
| POST | /api/contacts | 创建联系人 |
|
|
| PUT | /api/contacts/:id | 更新联系人 |
|
|
| DELETE | /api/contacts/:id | 删除联系人 |
|
|
| GET | /api/stats | 获取统计数据 |
|
|
|
|
## PC 微信自动化操作
|
|
|
|
### 环境要求
|
|
|
|
- Python 3.x
|
|
- pyautogui: 屏幕截图和鼠标键盘操作
|
|
- pyperclip: 剪贴板操作(用于中文输入)
|
|
- ollama + glm-ocr 模型: OCR 文字识别
|
|
|
|
### 安装依赖
|
|
|
|
```bash
|
|
pip install pyautogui pyperclip
|
|
```
|
|
|
|
安装 ollama 和 OCR 模型:
|
|
```bash
|
|
# 安装 ollama (https://ollama.ai)
|
|
ollama pull glm-ocr
|
|
```
|
|
|
|
### 微信定位和搜索流程
|
|
|
|
#### 1. 检查微信是否运行
|
|
|
|
```python
|
|
import pyautogui
|
|
|
|
# 检查微信进程
|
|
import subprocess
|
|
result = subprocess.run(['tasklist'], capture_output=True, text=True)
|
|
if 'WeChat' in result.stdout:
|
|
print('微信已在运行')
|
|
```
|
|
|
|
#### 2. 关键坐标位置
|
|
|
|
| 功能 | 坐标位置 | 说明 |
|
|
|------|----------|------|
|
|
| 搜索框 | (118, 40) | 左上角区域,点击后可输入搜索内容 |
|
|
| 搜索区域检测 | (96,31)-(132,48) | 用于 OCR 检测"搜索"字样 |
|
|
| 搜索结果区域 | (125,96)-(463,149) | 搜索后显示联系人列表的区域 |
|
|
| 聊天输入框 | (600, 895) | 底部消息输入区域 |
|
|
|
|
#### 3. 搜索联系人示例
|
|
|
|
```python
|
|
import pyautogui
|
|
import pyperclip
|
|
import time
|
|
|
|
def search_contact(name):
|
|
# 点击搜索框
|
|
pyautogui.click(118, 40)
|
|
time.sleep(0.5)
|
|
|
|
# 清空并输入中文(使用剪贴板)
|
|
pyautogui.hotkey('ctrl', 'a')
|
|
time.sleep(0.3)
|
|
pyperclip.copy(name)
|
|
pyautogui.hotkey('ctrl', 'v')
|
|
time.sleep(1)
|
|
|
|
# 按回车确认搜索
|
|
pyautogui.press('enter')
|
|
time.sleep(1)
|
|
|
|
# 使用示例
|
|
search_contact('夏骥')
|
|
```
|
|
|
|
#### 4. OCR 检测搜索框
|
|
|
|
```python
|
|
import pyautogui
|
|
import subprocess
|
|
|
|
def ocr_region(left, top, width, height, save_path='region.png'):
|
|
# 截图指定区域
|
|
region = (left, top, width, height)
|
|
screenshot = pyautogui.screenshot(region=region)
|
|
screenshot.save(save_path)
|
|
|
|
# 使用 ollama 进行 OCR
|
|
result = subprocess.run(
|
|
['ollama', 'run', 'glm-ocr', f'Text Recognition: ./{save_path}'],
|
|
capture_output=True, text=True
|
|
)
|
|
return result.stdout.strip()
|
|
|
|
# 检测搜索框是否有"搜索"字样
|
|
text = ocr_region(96, 31, 36, 17) # (132-96, 48-31)
|
|
if '搜索' in text:
|
|
print('搜索框已就绪')
|
|
```
|
|
|
|
#### 5. 发送消息示例
|
|
|
|
```python
|
|
def send_message(message):
|
|
# 点击聊天输入框
|
|
pyautogui.click(600, 895)
|
|
time.sleep(0.3)
|
|
|
|
# 输入消息
|
|
pyperclip.copy(message)
|
|
pyautogui.hotkey('ctrl', 'v')
|
|
time.sleep(0.5)
|
|
|
|
# 发送
|
|
pyautogui.press('enter')
|
|
|
|
# 使用示例
|
|
send_message('元宵节快乐!')
|
|
```
|
|
|
|
### 自动化脚本参数说明
|
|
|
|
| 参数 | 说明 |
|
|
|------|------|
|
|
| 搜索框位置 (118, 40) | 微信窗口左上角搜索区域 |
|
|
| 搜索区域 OCR (96,31)-(132,48) | 检测搜索框状态 |
|
|
| 结果区域 OCR (125,96)-(463,149) | 检测搜索结果 |
|
|
| 输入框位置 (600, 895) | 聊天窗口底部输入区域 |
|
|
|
|
> **注意**: 以上坐标基于特定分辨率和微信窗口大小,实际使用时可能需要根据环境调整。
|
|
|
|
## 许可证
|
|
|
|
MIT License
|