telegram: order notify

This commit is contained in:
Tokumeikoi 2020-07-31 15:22:27 +08:00
parent eb53067e67
commit 6e7fb4284a
3 changed files with 27 additions and 14 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Guest; namespace App\Http\Controllers\Guest;
use App\Services\OrderService; use App\Services\OrderService;
use App\Services\TelegramService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Order; use App\Models\Order;
@ -160,6 +161,16 @@ class OrderController extends Controller
abort(500, 'order is not found'); abort(500, 'order is not found');
} }
$orderService = new OrderService($order); $orderService = new OrderService($order);
return $orderService->success($callbackNo); if (!$orderService->success($callbackNo)) {
return false;
}
$telegramService = new TelegramService();
$message = sprintf(
"💰成功收款%s元\n———————————————\n订单号:%s",
$order->total_amount / 100,
$order->trade_no
);
$telegramService->sendMessageWithAdmin($message);
return true;
} }
} }

View File

@ -7,6 +7,7 @@ use App\Http\Requests\User\TicketSave;
use App\Http\Requests\User\TicketWithdraw; use App\Http\Requests\User\TicketWithdraw;
use App\Jobs\SendTelegramJob; use App\Jobs\SendTelegramJob;
use App\Models\User; use App\Models\User;
use App\Services\TelegramService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Ticket; use App\Models\Ticket;
use App\Models\TicketMessage; use App\Models\TicketMessage;
@ -180,21 +181,10 @@ class TicketController extends Controller
abort(500, '工单创建失败'); abort(500, '工单创建失败');
} }
DB::commit(); DB::commit();
$this->sendNotify($ticket, $ticketMessage); $telegramService = new TelegramService();
$telegramService->sendMessageWithAdmin("📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->subject}`\n内容:\n`{$ticketMessage->message}`");
return response([ return response([
'data' => true 'data' => true
]); ]);
} }
private function sendNotify(Ticket $ticket, TicketMessage $ticketMessage)
{
if (!config('v2board.telegram_bot_enable', 0)) return;
$users = User::where('is_admin', 1)
->where('telegram_id', '!=', NULL)
->get();
foreach ($users as $user) {
$text = "📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->subject}`\n内容:\n`{$ticketMessage->message}`";
SendTelegramJob::dispatch($user->telegram_id, $text);
}
}
} }

View File

@ -1,6 +1,7 @@
<?php <?php
namespace App\Services; namespace App\Services;
use App\Jobs\SendTelegramJob;
use \Curl\Curl; use \Curl\Curl;
class TelegramService { class TelegramService {
@ -43,4 +44,15 @@ class TelegramService {
} }
return $response; return $response;
} }
public function sendMessageWithAdmin($message)
{
if (!config('v2board.telegram_bot_enable', 0)) return;
$users = User::where('is_admin', 1)
->where('telegram_id', '!=', NULL)
->get();
foreach ($users as $user) {
SendTelegramJob::dispatch($user->telegram_id, $message);
}
}
} }