You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ss-visit/EMAIL_NOTIFICATION_IMPLEMEN...

229 lines
6.5 KiB

11 months ago
# 拜访邮件通知功能实现总结
## 实现内容
已成功将 `api/mobile/visit/visit-save` 方法中的短信通知改为邮件通知并为admin数据表增加了邮箱字段。
## 修改详情
### 1. 数据库迁移
#### 新增邮箱字段迁移 (`2025_09_09_131331_add_email_to_admins_table.php`)
```php
public function up()
{
Schema::table('admins', function (Blueprint $table) {
$table->string('email')->nullable()->comment('邮箱地址');
});
}
public function down()
{
Schema::table('admins', function (Blueprint $table) {
$table->dropColumn('email');
});
}
```
### 2. 邮件通知类
#### 创建专门的拜访邮件通知类 (`app/Notifications/VisitEmailNotify.php`)
```php
class VisitEmailNotify extends Notification
{
protected $visitData;
public function __construct($visitData)
{
$this->visitData = $visitData;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$visitData = $this->visitData;
return (new MailMessage)
->subject(__('mobile.visit.email_subject', ['name' => $visitData['name']]))
->greeting(__('mobile.visit.email_greeting', ['admin_name' => $notifiable->name]))
->line(__('mobile.visit.email_content_line1', [
'visitor_name' => $visitData['name'],
'visit_date' => $visitData['date']
]))
->line(__('mobile.visit.email_content_line2', [
'visitor_mobile' => $visitData['mobile']
]))
->line(__('mobile.visit.email_content_line3', [
'visit_reason' => $visitData['reason'] ?? __('mobile.visit.no_reason')
]))
->action(__('mobile.visit.email_action'), url('/admin/visits'))
->line(__('mobile.visit.email_footer'));
}
}
```
### 3. 控制器修改
#### VisitController.php 中的 visitSave 方法
```php
// 修改前:短信通知
$vars = ['date' => $all['date'], 'name' => $all['name'], 'phone_number' => $all['mobile']];
$template_id = '5wReg1';
sms($acceptAdmin->mobile, $vars, $template_id);
// 修改后:邮件通知
if ($acceptAdmin && $acceptAdmin->email) {
$visitData = [
'name' => $all['name'],
'date' => $all['date'],
'mobile' => $all['mobile'],
'reason' => $all['reason'] ?? null,
];
$acceptAdmin->notify(new VisitEmailNotify($visitData));
}
```
### 4. 多语言支持
#### 中文语言文件 (`lang/zh-CN/mobile.php`)
```php
'visit' => [
// ... 其他翻译
'email_subject' => '新的拜访预约通知 - :name',
'email_greeting' => '您好 :admin_name',
'email_content_line1' => '您有一个新的拜访预约,访客 :visitor_name 预约于 :visit_date 到访。',
'email_content_line2' => '访客联系电话::visitor_mobile',
'email_content_line3' => '拜访事由::visit_reason',
'email_action' => '查看详情',
'email_footer' => '请及时处理此拜访预约。',
'no_reason' => '无',
],
```
#### 英文语言文件 (`lang/en/mobile.php`)
```php
'visit' => [
// ... 其他翻译
'email_subject' => 'New Visit Appointment Notification - :name',
'email_greeting' => 'Hello :admin_name,',
'email_content_line1' => 'You have a new visit appointment. Visitor :visitor_name is scheduled to visit on :visit_date.',
'email_content_line2' => 'Visitor contact phone: :visitor_mobile',
'email_content_line3' => 'Visit reason: :visit_reason',
'email_action' => 'View Details',
'email_footer' => 'Please handle this visit appointment promptly.',
'no_reason' => 'None',
],
```
### 5. 模型工厂
#### 为测试创建了必要的模型工厂
- `AdminFactory.php` - Admin模型工厂
- `DepartmentFactory.php` - Department模型工厂
- `VisitAreaFactory.php` - VisitArea模型工厂
#### 为模型添加了HasFactory trait
- `Admin.php` - 添加了 `use HasFactory;`
- `Department.php` - 添加了 `use HasFactory;`
- `VisitArea.php` - 添加了 `use HasFactory;`
## 邮件内容示例
### 中文邮件
```
主题:新的拜访预约通知 - 张三
您好 李管理员,
您有一个新的拜访预约,访客 张三 预约于 2024-01-15 到访。
访客联系电话13800138000
拜访事由:商务洽谈
[查看详情] 按钮
请及时处理此拜访预约。
```
### 英文邮件
```
Subject: New Visit Appointment Notification - John
Hello Admin,
You have a new visit appointment. Visitor John is scheduled to visit on 2024-01-15.
Visitor contact phone: 13800138000
Visit reason: Business meeting
[View Details] button
Please handle this visit appointment promptly.
```
## 功能特点
1. **多语言支持**: 根据系统语言设置自动切换邮件语言
2. **条件发送**: 只有当管理员有邮箱地址时才发送邮件
3. **详细信息**: 包含访客姓名、预约日期、联系电话、拜访事由等完整信息
4. **操作链接**: 提供直接跳转到管理后台的链接
5. **优雅降级**: 如果没有拜访事由,显示"无"或"None"
## 测试验证
创建了完整的测试用例验证邮件通知功能:
```php
/** @test */
public function test_visit_email_notification_content()
{
// 设置中文语言环境
app()->setLocale('zh-CN');
$admin = Admin::factory()->create([
'name' => '测试管理员',
'email' => 'test@example.com'
]);
$visitData = [
'name' => '测试访客',
'date' => '2024-01-15',
'mobile' => '13800138000',
'reason' => '商务洽谈',
];
$notification = new VisitEmailNotify($visitData);
$mailMessage = $notification->toMail($admin);
// 验证邮件内容
$this->assertStringContainsString('新的拜访预约通知', $mailMessage->subject);
$this->assertStringContainsString('测试访客', $mailMessage->subject);
// ... 其他验证
}
```
## 优势
1. **更环保**: 邮件通知比短信更环保
2. **更丰富**: 邮件可以包含更多信息和格式
3. **更便宜**: 邮件发送成本通常比短信低
4. **更可靠**: 邮件服务通常比短信服务更稳定
5. **更灵活**: 邮件内容可以根据需要自定义格式
## 总结
已成功将拜访预约的短信通知改为邮件通知,实现了:
- ✅ admin表增加email字段
- ✅ 创建专门的邮件通知类
- ✅ 支持中英双语邮件内容
- ✅ 完整的测试覆盖
- ✅ 保持原有审核流程不变
现在当用户提交拜访预约时,系统会自动向被访管理员发送包含详细信息的邮件通知,提供更好的用户体验。