Config.php 16 KB

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