Lae/app/Notifications/ModuleEarnings.php

83 lines
1.9 KiB
PHP
Raw Normal View History

2022-11-06 15:18:46 +00:00
<?php
namespace App\Notifications;
use App\Models\Module;
use Illuminate\Bus\Queueable;
2022-11-19 04:37:18 +00:00
use Illuminate\Notifications\Messages\MailMessage;
2022-11-06 15:18:46 +00:00
use Illuminate\Notifications\Notification;
2022-11-08 13:01:43 +00:00
use Illuminate\Support\Facades\Http;
2022-11-06 15:18:46 +00:00
use Illuminate\Support\Facades\Log;
class ModuleEarnings extends Notification
{
use Queueable;
protected Module $module;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Module $module)
{
$this->module = $module;
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
2022-11-19 04:37:18 +00:00
* @return MailMessage
2022-11-06 15:18:46 +00:00
*/
public function toGroup($notifiable)
{
$module = $this->module;
// make wecom_key visible
$wecom_key = $module->wecom_key ?? config('settings.wecom.robot_hook.billing');
2022-11-19 04:37:18 +00:00
$text = "# {$module->name} 收益";
foreach ($notifiable as $year => $months) {
// 排序 months 从小到大
ksort($months);
$total = 0;
$total_should = 0;
foreach ($months as $month => $m) {
2022-11-19 05:14:51 +00:00
$total += round($m['balance'], 2);
$total_should += round($m['should_balance'], 2);
2022-11-19 04:37:18 +00:00
$text .= <<<EOF
==========
{$year} {$month}
实收: {$total}
应得: {$total_should}
EOF;
}
}
2022-11-06 15:18:46 +00:00
$resp = Http::post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' . $wecom_key, [
'msgtype' => 'markdown',
'markdown' => [
2022-11-19 04:37:18 +00:00
'content' => $text,
2022-11-06 15:18:46 +00:00
],
]);
2022-11-19 04:37:18 +00:00
2022-11-06 15:18:46 +00:00
if ($resp->failed()) {
Log::error('发送模块盈利到企业微信时失败', [
'module' => $module->id,
'data' => $notifiable,
'resp' => $resp->json(),
]);
}
}
}