Config.php 16 KB

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