452 lines
15 KiB
PHP
452 lines
15 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 系统控制器
|
|
*/
|
|
|
|
namespace app\admin\controller;
|
|
|
|
class System extends Base
|
|
{
|
|
// 自动回复设置
|
|
public function reply()
|
|
{
|
|
if(request()->isPost()){
|
|
|
|
$param = input('post.');
|
|
$param['word'] = htmlentities($param['word']);
|
|
if(empty($param['word'])){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '回复内容不能为空']);
|
|
}
|
|
|
|
try{
|
|
db('reply')->where('id', 1)->update($param);
|
|
}catch(\Exception $e){
|
|
return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
|
|
return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
|
|
}
|
|
|
|
$info = db('reply')->where('id', 1)->find();
|
|
$this->assign([
|
|
'info' => $info,
|
|
'status' => config('kf_status')
|
|
]);
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
// 客服设置
|
|
public function customerService()
|
|
{
|
|
if(request()->isPost()){
|
|
|
|
$param = input('post.');
|
|
db('kf_config')->where('id', 1)->update($param);
|
|
|
|
return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
|
|
}
|
|
|
|
$this->assign([
|
|
'config' => db('kf_config')->where('id', 1)->find(),
|
|
'status' => config('kf_status')
|
|
]);
|
|
|
|
return $this->fetch('customerservice');
|
|
}
|
|
|
|
|
|
// 历史会话记录改进
|
|
public function wordsLogs()
|
|
{
|
|
if(request()->isAjax()){
|
|
$param = input('param.');
|
|
|
|
$limit = $param['pageSize'];
|
|
$offset = ($param['pageNumber'] - 1) * $limit;
|
|
|
|
// 默认显示最近7天
|
|
$start = input('param.start');
|
|
$end = input('param.end');
|
|
|
|
$udb = db('users');
|
|
// 平台的客服
|
|
$users = $udb->where($this->where)->field('id')->select();
|
|
$users_ids = [];
|
|
foreach($users as $vo) {
|
|
$users_ids[] = "KF{$vo['id']}";
|
|
}
|
|
$temp = db('chat_log');
|
|
$countTmp = db('chat_log');
|
|
if(empty($param['kf_id']) && empty($param['user_name'])){
|
|
//取出所有有聊天记录的的客服
|
|
$result = $temp->where('from_id','LIKE','KF%')
|
|
// ->where($this->where)
|
|
->field('from_name,from_id,id,to_id')
|
|
->group('from_id')
|
|
// ->limit($offset, $limit)
|
|
->select();
|
|
$count = $countTmp->where('from_id','LIKE','KF%')
|
|
->field('from_name,from_id,id')
|
|
->group('from_id');
|
|
$filter = $this->filterUsers($users_ids, $result);
|
|
$result = $filter['result'];
|
|
// $count = $filter['count'];
|
|
}
|
|
|
|
//取出对应客服的有会话的用户
|
|
if(!empty($param['kf_id']) && empty($param['user_name'])){
|
|
$result = $temp->where('from_id',$param['kf_id'])
|
|
->field('to_name,to_id,id,from_id')
|
|
->group('to_id')
|
|
->limit($offset, $limit)
|
|
->select();
|
|
$count = $countTmp->where('from_id',$param['kf_id'])
|
|
->field('to_name,to_id,id')
|
|
->group('to_id');
|
|
$filter = $this->filterUsers($users_ids, $result);
|
|
$result = $filter['result'];
|
|
// $count = $filter['count'];
|
|
}
|
|
|
|
//取出对应客服和对应用户的历史会话记录
|
|
if(!empty($param['user_name']) && !empty($param['kf_id'])){
|
|
$result = $temp->where('from_id|to_id',$param['kf_id'])
|
|
->where('from_name|to_name',$param['user_name'])
|
|
->limit($offset, $limit)
|
|
->order('id', 'desc')
|
|
->select();
|
|
$count = $countTmp->where('from_id|to_id',$param['kf_id'])
|
|
->where('from_name|to_name',$param['user_name'])->count();
|
|
$filter = $this->filterUsers($users_ids, $result);
|
|
$result = $filter['result'];
|
|
// $count = $filter['count'];
|
|
//转换时间格式
|
|
foreach($result as $key=>$vo){
|
|
$result[$key]['time_line'] = date('Y-m-d H:i:s', $vo['time_line']);
|
|
}
|
|
}
|
|
|
|
$return['total'] = $count; //总数据条数
|
|
|
|
|
|
//搜索对应历史会话记录
|
|
if(!empty($param['searchText']) || !empty($start) && !empty($end) && $start <= $end){
|
|
//根据对应客服检索对应会话记录
|
|
if(!empty($param['searchText']) && empty($start) && empty($end)){
|
|
$temp = $temp->where('from_name|to_id', $param['searchText'])->whereOr('to_name', $param['searchText']);
|
|
}
|
|
|
|
//根据时间进行会话记录检索
|
|
if(!empty($start) && !empty($end) && $start <= $end){
|
|
$temp = $temp->whereBetween('time_line', [strtotime($start), strtotime($end . ' 23:59:59')]);
|
|
}
|
|
|
|
//根据对应时间以及对应客服检索会话记录
|
|
if(!empty($start) && !empty($end) && $start <= $end && !empty($param['searchText'])){
|
|
$temp = $temp->where('from_name|to_name|', $param['searchText'])->whereBetween('time_line', [strtotime($start), strtotime($end . ' 23:59:59')]);
|
|
}
|
|
|
|
$result = $temp
|
|
->limit($offset, $limit)
|
|
->order('id', 'desc')
|
|
->select();
|
|
$filter = $this->filterUsers($users_ids, $result);
|
|
$result = $filter['result'];
|
|
|
|
foreach($result as $key=>$vo){
|
|
$result[$key]['time_line'] = date('Y-m-d H:i:s', $vo['time_line']);
|
|
}
|
|
|
|
// $return['total'] = $filter['count']; //总数据条数
|
|
$return['total'] = $udb->where($this->where)->count(); //总数据条数
|
|
}
|
|
|
|
$return['rows'] = $result;
|
|
|
|
return json($return);
|
|
|
|
}
|
|
|
|
return $this->fetch('wordslogs');
|
|
}
|
|
|
|
/**
|
|
* 过滤聊天记录中客服
|
|
* @Parm $user_ids 平台客服
|
|
* @Parm $result 结果集合
|
|
*/
|
|
public function filterUsers($users_ids, $result)
|
|
{
|
|
$new_result = [];
|
|
foreach ($result as $vo) {
|
|
if (!in_array($vo['from_id'], $users_ids) && !in_array($vo['to_id'], $users_ids)) continue;
|
|
$new_result[] = $vo;
|
|
}
|
|
$data['result'] = $new_result;
|
|
$data['count'] = count($result);
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 菜单列表
|
|
*/
|
|
public function menuList()
|
|
{
|
|
$mdb = db('admin_menus');
|
|
// 返回视图
|
|
if(!request()->isAjax()) return $this->fetch('system/menuList');
|
|
|
|
// 分页请求
|
|
$param = input('get.');
|
|
// 查询条件
|
|
|
|
//获取总条数
|
|
$count = $mdb->count();
|
|
// $count=count($list);
|
|
//获取每页显示的条数
|
|
$limit = $param['limit'];
|
|
//获取当前页数
|
|
$page = $param['page'];
|
|
//计算出从那条开始查询
|
|
$start = ($page-1)*$limit;
|
|
$list = $mdb
|
|
->limit($start, $limit)
|
|
->order('path asc')
|
|
->select();
|
|
// 对列表进行处理
|
|
foreach ($list as &$vo) {
|
|
// 在线状态
|
|
$vo['status_name'] = $vo['status'] > 0 ? '正常' : '停用';
|
|
}
|
|
$result = resultJson(0, '获取成功', $list);
|
|
$result['count'] = $count;
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 添加菜单
|
|
*/
|
|
public function addMenu()
|
|
{
|
|
// return;
|
|
$mdb = db('admin_menus');
|
|
// 返回视图
|
|
if(request()->isAjax()) {
|
|
$data['name'] = htmlentities(input('post.name'));
|
|
$data['pid'] = input('post.pid');
|
|
$data['url'] = input('post.url');
|
|
$data['sort'] = input('post.sort');
|
|
$data['icon'] = input('post.icon');
|
|
$data['status'] = 1;
|
|
$data['date'] = date("Y-m-d H:i:s");
|
|
if (!$data['name']) return resultJson(0, '参数错误');
|
|
$save = $mdb->insertGetId($data);
|
|
if (!$save) return resultJson(0, '添加失败');
|
|
$path = $data['pid'] > 0 ? $data['pid'].'_|_'.$save : $save;
|
|
$update = $mdb->where('id', $save)->update(['path' => $path]);
|
|
return resultJson(1, '添加成功');
|
|
}
|
|
$menu_one = $mdb->where('pid', 0)->order('sort')->select();
|
|
$this->assign([
|
|
'menu_one' => $menu_one,
|
|
]);
|
|
return $this->fetch('system/addMenu');
|
|
}
|
|
|
|
/**
|
|
* 编辑菜单
|
|
*/
|
|
public function editMenu()
|
|
{
|
|
$mdb = db('admin_menus');
|
|
// 返回视图
|
|
if(request()->isAjax()) {
|
|
$id = input('post.id');
|
|
$data['name'] = htmlentities(input('post.name'));
|
|
$data['pid'] = input('post.pid');
|
|
$data['url'] = input('post.url');
|
|
$data['sort'] = input('post.sort');
|
|
$data['icon'] = input('post.icon');
|
|
$data['status'] = 1;
|
|
|
|
// $data = array_filter($data);
|
|
if (!$id) return resultJson(0, '参数错误');
|
|
$data['path'] = isset($data['pid']) && ($data['pid'] > 0) ? $data['pid'].'_|_'.$id : $id;
|
|
$save = $mdb->where('id', $id)->update($data);
|
|
if (!$save) return resultJson(0, '保存失败');
|
|
return resultJson(1, '成功');
|
|
}
|
|
$id = input('id');
|
|
$menu_one = $mdb->where('pid', 0)->order('sort')->select();
|
|
$info = $mdb->find($id);
|
|
$this->assign([
|
|
'menu_one' => $menu_one,
|
|
'info' => $info,
|
|
'id' => $id,
|
|
]);
|
|
return $this->fetch('system/editMenu');
|
|
}
|
|
|
|
/**
|
|
* 删除菜单
|
|
*/
|
|
public function delMenu()
|
|
{
|
|
$mdb = db('admin_menus');
|
|
// 返回视图
|
|
if(request()->isAjax()) {
|
|
$id = input('post.id');
|
|
if (!$id) return resultJson(0, '参数错误');
|
|
$save = $mdb->where('id', $id)->delete();
|
|
if (!$save) return resultJson(0, '删失败');
|
|
return resultJson(1, '成功');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 权限列表
|
|
*/
|
|
public function roleList()
|
|
{
|
|
$mdb = db('role');
|
|
// 返回视图
|
|
if(!request()->isAjax()) return $this->fetch('system/roleList');
|
|
|
|
// 分页请求
|
|
$param = input('get.');
|
|
// 查询条件
|
|
|
|
//获取总条数
|
|
$count = $mdb->count();
|
|
// $count=count($list);
|
|
//获取每页显示的条数
|
|
$limit = $param['limit'];
|
|
//获取当前页数
|
|
$page = $param['page'];
|
|
//计算出从那条开始查询
|
|
$start = ($page-1)*$limit;
|
|
$list = $mdb
|
|
->limit($start, $limit)
|
|
->order('date desc')
|
|
->select();
|
|
// 对列表进行处理
|
|
foreach ($list as &$vo) {
|
|
// 在线状态
|
|
$vo['status_name'] = $vo['status'] > 0 ? '正常' : '停用';
|
|
}
|
|
$result = resultJson(0, '获取成功', $list);
|
|
$result['count'] = $count;
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 添加菜单
|
|
*/
|
|
public function addRole()
|
|
{
|
|
$pdb = db('role');
|
|
$mdb = db('admin_menus');
|
|
if(request()->isAjax()) {
|
|
$data['name'] = htmlentities(input('post.name'));
|
|
$menu_one = isset($_POST['menu_one']) ? $_POST['menu_one'] : [];
|
|
$menu_two = isset($_POST['menu_two']) ? $_POST['menu_two'] : [];
|
|
if (!is_array($menu_one) || !is_array($menu_two)) return resultJson(0, '参数错误');
|
|
$data['menu_id_one'] = implode(',', $menu_one);
|
|
$data['menu_id_two'] = implode(',', $menu_two);
|
|
$data['status'] = 1;
|
|
$data['date'] = date("Y-m-d H:i:s");
|
|
|
|
if (!$data['name'] || !$data['menu_id_one'] || !$data['menu_id_two']) return resultJson(0, '参数错误');
|
|
$save = $pdb->insertGetId($data);
|
|
if (!$save) return resultJson(0, '添加失败');
|
|
return resultJson(1, '添加成功');
|
|
}
|
|
$menu_one = $mdb->where('pid', 0)->order('sort')->select();
|
|
$menu_two = $mdb->where('pid', '>', 0)->order('sort')->select();
|
|
$this->assign([
|
|
'menu_one' => $menu_one,
|
|
'menu_two' => $menu_two,
|
|
]);
|
|
return $this->fetch('system/addRole');
|
|
}
|
|
|
|
/**
|
|
* 编辑菜单
|
|
*/
|
|
public function editRole()
|
|
{
|
|
$pdb = db('role');
|
|
$mdb = db('admin_menus');
|
|
if(request()->isAjax()) {
|
|
$id = input('post.id');
|
|
$data['name'] = htmlentities(input('post.name'));
|
|
$menu_one = isset($_POST['menu_one']) ? $_POST['menu_one'] : [];
|
|
$menu_two = isset($_POST['menu_two']) ? $_POST['menu_two'] : [];
|
|
if (!is_array($menu_one) || !is_array($menu_two)) return resultJson(0, '参数错误');
|
|
$data['menu_id_one'] = implode(',', $menu_one);
|
|
$data['menu_id_two'] = implode(',', $menu_two);
|
|
$data['status'] = 1;
|
|
$data['date'] = date("Y-m-d H:i:s");
|
|
$data = array_filter($data);
|
|
if (!$id) return resultJson(0, '参数错误');
|
|
|
|
$save = $pdb->where('id', $id)->update($data);
|
|
if (!$save) return resultJson(0, '保存失败');
|
|
return resultJson(1, '成功');
|
|
}
|
|
$id = input('id');
|
|
$menu_one = $mdb->where('pid', 0)->order('sort')->select();
|
|
$menu_two = $mdb->where('pid', '>', 0)->order('sort')->select();
|
|
$info = $pdb->find($id);
|
|
// 检测是否已经选中
|
|
$menu_one_arr = explode(',', $info['menu_id_one']);
|
|
$menu_two_arr = explode(',', $info['menu_id_two']);
|
|
foreach($menu_one as &$vo) {
|
|
if(!in_array($vo['id'], $menu_one_arr)) {
|
|
$vo['checked'] = false;
|
|
continue;
|
|
}
|
|
$vo['checked'] = true;
|
|
}
|
|
foreach($menu_two as &$vo) {
|
|
if(!in_array($vo['id'], $menu_two_arr)) {
|
|
$vo['checked'] = false;
|
|
continue;
|
|
}
|
|
$vo['checked'] = true;
|
|
}
|
|
$this->assign([
|
|
'menu_one' => $menu_one,
|
|
'menu_two' => $menu_two,
|
|
'info' => $info,
|
|
'id' => $id,
|
|
]);
|
|
return $this->fetch('system/editRole');
|
|
}
|
|
|
|
/**
|
|
* 删除菜单
|
|
*/
|
|
public function delRole()
|
|
{
|
|
$mdb = db('role');
|
|
// 返回视图
|
|
if(request()->isAjax()) {
|
|
$id = input('post.id');
|
|
if (!$id) return resultJson(0, '参数错误');
|
|
$save = $mdb->where('id', $id)->delete();
|
|
if (!$save) return resultJson(0, '删失败');
|
|
return resultJson(1, '成功');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 图标
|
|
*/
|
|
public function icon()
|
|
{
|
|
header("location:http://www.fontawesome.com.cn/faicons/");
|
|
}
|
|
} |