Config.php 18 KB

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