MarketingController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Components\Helpers;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Marketing;
  6. use DB;
  7. use Exception;
  8. use GuzzleHttp\Client;
  9. use Illuminate\Http\JsonResponse;
  10. use Illuminate\Http\Request;
  11. use Log;
  12. use Response;
  13. use RuntimeException;
  14. /**
  15. * 促销控制器
  16. *
  17. * Class MarketingController
  18. *
  19. * @package App\Http\Controllers\Controller
  20. */
  21. class MarketingController extends Controller {
  22. protected static $systemConfig;
  23. public function __construct() {
  24. self::$systemConfig = Helpers::systemConfig();
  25. }
  26. // 邮件群发消息列表
  27. public function emailList(Request $request): \Illuminate\Http\Response {
  28. $status = $request->input('status');
  29. $query = Marketing::query()->whereType(1);
  30. if(isset($status)){
  31. $query->whereStatus($status);
  32. }
  33. $view['list'] = $query->paginate(15)->appends($request->except('page'));
  34. return Response::view('admin.marketing.emailList', $view);
  35. }
  36. // 消息通道群发列表
  37. public function pushList(Request $request): \Illuminate\Http\Response {
  38. $status = $request->input('status');
  39. $query = Marketing::query()->whereType(2);
  40. if(isset($status)){
  41. $query->whereStatus($status);
  42. }
  43. $view['list'] = $query->paginate(15);
  44. return Response::view('admin.marketing.pushList', $view);
  45. }
  46. // 添加推送消息
  47. public function addPushMarketing(Request $request): ?JsonResponse {
  48. $title = $request->input('title');
  49. $content = $request->input('content');
  50. if(!self::$systemConfig['is_push_bear']){
  51. return Response::json(['status' => 'fail', 'message' => '推送失败:请先启用并配置PushBear']);
  52. }
  53. try{
  54. DB::beginTransaction();
  55. $response = (new Client())->get('https://pushbear.ftqq.com/sub', [
  56. 'query' => [
  57. 'sendkey' => self::$systemConfig['push_bear_send_key'],
  58. 'text' => $title,
  59. 'desp' => $content
  60. ]
  61. ]);
  62. $result = json_decode($response->getBody(), true);
  63. if($result->code){ // 失败
  64. $this->addMarketing(2, $title, $content, -1, $result->message);
  65. throw new RuntimeException($result->message);
  66. }
  67. $this->addMarketing(2, $title, $content, 1);
  68. DB::commit();
  69. return Response::json(['status' => 'success', 'message' => '推送成功']);
  70. }catch(Exception $e){
  71. Log::info('PushBear消息推送失败:'.$e->getMessage());
  72. DB::rollBack();
  73. return Response::json(['status' => 'fail', 'message' => '推送失败:'.$e->getMessage()]);
  74. }
  75. }
  76. private function addMarketing($type = 1, $title = '', $content = '', $status = 1, $error = '', $receiver = ''
  77. ): bool {
  78. $marketing = new Marketing();
  79. $marketing->type = $type;
  80. $marketing->receiver = $receiver;
  81. $marketing->title = $title;
  82. $marketing->content = $content;
  83. $marketing->error = $error;
  84. $marketing->status = $status;
  85. return $marketing->save();
  86. }
  87. }