Config.php 16 KB

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