KeyboardService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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['button']) && !empty($search['button'])) {
  50. $where[] = ['button', '=', $search['button']];
  51. }
  52. if (isset($search['explain']) && !empty($search['explain'])) {
  53. $where[] = ['explain', '=', $search['explain']];
  54. }
  55. if (isset($search['language']) && !empty($search['language'])) {
  56. $where[] = ['language', '=', $search['language']];
  57. }
  58. return $where;
  59. }
  60. /**
  61. * @description: 查询单条数据
  62. * @param array $search
  63. * @return \App\Models\Coin|null
  64. */
  65. public static function findOne(array $search): ?Keyboard
  66. {
  67. return self::model()::where(self::getWhere($search))->first();
  68. }
  69. /**
  70. * @description: 查询所有数据
  71. * @param array $search
  72. * @return \Illuminate\Database\Eloquent\Collection
  73. */
  74. public static function findAll(array $search = [])
  75. {
  76. return self::model()::where(self::getWhere($search))->get();
  77. }
  78. /**
  79. * @description: 分页查询
  80. * @param array $search
  81. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  82. */
  83. public static function paginate(array $search = [])
  84. {
  85. $limit = isset($search['limit']) ? $search['limit'] : 15;
  86. $paginator = self::model()::where(self::getWhere($search))
  87. // ->orderBy("sort", 'asc')
  88. ->paginate($limit);
  89. foreach ($paginator->items() as $item) {
  90. if (isset($item->buttons) && !empty($item->buttons)) {
  91. $item->buttons = json_decode($item->buttons, true);
  92. } else {
  93. $item->buttons = [];
  94. }
  95. }
  96. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  97. }
  98. /**
  99. * @description:
  100. * @param {*} $params
  101. * @return {*}
  102. */
  103. public static function submit($params = [])
  104. {
  105. $result = false;
  106. $msg['code'] = self::NOT;
  107. $msg['msg'] = '';
  108. if (isset($params['buttons']) && !empty($params['buttons'])) {
  109. if (is_array($params['buttons'])) {
  110. $params['buttons'] = json_encode($params['buttons'], JSON_UNESCAPED_UNICODE);
  111. }
  112. } else {
  113. $params['buttons'] = json_encode([], JSON_UNESCAPED_UNICODE);
  114. }
  115. // 2. 判断是否是更新
  116. if (!empty($params['id'])) {
  117. $info = self::findOne(['id' => $params['id']]);
  118. if (!$info) {
  119. $msg['msg'] = '数据不存在!';
  120. } else {
  121. if ($info->group_id != 1) {
  122. unset($params['button']);
  123. }
  124. $result = $info->update($params);
  125. $id = $params['id'];
  126. }
  127. } else {
  128. // 创建
  129. $params['group_id'] = 1;
  130. $result = $info = static::$MODEL::create($params);
  131. $id = $result->id;
  132. }
  133. if ($result) {
  134. $msg['code'] = self::YES;
  135. $msg['msg'] = '设置成功';
  136. } else {
  137. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  138. }
  139. return $msg;
  140. }
  141. // 校验开始菜单 事件
  142. public static function checkStart($chatId, $keyword = '')
  143. {
  144. // Log::error('开始使用菜单事件:',['chatId'=>$chatId,'keyword'=>$keyword]);
  145. // 查找开始使用的菜单
  146. $list = self::findAll(['button' => '开始使用'])->toArray();
  147. foreach ($list as $item) {
  148. $buttons = [];
  149. if (isset($item['buttons']) && !empty($item['buttons'])) {
  150. $buttons = json_decode($item['buttons'], true);
  151. }
  152. // var_dump($buttons);
  153. foreach ($buttons as $row) {
  154. foreach ($row as $k => $v) {
  155. if (isset($v['text']) && $v['text'] == $keyword) {
  156. if (isset($v['url']) && !empty($v['url'])) {
  157. return self::menuEvent($chatId, $v['url']);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. return false;
  164. }
  165. // 开始菜单触发事件
  166. public static function menuEvent($chatId, $event = '')
  167. {
  168. switch ($event) {
  169. case 'recentBets': // 近期注单
  170. // 删除个人缓存
  171. Util::delCache($chatId);
  172. return BetService::record($chatId);
  173. break;
  174. case 'flowList': // 流水列表
  175. // 删除个人缓存
  176. Util::delCache($chatId);
  177. return BalanceLogService::getFlowingHistory($chatId);
  178. break;
  179. case 'winningHistory': // 开奖历史
  180. // 删除个人缓存
  181. Util::delCache($chatId);
  182. return IssueService::currentLotteryResults($chatId);
  183. break;
  184. case 'currentBetting': // 本期下注
  185. // 删除个人缓存
  186. Util::delCache($chatId);
  187. return BetService::currentBet($chatId);
  188. case 'checkBalance': // 查看余额
  189. // 删除个人缓存
  190. Util::delCache($chatId);
  191. return WalletService::getBalance($chatId);
  192. case 'selectLanguage': // 选择语言
  193. // 删除个人缓存
  194. Util::delCache($chatId);
  195. return UserService::getLanguages($chatId);
  196. break;
  197. case 'topUp': // 充值上分
  198. // 删除个人缓存
  199. Util::delCache($chatId);
  200. return SanJinRechargeService::qbApply($chatId);
  201. break;
  202. default:
  203. return false;
  204. }
  205. }
  206. private static function isValidURL($url)
  207. {
  208. return filter_var($url, FILTER_VALIDATE_URL) !== false;
  209. }
  210. public static function getKeyWordReply($chatId, $text): ?array
  211. {
  212. // 关键字回复
  213. $keyboardText = KeyboardService::findOne(['button' => $text]);
  214. if ($keyboardText) {
  215. $keyboard = [];
  216. if ($keyboardText['buttons']) {
  217. $keyboard = json_decode($keyboardText['buttons'], true);
  218. $newKeyboard = [];
  219. foreach ($keyboard as $item) {
  220. $t = [];
  221. foreach ($item as $item1) {
  222. if (static::isValidURL($item1['url'])) {
  223. $t[] = $item1;
  224. }
  225. }
  226. if (!empty($t)) $newKeyboard[] = $t;
  227. }
  228. $keyboard = $newKeyboard;
  229. }
  230. Util::delCache($chatId);
  231. $res = [
  232. 'chat_id' => $chatId,
  233. 'text' => $keyboardText['reply'],
  234. // 'reply_to_message_id' => $messageId
  235. ];
  236. if ($keyboard) {
  237. $res['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  238. }
  239. if ($keyboardText['image']) {
  240. $res['photo'] = InputFile::create(url($keyboardText['image']));
  241. unset($res['text']);
  242. $res['caption'] = $text;
  243. $res['protect_content'] = true; // 防止转发
  244. }
  245. return $res;
  246. }
  247. return null;
  248. }
  249. }