Config.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. * @apiExample {js} button 示例
  280. * //button 的数据格式如下
  281. * [
  282. * [ //第一行
  283. * { //第一行按钮 的第一个按钮
  284. * "text": "百度", //按钮文字
  285. * "url": "https://baidu.com" //按钮跳转的链接
  286. * },
  287. * { //第一行按钮 的第二个按钮
  288. * "text": "百度",
  289. * "url": "https://baidu.com"
  290. * }
  291. * //更多按钮...
  292. * ],
  293. * [ //第二行
  294. * {
  295. * "text": "百度",
  296. * "url": "https://baidu.com"
  297. * }
  298. * ]
  299. * //更多行...
  300. * ]
  301. */
  302. public function sendChannelMessage()
  303. {
  304. set_time_limit(0);
  305. DB::beginTransaction();
  306. try {
  307. $type = request()->input('type', 'image');
  308. if ($type == 'image') {
  309. request()->validate([
  310. 'chatId' => ['required', 'string', 'min:1'],
  311. 'image' => ['required', 'url'],
  312. 'text' => ['nullable', 'string'],
  313. 'button' => ['required', 'array'],
  314. 'button.*' => ['array'],
  315. 'button.*.*.text' => ['required', 'string'],
  316. 'button.*.*.url' => ['required', 'url'],
  317. 'isSend' => ['nullable', 'boolean'],
  318. 'isTop' => ['nullable', 'boolean'],
  319. ]);
  320. } else {
  321. request()->validate([
  322. 'chatId' => ['required', 'string', 'min:1'],
  323. 'video' => ['required', 'url'],
  324. 'video_caption' => ['nullable', 'string'],
  325. 'isSend' => ['nullable', 'boolean'],
  326. 'isTop' => ['nullable', 'boolean'],
  327. ]);
  328. }
  329. $chatId = request()->input('chatId');
  330. $image = request()->input('image');
  331. $button = request()->input('button');
  332. $text = request()->input('text');
  333. $isSend = request()->input('isSend', true);
  334. $isTop = request()->input('isTop', true);
  335. $video = request()->input('video');
  336. $video_caption = request()->input('video_caption');
  337. ConfigModel::where('field', 'channel_message')
  338. ->update([
  339. 'val' => json_encode([
  340. 'chatId' => $chatId,
  341. 'image' => $image,
  342. 'video' => $video,
  343. 'video_caption' => $video_caption,
  344. 'text' => $text,
  345. 'button' => $button
  346. ])
  347. ]);
  348. DB::commit();
  349. } catch (ValidationException $e) {
  350. DB::rollBack();
  351. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  352. } catch (Exception $e) {
  353. DB::rollBack();
  354. return $this->error(intval($e->getCode()), $e->getMessage());
  355. }
  356. if ($isSend) {
  357. try {
  358. $config = ConfigModel::where('field', 'channel_message')
  359. ->first()->val;
  360. $config = json_decode($config, true);
  361. $telegram = new Api(config('services.telegram.token'));
  362. if ($type == 'image') {
  363. // 发送图片消息
  364. $response = $telegram->sendPhoto([
  365. 'chat_id' => "@{$config['chatId']}",
  366. 'photo' => InputFile::create($config['image']),
  367. 'caption' => $config['text'],
  368. 'protect_content' => false,
  369. 'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
  370. ]);
  371. } else {
  372. // 发送视频消息
  373. $response = $telegram->sendVideo([
  374. 'chat_id' => "@{$config['chatId']}",
  375. 'video' => InputFile::create($config['video']),
  376. 'caption' => $config['video_caption'],
  377. 'protect_content' => false
  378. // 'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
  379. ]);
  380. }
  381. if ($isTop) {
  382. // 获取消息ID
  383. $messageId = $response->get('message_id');
  384. // 置顶消息
  385. $telegram->pinChatMessage([
  386. 'chat_id' => "@{$config['chatId']}",
  387. 'message_id' => $messageId
  388. ]);
  389. }
  390. } catch (TelegramSDKException $e) {
  391. return $this->error(HttpStatus::CUSTOM_ERROR, '保存成功,发送失败');
  392. } catch (Exception $e) {
  393. return $this->error(intval($e->getCode()), '保存成功,发送失败');
  394. }
  395. }
  396. return $this->success();
  397. }
  398. // public function sendChannelVideo()
  399. // {
  400. // $chatId = request()->input('chatId');
  401. // $video = request()->input('video');
  402. // // $config = ConfigModel::where('field', 'channel_message')
  403. // // ->first()->val;
  404. // // $config = json_decode($config, true);
  405. // $telegram = new Api(config('services.telegram.token'));
  406. // // 发送图片消息
  407. // $response = $telegram->sendVideo([
  408. // 'chat_id' => "@{$chatId}",
  409. // 'caption' => '这是一个视频消息',
  410. // 'video' => InputFile::create($video),
  411. // 'protect_content' => false,
  412. // ]);
  413. // // 获取消息ID
  414. // $messageId = $response->get('message_id');
  415. // // 获取消息ID
  416. // $messageId = $response->get('message_id');
  417. // // 置顶消息
  418. // $telegram->pinChatMessage([
  419. // 'chat_id' => "@{$chatId}",
  420. // 'message_id' => $messageId
  421. // ]);
  422. // return $this->success($messageId);
  423. // }
  424. /**
  425. * @api {post} /admin/config/set 修改配置
  426. * @apiGroup 配置
  427. * @apiUse result
  428. * @apiUse header
  429. * @apiVersion 1.0.0
  430. *
  431. * @apiParam {int[]} base_score 房间底分数组
  432. * @apiParam {string} brokerage 抽佣比例
  433. * @apiParam {string} service_charge 提现手续费
  434. * @apiParam {string} service_account 客服账号
  435. * @apiParam {string} receiving_address 充值收款地址
  436. * @apiParam {string} receiving_type 收款方式 1-自动 2-手动
  437. *
  438. */
  439. public function set()
  440. {
  441. DB::beginTransaction();
  442. try {
  443. request()->validate([
  444. 'base_score' => ['required', 'array', 'min:1', 'max:30'],
  445. 'base_score.*' => ['integer', 'min:1'],
  446. 'brokerage' => ['required', 'numeric', 'min:0.01', 'max:1', 'regex:/^\d*(\.\d{1,2})?$/'],
  447. 'service_charge' => ['required', 'integer', 'min:1'],
  448. 'service_account' => ['required', 'string', 'min:1'],
  449. 'receiving_address' => ['required', 'string', 'min:34'],
  450. 'receiving_type' => ['required', 'integer', 'in:1,2']
  451. ]);
  452. $baseScore = request()->input('base_score');
  453. sort($baseScore);
  454. $baseScore = array_unique($baseScore);
  455. ConfigModel::where('field', 'base_score')
  456. ->update(['val' => json_encode($baseScore)]);
  457. $val = request()->input('brokerage');
  458. ConfigModel::where('field', 'brokerage')
  459. ->update(['val' => $val]);
  460. $val = request()->input('service_charge');
  461. ConfigModel::where('field', 'service_charge')
  462. ->update(['val' => $val]);
  463. $val = request()->input('service_account');
  464. ConfigModel::where('field', 'service_account')
  465. ->update(['val' => $val]);
  466. $val = request()->input('receiving_address');
  467. ConfigModel::where('field', 'receiving_address')
  468. ->update(['val' => $val]);
  469. $val = request()->input('receiving_type');
  470. ConfigModel::where('field', 'receiving_type')
  471. ->update(['val' => $val]);
  472. DB::commit();
  473. } catch (ValidationException $e) {
  474. DB::rollBack();
  475. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  476. } catch (Exception $e) {
  477. DB::rollBack();
  478. return $this->error(intval($e->getCode()), $e->getMessage());
  479. }
  480. return $this->success();
  481. }
  482. }