// +---------------------------------------------------------------------- namespace app\admin\controller\kefu; use app\admin\controller\AuthController; use app\admin\model\user\UserBill; use service\JsonService as Json; use app\admin\model\finance\FinanceModel; use app\admin\model\merchant\MerchantFlowingWater; use app\admin\model\merchant\MerchantBill; use app\admin\model\merchant\Merchant; use service\SystemConfigService; use service\FormBuilder as Form; use service\HookService; use think\Url; use app\admin\model\user\User; use app\admin\model\user\UserExtract; use app\admin\model\kefu\KefuModel; use think\Request; use service\JwtService; use app\admin\model\kefu\KefuSpeechCraft; use app\admin\model\kefu\KefuSpeechCraftCate; class Speechcraft extends AuthController { private function get_all_cate($owner_id = 0, $type = 1, $id = '') { return (new KefuSpeechCraftCate) ->where([ 'owner_id' => $owner_id, 'type' => $type ]) ->where('mer_id',0) ->where(function ($q) use ($id) { if ($id && $id != 0) { $q->where('id', $id); } }) ->select() ->toArray(); } public function index($id = '', $owner_id = 0, $type = 1) { $category = $this->get_all_cate($owner_id, $type, $id); array_unshift($category, [ 'id' => 0, 'name' => '全部' ]); $this->assign("cate", $category); return $this->fetch(); } private function create_query($cate_id) { return (new KefuSpeechCraft) ->where(function ($q) use ($cate_id) { if ($cate_id && $cate_id != 0) { $q->where('cate_id', $cate_id); } }) ->where('kefu_id', 0); } public function list($page = 1, $limit = 20, $cate_id = 0) { $count = $this->create_query($cate_id)->count(); $data = $this->create_query($cate_id) ->with('category') ->page($page, $limit) ->where('mer_id',0) ->select() ->each(function ($t) { $t->cate_name = $t->category ? $t->category->name : "系统默认"; unset($t->category); }); return Json::successlayui(compact("count", "data")); } public function add_speech_cate($id = '') { $speech_cate = [ 'name' => '', 'sort' => 0 ]; if ($id) { $old_speech_cate = KefuSpeechCraftCate::get($id); if ($old_speech_cate) { $speech_cate['name'] = $old_speech_cate->name; $speech_cate['sort'] = $old_speech_cate->sort; } } $this->assign("id", $id); $this->assign("speech_cate", $speech_cate); return $this->fetch(); } public function save_speech_cate($name, $sort = 0, $id) { $name = trim($name); if (!$name || mb_strlen($name) > 10) return Json::fail("请输入合适的分类名称!"); $updateData = [ 'name' => $name, 'sort' => $sort, 'type' => 1, 'add_time' => time() ]; if ($id) { $speech_cate = KefuSpeechCraftCate::get($id); if (!$speech_cate) return Json::fail("分类不存在!"); $speech_cate->update($updateData, ['id' => $id]); } else { KefuSpeechCraftCate::create($updateData); } return Json::successful('保存成功!'); } public function add_speech($id = '') { $category = $this->get_all_cate(); $this->assign('cate', $category); $this->assign('id', $id); $speech = [ 'cate_id' => $category[0]['id'], 'title' => '', 'message' => '', 'sort' => 0 ]; if ($id) { $old_speech = KefuSpeechCraft::get($id); if ($old_speech) { $speech['cate_id'] = $old_speech->cate_id; $speech['title'] = $old_speech->title; $speech['message'] = $old_speech->message; $speech['sort'] = $old_speech->sort; } } $this->assign("speech", $speech); return $this->fetch(); } public function save_speech($title, $message, $cate_id, $sort = 0, $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, 'sort' => $sort, ], ['id' => $id]); } else { KefuSpeechCraft::create([ 'cate_id' => $cate_id, 'title' => $title, 'message' => $message, 'sort' => $sort, 'add_time' => time() ]); } return Json::successful('保存成功!'); } public function del_speech($id) { $speech = KefuSpeechCraft::get($id); if (!$speech) return Json::fail("话术不存在!"); $speech->delete(); return Json::successful("删除成功!"); } public function del_speech_cate($id) { $speech_cate = KefuSpeechCraftCate::get($id); 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("删除成功!"); } }