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.

314 lines
12 KiB

6 months ago
<?php
namespace App\Console\Commands;
use App\Models\Appointment;
use App\Models\AppointmentAccompany;
use App\Models\AppointmentConfig;
use App\Models\Config;
use App\Models\Course;
use App\Models\CourseSign;
use App\Models\Notifications;
6 months ago
use App\Models\User;
6 months ago
use EasyWeChat\Factory;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class SendNotification extends Command
{
protected $tryTimes = 3;
protected $scheduleNumber = 10;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sms:notification';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send notification';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$smsSchedule = Notifications::with("notifiable")
->where(DB::raw('DATEDIFF(now(),created_at)'), 0)
->whereBetween("via_times", [0, $this->tryTimes])
->whereNull('send_time')
->whereNull('sms_send_time')
->limit($this->scheduleNumber)
->get();
foreach ($smsSchedule as $vo) {
$this->sendNotifications($vo);
}
}
/**
* 发送通知
*/
public function sendNotifications($vo)
{
// 写入发送结果
$data = json_decode($vo->data, true);
// 短信签名
$smsSign = Config::getValueByKey('sms_sign');
// 实际发送逻辑
switch ($vo->type) {
case "App\Notifications\FeeNotify":
// 催缴费通知
$template_id = Config::getValueByKey('fee_template');
$course = Course::find($data['course_id']);
$sendData = [
'template_id' => $template_id,
'touser' => $vo->notifiable->openid,
'path' => "/packages/mycourse/coursePay?id={$data['course_id']}",
'data' => [
'thing4' => [
'value' => $this->strlen($course->name),
],
'thing3' => [
'value' => $this->strlen($data['content']),
],
]
];
$this->appletNotice($vo, $sendData);
break;
case "App\Notifications\FeeResultNotify":
// 缴费结果通知
$template_id = Config::getValueByKey('fee_result_template');
$courseSign = CourseSign::find($data['course_sign_id']);
$course = Course::find($courseSign->course_id);
$fee_status_text = CourseSign::$intToString['fee_status'][$courseSign->fee_status];
// 订阅消息
$sendData = [
'template_id' => $template_id,
'touser' => $vo->notifiable->openid,
'path' => "/packages/mycourse/coursePay?id={$courseSign->course_id}",
'data' => [
'thing2' => [
'value' => $this->strlen($course->name . '缴费审核'),
],
'phrase3' => [
'value' => $fee_status_text,
],
]
];
$this->appletNotice($vo, $sendData);
// 发送短信
if ($courseSign->fee_status == 1) {
// 通过
$content = "{$smsSign}恭喜,您的缴费凭证:已通过审核,请准时参加课程。";
$this->smsNotice($vo, $content);
}
if ($courseSign->fee_status == 2) {
// 不通过
$content = "{$smsSign}您的缴费凭证未通过审核请进入STBC小程序重新提交。感谢您的配合。";
$this->smsNotice($vo, $content);
}
break;
case "App\Notifications\AuditNotify":
// 报名审核通知
$courseSign = CourseSign::find($data['course_sign_id']);
$course = Course::find($courseSign->course_id);
$statusList = [0 => '待审核', 1 => '审核通过', 2 => '审核不通过', 3 => '待审核'];
$status_text = $statusList[$courseSign->status];
// 订阅消息
$template_id = Config::getValueByKey('sign_template');
$sendData = [
'template_id' => $template_id,
'touser' => $vo->notifiable->openid,
'path' => "/packages/mycourse/coursePay?id={$courseSign->course_id}",
'data' => [
'thing7' => [
'value' => $this->strlen($course->name . '报名审核'),
],
'phrase1' => [
'value' => $status_text,
],
'thing5' => [
'value' => '感谢配合',
],
]
];
$this->appletNotice($vo, $sendData);
// 发送短信
if ($course->is_fee == 0) {
// 免费课程
if ($courseSign->status == 1) {
// 通过
$content = "{$smsSign}恭喜!您已被【{$course->name}】录取您可在STBC小程序“我的课程”中查看详细内容。";
$this->smsNotice($vo, $content);
}
if ($courseSign->status == 2) {
// 不通过
$content = "{$smsSign}很遗憾,您报名【{$course->name}】未被录取。感谢您的支持!";
$this->smsNotice($vo, $content);
}
}
if ($course->is_fee == 1) {
// 付费课程
if ($courseSign->status == 1) {
// 通过
$content = "{$smsSign}恭喜!您已被【{$course->name}】录取请在STBC小程序“我的”-“我的课程”-“报名状态”中完成入学手续。";
$this->smsNotice($vo, $content);
}
if ($courseSign->status == 2) {
// 不通过
$content = "{$smsSign}很遗憾,您报名【{$course->name}】未被录取。感谢您的支持!";
$this->smsNotice($vo, $content);
}
}
break;
case "App\Notifications\AppointmentNotify":
// 预约审核通知
$appointments = Appointment::find($data['appointment_id']);
$appointmentConfig = AppointmentConfig::find($appointments->site);
$status_text = Appointment::$intToString['status'][$appointments->status];
// 订阅消息
$template_id = Config::getValueByKey('appointment_template');
$sendData = [
'template_id' => $template_id,
'touser' => $vo->notifiable->openid,
'path' => "packages/mycourse/index",
'data' => [
'thing11' => [
'value' => $appointmentConfig->name,
],
'phrase3' => [
'value' => $status_text,
],
]
];
$this->appletNotice($vo, $sendData);
// 发送短信
// if ($appointments->status == 1) {
// // 通过
// $content = "{$smsSign}您预约的{$appointmentConfig->name}审核已经通过,感谢您的配合。";
// $this->smsNotice($vo, $content);
// }
if ($appointments->status == 2) {
// 不通过
$content = "{$smsSign}您申请的【{$appointmentConfig->name}】预约失败您可在STBC小程序我的-我的预约中查看原因,并重新预约。";
$this->smsNotice($vo, $content);
}
if ($appointments->status == 4) {
// 预约门禁失败
$content = "{$smsSign}非常抱歉,您预约的【{$appointmentConfig->name}】,预约时间:{$appointments->start_time},未能成功预约。";
$this->smsNotice($vo, $content);
}
break;
case "App\Notifications\MeetNotify":
// 门禁二维码通知
$appointments = Appointment::find($data['appointment_id']);
$appointmentConfig = $appointments->site_detail;
// 预约成功发送入场二维码通知
$url = \config('app.url') . '/user/h5-show?code=' . $appointments->code;
$time = date('Y-m-d', strtotime($appointments->start_time)) . '至' . date('Y-m-d', strtotime($appointments->end_time));
$content = "{$smsSign}您已成功预约{$appointmentConfig->pluck('name')->implode('、')},预约时间:{$time},预约二维码获取地址:{$url}";
// 给主预约人发信息
$this->smsNotice($vo, $content);
// 同行人也要发送
$appointmentAccompanies = AppointmentAccompany::where('appointment_id', $data['appointment_id'])->get();
foreach ($appointmentAccompanies as $item) {
ymSms($item->mobile, $content);
}
break;
case "App\Notifications\CourseContentNotify":
// 排课通知
$course = Course::find($data['course_id']);
$content = "{$smsSign}亲爱的同学最新课表已发布您可在STBC小程序“我的”-“我的课程”-“本班课表”中查看。";
$this->smsNotice($vo, $content);
break;
6 months ago
case "App\Notifications\BirthdayNotify":
6 months ago
// 生日通知todo::文案待定
6 months ago
$user = User::find($data['user_id']);
6 months ago
$url = $this->urlLink();
$content = "{$smsSign}亲爱的同学:祝您生日快乐!登陆小程序有惊喜:{$url}";
6 months ago
$this->smsNotice($vo, $content);
break;
6 months ago
}
return $this->info('发送完成');
}
/**
* 截断文字
*/
public function strlen($str, $lenth = 16)
{
$len = mb_strlen($str);
if ($len > $lenth) {
return mb_substr($str, 0, $lenth) . "...";
}
return $str;
}
6 months ago
/**
* 获取跳转短链接
*/
public function urlLink()
{
$config = [
'app_id' => config('app.applet_appid'),
'secret' => config('app.applet_secret')
];
$app = Factory::miniProgram($config);
$result = $app->url_link->generate([]);
return $result['url_link'] ?? '';
}
6 months ago
/**
* 发送小程序通知
*/
public function appletNotice($vo, $sendData)
{
$config = [
'app_id' => config('app.applet_appid'),
'secret' => config('app.applet_secret')
];
$app = Factory::miniProgram($config);
$result = $app->subscribe_message->send($sendData);
// 写记录
$vo->via_times++;
$vo->send_time = now();
$vo->send_data = json_encode($sendData, JSON_UNESCAPED_UNICODE);
$vo->retrun_data = json_encode($result, JSON_UNESCAPED_UNICODE);
$vo->save();
return $result;
}
/**
* 发送短信通知
*/
public function smsNotice($vo, $content)
{
$result = ymSms($vo->notifiable->mobile, $content);
// 写记录
$vo->via_times++;
$vo->sms_send_time = now();
$vo->sms_send_data = $vo->notifiable->mobile . '-' . $content;
$vo->sms_retrun_data = $result;
$vo->save();
return $result;
}
}