Config.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. }
  111. $params = request()->validate($validator);
  112. if (!isset($params['id'])) $params['group_id'] = 3;
  113. $ret = ConfigService::submit($params);
  114. if ($ret['code'] == ConfigService::NOT) {
  115. throw new Exception(HttpStatus::CUSTOM_ERROR, $ret['code']);
  116. }
  117. } catch (ValidationException $e) {
  118. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  119. } catch (Exception $e) {
  120. return $this->error(intval($e->getCode()));
  121. }
  122. return $this->success([], $ret['msg']);
  123. }
  124. /**
  125. * 删除
  126. */
  127. public
  128. function destroy()
  129. {
  130. $id = request()->post('id');
  131. // 示例:通过 ID 删除
  132. $info = ConfigService::findOne(['id' => $id]);
  133. if (!$info) {
  134. return $this->error(HttpStatus::CUSTOM_ERROR, '配置不存在');
  135. }
  136. if ($info->group_id !== 3) {
  137. return $this->error(HttpStatus::CUSTOM_ERROR, '禁止删除');
  138. }
  139. $info->delete();
  140. return $this->success([], '删除成功');
  141. }
  142. /**
  143. * @api {get} /admin/config/get 获取指定配置
  144. * @apiGroup 配置
  145. * @apiUse result
  146. * @apiUse header
  147. * @apiVersion 1.0.0
  148. *
  149. * @apiParam {string} field 配置项
  150. * - base_score 房间底分数组
  151. * - brokerage 抽佣比例
  152. * - service_charge 提现手续费
  153. * - service_account 客服账号
  154. * - receiving_address 手动收款 的地址
  155. * - receiving_type 收款方式 1-自动 2-手动
  156. * - channel_message 频道消息
  157. *
  158. * @apiSuccess (data) {Object} data
  159. * @apiSuccess (data) {int[]} [data.base_score] 房间底分数组
  160. * @apiSuccess (data) {float} [data.brokerage] 抽佣比例
  161. * @apiSuccess (data) {int} [data.service_charge] 提现手续费
  162. * @apiSuccess (data) {string} [data.service_account] 客服账号
  163. * @apiSuccess (data) {string} [data.receiving_address] 手动收款 的地址
  164. * @apiSuccess (data) {int} [data.receiving_type] 收款方式 1-自动 2-手动
  165. * @apiSuccess (data) {Object} [data.channel_message] 频道消息
  166. * @apiSuccess (data) {string} data.channel_message.chatId 频道账号或频道ID
  167. * @apiSuccess (data) {string} data.channel_message.image 要发送频道消息 的图片URL
  168. * @apiSuccess (data) {string} data.channel_message.text 要发送频道消息文 的本内容
  169. * @apiSuccess (data) {array} data.channel_message.button 要发送频道消息 的内联按钮,具体结构见下方
  170. * @apiSuccessExample {js} 内联按钮结构
  171. * //button 的数据格式如下
  172. * [
  173. * [ //第一行
  174. * { //第一行按钮 的第一个按钮
  175. * "text": "百度", //按钮文字
  176. * "url": "https://baidu.com" //按钮跳转的链接
  177. * },
  178. * { //第一行按钮 的第二个按钮
  179. * "text": "百度",
  180. * "url": "https://baidu.com"
  181. * }
  182. * //更多按钮...
  183. * ],
  184. * [ //第二行
  185. * {
  186. * "text": "百度",
  187. * "url": "https://baidu.com"
  188. * }
  189. * ]
  190. * //更多行...
  191. * ]
  192. *
  193. */
  194. public function get()
  195. {
  196. try {
  197. request()->validate([
  198. 'field' => ['required', 'string', 'min:1'],
  199. ]);
  200. $field = request()->input('field');
  201. $config = ConfigModel::where('field', $field)->first();
  202. $res = [];
  203. if ($config) {
  204. $val = $config->val;
  205. switch ($field) {
  206. case "channel_message":
  207. case "base_score":
  208. $val = json_decode($config->val);
  209. break;
  210. case "brokerage":
  211. case "service_charge":
  212. $val = floatval($config->val);
  213. break;
  214. }
  215. $res[$field] = $val;
  216. }
  217. } catch (ValidationException $e) {
  218. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  219. } catch (Exception $e) {
  220. return $this->error(intval($e->getCode()));
  221. }
  222. return $this->success($res);
  223. }
  224. /**
  225. * @api {get} /admin/config/getAll 获取所有配置
  226. * @apiGroup 配置
  227. * @apiUse result
  228. * @apiUse header
  229. * @apiVersion 1.0.0
  230. */
  231. public
  232. function getAll()
  233. {
  234. $list = ConfigModel::where('id', '>', 0)->get();
  235. $arr = [];
  236. foreach ($list as $item) {
  237. $val = $item['val'];
  238. switch ($item['field']) {
  239. case "channel_message":
  240. case "base_score":
  241. $val = json_decode($item['val'], true);
  242. break;
  243. case "brokerage":
  244. case "service_charge":
  245. $val = floatval($item['val']);
  246. break;
  247. }
  248. $arr[$item['field']] = $val;
  249. }
  250. return $this->success($arr);
  251. }
  252. /**
  253. * @api {post} /admin/config/sendChannelMessage 发送频道消息
  254. * @apiGroup 配置
  255. * @apiUse result
  256. * @apiUse header
  257. * @apiVersion 1.0.0
  258. * @apiDescription 该接口会保存配置,并发送频道消息; 创建频道之后需要将 bot 拉进频道内,然后将 bot 设置为管理员
  259. *
  260. *
  261. * @apiParam {String} chatId 频道的username - 创建频道时候填写的 username
  262. * @apiParam {String} type 发送的类型 - image:图片 - video:视频
  263. * @apiParam {String} image 要发送的图片
  264. * @apiParam {String} text 要发送的文字
  265. * @apiParam {Array} button 消息中的按钮 具体结构请看示例代码
  266. * @apiParam {String} video 视频
  267. * @apiParam {String} video_caption 视频文案
  268. * @apiParam {Boolean} [isSend=true] 是否发送
  269. * @apiParam {Boolean} [isTop=true] 是否置顶
  270. * @apiExample {js} button 示例
  271. * //button 的数据格式如下
  272. * [
  273. * [ //第一行
  274. * { //第一行按钮 的第一个按钮
  275. * "text": "百度", //按钮文字
  276. * "url": "https://baidu.com" //按钮跳转的链接
  277. * },
  278. * { //第一行按钮 的第二个按钮
  279. * "text": "百度",
  280. * "url": "https://baidu.com"
  281. * }
  282. * //更多按钮...
  283. * ],
  284. * [ //第二行
  285. * {
  286. * "text": "百度",
  287. * "url": "https://baidu.com"
  288. * }
  289. * ]
  290. * //更多行...
  291. * ]
  292. */
  293. public function sendChannelMessage()
  294. {
  295. set_time_limit(0);
  296. DB::beginTransaction();
  297. try {
  298. $type = request()->input('type', 'image');
  299. if ($type == 'image') {
  300. request()->validate([
  301. 'chatId' => ['required', 'string', 'min:1'],
  302. 'image' => ['required', 'url'],
  303. 'text' => ['nullable', 'string'],
  304. 'button' => ['required', 'array'],
  305. 'button.*' => ['array'],
  306. 'button.*.*.text' => ['required', 'string'],
  307. 'button.*.*.url' => ['required', 'url'],
  308. 'isSend' => ['nullable', 'boolean'],
  309. 'isTop' => ['nullable', 'boolean'],
  310. ]);
  311. } else {
  312. request()->validate([
  313. 'chatId' => ['required', 'string', 'min:1'],
  314. 'video' => ['required', 'url'],
  315. 'video_caption' => ['nullable', 'string'],
  316. 'isSend' => ['nullable', 'boolean'],
  317. 'isTop' => ['nullable', 'boolean'],
  318. ]);
  319. }
  320. $chatId = request()->input('chatId');
  321. $image = request()->input('image');
  322. $button = request()->input('button');
  323. $text = request()->input('text');
  324. $isSend = request()->input('isSend', true);
  325. $isTop = request()->input('isTop', true);
  326. $video = request()->input('video');
  327. $video_caption = request()->input('video_caption');
  328. ConfigModel::where('field', 'channel_message')
  329. ->update([
  330. 'val' => json_encode([
  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')
  350. ->first()->val;
  351. $config = json_decode($config, true);
  352. $telegram = new Api(config('services.telegram.token'));
  353. if ($type == 'image') {
  354. // 发送图片消息
  355. $response = $telegram->sendPhoto([
  356. 'chat_id' => "@{$config['chatId']}",
  357. 'photo' => InputFile::create($config['image']),
  358. 'caption' => $config['text'],
  359. 'protect_content' => false,
  360. 'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
  361. ]);
  362. } else {
  363. // 发送视频消息
  364. $response = $telegram->sendVideo([
  365. 'chat_id' => "@{$config['chatId']}",
  366. 'video' => InputFile::create($config['video']),
  367. 'caption' => $config['video_caption'],
  368. 'protect_content' => false
  369. // 'reply_markup' => json_encode(['inline_keyboard' => $config['button']])
  370. ]);
  371. }
  372. if ($isTop) {
  373. // 获取消息ID
  374. $messageId = $response->get('message_id');
  375. // 置顶消息
  376. $telegram->pinChatMessage([
  377. 'chat_id' => "@{$config['chatId']}",
  378. 'message_id' => $messageId
  379. ]);
  380. }
  381. } catch (TelegramSDKException $e) {
  382. return $this->error(HttpStatus::CUSTOM_ERROR, '保存成功,发送失败');
  383. } catch (Exception $e) {
  384. return $this->error(intval($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. }