KeyboardService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /**
  14. * 菜单
  15. */
  16. class KeyboardService extends BaseService
  17. {
  18. /**
  19. * @description: 模型
  20. * @return {string}
  21. */
  22. public static function model(): string
  23. {
  24. return Keyboard::class;
  25. }
  26. /**
  27. * @description: 枚举
  28. * @return {*}
  29. */
  30. public static function enum(): string
  31. {
  32. return '';
  33. }
  34. /**
  35. * @description: 获取查询条件
  36. * @param {array} $search 查询内容
  37. * @return {array}
  38. */
  39. public static function getWhere(array $search = []): array
  40. {
  41. $where = [];
  42. if (isset($search['id']) && !empty($search['id'])) {
  43. $where[] = ['id', '=', $search['id']];
  44. }
  45. if (isset($search['button']) && !empty($search['button'])) {
  46. $where[] = ['button', '=', $search['button']];
  47. }
  48. return $where;
  49. }
  50. /**
  51. * @description: 查询单条数据
  52. * @param array $search
  53. * @return \App\Models\Coin|null
  54. */
  55. public static function findOne(array $search): ?Keyboard
  56. {
  57. return self::model()::where(self::getWhere($search))->first();
  58. }
  59. /**
  60. * @description: 查询所有数据
  61. * @param array $search
  62. * @return \Illuminate\Database\Eloquent\Collection
  63. */
  64. public static function findAll(array $search = [])
  65. {
  66. return self::model()::where(self::getWhere($search))->get();
  67. }
  68. /**
  69. * @description: 分页查询
  70. * @param array $search
  71. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  72. */
  73. public static function paginate(array $search = [])
  74. {
  75. $limit = isset($search['limit']) ? $search['limit'] : 15;
  76. $paginator = self::model()::where(self::getWhere($search))
  77. // ->orderBy("sort", 'asc')
  78. ->paginate($limit);
  79. foreach($paginator->items() as $item){
  80. if(isset($item->buttons) && !empty($item->buttons)){
  81. $item->buttons = json_decode($item->buttons,true);
  82. }else{
  83. $item->buttons = [];
  84. }
  85. }
  86. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  87. }
  88. /**
  89. * @description:
  90. * @param {*} $params
  91. * @return {*}
  92. */
  93. public static function submit($params = [])
  94. {
  95. $result = false;
  96. $msg['code'] = self::NOT;
  97. $msg['msg'] = '';
  98. if(isset($params['buttons']) && !empty($params['buttons'])){
  99. if(is_array($params['buttons'])){
  100. $params['buttons'] = json_encode($params['buttons'],JSON_UNESCAPED_UNICODE);
  101. }
  102. }else{
  103. $params['buttons'] = json_encode([],JSON_UNESCAPED_UNICODE);
  104. }
  105. // 2. 判断是否是更新
  106. if (!empty($params['id'])) {
  107. // 更新
  108. $info = self::findOne(['id'=>$params['id']] );
  109. if (!$info) {
  110. $msg['msg'] = '数据不存在!';
  111. }else{
  112. $result = $info->update($params);
  113. $id = $params['id'];
  114. }
  115. } else {
  116. // 创建
  117. $result = $info = self::model()::create($params);
  118. $id = $result->id;
  119. }
  120. if($result){
  121. $msg['code'] = self::YES;
  122. $msg['msg'] = '设置成功';
  123. }else{
  124. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  125. }
  126. return $msg;
  127. }
  128. // 校验开始菜单 事件
  129. public static function checkStart($chatId,$keyword = '')
  130. {
  131. // 查找开始使用的菜单
  132. $list = self::findAll(['button' => '开始使用'])->toArray();
  133. foreach($list as $item){
  134. $buttons = [];
  135. if(isset($item['buttons']) && !empty($item['buttons'])){
  136. $buttons = json_decode($item['buttons'],true);
  137. }
  138. foreach($buttons as $row){
  139. if(isset($row['text']) && $row['text'] == $keyword){
  140. if(isset($row['url']) && !empty($row['url'])){
  141. return self::menuEvent($chatId,$row['url']);
  142. }
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148. // 开始菜单触发事件
  149. public static function menuEvent($chatId,$event = '')
  150. {
  151. switch($event){
  152. case 'recentBets': // 近期注单
  153. // 删除个人缓存
  154. Util::delCache($chatId);
  155. return BetService::record($chatId);
  156. break;
  157. case 'flowList': // 流水列表
  158. // 删除个人缓存
  159. Util::delCache($chatId);
  160. return BalanceLogService::getFlowingHistory($chatId);
  161. break;
  162. case 'winningHistory': // 开奖历史
  163. // 删除个人缓存
  164. Util::delCache($chatId);
  165. return IssueService::currentLotteryResults($chatId);
  166. break;
  167. case 'currentBetting': // 本期下注
  168. // 删除个人缓存
  169. Util::delCache($chatId);
  170. return BetService::currentBet($chatId);
  171. case 'checkBalance': // 查看余额
  172. // 删除个人缓存
  173. Util::delCache($chatId);
  174. return WalletService::getBalance($chatId);
  175. case 'selectLanguage': // 选择语言
  176. // 删除个人缓存
  177. Util::delCache($chatId);
  178. return UserService::getLanguages($chatId);
  179. break;
  180. default:
  181. return false;
  182. }
  183. }
  184. }