website/application/admin/controller/jdgw/Nav.php
2025-05-09 01:14:04 +08:00

176 lines
6.0 KiB
PHP

<?php
namespace app\admin\controller\jdgw;
use app\common\controller\Backend;
use fast\Tree;
use think\Db;
/**
*
*
* @icon fa fa-circle-o
*/
class Nav extends Backend
{
/**
* Nav模型对象
* @var \app\admin\model\jdgw\Nav
*/
protected $model = null;
protected $modelValidate = false;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\jdgw\Nav;
$this->view->assign("typedataList", $this->model->getTypedataList());
$this->view->assign("statusList", $this->model->getStatusList());
$nav = $this->model->where(['status'=>'normal'])->order('weigh desc')->select();
$tree = Tree::instance()->init($nav, 'pid');
$nav = $tree->getTreeList($tree->getTreeArray(0), 'title');
array_unshift($nav, ['id' => 0, 'pid'=>0, 'title' => '无', 'typedata'=>'article']);
$this->view->assign('nav', $nav);
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
$f = $this->model->where('diyname', $params['diyname'])->find();
if($f) $this->error('自定义名称已存在!');
$result = $this->model->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException(true)->validate($validate);
}
$f = $this->model->where('diyname', $params['diyname'])->where('id','<>',$ids)->find();
if($f) $this->error('自定义名称已存在!');
$result = $row->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$this->view->assign("row", $row);
return $this->view->fetch();
}
public function index()
{
if ($this->request->isAjax()) {
$openIds = $this->request->param('openIds/a');
$openIds = $openIds? $openIds : [];
$nav = $this->model->order('weigh desc')->select();
foreach ($nav as $k=>$v){
if(in_array($v['id'],$openIds)){
$nav[$k]['open'] = true;
}else{
$nav[$k]['open'] = false;
}
$nav[$k]['addon_url'] = addon_url('jdgw/index/list', [':diyname'=>$v['diyname']]);
}
$nav = Tree::instance()->init($nav, 'pid')->getTreeArray(0);
$this->success('ok', null, $nav);
}
return $this->view->fetch();
}
public function editWeigh(){
$id = $this->request->param('id');
$weigh = $this->request->param('weigh');
$up = $this->model->where('id', $id)->update(['weigh'=>$weigh]);
if($up){
$this->success('ok');
}else{
$this->success('err');
}
}
}