Config.php 16 KB

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