Region.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\admin\model\general\Lang as LangModel;
  4. use app\admin\model\general\Region as RegionModel;
  5. use app\common\controller\Backend;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. class Region extends Backend
  9. {
  10. protected $searchFields = 'id,name,code,lang,shopurl';
  11. protected function initialize()
  12. {
  13. parent::initialize();
  14. $this->modelClass = new RegionModel;
  15. }
  16. public function index()
  17. {
  18. if ($this->request->isAjax()) {
  19. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  20. $count = $this->modelClass->where($where)->count();
  21. $data = $this->modelClass->where($where)->order($sort, $order)->page($page, $limit)->select();
  22. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  23. }
  24. return $this->fetch();
  25. }
  26. public function add()
  27. {
  28. if ($this->request->isPost()) {
  29. $this->token();
  30. $params = $this->request->post('row/a');
  31. if (!$params) {
  32. $this->error('参数不能为空');
  33. }
  34. Db::startTrans();
  35. try {
  36. $this->validate($params, 'app\\admin\\validate\\Region.add');
  37. $this->assertLangExists($params['lang'] ?? '');
  38. if (!empty($params['is_default'])) {
  39. Db::name('region')->where('id', '>', 0)->update(['is_default' => 0]);
  40. }
  41. $this->modelClass->save($params);
  42. Db::commit();
  43. } catch (ValidateException | \Exception $e) {
  44. Db::rollback();
  45. $this->error($e->getMessage());
  46. }
  47. $this->success('新增成功');
  48. }
  49. $this->assign('langList', LangModel::getEnabledList());
  50. return $this->fetch();
  51. }
  52. public function edit()
  53. {
  54. $id = $this->request->param('id/d');
  55. $row = $this->modelClass->find($id);
  56. if (!$row) {
  57. $this->error('记录未找到');
  58. }
  59. if ($this->request->isPost()) {
  60. $this->token();
  61. $params = $this->request->post('row/a');
  62. if (!$params) {
  63. $this->error('参数不能为空');
  64. }
  65. Db::startTrans();
  66. try {
  67. $validate = new \app\admin\validate\Region();
  68. $validate->scene('edit');
  69. $validate->rule([
  70. 'code|地区编码' => 'require|regex:^[A-Za-z]{2,20}$|unique:region,code,' . $id,
  71. 'shopurl|商城链接' => 'max:255|url',
  72. ]);
  73. $validate->check($params);
  74. $this->assertLangExists($params['lang'] ?? '');
  75. if (!empty($params['is_default'])) {
  76. Db::name('region')->where('id', '<>', $id)->update(['is_default' => 0]);
  77. }
  78. $row->save($params);
  79. Db::commit();
  80. } catch (ValidateException | \Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. }
  84. $this->success('编辑成功');
  85. }
  86. $this->assign('data', $row);
  87. $this->assign('langList', LangModel::getEnabledList());
  88. return $this->fetch();
  89. }
  90. public function del()
  91. {
  92. if (!$this->request->isPost()) {
  93. $this->error('未知参数');
  94. }
  95. $ids = $this->request->param('id/a', []);
  96. if (empty($ids)) {
  97. $this->error('参数错误');
  98. }
  99. $defaultCount = $this->modelClass->where('id', 'in', $ids)->where('is_default', 1)->count();
  100. if ($defaultCount > 0) {
  101. $this->error('默认地区禁止删除,请先切换默认地区');
  102. }
  103. $this->modelClass->where('id', 'in', $ids)->delete();
  104. $this->success('删除成功');
  105. }
  106. public function setDefault()
  107. {
  108. $id = $this->request->param('id/d');
  109. $row = $this->modelClass->find($id);
  110. if (!$row) {
  111. $this->error('记录未找到');
  112. }
  113. Db::startTrans();
  114. try {
  115. Db::name('region')->where('id', '>', 0)->update(['is_default' => 0]);
  116. $row->is_default = 1;
  117. $row->save();
  118. Db::commit();
  119. } catch (\Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. $this->success('默认地区设置成功');
  124. }
  125. protected function assertLangExists(string $mark): void
  126. {
  127. if ($mark === '' || !LangModel::where('mark', strtolower($mark))->where('is_enabled', 1)->find()) {
  128. $this->error('绑定语言不存在或未启用');
  129. }
  130. }
  131. }