Ken 2 dagen geleden
bovenliggende
commit
a71d544be2
1 gewijzigde bestanden met toevoegingen van 63 en 42 verwijderingen
  1. 63 42
      app/Services/KeyboardService.php

+ 63 - 42
app/Services/KeyboardService.php

@@ -48,7 +48,7 @@ class KeyboardService extends BaseService
     public static function getWhere(array $search = []): array
     {
         $where = [];
-       
+
         if (isset($search['id']) && !empty($search['id'])) {
             $where[] = ['id', '=', $search['id']];
         }
@@ -61,7 +61,7 @@ class KeyboardService extends BaseService
         if (isset($search['language']) && !empty($search['language'])) {
             $where[] = ['language', '=', $search['language']];
         }
-       
+
         return $where;
     }
 
@@ -82,9 +82,9 @@ class KeyboardService extends BaseService
      */
     public static function findAll(array $search = [])
     {
-        
+
         return self::model()::where(self::getWhere($search))->get();
-        
+
     }
 
     /**
@@ -98,10 +98,10 @@ class KeyboardService extends BaseService
         $paginator = self::model()::where(self::getWhere($search))
             // ->orderBy("sort", 'asc')
             ->paginate($limit);
-        foreach($paginator->items() as $item){
-            if(isset($item->buttons) && !empty($item->buttons)){
-                $item->buttons = json_decode($item->buttons,true);
-            }else{
+        foreach ($paginator->items() as $item) {
+            if (isset($item->buttons) && !empty($item->buttons)) {
+                $item->buttons = json_decode($item->buttons, true);
+            } else {
                 $item->buttons = [];
             }
         }
@@ -109,30 +109,30 @@ class KeyboardService extends BaseService
     }
 
     /**
-     * @description: 
+     * @description:
      * @param {*} $params
      * @return {*}
-     */    
+     */
     public static function submit($params = [])
     {
         $result = false;
         $msg['code'] = self::NOT;
         $msg['msg'] = '';
-        if(isset($params['buttons']) && !empty($params['buttons'])){
-            if(is_array($params['buttons'])){
-                $params['buttons'] = json_encode($params['buttons'],JSON_UNESCAPED_UNICODE);
+        if (isset($params['buttons']) && !empty($params['buttons'])) {
+            if (is_array($params['buttons'])) {
+                $params['buttons'] = json_encode($params['buttons'], JSON_UNESCAPED_UNICODE);
             }
-        }else{
-            $params['buttons'] = json_encode([],JSON_UNESCAPED_UNICODE);
+        } else {
+            $params['buttons'] = json_encode([], JSON_UNESCAPED_UNICODE);
         }
-        
+
         // 2. 判断是否是更新
         if (!empty($params['id'])) {
             // 更新
-            $info = self::findOne(['id'=>$params['id']] );
+            $info = self::findOne(['id' => $params['id']]);
             if (!$info) {
                 $msg['msg'] = '数据不存在!';
-            }else{
+            } else {
                 $result = $info->update($params);
                 $id = $params['id'];
             }
@@ -142,43 +142,43 @@ class KeyboardService extends BaseService
             $result = $info = self::model()::create($params);
             $id = $result->id;
         }
-        
-       
-        if($result){
-           $msg['code'] = self::YES;
-           $msg['msg'] = '设置成功';
-        }else{
-            $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
+
+
+        if ($result) {
+            $msg['code'] = self::YES;
+            $msg['msg'] = '设置成功';
+        } else {
+            $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
         }
 
         return $msg;
     }
 
     // 校验开始菜单 事件
-    public static function checkStart($chatId,$keyword = '')
+    public static function checkStart($chatId, $keyword = '')
     {
 
         // Log::error('开始使用菜单事件:',['chatId'=>$chatId,'keyword'=>$keyword]);
         // 查找开始使用的菜单
         $list = self::findAll(['button' => '开始使用'])->toArray();
-        
-        foreach($list as $item){
+
+        foreach ($list as $item) {
             $buttons = [];
-            if(isset($item['buttons']) && !empty($item['buttons'])){
-                $buttons = json_decode($item['buttons'],true);
+            if (isset($item['buttons']) && !empty($item['buttons'])) {
+                $buttons = json_decode($item['buttons'], true);
             }
-            
+
             // var_dump($buttons);
-            foreach($buttons as $row){
-                foreach($row as $k => $v){
-                    if(isset($v['text']) && $v['text'] == $keyword){
-                        
-                        if(isset($v['url']) && !empty($v['url'])){
-                            return self::menuEvent($chatId,$v['url']);
+            foreach ($buttons as $row) {
+                foreach ($row as $k => $v) {
+                    if (isset($v['text']) && $v['text'] == $keyword) {
+
+                        if (isset($v['url']) && !empty($v['url'])) {
+                            return self::menuEvent($chatId, $v['url']);
                         }
                     }
                 }
-                
+
             }
         }
 
@@ -186,14 +186,14 @@ class KeyboardService extends BaseService
     }
 
     // 开始菜单触发事件
-    public static function menuEvent($chatId,$event = '')
+    public static function menuEvent($chatId, $event = '')
     {
-        switch($event){
+        switch ($event) {
             case 'recentBets': // 近期注单
                 // 删除个人缓存
                 Util::delCache($chatId);
                 return BetService::record($chatId);
-                
+
                 break;
             case 'flowList': // 流水列表
                 // 删除个人缓存
@@ -232,7 +232,12 @@ class KeyboardService extends BaseService
         }
     }
 
-    public static function getKeyWordReply($chatId,$text): ?array
+    private static function isValidURL($url)
+    {
+        return filter_var($url, FILTER_VALIDATE_URL) !== false;
+    }
+
+    public static function getKeyWordReply($chatId, $text): ?array
     {
         // 关键字回复
         $keyboardText = KeyboardService::findOne(['button' => $text]);
@@ -240,6 +245,22 @@ class KeyboardService extends BaseService
             $keyboard = [];
             if ($keyboardText['buttons']) {
                 $keyboard = json_decode($keyboardText['buttons'], true);
+                $newKeyboard = [];
+                foreach ($keyboard as $item) {
+                    $t = [];
+                    foreach ($item as $item1) {
+                        if(static::isValidURL($item1['url'])){
+                            $t[] = $item1;
+                        }else{
+                            $t[] = [
+                                'text' => $item1['text'],
+                                'callback_data' => $item1['url']
+                            ];
+                        }
+                    }
+                    if (!empty($t)) $newKeyboard[] = $t;
+                }
+                $keyboard = $newKeyboard;
             }
             Util::delCache($chatId);
             $res = [