Config.php 16 KB

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