447 lines
17 KiB
PHP
447 lines
17 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 客服控制器
|
|
*/
|
|
|
|
namespace app\admin\controller;
|
|
use Repository\LogRepository;
|
|
|
|
class Users extends Base
|
|
{
|
|
// 客服列表
|
|
public function index()
|
|
{
|
|
if(request()->isAjax()){
|
|
$param = input('param.');
|
|
$limit = $param['pageSize'];
|
|
$offset = ($param['pageNumber'] - 1) * $limit;
|
|
|
|
$result = db('users')->where($this->where)->limit($offset, $limit)->order('id', 'desc')->select();
|
|
foreach($result as $key=>$vo){
|
|
// 优化显示头像
|
|
$result[$key]['user_avatar'] = '<img src="' . $vo['user_avatar'] . '" width="40px" height="40px">';
|
|
|
|
// 优化显示状态
|
|
if(1 == $vo['status']){
|
|
$result[$key]['status'] = '<button class="label label-primary" onclick="chageStatus(' . $vo['id'] . ', 2)">启用</button>';
|
|
} else {
|
|
$result[$key]['status'] = '<button class="label label-danger" onclick="chageStatus(' . $vo['id'] . ', 1)">禁用</button>';
|
|
}
|
|
|
|
// 查询分组
|
|
$result[$key]['group'] = '-';
|
|
$groups = db('groups')->field('name')->where('id', $vo['group_id'])->find();
|
|
if(!empty($groups)){
|
|
$result[$key]['group'] = $groups['name'];
|
|
}
|
|
$result[$key]['kf_type_name'] = $vo['kf_type'] == 1 ? '充值客服' : '聊天客服';
|
|
|
|
// 所属分组
|
|
$organize = db('organize')->field('name')->where('id', $vo['organize_id'])->find();
|
|
$result[$key]['organize_name'] = !empty($organize) ? $organize['name'] : '暂无';
|
|
|
|
//生成支付账号详情按钮
|
|
$result[$key]['show_payment'] = $this->makeShowBtn($vo['id']);
|
|
|
|
// 生成操作按钮
|
|
$result[$key]['operate'] = $this->makeBtn($vo['id']);
|
|
$result[$key]['user_log'] = $this->makeUserLogBtn($vo['id']);
|
|
}
|
|
|
|
$return['total'] = db('users')->where($this->where)->count(); //总数据
|
|
$return['rows'] = $result;
|
|
|
|
return json($return);
|
|
|
|
}
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
// 添加客服
|
|
public function addUser()
|
|
{
|
|
$gdb = db('groups');
|
|
$odb = db('organize');
|
|
if(request()->isPost()){
|
|
|
|
$param = input('post.');
|
|
|
|
$param['user_name'] = htmlentities($param['user_name']);
|
|
unset($param['file']); // 删除layui头像上传隐藏字段
|
|
// 检测头像
|
|
if(empty($param['user_avatar'])){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '请上传头像']);
|
|
}
|
|
|
|
// 检测客服的限制金额是否为正整数
|
|
if(!preg_match("/^[0-9][0-9]*$/",$param['remaining_amount']) && !empty($param['remaining_amount'])){
|
|
return json(['code' => -6, 'data' => '', 'msg' => '可充值总额度只能为正整数或为0']);
|
|
}
|
|
|
|
if(empty($param['group_id']) && $this->is_root){
|
|
return json(['code' => -4, 'data' => '', 'msg' => '请选择平台']);
|
|
} elseif(empty($param['group_id']) && $this->role_name == '平台管理员') {
|
|
$param['group_id'] = $this->where['group_id'];
|
|
}
|
|
|
|
// 检测分组
|
|
if ($this->role_name == '组长') {
|
|
$param['organize_id'] = $this->where['organize_id'];
|
|
$organize = $odb->where('id', $this->where['organize_id'])->find();
|
|
$param['group_id'] = $organize['group_id'];
|
|
}
|
|
if (!isset($param['organize_id'])) return resultJson(0, '请选择分组');
|
|
|
|
// 减去小组的金额
|
|
$group = $odb->where('id', $param['organize_id'])->find();
|
|
if ($param['remaining_amount'] > $group['money']) return json(['code' => -6, 'data' => '', 'msg' => '分配金额超出分组充值余额']);
|
|
|
|
$has = db('users')->field('id')->where('user_name', $param['user_name'])->find();
|
|
if(!empty($has)){
|
|
return json(['code' => -2, 'data' => '', 'msg' => '该客服已经存在']);
|
|
}
|
|
$rule = "/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/";
|
|
|
|
if (!preg_match($rule, $param['user_pwd'])) {
|
|
return json(['code' => -2, 'data' => '', 'msg' => '密码必须是8-16位的字母和数字组成!']);
|
|
}
|
|
|
|
$param['user_pwd'] = password_hash($param['user_pwd'], PASSWORD_DEFAULT);
|
|
$param['online'] = 2; // 离线状态
|
|
$param['access_token'] = md5(config('salt').$param['user_name'].$param['user_pwd'].time());
|
|
$param['expire_time'] = time()+7*24*60*60;
|
|
$param['rate_num'] = random_int(20000,90000);
|
|
$param['rank_num'] = 10;
|
|
|
|
try{
|
|
$save = db('users')->insertGetId($param);
|
|
$odb->where('id', $param['organize_id'])->setDec('money', $param['remaining_amount']);
|
|
$this->saveMoneyFlowLog($group['admin_id'], 'k_' . $save, $param['remaining_amount'], "'{$param['user_name']}'客服分配金额");
|
|
}catch(\Exception $e){
|
|
return json(['code' => -3, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
LogRepository::write('系统管理', '添加客服成功');
|
|
return json(['code' => 1, 'data' => '', 'msg' => '添加客服成功']);
|
|
}
|
|
|
|
// 客服类型
|
|
$kf_type = [
|
|
[
|
|
'name' => '充值客服',
|
|
'value' => 1,
|
|
],
|
|
[
|
|
'name' => '聊天客服',
|
|
'value' => 2,
|
|
],
|
|
];
|
|
|
|
// 如果是平台管理员获取分组
|
|
if ($this->role_name == '平台管理员') {
|
|
$organize = $odb->where($this->where)->select();
|
|
} else {
|
|
$organize = [];
|
|
}
|
|
|
|
$this->assign([
|
|
'groups' => db('groups')->select(),
|
|
'organize' => $organize,
|
|
'status' => config('kf_status'),
|
|
'kf_type' => $kf_type,
|
|
'is_root' => $this->is_root,
|
|
]);
|
|
|
|
return $this->fetch('adduser');
|
|
}
|
|
|
|
// 编辑客服
|
|
public function editUser()
|
|
{
|
|
$gdb = db('groups');
|
|
$odb = db('organize');
|
|
$udb = db('users');
|
|
if(request()->isAjax()){
|
|
$param = input('post.');
|
|
|
|
$param['user_name'] = htmlentities($param['user_name']);
|
|
unset($param['file']); // 删除layui头像上传隐藏字段
|
|
|
|
if(empty($param['group_id']) && $this->is_root){
|
|
return json(['code' => -4, 'data' => '', 'msg' => '请选择平台']);
|
|
} elseif(empty($param['group_id']) && $this->role_name == '平台管理员') {
|
|
$param['group_id'] = $this->where['group_id'];
|
|
}
|
|
// 检测分组
|
|
if ($this->role_name == '组长') {
|
|
$param['organize_id'] = $this->where['organize_id'];
|
|
$organize = $odb->where('id', $this->where['organize_id'])->find();
|
|
$param['group_id'] = $organize['group_id'];
|
|
}
|
|
if (!isset($param['organize_id'])) return resultJson(0, '请选择分组');
|
|
|
|
// 检测客服的限制金额是否为正整数
|
|
if(!preg_match("/^[0-9][0-9]*$/",$param['remaining_amount'])){
|
|
return json(['code' => -3, 'data' => '', 'msg' => '可充值总额度只能为正整数']);
|
|
}
|
|
|
|
$user = db('users')->where('id', $param['id'])->find();
|
|
|
|
// 减去平台的金额
|
|
$group = $odb->where('id', $param['organize_id'])->find();
|
|
$money = $param['remaining_amount'] > $user['remaining_amount'] ? intval($param['remaining_amount'] - $user['remaining_amount']) : 0;
|
|
if ($money > $group['money']) return json(['code' => -6, 'data' => '', 'msg' => '分配金额超出组的充值余额']);
|
|
|
|
// 检测用户修改的用户名是否重复
|
|
$has = db('users')->where('user_name', $param['user_name'])->where('id', '<>', $param['id'])->find();
|
|
if(!empty($has)){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '该客服已经存在']);
|
|
}
|
|
|
|
// 修改用户头像
|
|
if(empty($param['user_avatar'])){
|
|
unset($param['user_avatar']);
|
|
}
|
|
|
|
// 修改用户密码
|
|
if(empty($param['user_pwd'])){
|
|
unset($param['user_pwd']);
|
|
}else{
|
|
// $param['user_pwd'] = md5($param['user_pwd'] . config('salt'));
|
|
$param['user_pwd'] = password_hash($param['user_pwd'], PASSWORD_DEFAULT);
|
|
}
|
|
|
|
$save = db('users')->where('id', $param['id'])->update($param);
|
|
if (!$save) return resultJson(0, '保存失败');
|
|
if ($money > 0) {
|
|
// $odb->where('id', $param['organize_id'])->setDec('money', $money);
|
|
// 资金流动记录
|
|
$compare = $param['remaining_amount'] > $user['remaining_amount'];
|
|
$money = $compare ? $param['remaining_amount'] - $user['remaining_amount'] : abs($param['remaining_amount'] - $user['remaining_amount']);
|
|
if ($compare) {
|
|
$this->saveMoneyFlowLog($group['admin_id'], 'k_' . $param['id'], $money, "'{$param['user_name']}'客服分配金额");
|
|
}
|
|
}
|
|
LogRepository::write('系统管理', '编辑客服成功');
|
|
return json(['code' => 1, 'data' => '', 'msg' => '编辑客服成功']);
|
|
}
|
|
|
|
$id = input('param.id/d');
|
|
$info = db('users')->where('id', $id)->find();
|
|
|
|
// 客服类型
|
|
$kf_type = [
|
|
[
|
|
'name' => '充值客服',
|
|
'value' => 1,
|
|
],
|
|
[
|
|
'name' => '聊天客服',
|
|
'value' => 2,
|
|
],
|
|
];
|
|
// 如果是平台管理员获取分组
|
|
if ($this->role_name == '平台管理员' || $info['group_id']) {
|
|
$where = $info['group_id'] ? ['group_id' => $info['group_id']] : $this->where;
|
|
$organize = db('organize')->where($where)->select();
|
|
} else {
|
|
$organize = [];
|
|
}
|
|
|
|
$this->assign([
|
|
'info' => $info,
|
|
'status' => config('kf_status'),
|
|
'groups' => db('groups')->select(),
|
|
'kf_type' => $kf_type,
|
|
'is_root' => $this->is_root,
|
|
'organize' => $organize,
|
|
]);
|
|
return $this->fetch('edituser');
|
|
}
|
|
|
|
// 删除客服
|
|
public function delUser()
|
|
{
|
|
$gdb = db('groups');
|
|
$udb = db('users');
|
|
if(request()->isAjax()){
|
|
$id = input('param.id/d');
|
|
$user = $udb->where('id', $id)->field('remaining_amount, group_id')->find();
|
|
try{
|
|
$del = $udb->where('id', $id)->delete();
|
|
$gdb->where('id', $user['group_id'])->setInc('money', $user['remaining_amount']);
|
|
}catch(\Exception $e){
|
|
return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
LogRepository::write('系统管理', '删除客服成功');
|
|
return json(['code' => 1, 'data' => '', 'msg' => '删除客服成功']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 启用或禁用客服
|
|
*/
|
|
public function chageStatus()
|
|
{
|
|
$gdb = db('groups');
|
|
$udb = db('users');
|
|
if(request()->isAjax()){
|
|
$id = input('param.id');
|
|
$type = input('param.type');
|
|
try{
|
|
$del = $udb->where('id', $id)->update(['status' => $type]);
|
|
}catch(\Exception $e){
|
|
return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
$hint = $type == 1 ? '启用' : '禁用';
|
|
LogRepository::write('系统管理', $hint . '客服成功');
|
|
return json(['code' => 1, 'data' => '', 'msg' => $hint . '客服成功']);
|
|
}
|
|
}
|
|
|
|
// 上传客服头像
|
|
public function upAvatar()
|
|
{
|
|
if(request()->isAjax()) {
|
|
|
|
$file = request()->file('file');
|
|
if (!empty($file)) {
|
|
$fileInfo = $file->getInfo();
|
|
/*if($fileInfo['size'] > 1024 * 1024 * 2){
|
|
// 上传失败获取错误信息
|
|
return json( ['code' => -2, 'data' => '', 'msg' => '文件超过2M'] );
|
|
}*/
|
|
|
|
//检测图片格式
|
|
$ext = explode('.', $fileInfo['name']);
|
|
$ext = array_pop($ext);
|
|
|
|
$extArr = explode('|', 'jpg|png|gif|jpeg');
|
|
if(!in_array($ext, $extArr)){
|
|
return json(['code' => -3, 'data' => '', 'msg' => '只能上传jpg|png|gif|jpeg的文件']);
|
|
}
|
|
// 移动到框架应用根目录/public/uploads/ 目录下
|
|
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'avatar');
|
|
if ($info) {
|
|
$src = config('img_take_prefix').'/uploads/avatar' . '/' . date('Ymd') . '/' . $info->getFilename();
|
|
return json(['code' => 0, 'data' => ['src' => $src], 'msg' => 'ok']);
|
|
} else {
|
|
// 上传失败获取错误信息
|
|
return json(['code' => -1, 'data' => '', 'msg' => $file->getError()]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//获取客服所拥有的支付账号
|
|
public function getPayment(){
|
|
$id = input('param.id/d');
|
|
if(request()->isAjax()) {
|
|
$res = db('payment')->alias('a')
|
|
->field('a.id,a.account_num,b.type_name')
|
|
->join('ws_payment_type b','a.payment_type = b.id')
|
|
->where('kf_id',$id)
|
|
->where('is_use',1)
|
|
->select();
|
|
if($res){
|
|
$return['rows'] = $res;
|
|
return json($return);
|
|
}else{
|
|
return json(['code' => 0, 'data' => '', 'msg' => '获取支付账号失败']);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 生成操作按钮
|
|
private function makeBtn($id)
|
|
{
|
|
$operate = '<a href="' . url('users/edituser', ['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> ';
|
|
|
|
//$operate .= '<a href="javascript:;">';
|
|
//$operate .= '<button type="button" class="btn btn-info btn-sm"><i class="fa fa-institution"></i> 详情</button></a>';
|
|
|
|
return $operate;
|
|
}
|
|
|
|
// 生成支付账号详情按钮
|
|
private function makeShowBtn($id)
|
|
{
|
|
$operate = '<a href="javascript:getPayment(' . $id . ')">';
|
|
$operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 详情</button></a> ';
|
|
|
|
return $operate;
|
|
}
|
|
|
|
// 用户操作记录
|
|
private function makeUserLogBtn($id)
|
|
{
|
|
$operate = '<a href="javascript:getUserLog(' . $id . ')">';
|
|
$operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 客服操作记录</button></a> ';
|
|
|
|
return $operate;
|
|
}
|
|
|
|
/**
|
|
* 用户操作记录列表
|
|
* @return mixed|\think\response\Json
|
|
* @throws \think\Exception
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function user_log_list(){
|
|
if(request()->isAjax()){
|
|
$param = input('param.');
|
|
$limit = $param['pageSize'];
|
|
$offset = ($param['pageNumber'] - 1) * $limit;
|
|
|
|
$result = db('user_action_log')->where(['kf_id' => $param['id']])->limit($offset, $limit)->order('id', 'desc')->select();
|
|
$ip2region = new \Ip2Region();
|
|
|
|
foreach($result as $key=>$vo){
|
|
$info = $ip2region->btreeSearch($vo['ip']);
|
|
|
|
$city = explode('|', $info['region']);
|
|
if(0 != $info['city_id']){
|
|
$result[$key]['ip'] = $city['2'] . $city['3'] .'---'. $city['4'];
|
|
}else{
|
|
$result[$key]['ip'] = $vo['ip'] . $city['3'] .'---'. $city['4'];
|
|
}
|
|
|
|
$result[$key]['operate'] = "";
|
|
// $result[$key]['operate'] = $this->makeBtn($vo['id']);
|
|
}
|
|
$return['total'] = db('user_action_log')->where(['kf_id' => $param['id']])->count(); //总数据
|
|
$return['rows'] = $result;
|
|
|
|
return json($return);
|
|
|
|
}
|
|
$this->assign([
|
|
'id' => input('id'),
|
|
]);
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* 删除用户操作记录
|
|
* @return \think\response\Json
|
|
*/
|
|
public function user_log_del(){
|
|
if(request()->isAjax()){
|
|
$id = input('param.id/d');
|
|
|
|
try{
|
|
db('user_action_log')->where('id', $id)->delete();
|
|
}catch(\Exception $e){
|
|
return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
|
|
}
|
|
return json(['code' => 1, 'data' => '', 'msg' => '删除成功']);
|
|
}
|
|
}
|
|
} |