- 重构了 Activity、Circle、Club、Game 和 Gym 控制器中的查询方法 - 添加了分页功能,支持指定页码和每页数量 - 优化了查询结果,返回包含总数的格式化数据 - 使用 alias 和 join 方法改进了查询效率 - 删除了 Base 控制器中的通用查询方法
74 lines
1.8 KiB
PHP
74 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\controller\zy;
|
|
|
|
use think\Db;
|
|
use think\Exception;
|
|
use think\exception\PDOException;
|
|
use addons\shopro\controller\Common;
|
|
use think\exception\ValidateException;
|
|
|
|
class Base extends Common
|
|
{
|
|
protected $noNeedLogin = ['index'];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
protected $model;
|
|
protected $user;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->user = auth_user();
|
|
}
|
|
|
|
public function add()
|
|
{
|
|
$result = false;
|
|
$params = $this->request->param();
|
|
Db::startTrans();
|
|
try {
|
|
$result = $this->model->allowField(true)->save($params);
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($result === false) {
|
|
$this->error('操作失败');
|
|
}
|
|
$this->success('Success');
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$result = false;
|
|
$params = $this->request->param();
|
|
$model = $this->model->get($params['id']);
|
|
if (empty($model)) {
|
|
$this->error(__('No rows were found'));
|
|
}
|
|
Db::startTrans();
|
|
try {
|
|
$result = $model->allowField(true)->save($params);
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($result === false) {
|
|
$this->error('操作失败');
|
|
}
|
|
$this->success('Success');
|
|
}
|
|
|
|
public function view()
|
|
{
|
|
$model = $this->model->get($this->request->param('id'));
|
|
if (empty($model)) {
|
|
$this->error(__('No rows were found'));
|
|
}
|
|
$this->success('Success', $model);
|
|
}
|
|
}
|