Config.php 14 KB

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