Config.php 18 KB

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