<?php
|
|
namespace app\admin\controller;
|
|
use think\facade\Cache;
|
use think\facade\Db;
|
use think\facade\View;
|
use think\facade\Request;
|
|
class MsgCategory extends Common {
|
|
private $viewPath = 'msgCategory';
|
/**
|
* 消息分类列表
|
*/
|
public function index() {
|
////////
|
if (!Request::isPost()) {
|
$other['title'] = '消息类别';
|
$guide['one']['text'] = '消息中心';
|
$guide['two']['text'] = '消息类别';
|
View::assign('guide', $guide);
|
View::assign('other', $other);
|
View::assign('viewPath', $this->viewPath);
|
return View::fetch('index');
|
} else {
|
$order = 'order_id asc,id asc';
|
$list = Db::name('msg_category')->field("id,title,ifshow,father_id,order_id")->order($order)->select()->toArray();
|
$list = _generateListTree($list, 0, ['id', 'father_id']);;
|
$result['list'] = $list;
|
return $this->successResponse($result);
|
}
|
}
|
|
/**
|
* 更新消息分类显示状态
|
*/
|
public function updateShowCate() {
|
header('Content-Type:text/html;charset=utf-8');
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
if (!is_numeric($id)) {
|
return $this->errorResponse('参数错误');
|
}
|
$ifshow = input('ifshow');
|
$info = Db::name('msg_category')->field('id')->where('id', $id)->find();
|
if (empty($info)) {
|
return $this->errorResponse('信息不存在');
|
}
|
if ($ifshow == '1') {
|
$data['ifshow'] = 1;
|
} else {
|
$data['ifshow'] = 0;
|
}
|
$backval = Db::name('msg_category')->where('id', $id)->save($data);
|
if ($backval === FALSE) {
|
return $this->errorResponse('更新失败');
|
} else {
|
$this->delCache();
|
return $this->successResponse('更新成功');
|
}
|
}
|
|
/**
|
* 保存消息分类
|
* @return array|int[]
|
*/
|
public function saveCate() {
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
$data = Request::post();
|
$id = $data['id'];
|
if (is_numeric($id)) {
|
//更新数据
|
$validate = new \app\admin\validate\MsgCategory();
|
$result = $validate->check($data);
|
if (!$result) {
|
return $this->errorResponse($validate->getError());
|
}
|
//////保存////////////
|
Db::name('msg_category')->where('id', $id)->withoutField('id')->save($data);
|
////////////////////////
|
$this->delCache();
|
$res['message'] = '消息分类编辑成功';
|
return $this->successResponse($res);
|
} else {
|
unset($data['id']);
|
$validate = new \app\admin\validate\MsgCategory();
|
$result = $validate->check($data);
|
if (!$result) {
|
return $this->errorResponse($validate->getError());
|
}
|
//////////////////////
|
Db::startTrans();
|
try {
|
$data['add_time'] = time();
|
$data['ifshow'] = 1;//默认显示
|
$new_id = Db::name('msg_category')->insertGetId($data);
|
if ($new_id) {
|
Db::name('msg_category')->where('id', $new_id)->save(['order_id' => $new_id]);
|
///////执行成功,提交事务///////
|
Db::commit();
|
$this->delCache();
|
$res['message'] = '消息分类添加成功';
|
return $this->successResponse($res);
|
} else {
|
//任一执行失败,执行回滚操作
|
Db::rollback();
|
return $this->errorResponse('消息分类添加失败');
|
}
|
} catch (\Exception $e) {
|
// 回滚事务
|
Db::rollback();
|
return $this->errorResponse('消息分类添加失败');
|
}
|
}
|
}
|
|
/**
|
* 清除缓存
|
* @return void
|
*/
|
private function delCache() {
|
Cache::tag('msg_category')->clear();
|
}
|
|
}
|