Config.php 16 KB

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