diff --git a/app/Admin/routes.php b/app/Admin/routes.php index f33be86..87b2482 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -8,7 +8,7 @@ Route::group([ 'prefix' => config('admin.route.prefix'), 'middleware' => config('admin.route.middleware'), ], function (Router $router) { - $router->resource('dashboard', \App\Admin\Controllers\HomeController::class); + $router->get('dashboard', [\App\Admin\Controllers\HomeController::class, 'index']); $router->get('menus', [\App\Admin\Controllers\HomeController::class, 'menus']); $router->get('current-user', [\App\Admin\Controllers\AuthController::class, 'currentUser']); $router->post('login', [\App\Admin\Controllers\AuthController::class, 'login']); diff --git a/app/Console/Commands/PatientRecordNotify.php b/app/Console/Commands/PatientRecordNotify.php index 5d95181..55845bc 100644 --- a/app/Console/Commands/PatientRecordNotify.php +++ b/app/Console/Commands/PatientRecordNotify.php @@ -3,8 +3,10 @@ namespace App\Console\Commands; use Illuminate\Console\Command; -use App\Models\PatientRecord; +use App\Models\{PatientRecord, UserSocialite}; use Overtrue\LaravelWeChat\EasyWeChat; +use Slowlyo\OwlAdmin\Models\AdminUser; +use App\Enums\SocialiteType; class PatientRecordNotify extends Command { @@ -20,21 +22,44 @@ class PatientRecordNotify extends Command * * @var string */ - protected $description = '病历记录: 通知医生'; + protected $description = '病历记录: 通知'; /** * Execute the console command. */ public function handle() { - $list = PatientRecord::where('is_notified', 0)->whereNotNul('notify_user_id')->whereNotNul('notify_at')->get(); + $now = now(); + $list = PatientRecord::where('is_notified', 0) + ->whereNotNull('notify_user_id') + ->whereBetween('notify_at', [$now->copy()->startOfDay(), $now->copy()->endOfDay()]) + ->get(); $app = EasyWeChat::officialAccount(); $app->setAccessToken(new \App\Services\WechatOfficialAccessToken( - appId: $this->getAccount()->getAppId(), - secret: $this->getAccount()->getSecret(), - cache: $this->getCache(), - httpClient: $this->getHttpClient(), - stable: $this->config->get('use_stable_access_token', false), + 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(); + // 微信公众号关联账户 + $users = UserSocialite::where('user_type', (new AdminUser)->getMorphClass())->where('type', SocialiteType::WxOfficial)->whereIn('user_id', $list->pluck('notify_user_id'))->get(); + foreach ($list as $item) { + $user = $users->firstWhere('user_id', $item->notify_user_id); + if ($user) { + $response = $api->postJson('/cgi-bin/message/template/send', [ + 'touser' => $user->openid, + 'template_id' => '', + 'url' => url('/h5/pages/record/detail?id=' . $item->id), + 'data' => [] + ]); + if ($response->isFailed()) { + logger('病历记录: 通知, 模板消息发送, 失败', $response->toArray(false)); + continue; + } + $item->update(['is_notified' => 1]); + } + } } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index e6b9960..7ac62c4 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -13,6 +13,8 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule): void { // $schedule->command('inspire')->hourly(); + $schedule->call(fn () => logger('schdule running'))->everyMinute(); + $schedule->command('patient-record:notify')->daily(); } /** diff --git a/config/easywechat.php b/config/easywechat.php index a7df7f2..8dec1c8 100644 --- a/config/easywechat.php +++ b/config/easywechat.php @@ -7,6 +7,8 @@ return [ 'defaults' => [ 'http' => [ 'timeout' => 5.0, + 'throw' => false, + 'retry' => false, ], ],