Lang.php 4.1 KB

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