KeyboardService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // 2. 判断是否是更新
  119. if (!empty($params['id'])) {
  120. $info = self::findOne(['id' => $params['id']]);
  121. if (!$info) {
  122. $msg['msg'] = '数据不存在!';
  123. } else {
  124. if ($info->group_id != 1) {
  125. unset($params['button']);
  126. }
  127. $result = $info->update($params);
  128. $id = $params['id'];
  129. }
  130. } else {
  131. // 创建
  132. $params['group_id'] = 1;
  133. $result = $info = static::$MODEL::create($params);
  134. $id = $result->id;
  135. }
  136. if ($result) {
  137. $msg['code'] = self::YES;
  138. $msg['msg'] = '设置成功';
  139. } else {
  140. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  141. }
  142. return $msg;
  143. }
  144. // 校验开始菜单 事件
  145. public static function checkStart($chatId, $keyword = '')
  146. {
  147. // Log::error('开始使用菜单事件:',['chatId'=>$chatId,'keyword'=>$keyword]);
  148. // 查找开始使用的菜单
  149. $list = self::findAll(['button' => '开始使用'])->toArray();
  150. foreach ($list as $item) {
  151. $buttons = [];
  152. if (isset($item['buttons']) && !empty($item['buttons'])) {
  153. $buttons = json_decode($item['buttons'], true);
  154. }
  155. // var_dump($buttons);
  156. foreach ($buttons as $row) {
  157. foreach ($row as $k => $v) {
  158. if (isset($v['text']) && $v['text'] == $keyword) {
  159. if (isset($v['url']) && !empty($v['url'])) {
  160. return self::menuEvent($chatId, $v['url']);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. return false;
  167. }
  168. // 开始菜单触发事件
  169. public static function menuEvent($chatId, $event = '')
  170. {
  171. switch ($event) {
  172. case 'recentBets': // 近期注单
  173. // 删除个人缓存
  174. Util::delCache($chatId);
  175. return BetService::record($chatId);
  176. break;
  177. case 'flowList': // 流水列表
  178. // 删除个人缓存
  179. Util::delCache($chatId);
  180. return BalanceLogService::getFlowingHistory($chatId);
  181. break;
  182. case 'winningHistory': // 开奖历史
  183. // 删除个人缓存
  184. Util::delCache($chatId);
  185. return IssueService::currentLotteryResults($chatId);
  186. break;
  187. case 'currentBetting': // 本期下注
  188. // 删除个人缓存
  189. Util::delCache($chatId);
  190. return BetService::currentBet($chatId);
  191. case 'checkBalance': // 查看余额
  192. // 删除个人缓存
  193. Util::delCache($chatId);
  194. return WalletService::getBalance($chatId);
  195. case 'selectLanguage': // 选择语言
  196. // 删除个人缓存
  197. Util::delCache($chatId);
  198. return UserService::getLanguages($chatId);
  199. break;
  200. case 'topUp': // 充值上分
  201. // 删除个人缓存
  202. Util::delCache($chatId);
  203. return SanJinRechargeService::qbApply($chatId);
  204. break;
  205. default:
  206. return false;
  207. }
  208. }
  209. private static function isValidURL($url)
  210. {
  211. return filter_var($url, FILTER_VALIDATE_URL) !== false;
  212. }
  213. public static function getKeyWordReply($chatId, $text): ?array
  214. {
  215. // 关键字回复
  216. $keyboardText = KeyboardService::findOne(['button' => $text]);
  217. if ($keyboardText) {
  218. $keyboard = [];
  219. if ($keyboardText['buttons']) {
  220. $keyboard = json_decode($keyboardText['buttons'], true);
  221. $newKeyboard = [];
  222. foreach ($keyboard as $item) {
  223. $t = [];
  224. foreach ($item as $item1) {
  225. if (static::isValidURL($item1['url'])) {
  226. $t[] = $item1;
  227. }
  228. }
  229. if (!empty($t)) $newKeyboard[] = $t;
  230. }
  231. $keyboard = $newKeyboard;
  232. }
  233. Util::delCache($chatId);
  234. $res = [
  235. 'chat_id' => $chatId,
  236. 'text' => $keyboardText['reply'],
  237. // 'reply_to_message_id' => $messageId
  238. ];
  239. if ($keyboard) {
  240. $res['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  241. }
  242. if ($keyboardText['image']) {
  243. $res['photo'] = InputFile::create(url($keyboardText['image']));
  244. unset($res['text']);
  245. $res['caption'] = $text;
  246. $res['protect_content'] = true; // 防止转发
  247. }
  248. return $res;
  249. }
  250. return null;
  251. }
  252. }