KeyboardService.php 6.2 KB

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