// +---------------------------------------------------------------------- namespace app\kefu\controller; use app\kefu\controller\AuthController; use app\kefu\model\KefuModel; use app\kefu\services\KefuChatService; use service\FormBuilder as Form; use service\JsonService as Json; use service\UploadService as Upload; use think\Request; use think\Url; use app\admin\model\article\ArticleCategory as ArticleCategoryModel; use app\admin\model\article\Article as ArticleModel; use app\admin\model\user\User; use app\kefu\model\user\UserGroupModel; use think\Session; use service\JwtService; use app\kefu\services\KefuRecordService; use app\admin\model\kefu\KefuSpeechCraftCate; use app\admin\model\kefu\KefuSpeechCraft; class Speech extends AuthController { /** * 白名单 */ public function cate($type) { $data = (new KefuSpeechCraftCate)->where([ 'type' => 1, 'owner_id' => $type == 1 ? $this->kefuId : 0 ])->select(); return Json::successful($data); } public function list($page = 1, $limit = 20, $title = '', $cate_id = 0, $type) { $data = (new KefuSpeechCraft) ->where(function ($q) use ($title, $cate_id) { if (trim($title)) { $q->where("title", 'like', '%' . trim($title) . '%'); } if ($cate_id) { $q->where('cate_id', $cate_id); } }) ->where([ 'kefu_id' => $type == 1 ? $this->kefuId : 0 ]) ->order('sort desc') ->page($page, $limit) ->select(); return Json::successful($data); } public function save_cate($id = '', $name = '', $sort = 0) { $name = trim($name); if (!$name || mb_strlen($name) > 10) return Json::fail("请输入合适的分类名称!"); $saveData = [ 'name' => $name, 'sort' => $sort, 'type' => 1, 'add_time' => time(), 'owner_id' => $this->kefuId ]; if ($id) { $speech_cate = KefuSpeechCraftCate::get($id); if (!$speech_cate) return Json::fail("分类不存在!"); $speech_cate->update($saveData, ['id' => $id]); } else { KefuSpeechCraftCate::create($saveData); } return Json::successful('保存成功!'); } public function save($title = "", $cate_id = "", $message = "", $id = '') { $title = trim($title); $message = trim($message); if (!$cate_id) return Json::fail("请选择话术分类!"); $cate = KefuSpeechCraftCate::get($cate_id); if (!$cate) return Json::fail("话术分类不存在!"); if (!$title || mb_strlen($title) > 15) return Json::fail("请输入合适的话术标题!"); if (!$message || mb_strlen($message) > 80) return Json::fail("请输入合适的话术内容!"); if ($id) { $speech = KefuSpeechCraft::get($id); if (!$speech) return Json::fail("话术不存在!"); $speech->update([ 'cate_id' => $cate_id, 'title' => $title, 'message' => $message, 'kefu_id' => $this->kefuId, ], ['id' => $id]); } else { KefuSpeechCraft::create([ 'kefu_id' => $this->kefuId, 'cate_id' => $cate_id, 'title' => $title, 'message' => $message, 'add_time' => time() ]); } return Json::successful('保存成功!'); } public function delete($id) { $speech = (new KefuSpeechCraft)->hasWhere('category', ['owner_id' => $this->kefuId])->find($id); if (!$speech) return Json::fail("话术不存在!"); $speech->delete(); return Json::successful('删除成功!'); } public function delete_cate($id) { $speech_cate = (new KefuSpeechCraftCate)->where([ 'owner_id' => $this->kefuId, 'id' => $id ])->find(); if (!$speech_cate) return Json::fail("分类不存在!"); $speech = (new KefuSpeechCraft)->where('cate_id', $id)->find(); if ($speech) { return Json::fail("分类正在使用,无法删除!"); } $speech_cate->delete(); return Json::successful('删除成功!'); } }