lip hace 3 meses
padre
commit
eaeddcad0c

+ 5 - 5
app/admin/controller/Config.php

@@ -33,7 +33,7 @@ class Config extends BaseController
      */
     function update()
     {
-        DB::startTrans();
+        Db::startTrans();
         try {
             $id = $this->request->post('id');
             if ($id) {
@@ -42,20 +42,20 @@ class Config extends BaseController
                 $params = (new ConfigValidate())->post()->goCheck('add');
             }
             $params['language_code'] = $params['language_code'] ?? 'zh';
-            $params['val'] = json_encode($params['val']);
-            
+
             if (!empty($id)) {
                 $exist = ConfigModel::where('id', $id)->find();
                 if (!$exist) {
                     return $this->error('配置不存在');
                 }
+                $params['val'] = json_encode($params['val']);
                 ConfigModel::where('id', $id)->update($params);
             } else {
                 ConfigModel::create($params);
             }
-            DB::commit();
+            Db::commit();
         } catch (Exception $e) {
-            DB::rollBack();
+            Db::rollBack();
             return $this->error($e->getMessage());
         }
         return $this->success();

+ 11 - 8
app/admin/controller/IpConfig.php

@@ -6,7 +6,7 @@ use app\BaseController;
 use app\admin\model\IpConfig as IpConfigModel;
 use app\admin\validate\IpConfigValidate;
 use Exception;
-use think\facade\DB;
+use think\facade\Db;
 
 class IpConfig extends BaseController
 {
@@ -17,14 +17,14 @@ class IpConfig extends BaseController
     function delete()
     {
 
-        DB::startTrans();
+        Db::startTrans();
         try {
-            $params = (new IpConfigValidate())->post()->goCheck('id');
+            $params = (new IpConfigValidate())->goCheck('id');
             $count = IpConfigModel::where('id', $params['id'])->delete();
             if ($count < 1) throw new Exception('操作失败');
-            DB::commit();
+            Db::commit();
         } catch (Exception $e) {
-            DB::rollBack();
+            Db::rollBack();
             return $this->error($e->getMessage());
         }
         return $this->success();
@@ -36,7 +36,7 @@ class IpConfig extends BaseController
      */
     function update()
     {
-        DB::startTrans();
+        Db::startTrans();
         try {
 
             $params = (new IpConfigValidate())->post()->goCheck('edit');
@@ -55,9 +55,9 @@ class IpConfig extends BaseController
                 }
                 IpConfigModel::where('id', $id)->update($params);
             }
-            DB::commit();
+            Db::commit();
         } catch (Exception $e) {
-            DB::rollBack();
+            Db::rollBack();
             return $this->error($e->getMessage());
         }
         return $this->success();
@@ -79,6 +79,9 @@ class IpConfig extends BaseController
             if (!empty($params['status'])) {
                 $query->where('ip_config.status', $params['status']);
             }
+            if (!empty($params['type'])) {
+                $query->where('ip_config.type', $params['type']);
+            }
             $count = $query->count();
             $list = $query
                 ->field(['ip_config.*', 'admin.nickname as operator_name'])

+ 32 - 0
app/admin/model/KeywordLanguages.php

@@ -16,4 +16,36 @@ class KeywordLanguages  extends BaseModel
     {
         return $this->belongsTo(Keyword::class, 'keyword_id');
     }
+
+
+    //根据聊天内容匹配关键词
+    public static function getKeywordByContent($content){
+        //缓存关键词列表
+        $keywordList = cache('keywordList');
+        if (!$keywordList) {
+            $keywordList = self::where('status', 1)->field(['id','language_code', 'name'])->order('weight','desc')->select();
+            cache('keywordList', $keywordList);
+        }
+        $list = [];
+        $all_match = [];//完全匹配的关键词
+        $match = []; //模糊匹配的关键词
+        foreach ($keywordList as $keyword) {
+            //判断content是否包含关键词
+            if (strpos($content, $keyword->name) !== false) {
+                if ($keyword->name == $content) {
+                    $all_match[] = $keyword->id;
+                } else {
+                    $match[] = $keyword->id;
+                }
+                $list[] = $keyword->id;
+            }
+        }
+        if ($all_match) {
+            return self::whereIn('id', $all_match)->increment('all_match_num', 1);
+        }
+        if ($match) {
+            return self::whereIn('id', $match)->increment('match_num', 1);
+        }
+        return $list;
+    }
 }

+ 17 - 0
app/admin/model/QuestionLanguages.php

@@ -10,4 +10,21 @@ class QuestionLanguages extends BaseModel
     protected $createTime = 'created_at';
     protected $updateTime = 'updated_at';
     
+    public static function getQuestion($keyword_ids, $content)
+    {
+        //缓存问题列表
+        $questionList = cache('questionList');
+        if (!$questionList) {
+            $questionList = self::where('status', 1)->field(['id','category_id','question','answer','keyword_id','language_code', 'name'])->order('weight','desc')->select();
+            cache('questionList', $questionList);
+        }
+        $list = [];
+        foreach ($questionList as $item) {
+            //判断content是否包含关键词
+            if (in_array($item['keyword_id'], $keyword_ids) || strpos($item['question'], $content) !== false) {
+                $list[] = $item;
+            }
+        }
+        return $list;
+    }
 }

+ 33 - 2
app/enterprise/model/Message.php

@@ -5,9 +5,13 @@
  */
 namespace app\enterprise\model;
 
+use app\admin\model\KeywordLanguages;
+use app\admin\model\QuestionLanguages;
+use app\manage\model\Config;
 use app\BaseModel;
 use think\facade\Db;
 use think\facade\Cache;
+
 class Message extends BaseModel
 {
     protected $pk="msg_id";
@@ -39,6 +43,7 @@ class Message extends BaseModel
     public function sendMessage($param,$globalConfig=false){
         $is_group = $param['is_group'] ?? 0;
         $uid=self::$uid ? : ($param['user_id'] ?? 1);
+        $is_robot = false; //是否给机器人发送消息
         if($param['toContactId']==-1){
             $is_group=0;
         }
@@ -103,7 +108,9 @@ class Message extends BaseModel
                 }
 
                 //判断是否给机器人客服发送消息
-                if ($param['toContactId'] == 0) { 
+                $autoTask = Config::autoTask();
+                if (!empty($autoTask['user_id']) && $param['toContactId'] == $autoTask['user_id']) { 
+                    $is_robot = true;
                 }
             }else{
                 // 群聊必须群成员才能发送消息
@@ -134,7 +141,31 @@ class Message extends BaseModel
         if ($sendInterval) {
             Cache::set('send_' . $uid, time(), $sendInterval);
         }
-        return self::sendMsg($param,$is_group);
+        $data = self::sendMsg($param,$is_group);
+
+        // 机器人自动回复问题推送给用户
+        if ($is_robot && $param['type'] == 'text') {
+            //获取关键词匹配
+            $keyword_ids = KeywordLanguages::getKeywordByContent($param['content']);
+            $question = QuestionLanguages::getQuestion($keyword_ids, $param['content']);
+            if ($question) {
+                $param['type'] = 'list';
+                $param['content'] = $question;
+            } else {
+                $param['type'] = 'text';
+                $param['content'] = Config::where('field','reply_keyword_math_fail')->value('val');
+            }
+
+            $param['toContactId'] = $uid;
+            $param['fromUser'] = [
+                'id' => $data['contactInfo']['id'],
+                'displayName' => $data['contactInfo']['displayName'],
+                'avatar' => $data['contactInfo']['avatar'],
+                'account' => '',
+            ];
+            self::sendMsg($param,$is_group);
+        }
+        return $data;
     }
 
     //实际发送消息