183 lines
6.2 KiB
PHP
183 lines
6.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 文本控制器
|
|
*/
|
|
|
|
namespace app\admin\controller;
|
|
|
|
class Words extends Base
|
|
{
|
|
// 常用语列表
|
|
public function index()
|
|
{
|
|
if(request()->isAjax()){
|
|
|
|
$param = input('param.');
|
|
|
|
$limit = $param['pageSize'];
|
|
$offset = ($param['pageNumber'] - 1) * $limit;
|
|
|
|
$where = [];
|
|
if (!empty($param['searchText'])) {
|
|
$where['content'] = $param['searchText'];
|
|
}
|
|
|
|
$result = db('words')->where($where)->where($this->where)->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>';
|
|
}
|
|
// 所属分组
|
|
$organize = db('organize')->field('name')->where('id', $vo['organize_id'])->find();
|
|
$result[$key]['organize_name'] = !empty($organize) ? $organize['name'] : '暂无';
|
|
|
|
// 生成操作按钮
|
|
$result[$key]['operate'] = $this->makeBtn($vo['id']);
|
|
$platform = db('groups')->where('id', $vo['group_id'])->field('name')->find();
|
|
$result[$key]['platform_name'] = isset($platform['name']) ? $platform['name'] : '暂无';
|
|
}
|
|
|
|
$return['total'] = db('words')->where($where)->where($this->where)->count(); //总数据
|
|
$return['rows'] = $result;
|
|
|
|
return json($return);
|
|
|
|
}
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
// 添加常用语
|
|
public function addWord()
|
|
{
|
|
if(request()->isPost()){
|
|
|
|
$param = input('post.');
|
|
$param['content'] = htmlentities(trim($param['content']));
|
|
|
|
// 检测分组
|
|
if ($this->role_name == '组长') {
|
|
$param['organize_id'] = $this->where['organize_id'];
|
|
$organize = db('organize')->where('id', $this->where['organize_id'])->find();
|
|
$param['group_id'] = $organize['group_id'];
|
|
} else {
|
|
$param['group_id'] = $this->where['group_id'];
|
|
}
|
|
if (!isset($param['organize_id'])) return resultJson(0, '请选择分组');
|
|
|
|
$has = db('words')->field('id')
|
|
->where('content', '=', $param['content'])
|
|
->where('group_id', '=', $param['group_id'])
|
|
->find();
|
|
if(!empty($has)){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
|
|
}
|
|
|
|
$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' => '添加常用语成功']);
|
|
}
|
|
|
|
// 如果是平台管理员获取分组
|
|
if ($this->role_name == '平台管理员') {
|
|
$organize = db('organize')->where($this->where)->select();
|
|
} else {
|
|
$organize = [];
|
|
}
|
|
$this->assign([
|
|
'status' => config('kf_status'),
|
|
'organize' => $organize,
|
|
]);
|
|
|
|
return $this->fetch('addword');
|
|
}
|
|
|
|
// 编辑常用语
|
|
public function editWord()
|
|
{
|
|
if(request()->isAjax()){
|
|
|
|
$param = input('post.');
|
|
$param['content'] = htmlentities(trim($param['content']));
|
|
|
|
// 检测用户修改的常用语是否重复
|
|
$has = db('words')->where('content', $param['content'])->where('id', '<>', $param['id'])->find();
|
|
if(!empty($has)){
|
|
return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
|
|
}
|
|
|
|
// 检测分组
|
|
if ($this->role_name == '组长') {
|
|
$param['organize_id'] = $this->where['organize_id'];
|
|
$organize = db('organize')->where('id', $this->where['organize_id'])->find();
|
|
$param['group_id'] = $organize['group_id'];
|
|
}
|
|
if (!isset($param['organize_id'])) return resultJson(0, '请选择分组');
|
|
|
|
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();
|
|
// 如果是平台管理员获取分组
|
|
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'),
|
|
'organize' => $organize,
|
|
]);
|
|
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 = '';
|
|
if (!$this->is_root) {
|
|
$operate = '<a href="' . url('words/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;
|
|
}
|
|
} |