Config.php 16 KB

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