KeyboardService.php 6.5 KB

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