|
@@ -5,61 +5,59 @@ namespace app\admin\controller;
|
|
|
use app\common\controller\Backend;
|
|
|
use app\common\model\BodyTypeConfig as BodyTypeConfigModel;
|
|
|
use think\Db;
|
|
|
+use think\exception\PDOException;
|
|
|
+use think\exception\ValidateException;
|
|
|
|
|
|
/**
|
|
|
* 身体类型配置管理
|
|
|
+ *
|
|
|
+ * @icon fa fa-user-md
|
|
|
+ * @remark 身体类型配置管理,支持按性别分类管理身体类型
|
|
|
*/
|
|
|
class BodyTypeConfig extends Backend
|
|
|
{
|
|
|
+ /**
|
|
|
+ * BodyTypeConfig模型对象
|
|
|
+ * @var \app\common\model\BodyTypeConfig
|
|
|
+ */
|
|
|
protected $model = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无需登录的方法
|
|
|
+ */
|
|
|
protected $noNeedLogin = [];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无需鉴权的方法
|
|
|
+ */
|
|
|
protected $noNeedRight = [];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 快速搜索字段
|
|
|
+ */
|
|
|
protected $searchFields = 'type_name,description';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否开启模型验证
|
|
|
+ */
|
|
|
+ protected $modelValidate = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否开启模型场景验证
|
|
|
+ */
|
|
|
+ protected $modelSceneValidate = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Multi方法可批量修改的字段
|
|
|
+ */
|
|
|
+ protected $multiFields = 'status';
|
|
|
|
|
|
public function _initialize()
|
|
|
{
|
|
|
parent::_initialize();
|
|
|
$this->model = new BodyTypeConfigModel;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查看列表
|
|
|
- */
|
|
|
- public function index()
|
|
|
- {
|
|
|
- if ($this->request->isAjax()) {
|
|
|
- // 分页参数
|
|
|
- $page = $this->request->get('page/d', 1);
|
|
|
- $limit = $this->request->get('limit/d', 10);
|
|
|
- $search = $this->request->get('search', '');
|
|
|
- $type_category = $this->request->get('type_category', '');
|
|
|
- $gender = $this->request->get('gender', '');
|
|
|
-
|
|
|
- $where = [];
|
|
|
-
|
|
|
- // 搜索条件
|
|
|
- if ($search) {
|
|
|
- $where[] = ['type_name|description', 'like', '%' . $search . '%'];
|
|
|
- }
|
|
|
-
|
|
|
- if ($type_category !== '') {
|
|
|
- $where[] = ['type_category', '=', $type_category];
|
|
|
- }
|
|
|
-
|
|
|
- if ($gender !== '') {
|
|
|
- $where[] = ['gender', '=', $gender];
|
|
|
- }
|
|
|
-
|
|
|
- // 查询数据
|
|
|
- $list = $this->model
|
|
|
- ->where($where)
|
|
|
- ->order('type_category ASC, sort ASC, id ASC')
|
|
|
- ->paginate($limit);
|
|
|
-
|
|
|
- return json(['code' => 0, 'msg' => '', 'count' => $list->total(), 'data' => $list->items()]);
|
|
|
- }
|
|
|
-
|
|
|
- // 获取分类选项
|
|
|
+
|
|
|
+ // 分类选项
|
|
|
$categories = [
|
|
|
'shoulder' => '肩型',
|
|
|
'chest' => '胸型(男)',
|
|
@@ -68,29 +66,55 @@ class BodyTypeConfig extends Backend
|
|
|
'hip' => '臀型',
|
|
|
'leg' => '腿型'
|
|
|
];
|
|
|
+
|
|
|
+ $this->view->assign('categories', $categories);
|
|
|
+ $this->assignconfig('categories', $categories);
|
|
|
+ }
|
|
|
|
|
|
- $this->assign('categories', $categories);
|
|
|
- return $this->view->fetch();
|
|
|
+ /**
|
|
|
+ * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
|
|
+ * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
|
|
+ * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
|
|
+ */
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查看
|
|
|
+ */
|
|
|
+ public function index()
|
|
|
+ {
|
|
|
+ //设置过滤方法
|
|
|
+ $this->request->filter(['strip_tags', 'trim']);
|
|
|
+ if (false === $this->request->isAjax()) {
|
|
|
+ return $this->view->fetch();
|
|
|
+ }
|
|
|
+ //如果发送的来源是 Selectpage,则转发到 Selectpage
|
|
|
+ if ($this->request->request('keyField')) {
|
|
|
+ return $this->selectpage();
|
|
|
+ }
|
|
|
+ [$where, $sort, $order, $offset, $limit] = $this->buildparams();
|
|
|
+ $list = $this->model
|
|
|
+ ->where($where)
|
|
|
+ ->order($sort, $order)
|
|
|
+ ->paginate($limit);
|
|
|
+ $result = ['total' => $list->total(), 'rows' => $list->items()];
|
|
|
+ return json($result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 添加体型配置
|
|
|
+ * 添加
|
|
|
*/
|
|
|
public function add()
|
|
|
{
|
|
|
- if ($this->request->isPost()) {
|
|
|
- $params = $this->request->post('row/a');
|
|
|
-
|
|
|
- if (empty($params)) {
|
|
|
- $this->error(__('Parameter %s can not be empty', ''));
|
|
|
- }
|
|
|
-
|
|
|
- // 验证必填字段
|
|
|
- if (empty($params['type_category']) || empty($params['type_name'])) {
|
|
|
- $this->error('分类和名称不能为空');
|
|
|
- }
|
|
|
+ if (false === $this->request->isPost()) {
|
|
|
+ return $this->view->fetch();
|
|
|
+ }
|
|
|
+ $params = $this->request->post('row/a');
|
|
|
+ if (empty($params)) {
|
|
|
+ $this->error(__('Parameter %s can not be empty', ''));
|
|
|
+ }
|
|
|
|
|
|
- // 检查是否重复
|
|
|
+ // 检查是否重复
|
|
|
+ if (!empty($params['type_category']) && !empty($params['type_name'])) {
|
|
|
$exists = $this->model->where([
|
|
|
'type_category' => $params['type_category'],
|
|
|
'type_name' => $params['type_name'],
|
|
@@ -100,39 +124,31 @@ class BodyTypeConfig extends Backend
|
|
|
if ($exists) {
|
|
|
$this->error('该体型配置已存在');
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- Db::startTrans();
|
|
|
- try {
|
|
|
- $result = $this->model->save($params);
|
|
|
- if ($result === false) {
|
|
|
- throw new \Exception($this->model->getError());
|
|
|
- }
|
|
|
-
|
|
|
- Db::commit();
|
|
|
- } catch (\Throwable $e) {
|
|
|
- Db::rollback();
|
|
|
- $this->error($e->getMessage());
|
|
|
+ $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()->validate($validate);
|
|
|
}
|
|
|
-
|
|
|
- $this->success();
|
|
|
+ $result = $this->model->allowField(true)->save($params);
|
|
|
+ Db::commit();
|
|
|
+ } catch (ValidateException|PDOException|\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
}
|
|
|
-
|
|
|
- // 分类选项
|
|
|
- $categories = [
|
|
|
- 'shoulder' => '肩型',
|
|
|
- 'chest' => '胸型(男)',
|
|
|
- 'bust' => '胸型(女)',
|
|
|
- 'waist' => '腰型',
|
|
|
- 'hip' => '臀型',
|
|
|
- 'leg' => '腿型'
|
|
|
- ];
|
|
|
-
|
|
|
- $this->assign('categories', $categories);
|
|
|
- return $this->view->fetch();
|
|
|
+ if ($result === false) {
|
|
|
+ $this->error(__('No rows were inserted'));
|
|
|
+ }
|
|
|
+ $this->success();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 编辑体型配置
|
|
|
+ * 编辑
|
|
|
*/
|
|
|
public function edit($ids = null)
|
|
|
{
|
|
@@ -140,15 +156,17 @@ class BodyTypeConfig extends Backend
|
|
|
if (!$row) {
|
|
|
$this->error(__('No Results were found'));
|
|
|
}
|
|
|
+ if (false === $this->request->isPost()) {
|
|
|
+ $this->view->assign('row', $row);
|
|
|
+ return $this->view->fetch();
|
|
|
+ }
|
|
|
+ $params = $this->request->post('row/a');
|
|
|
+ if (empty($params)) {
|
|
|
+ $this->error(__('Parameter %s can not be empty', ''));
|
|
|
+ }
|
|
|
|
|
|
- if ($this->request->isPost()) {
|
|
|
- $params = $this->request->post('row/a');
|
|
|
-
|
|
|
- if (empty($params)) {
|
|
|
- $this->error(__('Parameter %s can not be empty', ''));
|
|
|
- }
|
|
|
-
|
|
|
- // 检查是否重复(排除自己)
|
|
|
+ // 检查是否重复(排除自己)
|
|
|
+ if (!empty($params['type_category']) && !empty($params['type_name'])) {
|
|
|
$exists = $this->model->where([
|
|
|
'type_category' => $params['type_category'],
|
|
|
'type_name' => $params['type_name'],
|
|
@@ -158,56 +176,45 @@ class BodyTypeConfig extends Backend
|
|
|
if ($exists) {
|
|
|
$this->error('该体型配置已存在');
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- Db::startTrans();
|
|
|
- try {
|
|
|
- $result = $row->save($params);
|
|
|
- if ($result === false) {
|
|
|
- throw new \Exception($row->getError());
|
|
|
- }
|
|
|
-
|
|
|
- Db::commit();
|
|
|
- } catch (\Throwable $e) {
|
|
|
- Db::rollback();
|
|
|
- $this->error($e->getMessage());
|
|
|
+ $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()->validate($validate);
|
|
|
}
|
|
|
-
|
|
|
- $this->success();
|
|
|
+ $result = $row->allowField(true)->save($params);
|
|
|
+ Db::commit();
|
|
|
+ } catch (ValidateException|PDOException|\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
}
|
|
|
-
|
|
|
- // 分类选项
|
|
|
- $categories = [
|
|
|
- 'shoulder' => '肩型',
|
|
|
- 'chest' => '胸型(男)',
|
|
|
- 'bust' => '胸型(女)',
|
|
|
- 'waist' => '腰型',
|
|
|
- 'hip' => '臀型',
|
|
|
- 'leg' => '腿型'
|
|
|
- ];
|
|
|
-
|
|
|
- $this->assign('categories', $categories);
|
|
|
- $this->assign('row', $row);
|
|
|
- return $this->view->fetch();
|
|
|
+ if (false === $result) {
|
|
|
+ $this->error(__('No rows were updated'));
|
|
|
+ }
|
|
|
+ $this->success();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 删除体型配置
|
|
|
+ * 删除
|
|
|
*/
|
|
|
public function del($ids = null)
|
|
|
{
|
|
|
- if (!$this->request->isPost()) {
|
|
|
+ if (false === $this->request->isPost()) {
|
|
|
$this->error(__("Invalid parameters"));
|
|
|
}
|
|
|
-
|
|
|
- $ids = $ids ? $ids : $this->request->post("ids");
|
|
|
+ $ids = $ids ?: $this->request->post("ids");
|
|
|
if (empty($ids)) {
|
|
|
$this->error(__('Parameter %s can not be empty', 'ids'));
|
|
|
}
|
|
|
-
|
|
|
$pk = $this->model->getPk();
|
|
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
|
|
- $count = 0;
|
|
|
|
|
|
+ $count = 0;
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
foreach ($list as $item) {
|
|
@@ -216,59 +223,56 @@ class BodyTypeConfig extends Backend
|
|
|
if ($used > 0) {
|
|
|
throw new \Exception("体型「{$item->type_name}」已被用户选择,无法删除");
|
|
|
}
|
|
|
-
|
|
|
$count += $item->delete();
|
|
|
}
|
|
|
Db::commit();
|
|
|
- } catch (\Throwable $e) {
|
|
|
+ } catch (PDOException|\Exception $e) {
|
|
|
Db::rollback();
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
-
|
|
|
- $this->success();
|
|
|
+ if ($count) {
|
|
|
+ $this->success();
|
|
|
+ }
|
|
|
+ $this->error(__('No rows were deleted'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 批量修改状态
|
|
|
+ * 批量更新
|
|
|
*/
|
|
|
public function multi($ids = null)
|
|
|
{
|
|
|
- if (!$this->request->isPost()) {
|
|
|
- $this->error(__("Invalid parameters"));
|
|
|
+ if (false === $this->request->isPost()) {
|
|
|
+ $this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
-
|
|
|
- $ids = $ids ? $ids : $this->request->post("ids");
|
|
|
- $action = $this->request->post("action", '');
|
|
|
-
|
|
|
- if (empty($ids) || empty($action)) {
|
|
|
- $this->error(__('Parameter %s can not be empty', 'ids/action'));
|
|
|
+ $ids = $ids ?: $this->request->post('ids');
|
|
|
+ if (empty($ids)) {
|
|
|
+ $this->error(__('Parameter %s can not be empty', 'ids'));
|
|
|
}
|
|
|
|
|
|
- $list = $this->model->where('id', 'in', $ids)->select();
|
|
|
+ if (false === $this->request->has('params')) {
|
|
|
+ $this->error(__('No rows were updated'));
|
|
|
+ }
|
|
|
+ parse_str($this->request->post('params'), $values);
|
|
|
+ $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
|
|
|
+ if (empty($values)) {
|
|
|
+ $this->error(__('You have no permission'));
|
|
|
+ }
|
|
|
$count = 0;
|
|
|
-
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
+ $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
|
|
|
foreach ($list as $item) {
|
|
|
- switch ($action) {
|
|
|
- case 'enable':
|
|
|
- $item->status = 1;
|
|
|
- break;
|
|
|
- case 'disable':
|
|
|
- $item->status = 0;
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- $count += $item->save();
|
|
|
+ $count += $item->allowField(true)->isUpdate(true)->save($values);
|
|
|
}
|
|
|
Db::commit();
|
|
|
- } catch (\Throwable $e) {
|
|
|
+ } catch (PDOException|\Exception $e) {
|
|
|
Db::rollback();
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
-
|
|
|
- $this->success();
|
|
|
+ if ($count) {
|
|
|
+ $this->success();
|
|
|
+ }
|
|
|
+ $this->error(__('No rows were updated'));
|
|
|
}
|
|
|
|
|
|
/**
|