123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- namespace App\Http\Controllers\admin;
- use App\Constants\HttpStatus;
- use App\Constants\Util;
- use App\Http\Controllers\Controller;
- use App\Models\Config as ConfigModel;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Validation\ValidationException;
- use Exception;
- use Telegram\Bot\Api;
- use Telegram\Bot\Exceptions\TelegramSDKException;
- use Telegram\Bot\FileUpload\InputFile;
- class Config extends Controller
- {
- /**
- * @api {get} /admin/config/get 获取指定配置
- * @apiGroup 配置
- * @apiUse result
- * @apiUse header
- * @apiVersion 1.0.0
- *
- * @apiParam {string} field 配置项
- * - base_score 房间底分数组
- * - brokerage 抽佣比例
- * - service_charge 提现手续费
- * - service_account 客服账号
- * - receiving_address 手动收款 的地址
- * - receiving_type 收款方式 1-自动 2-手动
- * - channel_message 频道消息
- *
- * @apiSuccess (data) {Object} data
- * @apiSuccess (data) {int[]} [data.base_score] 房间底分数组
- * @apiSuccess (data) {float} [data.brokerage] 抽佣比例
- * @apiSuccess (data) {int} [data.service_charge] 提现手续费
- * @apiSuccess (data) {string} [data.service_account] 客服账号
- * @apiSuccess (data) {string} [data.receiving_address] 手动收款 的地址
- * @apiSuccess (data) {int} [data.receiving_type] 收款方式 1-自动 2-手动
- * @apiSuccess (data) {Object} [data.channel_message] 频道消息
- * @apiSuccess (data) {string} data.channel_message.chatId 频道账号或频道ID
- * @apiSuccess (data) {string} data.channel_message.image 要发送频道消息 的图片URL
- * @apiSuccess (data) {string} data.channel_message.text 要发送频道消息文 的本内容
- * @apiSuccess (data) {array} data.channel_message.button 要发送频道消息 的内联按钮,具体结构见下方
- * @apiSuccessExample {js} 内联按钮结构
- * //button 的数据格式如下
- * [
- * [ //第一行
- * { //第一行按钮 的第一个按钮
- * "text": "百度", //按钮文字
- * "url": "https://baidu.com" //按钮跳转的链接
- * },
- * { //第一行按钮 的第二个按钮
- * "text": "百度",
- * "url": "https://baidu.com"
- * }
- * //更多按钮...
- * ],
- * [ //第二行
- * {
- * "text": "百度",
- * "url": "https://baidu.com"
- * }
- * ]
- * //更多行...
- * ]
- *
- */
- public function get()
- {
- try {
- request()->validate([
- 'field' => ['required', 'string', 'min:1'],
- ]);
- $field = request()->input('field');
- $config = ConfigModel::where('field', $field)->first();
- $res = [];
- if ($config) {
- $val = $config->val;
- switch ($field) {
- case "channel_message":
- case "base_score":
- $val = json_decode($config->val);
- break;
- case "brokerage":
- case "service_charge":
- $val = floatval($config->val);
- break;
- }
- $res[$field] = $val;
- }
- } catch (ValidationException $e) {
- return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
- } catch (Exception $e) {
- return $this->error(intval($e->getCode()));
- }
- return $this->success($res);
- }
- /**
- * @api {get} /admin/config/getAll 获取所有配置
- * @apiGroup 配置
- * @apiUse result
- * @apiUse header
- * @apiVersion 1.0.0
- */
- public function getAll()
- {
- $list = ConfigModel::where('id', '>', 0)->get();
- $arr = [];
- foreach ($list as $item) {
- $val = $item['val'];
- switch ($item['field']) {
- case "channel_message":
- case "base_score":
- $val = json_decode($item['val'], true);
- break;
- case "brokerage":
- case "service_charge":
- $val = floatval($item['val']);
- break;
- }
- $arr[$item['field']] = $val;
- }
- return $this->success($arr);
- }
- /**
- * @api {post} /admin/config/sendChannelMessage 发送频道消息
- * @apiGroup 配置
- * @apiUse result
- * @apiUse header
- * @apiVersion 1.0.0
- * @apiDescription 该接口会保存配置,并发送频道消息; 创建频道之后需要将 bot 拉进频道内,然后将 bot 设置为管理员
- *
- *
- * @apiParam {String} chatId 频道的username - 创建频道时候填写的 username
- * @apiParam {String} type 发送的类型 - image:图片 - video:视频
- * @apiParam {String} image 要发送的图片
- * @apiParam {String} text 要发送的文字
- * @apiParam {Array} button 消息中的按钮 具体结构请看示例代码
- * @apiParam {String} video 视频
- * @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()
- {
- set_time_limit(0);
- DB::beginTransaction();
- try {
- $type = request()->input('type', 'image');
- if ($type == 'image') {
- request()->validate([
- 'chatId' => ['required', 'string', 'min:1'],
- 'image' => ['required', 'url'],
- 'text' => ['nullable', 'string'],
- 'button' => ['required', 'array'],
- 'button.*' => ['array'],
- 'button.*.*.text' => ['required', 'string'],
- 'button.*.*.url' => ['required', 'url'],
- 'isSend' => ['nullable', 'boolean'],
- 'isTop' => ['nullable', 'boolean'],
- ]);
- } else {
- request()->validate([
- 'chatId' => ['required', 'string', 'min:1'],
- 'video' => ['required','url'],
- 'video_caption' => ['nullable', 'string'],
- 'isSend' => ['nullable', 'boolean'],
- 'isTop' => ['nullable', 'boolean'],
- ]);
- }
- $chatId = request()->input('chatId');
- $image = request()->input('image');
- $button = request()->input('button');
- $text = request()->input('text');
- $isSend = request()->input('isSend', true);
- $isTop = request()->input('isTop', true);
- $video = request()->input('video');
- $video_caption = request()->input('video_caption');
- ConfigModel::where('field', 'channel_message')
- ->update([
- 'val' => json_encode([
- 'chatId' => $chatId,
- 'image' => $image,
- 'video' => $video,
- 'video_caption' => $video_caption,
- 'text' => $text,
- 'button' => $button
- ])
- ]);
- DB::commit();
- } catch (ValidationException $e) {
- DB::rollBack();
- return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
- } catch (Exception $e) {
- DB::rollBack();
- return $this->error(intval($e->getCode()), $e->getMessage());
- }
- if ($isSend) {
- try {
- $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']])
- ]);
- }
- if ($isTop) {
- // 获取消息ID
- $messageId = $response->get('message_id');
- // 置顶消息
- $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->success();
- }
- // public function sendChannelVideo()
- // {
- // $chatId = request()->input('chatId');
- // $video = request()->input('video');
- // // $config = ConfigModel::where('field', 'channel_message')
- // // ->first()->val;
- // // $config = json_decode($config, true);
- // $telegram = new Api(config('services.telegram.token'));
- // // 发送图片消息
- // $response = $telegram->sendVideo([
- // 'chat_id' => "@{$chatId}",
- // 'caption' => '这是一个视频消息',
- // 'video' => InputFile::create($video),
- // 'protect_content' => false,
- // ]);
- // // 获取消息ID
- // $messageId = $response->get('message_id');
- // // 获取消息ID
- // $messageId = $response->get('message_id');
- // // 置顶消息
- // $telegram->pinChatMessage([
- // 'chat_id' => "@{$chatId}",
- // 'message_id' => $messageId
- // ]);
- // return $this->success($messageId);
- // }
- /**
- * @api {post} /admin/config/set 修改配置
- * @apiGroup 配置
- * @apiUse result
- * @apiUse header
- * @apiVersion 1.0.0
- *
- * @apiParam {int[]} base_score 房间底分数组
- * @apiParam {string} brokerage 抽佣比例
- * @apiParam {string} service_charge 提现手续费
- * @apiParam {string} service_account 客服账号
- * @apiParam {string} receiving_address 充值收款地址
- * @apiParam {string} receiving_type 收款方式 1-自动 2-手动
- *
- */
- public function set()
- {
- DB::beginTransaction();
- try {
- request()->validate([
- 'base_score' => ['required', 'array', 'min:1', 'max:30'],
- 'base_score.*' => ['integer', 'min:1'],
- 'brokerage' => ['required', 'numeric', 'min:0.01', 'max:1', 'regex:/^\d*(\.\d{1,2})?$/'],
- 'service_charge' => ['required', 'integer', 'min:1'],
- 'service_account' => ['required', 'string', 'min:1'],
- 'receiving_address' => ['required', 'string', 'min:34'],
- 'receiving_type' => ['required', 'integer', 'in:1,2']
- ]);
- $baseScore = request()->input('base_score');
- sort($baseScore);
- $baseScore = array_unique($baseScore);
- ConfigModel::where('field', 'base_score')
- ->update(['val' => json_encode($baseScore)]);
- $val = request()->input('brokerage');
- ConfigModel::where('field', 'brokerage')
- ->update(['val' => $val]);
- $val = request()->input('service_charge');
- ConfigModel::where('field', 'service_charge')
- ->update(['val' => $val]);
- $val = request()->input('service_account');
- ConfigModel::where('field', 'service_account')
- ->update(['val' => $val]);
- $val = request()->input('receiving_address');
- ConfigModel::where('field', 'receiving_address')
- ->update(['val' => $val]);
- $val = request()->input('receiving_type');
- ConfigModel::where('field', 'receiving_type')
- ->update(['val' => $val]);
- DB::commit();
- } catch (ValidationException $e) {
- DB::rollBack();
- return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
- } catch (Exception $e) {
- DB::rollBack();
- return $this->error(intval($e->getCode()), $e->getMessage());
- }
- return $this->success();
- }
- }
|