KeyboardService.php 6.0 KB

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