Files
orico-official-website-old/app/admin/controller/CustomerGroup.php
2024-10-29 14:04:59 +08:00

170 lines
6.8 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
class CustomerGroup extends BaseController {
public function index() {
$this->redirect('/admin/customer_group/lists');
}
public function lists() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
$arg_where = array('stat' => 0);
$arg_order = array('id' => 'asc');
$arg_field = array('id', 'name', 'description', 'sort', 'stat');
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['name|description'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$dataObject = Loader::model('CustomerGroup')->getPageList($arg_where, $arg_order, $arg_field, 12);
//$groupOption = $customer_groupModel->getCustomerGroupOption(0, $arg_where, $arg_order, ['id', 'name',], 50);
$value = [
//'groupOption' => $groupOption,
'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray()
'page' => $dataObject->render(),
//'page_previous' => $dataObject->getUrl($dataObject->currentPage() - 1),
//'page_next' => $dataObject->getUrl($dataObject->currentPage() + 1),
'search' => $search,
];
$this->assign($value);
return $this->fetch();
}
public function edit($id = 0) {
$id = intval($id);
if ($id > 0) {
$customer_group = Loader::model('CustomerGroup')->getRow($id);
if (empty($customer_group)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['customer_group'] = $customer_group;
} else {
return $this->error(Lang::get('incorrect operation'));
}
$this->assign($value);
return $this->fetch();
}
public function update() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
} else {
foreach ($data as $k => $v) {
if (is_string($v)) {
$data[$k] = trim($v);
}
}
}
$validaterule = ['name' => 'require|unique:customer_group,name', 'description' => 'require', 'agree' => 'require|eq:on',];
$validatemsg = ['name.require' => '名称不能为空', 'name.unique' => '名称已存在', 'description.require' => '描述不能为空',
'agree.require' => '请勾选确认框', 'agree.eq' => '请勾选确认框',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
if (isset($data['agree'])) {
unset($data['agree']);
}
$model = Loader::model('CustomerGroup')->updateRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/customer_group/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function add() {
return $this->fetch();
}
public function create() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
} else {
foreach ($data as $k => $v) {
if (is_string($v)) {
$data[$k] = trim($v);
}
}
}
$validaterule = ['name' => 'require|unique:customer_group,name', 'description' => 'require', 'agree' => 'require|eq:on',];
$validatemsg = ['name.require' => '名称不能为空', 'name.unique' => '名称已存在', 'description.require' => '描述不能为空',
'agree.require' => '请勾选确认框', 'agree.eq' => '请勾选确认框',];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
if (isset($data['agree'])) {
unset($data['agree']);
}
//$data['siteid'] = $this->siteid;
$model = Loader::model('CustomerGroup')->insertRow($data);
if ($model && $model->getData('id')) {
return $this->redirect(url('/admin/customer_group/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function delete($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = Loader::model('CustomerGroup')->deleteRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/customer_group/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function deletes() {
$ids = $this->request->post('ids');
if ($this->request->isPost() && $ids) {
$result = Loader::model('CustomerGroup')->deleteRows($ids);
//$result = Loader::model('Language')->updateRow(['stat' => -1], ['id' => ['in', $ids]]);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/customer_group/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function togglestat() {
$id = $this->request->get('id', 0);
$flag = $this->request->get('flag', 0);
if ($this->request->isGet() && $id) {
$model = Loader::model('CustomerGroup')->updateRow(['id' => $id, 'stat' => !$flag]);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/customer_group/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
}