|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Notifications\CustomerOrderCreated;
|
|
|
|
|
use App\Notifications\RechargePaid;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
|
|
|
|
|
|
class Notifications extends CommonModel
|
|
|
|
|
{
|
|
|
|
|
protected $table = "notifications";
|
|
|
|
|
protected $tryTimes = 3;
|
|
|
|
|
protected $scheduleNumber = 10;
|
|
|
|
|
protected $appends = ["title", "content"];
|
|
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
|
|
public function getTitleAttribute()
|
|
|
|
|
{
|
|
|
|
|
$data = json_decode($this->data, true);
|
|
|
|
|
switch ($this->type) {
|
|
|
|
|
case RechargePaid::class:
|
|
|
|
|
if (isset($data["recharge_id"])) {
|
|
|
|
|
$recharge = Recharge::find($data["recharge_id"]);
|
|
|
|
|
$title = $recharge->order ? "订单编号:" . $recharge->order->serial . "有一笔收款到账,请注意查看。" : $recharge->customer->name . "支付了一笔款项,请注意查看。";
|
|
|
|
|
} else {
|
|
|
|
|
$title = "有一笔新的收款到账,请注意查看。";
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CustomerOrderCreated::class:
|
|
|
|
|
$title = "有用户通过小程序进行了下单,订单编号:{$data["order_serial"]},请注意及时处理。";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$title = "创建于{$this->created_at}的通知";
|
|
|
|
|
}
|
|
|
|
|
return $title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getContentAttribute()
|
|
|
|
|
{
|
|
|
|
|
switch ($this->type) {
|
|
|
|
|
case RechargePaid::class:
|
|
|
|
|
$content = $this->getTitleAttribute();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$content = "";
|
|
|
|
|
}
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function notifiable()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphTo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sendSchedule()
|
|
|
|
|
{
|
|
|
|
|
$smsSchedule = $this
|
|
|
|
|
->whereNull("read_at")
|
|
|
|
|
->where(\DB::raw('DATEDIFF(now(),created_at)'), 0)
|
|
|
|
|
->whereBetween("via_sms_times", [1, $this->tryTimes])
|
|
|
|
|
->whereNull('via_sms_at')
|
|
|
|
|
->limit($this->scheduleNumber)
|
|
|
|
|
->with("notifiable")
|
|
|
|
|
->get();
|
|
|
|
|
foreach ($smsSchedule as $vo) {
|
|
|
|
|
$this->sendSms($vo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wechatSchedule = $this
|
|
|
|
|
->whereNull("read_at")
|
|
|
|
|
->where(\DB::raw('DATEDIFF(now(),created_at)'), 0)
|
|
|
|
|
->whereBetween("via_wechat_times", [1, $this->tryTimes])
|
|
|
|
|
->whereNull('via_wechat_at')
|
|
|
|
|
->limit($this->scheduleNumber)
|
|
|
|
|
->get();
|
|
|
|
|
foreach ($wechatSchedule as $vo) {
|
|
|
|
|
$this->sendWechat($vo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sendSms($vo)
|
|
|
|
|
{
|
|
|
|
|
$data = json_decode($vo->data, true);
|
|
|
|
|
|
|
|
|
|
switch ($vo->type) {
|
|
|
|
|
case RechargePaid::class:
|
|
|
|
|
if (isset($data["recharge_id"])) {
|
|
|
|
|
$recharge = Recharge::find($data["recharge_id"]);
|
|
|
|
|
$project = "huBdi3";
|
|
|
|
|
|
|
|
|
|
$vars = [
|
|
|
|
|
"customer" => $recharge->customer->name,
|
|
|
|
|
"money" => $recharge->money,
|
|
|
|
|
"order_serial" => $recharge->order ? ",相关订单号:" . $recharge->order->serial : ""
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($project)) {
|
|
|
|
|
$vo->via_sms_times++;
|
|
|
|
|
$vo->save();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$to = $vo->notifiable->mobile;
|
|
|
|
|
$result = sms($to, $vars, $project, self::class, $vo->id);
|
|
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
|
$vo->via_sms_at = date("Y-m-d H:i:s");
|
|
|
|
|
$vo->save();
|
|
|
|
|
} else {
|
|
|
|
|
$vo->via_sms_times++;
|
|
|
|
|
$vo->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sendWechat($vo)
|
|
|
|
|
{
|
|
|
|
|
//todo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sendTemplateMsg($to, $data, $template_id, $url = "", $type = null, $linked_id = null)
|
|
|
|
|
{
|
|
|
|
|
//todo
|
|
|
|
|
}
|
|
|
|
|
}
|