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.

129 lines
3.8 KiB

5 years ago
<?php
namespace App\Models;
3 years ago
use App\Notifications\CustomerOrderCreated;
5 years ago
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;
5 years ago
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;
3 years ago
case CustomerOrderCreated::class:
3 years ago
$title = "有用户通过小程序进行了下单,订单编号:{$data["order_serial"]},请注意及时处理。";
3 years ago
break;
5 years ago
default:
$title = "创建于{$this->created_at}的通知";
}
return $title;
}
public function getContentAttribute()
{
switch ($this->type) {
case RechargePaid::class:
$content = $this->getTitleAttribute();
break;
default:
$content = "";
}
return $content;
}
5 years ago
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
}
}