1
0
Fork 0
medical-record-server/app/Console/Commands/PatientRecordNotify.php

81 lines
2.8 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\{PatientRecord, UserSocialite};
use Overtrue\LaravelWeChat\EasyWeChat;
use Slowlyo\OwlAdmin\Models\AdminUser;
use App\Enums\SocialiteType;
class PatientRecordNotify extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'patient-record:notify';
/**
* The console command description.
*
* @var string
*/
protected $description = '病历记录: 通知';
/**
* Execute the console command.
*/
public function handle()
{
$now = now();
$list = PatientRecord::with(['patient.user'])
->where('is_notified', 0)
->whereNotNull('notify_user_id')
->whereNotNull('next_treat_at')
->where('notify_at', '<=', $now->copy()->endOfDay())
->get();
$app = EasyWeChat::officialAccount();
$app->setAccessToken(new \App\Services\WechatOfficialAccessToken(
appId: $app->getAccount()->getAppId(),
secret: $app->getAccount()->getSecret(),
cache: $app->getCache(),
httpClient: $app->getHttpClient(),
stable: $app->getConfig()->get('use_stable_access_token', false),
));
$api = $app->getClient();
// 微信公众号关联账户
$adminUsers = UserSocialite::where('user_type', (new AdminUser)->getMorphClass())
->where('type', SocialiteType::WxOfficial)
->whereIn('user_id', $list->pluck('notify_user_id'))
->get();
foreach ($list as $item) {
$adminUser = $adminUsers->firstWhere('user_id', $item->notify_user_id);
if ($adminUser) {
$response = $api->postJson('/cgi-bin/message/template/send', [
'touser' => $adminUser->openid,
'template_id' => 'zdkOoIk7bfyzpW9Tuu-pxqh2no-93FCcqstFKLOTfu0',
'url' => url('/h5/pages/record/detail?id=' . $item->id),
'data' => [
'thing1' => [
'value' => data_get($item->patient, 'name')
],
'time3' => [
'value' => $item->next_treat_at->format('Y年m月d日 H:i:s')
],
'phone_number6' => [
'value' => data_get($item->patient, 'phone')
]
]
]);
if ($response->isFailed()) {
logger('病历记录: 通知, 模板消息发送, 失败', $response->toArray(false));
continue;
}
}
$item->update(['is_notified' => 1]);
}
}
}