| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace app\admin\controller\general;
- use app\admin\model\general\Lang as LangModel;
- use app\admin\model\general\Region as RegionModel;
- use app\common\controller\Backend;
- use think\exception\ValidateException;
- use think\facade\Db;
- class Region extends Backend
- {
- protected $searchFields = 'id,name,code,lang,shopurl';
- protected function initialize()
- {
- parent::initialize();
- $this->modelClass = new RegionModel;
- }
- public function index()
- {
- if ($this->request->isAjax()) {
- [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
- $count = $this->modelClass->where($where)->count();
- $data = $this->modelClass->where($where)->order($sort, $order)->page($page, $limit)->select();
- return json(['code' => 0, 'count' => $count, 'data' => $data]);
- }
- return $this->fetch();
- }
- public function add()
- {
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post('row/a');
- if (!$params) {
- $this->error('参数不能为空');
- }
- Db::startTrans();
- try {
- $this->validate($params, 'app\\admin\\validate\\Region.add');
- $this->assertLangExists($params['lang'] ?? '');
- if (!empty($params['is_default'])) {
- Db::name('region')->where('id', '>', 0)->update(['is_default' => 0]);
- }
- $this->modelClass->save($params);
- Db::commit();
- } catch (ValidateException | \Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('新增成功');
- }
- $this->assign('langList', LangModel::getEnabledList());
- return $this->fetch();
- }
- public function edit()
- {
- $id = $this->request->param('id/d');
- $row = $this->modelClass->find($id);
- if (!$row) {
- $this->error('记录未找到');
- }
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post('row/a');
- if (!$params) {
- $this->error('参数不能为空');
- }
- Db::startTrans();
- try {
- $validate = new \app\admin\validate\Region();
- $validate->scene('edit');
- $validate->rule([
- 'code|地区编码' => 'require|regex:^[A-Za-z]{2,20}$|unique:region,code,' . $id,
- 'shopurl|商城链接' => 'max:255|url',
- ]);
- $validate->check($params);
- $this->assertLangExists($params['lang'] ?? '');
- if (!empty($params['is_default'])) {
- Db::name('region')->where('id', '<>', $id)->update(['is_default' => 0]);
- }
- $row->save($params);
- Db::commit();
- } catch (ValidateException | \Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('编辑成功');
- }
- $this->assign('data', $row);
- $this->assign('langList', LangModel::getEnabledList());
- return $this->fetch();
- }
- public function del()
- {
- if (!$this->request->isPost()) {
- $this->error('未知参数');
- }
- $ids = $this->request->param('id/a', []);
- if (empty($ids)) {
- $this->error('参数错误');
- }
- $defaultCount = $this->modelClass->where('id', 'in', $ids)->where('is_default', 1)->count();
- if ($defaultCount > 0) {
- $this->error('默认地区禁止删除,请先切换默认地区');
- }
- $this->modelClass->where('id', 'in', $ids)->delete();
- $this->success('删除成功');
- }
- public function setDefault()
- {
- $id = $this->request->param('id/d');
- $row = $this->modelClass->find($id);
- if (!$row) {
- $this->error('记录未找到');
- }
- Db::startTrans();
- try {
- Db::name('region')->where('id', '>', 0)->update(['is_default' => 0]);
- $row->is_default = 1;
- $row->save();
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('默认地区设置成功');
- }
- protected function assertLangExists(string $mark): void
- {
- if ($mark === '' || !LangModel::where('mark', strtolower($mark))->where('is_enabled', 1)->find()) {
- $this->error('绑定语言不存在或未启用');
- }
- }
- }
|