Config.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use Illuminate\Http\JsonResponse;
  4. use App\Constants\HttpStatus;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Config as ConfigModel;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Validation\ValidationException;
  10. use Exception;
  11. use Telegram\Bot\Api;
  12. use Telegram\Bot\Exceptions\TelegramSDKException;
  13. use Telegram\Bot\FileUpload\InputFile;
  14. use App\Services\ConfigService;
  15. class Config extends Controller
  16. {
  17. /**
  18. * @api {post} /admin/config/pc28Switch 游戏切换(0:pc28 1:急速28)
  19. * @apiGroup 配置
  20. * @apiUse result
  21. * @apiUse header
  22. * @apiVersion 1.0.0
  23. *
  24. * @apiParam {int=0,1} val (0:pc28 1:极速28)
  25. */
  26. public function pc28Switch()
  27. {
  28. try {
  29. $params = request()->validate([
  30. 'val' => ['required', 'integer', 'min:0', 'in:0,1']
  31. ]);
  32. Cache::put('pc28_switch', $params['val']);
  33. } catch (ValidationException $e) {
  34. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  35. } catch (Exception $e) {
  36. return $this->error(intval($e->getCode()));
  37. }
  38. return $this->success();
  39. }
  40. /**
  41. * @api {get} /admin/config/pcConfig 极速PC28相关配置
  42. * @apiGroup 配置
  43. * @apiUse result
  44. * @apiUse header
  45. * @apiVersion 1.0.0
  46. *
  47. * @apiParam {int} [page=1]
  48. * @apiParam {int} [limit=15]
  49. */
  50. public function pcConfig(): JsonResponse
  51. {
  52. try {
  53. $search['group_id'] = 4;
  54. $result = ConfigService::paginate($search);
  55. foreach ($result['data'] as $item) {
  56. if ($item['field'] == 'pc28_switch') {
  57. $item['val'] = Cache::get('pc28_switch', $item['val']);
  58. }
  59. }
  60. } catch (ValidationException $e) {
  61. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  62. } catch
  63. (Exception $e) {
  64. return $this->error(intval($e->getCode()));
  65. }
  66. return $this->success($result);
  67. }
  68. /**
  69. * 分页数据
  70. *
  71. */
  72. function index()
  73. {
  74. try {
  75. request()->validate([
  76. 'field' => ['nullable', 'string'],
  77. 'id' => ['nullable', 'string'],
  78. ]);
  79. $search = request()->all();
  80. $search['in_group_id'] = [1, 3];
  81. $result = ConfigService::paginate($search);
  82. } catch (ValidationException $e) {
  83. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  84. } catch (Exception $e) {
  85. return $this->error(intval($e->getCode()));
  86. }
  87. return $this->success($result);
  88. }
  89. /**
  90. * 修改|新增
  91. *
  92. */
  93. public
  94. function store()
  95. {
  96. try {
  97. $id = request()->post('id', '');
  98. if ($id) {
  99. $validator = [
  100. 'id' => ['required', 'integer', 'min:1'],
  101. 'val' => ['required', 'string'],
  102. 'remark' => 'required|nullable|string',
  103. ];
  104. } else {
  105. $validator = [
  106. 'field' => 'required|string|max:50|alpha_dash|unique:config,field',
  107. 'val' => 'required|string',
  108. 'remark' => 'required|nullable|string',
  109. ];
  110. $field = request()->input("field", 'aaa');
  111. switch ($field) {
  112. case 'group_language':
  113. $validator['val'] = ['required', 'string', 'in:zh,vi,en'];
  114. break;
  115. case 'pc28_switch':
  116. $validator['val'] = ['required', 'integer', 'in:0,1'];
  117. break;
  118. }
  119. }
  120. $params = request()->validate($validator);
  121. if (!isset($params['id'])) $params['group_id'] = 3;
  122. $ret = ConfigService::submit($params);
  123. if ($ret['code'] == ConfigService::NOT) {
  124. throw new Exception(HttpStatus::CUSTOM_ERROR, $ret['code']);
  125. }
  126. } catch (ValidationException $e) {
  127. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  128. } catch (Exception $e) {
  129. return $this->error(intval($e->getCode()));
  130. }
  131. return $this->success([], $ret['msg']);
  132. }
  133. /**
  134. * 删除
  135. */
  136. public
  137. function destroy()
  138. {
  139. $id = request()->post('id');
  140. // 示例:通过 ID 删除
  141. $info = ConfigService::findOne(['id' => $id]);
  142. if (!$info) {
  143. return $this->error(HttpStatus::CUSTOM_ERROR, '配置不存在');
  144. }
  145. if ($info->group_id !== 3) {
  146. return $this->error(HttpStatus::CUSTOM_ERROR, '禁止删除');
  147. }
  148. $info->delete();
  149. return $this->success([], '删除成功');
  150. }
  151. /**
  152. * @api {get} /admin/config/get 获取指定配置
  153. * @apiGroup 配置
  154. * @apiUse result
  155. * @apiUse header
  156. * @apiVersion 1.0.0
  157. *
  158. * @apiParam {string} field 配置项
  159. * - base_score 房间底分数组
  160. * - brokerage 抽佣比例
  161. * - service_charge 提现手续费
  162. * - service_account 客服账号
  163. * - receiving_address 手动收款 的地址
  164. * - receiving_type 收款方式 1-自动 2-手动
  165. * - channel_message 频道消息
  166. *
  167. * @apiSuccess (data) {Object} data
  168. * @apiSuccess (data) {int[]} [data.base_score] 房间底分数组
  169. * @apiSuccess (data) {float} [data.brokerage] 抽佣比例
  170. * @apiSuccess (data) {int} [data.service_charge] 提现手续费
  171. * @apiSuccess (data) {string} [data.service_account] 客服账号
  172. * @apiSuccess (data) {string} [data.receiving_address] 手动收款 的地址
  173. * @apiSuccess (data) {int} [data.receiving_type] 收款方式 1-自动 2-手动
  174. * @apiSuccess (data) {Object} [data.channel_message] 频道消息
  175. * @apiSuccess (data) {string} data.channel_message.chatId 频道账号或频道ID
  176. * @apiSuccess (data) {string} data.channel_message.image 要发送频道消息 的图片URL
  177. * @apiSuccess (data) {string} data.channel_message.text 要发送频道消息文 的本内容
  178. * @apiSuccess (data) {array} data.channel_message.button 要发送频道消息 的内联按钮,具体结构见下方
  179. * @apiSuccessExample {js} 内联按钮结构
  180. * //button 的数据格式如下
  181. * [
  182. * [ //第一行
  183. * { //第一行按钮 的第一个按钮
  184. * "text": "百度", //按钮文字
  185. * "url": "https://baidu.com" //按钮跳转的链接
  186. * },
  187. * { //第一行按钮 的第二个按钮
  188. * "text": "百度",
  189. * "url": "https://baidu.com"
  190. * }
  191. * //更多按钮...
  192. * ],
  193. * [ //第二行
  194. * {
  195. * "text": "百度",
  196. * "url": "https://baidu.com"
  197. * }
  198. * ]
  199. * //更多行...
  200. * ]
  201. *
  202. */
  203. public function get()
  204. {
  205. try {
  206. request()->validate([
  207. 'field' => ['required', 'string', 'min:1'],
  208. ]);
  209. $field = request()->input('field');
  210. $config = ConfigModel::where('field', $field)->first();
  211. $res = [];
  212. if ($config) {
  213. $val = $config->val;
  214. switch ($field) {
  215. case "channel_message":
  216. case "base_score":
  217. $val = json_decode($config->val);
  218. break;
  219. case "brokerage":
  220. case "service_charge":
  221. $val = floatval($config->val);
  222. break;
  223. }
  224. $res[$field] = $val;
  225. }
  226. } catch (ValidationException $e) {
  227. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  228. } catch (Exception $e) {
  229. return $this->error(intval($e->getCode()));
  230. }
  231. return $this->success($res);
  232. }
  233. /**
  234. * @api {get} /admin/config/getAll 获取所有配置
  235. * @apiGroup 配置
  236. * @apiUse result
  237. * @apiUse header
  238. * @apiVersion 1.0.0
  239. */
  240. public
  241. function getAll()
  242. {
  243. $list = ConfigModel::where('id', '>', 0)->get();
  244. $arr = [];
  245. foreach ($list as $item) {
  246. $val = $item['val'];
  247. switch ($item['field']) {
  248. case "channel_message":
  249. case "base_score":
  250. $val = json_decode($item['val'], true);
  251. break;
  252. case "brokerage":
  253. case "service_charge":
  254. $val = floatval($item['val']);
  255. break;
  256. }
  257. $arr[$item['field']] = $val;
  258. }
  259. return $this->success($arr);
  260. }
  261. /**
  262. * @api {post} /admin/config/sendChannelMessage 发送频道消息
  263. * @apiGroup 配置
  264. * @apiUse result
  265. * @apiUse header
  266. * @apiVersion 1.0.0
  267. * @apiDescription 该接口会保存配置,并发送频道消息; 创建频道之后需要将 bot 拉进频道内,然后将 bot 设置为管理员
  268. *
  269. *
  270. * @apiParam {String} chatId 频道的username - 创建频道时候填写的 username
  271. * @apiParam {String} type 发送的类型 - image:图片 - video:视频
  272. * @apiParam {String} image 要发送的图片
  273. * @apiParam {String} text 要发送的文字
  274. * @apiParam {Array} button 消息中的按钮 具体结构请看示例代码
  275. * @apiParam {String} video 视频
  276. * @apiParam {String} video_caption 视频文案
  277. * @apiParam {Boolean} [isSend=true] 是否发送
  278. * @apiParam {Boolean} [isTop=true] 是否置顶
  279. */
  280. public function sendChannelMessage()
  281. {
  282. set_time_limit(0);
  283. DB::beginTransaction();
  284. try {
  285. $type = request()->input('type', 'image');
  286. if ($type == 'image') {
  287. request()->validate([
  288. 'chatId' => ['required', 'string', 'min:1'],
  289. 'type' => ['required', 'string', 'in:image,video,text'],
  290. 'image' => ['required', 'url'],
  291. 'text' => ['nullable', 'string'],
  292. 'isSend' => ['nullable', 'boolean'],
  293. 'isTop' => ['nullable', 'boolean'],
  294. 'button' => ['array'],
  295. 'button.*' => ['required', 'array'],
  296. 'button.*.*.text' => ['required', 'string'],
  297. 'button.*.*.url' => ['required', 'url'],
  298. ]);
  299. } else if ($type == 'video') {
  300. request()->validate([
  301. 'chatId' => ['required', 'string', 'min:1'],
  302. 'video' => ['required', 'url'],
  303. 'video_caption' => ['nullable', 'string'],
  304. 'isSend' => ['nullable', 'boolean'],
  305. 'isTop' => ['nullable', 'boolean'],
  306. ]);
  307. } else {
  308. request()->validate([
  309. 'chatId' => ['required', 'string', 'min:1'],
  310. 'text' => ['required', 'string', 'min:1'],
  311. 'isSend' => ['nullable', 'boolean'],
  312. 'isTop' => ['nullable', 'boolean'],
  313. 'button' => ['array'],
  314. 'button.*' => ['required', 'array'],
  315. 'button.*.*.text' => ['required', 'string'],
  316. 'button.*.*.url' => ['required', 'url'],
  317. ]);
  318. }
  319. $chatId = request()->input('chatId');
  320. $image = request()->input('image');
  321. $button = request()->input('button');
  322. $text = request()->input('text');
  323. $isSend = request()->input('isSend', false);
  324. $isTop = request()->input('isTop', false);
  325. $video = request()->input('video');
  326. $video_caption = request()->input('video_caption');
  327. ConfigModel::where('field', 'channel_message')
  328. ->update([
  329. 'val' => json_encode([
  330. 'type' => $type,
  331. 'chatId' => $chatId,
  332. 'image' => $image,
  333. 'video' => $video,
  334. 'video_caption' => $video_caption,
  335. 'text' => $text,
  336. 'button' => $button
  337. ])
  338. ]);
  339. DB::commit();
  340. } catch (ValidationException $e) {
  341. DB::rollBack();
  342. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  343. } catch (Exception $e) {
  344. DB::rollBack();
  345. return $this->error(intval($e->getCode()), $e->getMessage());
  346. }
  347. if ($isSend) {
  348. try {
  349. $config = ConfigModel::where('field', 'channel_message')->first()->val;
  350. $config = json_decode($config, true);
  351. $telegram = new Api(config('services.telegram.token'));
  352. $msg = [];
  353. $msg['chat_id'] = "@{$config['chatId']}";
  354. $msg['protect_content'] = false;
  355. if (in_array($type, ['image', 'text']) && !empty($config['button'])) {
  356. $msg['reply_markup'] = json_encode(['inline_keyboard' => $config['button']]);
  357. }
  358. switch ($type) {
  359. case 'image':
  360. $msg['photo'] = InputFile::create($config['image']);
  361. $msg['caption'] = $config['text'];
  362. $response = $telegram->sendPhoto($msg);
  363. break;
  364. case 'video':
  365. $msg['video'] = InputFile::create($config['video']);
  366. $msg['caption'] = $config['video_caption'];
  367. $response = $telegram->sendVideo($msg);
  368. break;
  369. case 'text':
  370. $msg['text'] = $config['text'];
  371. $response = $telegram->sendMessage($msg);
  372. break;
  373. default:
  374. throw new Exception("保存成功,发送失败", HttpStatus::CUSTOM_ERROR);
  375. }
  376. // 置顶消息
  377. if ($isTop) {
  378. $messageId = $response->get('message_id');
  379. $telegram->pinChatMessage(['chat_id' => "@{$config['chatId']}", 'message_id' => $messageId]);
  380. }
  381. } catch (TelegramSDKException $e) {
  382. return $this->error(HttpStatus::CUSTOM_ERROR, '保存成功,发送失败',$e->getMessage());
  383. } catch (Exception $e) {
  384. return $this->error($e->getCode(), '保存成功,发送失败');
  385. }
  386. }
  387. return $this->success();
  388. }
  389. // public function sendChannelVideo()
  390. // {
  391. // $chatId = request()->input('chatId');
  392. // $video = request()->input('video');
  393. // // $config = ConfigModel::where('field', 'channel_message')
  394. // // ->first()->val;
  395. // // $config = json_decode($config, true);
  396. // $telegram = new Api(config('services.telegram.token'));
  397. // // 发送图片消息
  398. // $response = $telegram->sendVideo([
  399. // 'chat_id' => "@{$chatId}",
  400. // 'caption' => '这是一个视频消息',
  401. // 'video' => InputFile::create($video),
  402. // 'protect_content' => false,
  403. // ]);
  404. // // 获取消息ID
  405. // $messageId = $response->get('message_id');
  406. // // 获取消息ID
  407. // $messageId = $response->get('message_id');
  408. // // 置顶消息
  409. // $telegram->pinChatMessage([
  410. // 'chat_id' => "@{$chatId}",
  411. // 'message_id' => $messageId
  412. // ]);
  413. // return $this->success($messageId);
  414. // }
  415. /**
  416. * @api {post} /admin/config/set 修改配置
  417. * @apiGroup 配置
  418. * @apiUse result
  419. * @apiUse header
  420. * @apiVersion 1.0.0
  421. *
  422. * @apiParam {int[]} base_score 房间底分数组
  423. * @apiParam {string} brokerage 抽佣比例
  424. * @apiParam {string} service_charge 提现手续费
  425. * @apiParam {string} service_account 客服账号
  426. * @apiParam {string} receiving_address 充值收款地址
  427. * @apiParam {string} receiving_type 收款方式 1-自动 2-手动
  428. *
  429. */
  430. public function set()
  431. {
  432. DB::beginTransaction();
  433. try {
  434. request()->validate([
  435. 'base_score' => ['required', 'array', 'min:1', 'max:30'],
  436. 'base_score.*' => ['integer', 'min:1'],
  437. 'brokerage' => ['required', 'numeric', 'min:0.01', 'max:1', 'regex:/^\d*(\.\d{1,2})?$/'],
  438. 'service_charge' => ['required', 'integer', 'min:1'],
  439. 'service_account' => ['required', 'string', 'min:1'],
  440. 'receiving_address' => ['required', 'string', 'min:34'],
  441. 'receiving_type' => ['required', 'integer', 'in:1,2']
  442. ]);
  443. $baseScore = request()->input('base_score');
  444. sort($baseScore);
  445. $baseScore = array_unique($baseScore);
  446. ConfigModel::where('field', 'base_score')
  447. ->update(['val' => json_encode($baseScore)]);
  448. $val = request()->input('brokerage');
  449. ConfigModel::where('field', 'brokerage')
  450. ->update(['val' => $val]);
  451. $val = request()->input('service_charge');
  452. ConfigModel::where('field', 'service_charge')
  453. ->update(['val' => $val]);
  454. $val = request()->input('service_account');
  455. ConfigModel::where('field', 'service_account')
  456. ->update(['val' => $val]);
  457. $val = request()->input('receiving_address');
  458. ConfigModel::where('field', 'receiving_address')
  459. ->update(['val' => $val]);
  460. $val = request()->input('receiving_type');
  461. ConfigModel::where('field', 'receiving_type')
  462. ->update(['val' => $val]);
  463. DB::commit();
  464. } catch (ValidationException $e) {
  465. DB::rollBack();
  466. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  467. } catch (Exception $e) {
  468. DB::rollBack();
  469. return $this->error(intval($e->getCode()), $e->getMessage());
  470. }
  471. return $this->success();
  472. }
  473. }