Config.php 19 KB

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