Config.php 18 KB

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