|
<?php
namespace app\controller;
use think\App;
use think\facade\View;
use app\model\Department as DepartmentModel;
class Department extends Base
{
protected $model;
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new DepartmentModel();
}
public function index()
{
$list = $this->model->select();
return View::fetch('',['list'=>$list]);
}
public function info($id)
{
$data = $this->model->find($id);
if ($this->request->isAjax()) {
return json(['code'=>2, 'data'=>$data]);
}
}
public function save()
{
if ($this->request->isPost()) {
$param = $this->request->param();
try {
if ($param['id']) {
$department = $this->model->find(intval($param['id']));
} else {
$department = new DepartmentModel();
$department->create_time = time();
}
$department->name = $param['name'];
$department->director = $param['director'];
$department->save();
} catch (\Exception $e) {
return json(['code'=>0, 'msg'=>$e->getMessage()]);
}
return json(['code'=>2]);
}
}
public function delete($id)
{
if ($this->request->isPost()) {
if ($this->model->destroy($id)) {
return json(['code'=>2]);
} else {
return json(['code'=>0]);
}
}
}
}
|