Config.php 16 KB

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