276 lines
9.3 KiB
PHP
276 lines
9.3 KiB
PHP
<?php
|
|
namespace app\service\controller;
|
|
/**
|
|
* 客服相关设置
|
|
*/
|
|
class Setting extends Base{
|
|
|
|
//常用语列表
|
|
public function words()
|
|
{
|
|
if(request()->isAjax()){
|
|
|
|
$param = input('param.');
|
|
|
|
$limit = $param['pageSize'];
|
|
$offset = ($param['pageNumber'] - 1) * $limit;
|
|
|
|
$kf_info = db('users')->where('id', session('l_user_id'))->find();
|
|
|
|
$sql = '(kf_id = '.(int)session('l_user_id').' or (group_id = '.(int)$kf_info['group_id'].' and kf_id is null))';
|
|
if (!empty($param['searchText'])) {
|
|
$sql .= ' and content = "'.$param['searchText'].'"';
|
|
}
|
|
$result = db('words')->where($sql)
|
|
->limit($offset, $limit)->order('add_time desc')->select();
|
|
foreach($result as $key=>$vo){
|
|
// 优化显示状态
|
|
if(1 == $vo['status']){
|
|
$result[$key]['status'] = '<span class="label label-primary">启用</span>';
|
|
}else{
|
|
$result[$key]['status'] = '<span class="label label-danger">禁用</span>';
|
|
}
|
|
|
|
if ($vo['kf_id'] == session('l_user_id')) {
|
|
// 生成操作按钮
|
|
$result[$key]['operate'] = $this->makeBtn($vo['id']);
|
|
}
|
|
}
|
|
|
|
$return['total'] = db('words')->where($sql)->count(); //总数据
|
|
$return['rows'] = $result;
|
|
|
|
return json($return);
|
|
|
|
}
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
// 添加常用语
|
|
public function addWord()
|
|
{
|
|
if(request()->isPost()){
|
|
|
|
$param = input('post.');
|
|
$param['content'] = trim($param['content']);
|
|
$param['kf_id'] = session('l_user_id');
|
|
|
|
$has = db('words')->field('id')->where('content', $param['content'])->where('kf_id',session('l_user_id'))->find();
|
|
if(!empty($has)){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '该个人常用语已经存在']);
|
|
}
|
|
$kf_info = db('users')->where('id', $param['kf_id'])->find();
|
|
$param['group_id'] = $kf_info['group_id'];
|
|
$param['organize_id'] = $kf_info['organize_id'];
|
|
|
|
$param['add_time'] = date('Y-m-d H:i:s');
|
|
try{
|
|
db('words')->insert($param);
|
|
}catch(\Exception $e){
|
|
return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
|
|
return json(['code' => 1, 'data' => '', 'msg' => '添加个人常用语成功']);
|
|
}
|
|
|
|
$this->assign([
|
|
'status' => config('kf_status')
|
|
]);
|
|
|
|
return $this->fetch('addword');
|
|
}
|
|
|
|
// 编辑常用语
|
|
public function editWord()
|
|
{
|
|
if(request()->isAjax()){
|
|
|
|
$param = input('post.');
|
|
$param['content'] = trim($param['content']);
|
|
|
|
// 检测用户修改的常用语是否重复
|
|
$has = db('words')->where('content', $param['content'])->where('id', '<>', $param['id'])->where('kf_id',session('l_user_id'))->find();
|
|
if(!empty($has)){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '该个人常用语已经存在']);
|
|
}
|
|
|
|
try{
|
|
db('words')->where('id', $param['id'])->update($param);
|
|
}catch(\Exception $e){
|
|
return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
|
|
return json(['code' => 1, 'data' => '', 'msg' => '编辑个人常用语成功']);
|
|
}
|
|
|
|
$id = input('param.id/d');
|
|
$info = db('words')->where('id', $id)->find();
|
|
|
|
$this->assign([
|
|
'info' => $info,
|
|
'status' => config('kf_status')
|
|
]);
|
|
return $this->fetch('editword');
|
|
}
|
|
|
|
// 删除常用语
|
|
public function delWord()
|
|
{
|
|
if(request()->isAjax()){
|
|
$id = input('param.id/d');
|
|
|
|
try{
|
|
db('words')->where('id', $id)->delete();
|
|
}catch(\Exception $e){
|
|
return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
|
|
return json(['code' => 1, 'data' => '', 'msg' => '删除个人常用语成功']);
|
|
}
|
|
}
|
|
|
|
// 生成按钮
|
|
private function makeBtn($id)
|
|
{
|
|
$operate = '<a href="' . url('setting/editword', ['id' => $id]) . '">';
|
|
$operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
|
|
|
|
$operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
|
|
$operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
|
|
|
|
return $operate;
|
|
}
|
|
|
|
|
|
//修改客服头像
|
|
public function changeImg()
|
|
{
|
|
if(request()->isPost()){
|
|
|
|
$param = input('post.');
|
|
|
|
if(empty($param['user_avatar'])){
|
|
return json(['code' => 0, 'data' => $param, 'msg' => '上传头像不能为空']);
|
|
}
|
|
|
|
$result = db('users')->where('id', session('l_user_id'))->setField('user_avatar', $param['user_avatar']);
|
|
|
|
if(!$result){
|
|
return json(['code' => 1, 'data' => '', 'msg' => '头像修改失败,请重新上传']);
|
|
}
|
|
|
|
return json(['code' => 2, 'data' =>'', 'msg' => '修改头像成功']);
|
|
}
|
|
}
|
|
|
|
// 修改客服密码
|
|
public function changePassword()
|
|
{
|
|
$uid = session('l_user_id');
|
|
if(request()->isPost()){
|
|
|
|
$param = input('post.');
|
|
$reLogin = false;
|
|
|
|
if(empty($param['old_pwd']) && !empty($param['password'])){
|
|
return json(['code' => -2, 'data' => '', 'msg' => '请输入旧密码']);
|
|
}
|
|
|
|
if(!empty($param['old_pwd']) && empty($param['password'])){
|
|
return json(['code' => -3, 'data' => '', 'msg' => '请输入新密码']);
|
|
}
|
|
|
|
if ($param['password'] != $param['password_c']) {
|
|
return json(['code' => -3, 'data' => '', 'msg' => '两次密码不一致']);
|
|
}
|
|
|
|
$rule = '/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/';
|
|
|
|
if (!preg_match($rule, $param['password'])) {
|
|
return json(['code' => -2, 'data' => '', 'msg' => '密码必须是6-15位的字母和数字组成!']);
|
|
}
|
|
|
|
$userInfo = db('users')->where('id', $uid)->find();
|
|
|
|
if(empty($userInfo)){
|
|
return json(['code' => -4, 'data' => '', 'msg' => '客服不存在']);
|
|
}
|
|
|
|
if(!password_verify($param['old_pwd'], $userInfo['user_pwd'])){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '旧密码错误']);
|
|
}
|
|
|
|
$info['password'] =password_hash($param['password'], PASSWORD_DEFAULT);
|
|
$reLogin = true;
|
|
|
|
$save = db('users')->where('id', session('l_user_id'))->setField('user_pwd', $info['password']);
|
|
if (!$save) return json(['code' => 0, 'data' => '', 'msg' => '保存失败']);
|
|
return json(['code' => 1, 'data' => $reLogin, 'msg' => '修改信息成功']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 问候语设置
|
|
*/
|
|
public function greetings(){
|
|
if(request()->isAjax()){
|
|
$redis = new \Redis();
|
|
$redis->connect(config('cache.host'),config('cache.port'));
|
|
$redis->auth(config('cache.password'));
|
|
|
|
$operate = '<a href="javascript:editGreetings()">';
|
|
$operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
|
|
$greetings = json_decode($redis->hget('greetings',session('l_user_id')),true);
|
|
|
|
if(1 == $greetings['status']){
|
|
$status = '<span class="label label-primary">启用</span>';
|
|
}else{
|
|
$status = '<span class="label label-danger">禁用</span>';
|
|
}
|
|
|
|
return json([
|
|
'total' => 0,
|
|
'rows' => [
|
|
[
|
|
'id' => 1,
|
|
'content'=>$greetings['content'],
|
|
'status' => $status,
|
|
'operate'=>$operate,
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
return $this->fetch();
|
|
}
|
|
|
|
public function editGreetings(){
|
|
$redis = new \Redis();
|
|
$redis->connect(config('cache.host'),config('cache.port'));
|
|
$redis->auth(config('cache.password'));
|
|
|
|
if(request()->isAjax()){
|
|
$param = input('post.');
|
|
$greetings = ['content'=>trim($param['content']),'status'=>$param['status']];
|
|
|
|
$redis->hset('greetings',session('l_user_id'),json_encode($greetings));
|
|
|
|
return json(['code' => 1, 'data' => '', 'msg' => '编辑问候语成功']);
|
|
}
|
|
$greetings = json_decode($redis->hget('greetings',session('l_user_id')),true);
|
|
$info = [
|
|
'id' => 1,
|
|
'content'=> $greetings['content'],
|
|
'status' => $greetings['status'],
|
|
];
|
|
$this->assign([
|
|
'info' => $info,
|
|
'status' => [
|
|
'1' => '启用',
|
|
'0' => '禁用'
|
|
]
|
|
]);
|
|
|
|
return $this->fetch('editgreetings');
|
|
}
|
|
} |