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.

41 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services\SupplyDemand;
use App\Models\User;
use App\Models\Message;
use App\Models\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;
class Notices
{
public static function queue($userId) {
if (!$userId) return;
DB::table('supply_demand_sms_notices')->insertOrIgnore(['user_id' => $userId, 'created_at' => now(), 'updated_at' => now()]);
DB::table('supply_demand_sms_notices')->where('user_id', $userId)->whereNull('pending_at')->update(['pending_at' => now(), 'attempts' => 0, 'updated_at' => now()]);
}
public static function deliver($userId, callable $sender = null) {
$settings = Settings::all();
if ((int) $settings['sms_enabled'] !== 1) return false;
return DB::transaction(function () use ($userId, $settings, $sender) {
$row = DB::table('supply_demand_sms_notices')->where('user_id', $userId)->lockForUpdate()->first();
if (!$row || !$row->pending_at || $row->attempts >= 3) return false;
if ($row->sent_date !== now()->toDateString()) $row->sent_today = 0;
if ($row->sent_today >= (int) $settings['sms_daily_limit']) return false;
if ($row->last_sent_at && Carbon::parse($row->last_sent_at)->addMinutes((int) $settings['sms_interval_minutes'])->isFuture()) return false;
$user = User::find($userId);
$hasUnread = Message::where('to_user_id', $userId)->where('is_read', 0)->whereHas('supplyDemand', function ($q) use ($userId) { $q->where('user_id', $userId); })->exists();
if (!$user || !$hasUnread) {
DB::table('supply_demand_sms_notices')->where('id', $row->id)->update(['pending_at' => null]); return false;
}
$content = (Config::getValueByKey('sms_sign') ?: '') . '您在STBC小程序发布的对接需求有新的对接,请登录小程序,在“校友权益—供需对接—我的私信”中查看并回复。';
try { $ok = ($sender ?: function ($mobile, $text) { return ymSms($mobile, $text, 0, 'supply_demand', null); })($user->getRawOriginal('mobile'), $content); }
catch (\Throwable $e) { $ok = false; }
DB::table('supply_demand_sms_notices')->where('id', $row->id)->update([
'pending_at' => $ok ? null : $row->pending_at, 'last_sent_at' => $ok ? now() : $row->last_sent_at,
'sent_date' => now()->toDateString(), 'sent_today' => $row->sent_today + ($ok ? 1 : 0),
'attempts' => $row->attempts + 1, 'last_error' => $ok ? null : '短信发送失败,请检查短信通道;三次失败后可后台重试', 'updated_at' => now(),
]);
return (bool) $ok;
});
}
}