Ken 3 giorni fa
parent
commit
1d9ee2f2b8
1 ha cambiato i file con 40 aggiunte e 53 eliminazioni
  1. 40 53
      app/Http/Controllers/admin/Config.php

+ 40 - 53
app/Http/Controllers/admin/Config.php

@@ -296,28 +296,6 @@ class Config extends Controller
      * @apiParam {String} video_caption 视频文案
      * @apiParam {Boolean} [isSend=true] 是否发送
      * @apiParam {Boolean} [isTop=true] 是否置顶
-     * @apiExample {js} button 示例
-     * //button 的数据格式如下
-     * [
-     *   [      //第一行
-     *     {    //第一行按钮 的第一个按钮
-     *       "text": "百度",  //按钮文字
-     *       "url": "https://baidu.com"   //按钮跳转的链接
-     *     },
-     *     {    //第一行按钮 的第二个按钮
-     *       "text": "百度",
-     *       "url": "https://baidu.com"
-     *     }
-     *     //更多按钮...
-     *   ],
-     *   [    //第二行
-     *     {
-     *       "text": "百度",
-     *       "url": "https://baidu.com"
-     *     }
-     *   ]
-     *   //更多行...
-     * ]
      */
     public function sendChannelMessage()
     {
@@ -328,18 +306,19 @@ class Config extends Controller
             if ($type == 'image') {
                 request()->validate([
                     'chatId' => ['required', 'string', 'min:1'],
+                    'type' => ['required', 'string', 'in:image,video,text'],
                     'image' => ['required', 'url'],
                     'text' => ['nullable', 'string'],
                     'button' => ['required', 'array'],
-                    'button.*' => ['array','min:1'],
+                    'button.*' => ['required', 'array'],
                     'button.*.*.text' => ['required', 'string'],
-                    'button.*.*.url' => ['required', 'url'],
+                    'button.*.*.url' => ['required', 'string'],
                     'isSend' => ['nullable', 'boolean'],
                     'isTop' => ['nullable', 'boolean'],
                 ]);
 
 
-            } else {
+            } else if ($type == 'video') {
                 request()->validate([
                     'chatId' => ['required', 'string', 'min:1'],
                     'video' => ['required', 'url'],
@@ -347,6 +326,14 @@ class Config extends Controller
                     'isSend' => ['nullable', 'boolean'],
                     'isTop' => ['nullable', 'boolean'],
                 ]);
+            } else {
+                request()->validate([
+                    'chatId' => ['required', 'string', 'min:1'],
+                    'text' => ['required', 'string', 'min:1'],
+                    'isSend' => ['nullable', 'boolean'],
+                    'isTop' => ['nullable', 'boolean'],
+
+                ]);
             }
             $chatId = request()->input('chatId');
             $image = request()->input('image');
@@ -360,6 +347,7 @@ class Config extends Controller
             ConfigModel::where('field', 'channel_message')
                 ->update([
                     'val' => json_encode([
+                        'type' => $type,
                         'chatId' => $chatId,
                         'image' => $image,
                         'video' => $video,
@@ -379,44 +367,43 @@ class Config extends Controller
 
         if ($isSend) {
             try {
-                $config = ConfigModel::where('field', 'channel_message')
-                    ->first()->val;
+                $config = ConfigModel::where('field', 'channel_message')->first()->val;
                 $config = json_decode($config, true);
                 $telegram = new Api(config('services.telegram.token'));
-                if ($type == 'image') {
-                    // 发送图片消息
-                    $response = $telegram->sendPhoto([
-                        'chat_id' => "@{$config['chatId']}",
-                        'photo' => InputFile::create($config['image']),
-                        'caption' => $config['text'],
-                        'protect_content' => false,
-                        'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
-                    ]);
-                } else {
-                    // 发送视频消息
-                    $response = $telegram->sendVideo([
-                        'chat_id' => "@{$config['chatId']}",
-                        'video' => InputFile::create($config['video']),
-                        'caption' => $config['video_caption'],
-                        'protect_content' => false
-                        // 'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
-                    ]);
+                $msg = [];
+                $msg['chat_id'] = "@{$config['chatId']}";
+                $msg['protect_content'] = false;
+                if (in_array($type, ['image', 'text']) && !empty($config['button'])) {
+                    $msg['reply_markup'] = json_encode(['inline_keyboard' => $config['button']]);
                 }
 
-
+                switch ($type) {
+                    case 'image':
+                        $msg['photo'] = InputFile::create($config['image']);
+                        $msg['caption'] = $config['text'];
+                        $response = $telegram->sendPhoto($msg);
+                        break;
+                    case 'video':
+                        $msg['video'] = InputFile::create($config['video']);
+                        $msg['caption'] = $config['video_caption'];
+                        $response = $telegram->sendVideo($msg);
+                        break;
+                    case 'text':
+                        $msg['text'] = $config['text'];
+                        $response = $telegram->sendMessage($msg);
+                        break;
+                    default:
+                        throw new Exception("保存成功,发送失败", HttpStatus::CUSTOM_ERROR);
+                }
+                // 置顶消息
                 if ($isTop) {
-                    // 获取消息ID
                     $messageId = $response->get('message_id');
-                    // 置顶消息
-                    $telegram->pinChatMessage([
-                        'chat_id' => "@{$config['chatId']}",
-                        'message_id' => $messageId
-                    ]);
+                    $telegram->pinChatMessage(['chat_id' => "@{$config['chatId']}", 'message_id' => $messageId]);
                 }
             } catch (TelegramSDKException $e) {
                 return $this->error(HttpStatus::CUSTOM_ERROR, '保存成功,发送失败');
             } catch (Exception $e) {
-                return $this->error(intval($e->getCode()), '保存成功,发送失败');
+                return $this->error($e->getCode(), '保存成功,发送失败');
             }
         }
         return $this->success();