Config.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. 'text' => ['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. ConfigModel::where('field', 'channel_message')
  327. ->update([
  328. 'val' => json_encode([
  329. 'type' => $type,
  330. 'chatId' => $chatId,
  331. 'image' => $image,
  332. 'video' => $video,
  333. 'text' => $text,
  334. 'button' => $button
  335. ])
  336. ]);
  337. DB::commit();
  338. } catch (ValidationException $e) {
  339. DB::rollBack();
  340. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  341. } catch (Exception $e) {
  342. DB::rollBack();
  343. return $this->error(intval($e->getCode()), $e->getMessage());
  344. }
  345. if ($isSend) {
  346. try {
  347. $config = ConfigModel::where('field', 'channel_message')->first()->val;
  348. $config = json_decode($config, true);
  349. $telegram = new Api(config('services.telegram.token'));
  350. $msg = [];
  351. $msg['chat_id'] = "@{$config['chatId']}";
  352. $msg['protect_content'] = false;
  353. if (!empty($config['button'])) {
  354. $msg['reply_markup'] = json_encode(['inline_keyboard' => $config['button']]);
  355. }
  356. switch ($type) {
  357. case 'image':
  358. $msg['photo'] = InputFile::create($config['image']);
  359. $msg['caption'] = $config['text'];
  360. $response = $telegram->sendPhoto($msg);
  361. break;
  362. case 'video':
  363. $msg['video'] = InputFile::create($config['video']);
  364. $msg['caption'] = $config['text'];
  365. $response = $telegram->sendVideo($msg);
  366. break;
  367. case 'text':
  368. $msg['text'] = $config['text'];
  369. $response = $telegram->sendMessage($msg);
  370. break;
  371. default:
  372. throw new Exception("保存成功,发送失败", HttpStatus::CUSTOM_ERROR);
  373. }
  374. // 置顶消息
  375. if ($isTop) {
  376. $messageId = $response->get('message_id');
  377. $telegram->pinChatMessage(['chat_id' => "@{$config['chatId']}", 'message_id' => $messageId]);
  378. }
  379. } catch (TelegramSDKException $e) {
  380. return $this->error(HttpStatus::CUSTOM_ERROR, '保存成功,发送失败', $e->getMessage());
  381. } catch (Exception $e) {
  382. return $this->error($e->getCode(), '保存成功,发送失败');
  383. }
  384. }
  385. return $this->success();
  386. }
  387. // public function sendChannelVideo()
  388. // {
  389. // $chatId = request()->input('chatId');
  390. // $video = request()->input('video');
  391. // // $config = ConfigModel::where('field', 'channel_message')
  392. // // ->first()->val;
  393. // // $config = json_decode($config, true);
  394. // $telegram = new Api(config('services.telegram.token'));
  395. // // 发送图片消息
  396. // $response = $telegram->sendVideo([
  397. // 'chat_id' => "@{$chatId}",
  398. // 'caption' => '这是一个视频消息',
  399. // 'video' => InputFile::create($video),
  400. // 'protect_content' => false,
  401. // ]);
  402. // // 获取消息ID
  403. // $messageId = $response->get('message_id');
  404. // // 获取消息ID
  405. // $messageId = $response->get('message_id');
  406. // // 置顶消息
  407. // $telegram->pinChatMessage([
  408. // 'chat_id' => "@{$chatId}",
  409. // 'message_id' => $messageId
  410. // ]);
  411. // return $this->success($messageId);
  412. // }
  413. /**
  414. * @api {post} /admin/config/set 修改配置
  415. * @apiGroup 配置
  416. * @apiUse result
  417. * @apiUse header
  418. * @apiVersion 1.0.0
  419. *
  420. * @apiParam {int[]} base_score 房间底分数组
  421. * @apiParam {string} brokerage 抽佣比例
  422. * @apiParam {string} service_charge 提现手续费
  423. * @apiParam {string} service_account 客服账号
  424. * @apiParam {string} receiving_address 充值收款地址
  425. * @apiParam {string} receiving_type 收款方式 1-自动 2-手动
  426. *
  427. */
  428. public function set()
  429. {
  430. DB::beginTransaction();
  431. try {
  432. request()->validate([
  433. 'base_score' => ['required', 'array', 'min:1', 'max:30'],
  434. 'base_score.*' => ['integer', 'min:1'],
  435. 'brokerage' => ['required', 'numeric', 'min:0.01', 'max:1', 'regex:/^\d*(\.\d{1,2})?$/'],
  436. 'service_charge' => ['required', 'integer', 'min:1'],
  437. 'service_account' => ['required', 'string', 'min:1'],
  438. 'receiving_address' => ['required', 'string', 'min:34'],
  439. 'receiving_type' => ['required', 'integer', 'in:1,2']
  440. ]);
  441. $baseScore = request()->input('base_score');
  442. sort($baseScore);
  443. $baseScore = array_unique($baseScore);
  444. ConfigModel::where('field', 'base_score')
  445. ->update(['val' => json_encode($baseScore)]);
  446. $val = request()->input('brokerage');
  447. ConfigModel::where('field', 'brokerage')
  448. ->update(['val' => $val]);
  449. $val = request()->input('service_charge');
  450. ConfigModel::where('field', 'service_charge')
  451. ->update(['val' => $val]);
  452. $val = request()->input('service_account');
  453. ConfigModel::where('field', 'service_account')
  454. ->update(['val' => $val]);
  455. $val = request()->input('receiving_address');
  456. ConfigModel::where('field', 'receiving_address')
  457. ->update(['val' => $val]);
  458. $val = request()->input('receiving_type');
  459. ConfigModel::where('field', 'receiving_type')
  460. ->update(['val' => $val]);
  461. DB::commit();
  462. } catch (ValidationException $e) {
  463. DB::rollBack();
  464. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  465. } catch (Exception $e) {
  466. DB::rollBack();
  467. return $this->error(intval($e->getCode()), $e->getMessage());
  468. }
  469. return $this->success();
  470. }
  471. }