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