KeyboardService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Keyboard;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Services\BetService;
  9. use App\Services\BalanceLogService;
  10. use App\Services\IssueService;
  11. use App\Services\WalletService;
  12. use App\Constants\Util;
  13. use Illuminate\Support\Facades\Log;
  14. use App\Services\SanJinRechargeService;
  15. use Telegram\Bot\FileUpload\InputFile;
  16. /**
  17. * 菜单
  18. */
  19. class KeyboardService extends BaseService
  20. {
  21. public static string $MODEL = Keyboard::class;
  22. /**
  23. * @description: 模型
  24. * @return {string}
  25. */
  26. public static function model(): string
  27. {
  28. return Keyboard::class;
  29. }
  30. /**
  31. * @description: 枚举
  32. * @return {*}
  33. */
  34. public static function enum(): string
  35. {
  36. return '';
  37. }
  38. /**
  39. * @description: 获取查询条件
  40. * @param {array} $search 查询内容
  41. * @return {array}
  42. */
  43. public static function getWhere(array $search = []): array
  44. {
  45. $where = [];
  46. if (isset($search['id']) && !empty($search['id'])) {
  47. $where[] = ['id', '=', $search['id']];
  48. }
  49. if (isset($search['group_id']) && !empty($search['group_id'])) {
  50. $where[] = ['group_id', '=', $search['group_id']];
  51. }
  52. if (isset($search['button']) && !empty($search['button'])) {
  53. $where[] = ['button', '=', $search['button']];
  54. }
  55. if (isset($search['explain']) && !empty($search['explain'])) {
  56. $where[] = ['explain', '=', $search['explain']];
  57. }
  58. if (isset($search['language']) && !empty($search['language'])) {
  59. $where[] = ['language', '=', $search['language']];
  60. }
  61. return $where;
  62. }
  63. /**
  64. * @description: 查询单条数据
  65. * @param array $search
  66. * @return \App\Models\Coin|null
  67. */
  68. public static function findOne(array $search): ?Keyboard
  69. {
  70. return self::model()::where(self::getWhere($search))->first();
  71. }
  72. /**
  73. * @description: 查询所有数据
  74. * @param array $search
  75. * @return \Illuminate\Database\Eloquent\Collection
  76. */
  77. public static function findAll(array $search = [])
  78. {
  79. return self::model()::where(self::getWhere($search))->get();
  80. }
  81. /**
  82. * @description: 分页查询
  83. * @param array $search
  84. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  85. */
  86. public static function paginate(array $search = [])
  87. {
  88. $limit = isset($search['limit']) ? $search['limit'] : 15;
  89. $paginator = self::model()::where(self::getWhere($search))
  90. ->orderByDesc("id")
  91. ->paginate($limit);
  92. foreach ($paginator->items() as $item) {
  93. if (isset($item->buttons) && !empty($item->buttons)) {
  94. $item->buttons = json_decode($item->buttons, true);
  95. } else {
  96. $item->buttons = [];
  97. }
  98. }
  99. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  100. }
  101. /**
  102. * @description:
  103. * @param {*} $params
  104. * @return {*}
  105. */
  106. public static function submit($params = [])
  107. {
  108. $result = false;
  109. $msg['code'] = self::NOT;
  110. $msg['msg'] = '';
  111. if (isset($params['buttons']) && !empty($params['buttons'])) {
  112. if (is_array($params['buttons'])) {
  113. $params['buttons'] = json_encode($params['buttons'], JSON_UNESCAPED_UNICODE);
  114. }
  115. } else {
  116. $params['buttons'] = json_encode([], JSON_UNESCAPED_UNICODE);
  117. }
  118. if (isset($params['image'])) {
  119. $params['image'] = Util::replacePartInUrl($params['image']);
  120. }
  121. // 2. 判断是否是更新
  122. if (!empty($params['id'])) {
  123. $info = self::findOne(['id' => $params['id']]);
  124. if (!$info) {
  125. $msg['msg'] = '数据不存在!';
  126. } else {
  127. if ($info->group_id != 1) {
  128. unset($params['button']);
  129. }
  130. $result = $info->update($params);
  131. $id = $params['id'];
  132. }
  133. } else {
  134. // 创建
  135. $params['group_id'] = 1;
  136. $result = $info = static::$MODEL::create($params);
  137. $id = $result->id;
  138. }
  139. if ($result) {
  140. $msg['code'] = self::YES;
  141. $msg['msg'] = '设置成功';
  142. } else {
  143. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  144. }
  145. return $msg;
  146. }
  147. // 校验开始菜单 事件
  148. public static function checkStart($chatId, $keyword = '')
  149. {
  150. // Log::error('开始使用菜单事件:',['chatId'=>$chatId,'keyword'=>$keyword]);
  151. // 查找开始使用的菜单
  152. $list = self::findAll(['button' => '开始使用'])->toArray();
  153. foreach ($list as $item) {
  154. $buttons = [];
  155. if (isset($item['buttons']) && !empty($item['buttons'])) {
  156. $buttons = json_decode($item['buttons'], true);
  157. }
  158. // var_dump($buttons);
  159. foreach ($buttons as $row) {
  160. foreach ($row as $k => $v) {
  161. if (isset($v['text']) && $v['text'] == $keyword) {
  162. if (isset($v['url']) && !empty($v['url'])) {
  163. return self::menuEvent($chatId, $v['url']);
  164. }
  165. }
  166. }
  167. }
  168. }
  169. return false;
  170. }
  171. // 开始菜单触发事件
  172. public static function menuEvent($chatId, $event = '')
  173. {
  174. switch ($event) {
  175. case "promotionalActivity":
  176. // 删除个人缓存
  177. Util::delCache($chatId);
  178. return ActivityRewardService::promotionalActivity($chatId);
  179. case 'recentBets': // 近期注单
  180. // 删除个人缓存
  181. Util::delCache($chatId);
  182. return BetService::record($chatId);
  183. break;
  184. case 'flowList': // 流水列表
  185. // 删除个人缓存
  186. Util::delCache($chatId);
  187. return BalanceLogService::getFlowingHistory($chatId);
  188. break;
  189. case 'winningHistory': // 开奖历史
  190. // 删除个人缓存
  191. Util::delCache($chatId);
  192. return IssueService::currentLotteryResults($chatId);
  193. break;
  194. case 'currentBetting': // 本期下注
  195. // 删除个人缓存
  196. Util::delCache($chatId);
  197. return BetService::currentBet($chatId);
  198. case 'checkBalance': // 查看余额
  199. // 删除个人缓存
  200. Util::delCache($chatId);
  201. // 查看余额 同步充值记录
  202. RechargeService::markUsdtRechargePending($chatId);
  203. RechargeService::syncUsdtRechargeRecords($chatId);
  204. return WalletService::getBalance($chatId);
  205. case 'selectLanguage': // 选择语言
  206. // 删除个人缓存
  207. Util::delCache($chatId);
  208. return UserService::getLanguages($chatId);
  209. break;
  210. case 'topUp': // 充值上分
  211. // 删除个人缓存
  212. Util::delCache($chatId);
  213. return SanJinRechargeService::qbApply($chatId);
  214. break;
  215. case "football": //F1bet娱乐更多玩法(足球首页)
  216. // 删除个人缓存
  217. Util::delCache($chatId);
  218. return ActivityRewardService::footballUrl($chatId);
  219. break;
  220. default:
  221. return false;
  222. }
  223. }
  224. private static function isValidURL($url)
  225. {
  226. return filter_var($url, FILTER_VALIDATE_URL) !== false;
  227. }
  228. public static function getKeyWordReply($chatId, $text): ?array
  229. {
  230. // 关键字回复
  231. $keyboardText = KeyboardService::findOne(['button' => $text]);
  232. if ($keyboardText) {
  233. $keyboard = [];
  234. if ($keyboardText['buttons']) {
  235. $keyboard = json_decode($keyboardText['buttons'], true);
  236. $newKeyboard = [];
  237. foreach ($keyboard as $item) {
  238. $t = [];
  239. foreach ($item as $item1) {
  240. if (static::isValidURL($item1['url'])) {
  241. $t[] = $item1;
  242. }
  243. }
  244. if (!empty($t)) $newKeyboard[] = $t;
  245. }
  246. $keyboard = $newKeyboard;
  247. }
  248. Util::delCache($chatId);
  249. $res = [
  250. 'chat_id' => $chatId,
  251. 'text' => $keyboardText['reply'],
  252. // 'reply_to_message_id' => $messageId
  253. ];
  254. if ($keyboard) {
  255. $res['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  256. }
  257. if ($keyboardText['image']) {
  258. $res['photo'] = InputFile::create(url($keyboardText['image']));
  259. unset($res['text']);
  260. $res['caption'] = $text;
  261. $res['protect_content'] = true; // 防止转发
  262. }
  263. return $res;
  264. }
  265. return null;
  266. }
  267. }