Cityconfig.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\controller\service;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 城市出行配置管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Cityconfig extends Backend
  11. {
  12. /**
  13. * Cityconfig模型对象
  14. * @var \app\admin\model\service\Cityconfig
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\service\Cityconfig;
  21. }
  22. /**
  23. * 查看
  24. * @return string|\think\response\Json
  25. * @throws \think\Exception
  26. * @throws \think\exception\DbException
  27. */
  28. public function index()
  29. {
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags', 'trim']);
  32. if (false === $this->request->isAjax()) {
  33. return $this->view->fetch();
  34. }
  35. //如果发送的来源是 Selectpage,则转发到 Selectpage
  36. if ($this->request->request('keyField')) {
  37. return $this->selectpage();
  38. }
  39. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  40. $list = $this->model
  41. ->where($where)
  42. ->order($sort, $order)
  43. ->paginate($limit);
  44. $result = ['total' => $list->total(), 'rows' => $list->items()];
  45. return json($result);
  46. }
  47. public function add()
  48. {
  49. if (false === $this->request->isPost()) {
  50. return $this->view->fetch();
  51. }
  52. $params = $this->request->post('row/a');
  53. if (empty($params)) {
  54. $this->error(__('Parameter %s can not be empty', ''));
  55. }
  56. $params = $this->preExcludeFields($params);
  57. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  58. $params[$this->dataLimitField] = $this->auth->id;
  59. }
  60. $params['city'] = explode('/',$params['city'])[1];
  61. $exist = $this->model->where(['city'=>$params['city']])->value('id');
  62. $exist && $this->error('当前城市已配置');
  63. $result = false;
  64. Db::startTrans();
  65. try {
  66. //是否采用模型验证
  67. if ($this->modelValidate) {
  68. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  69. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  70. $this->model->validateFailException()->validate($validate);
  71. }
  72. $result = $this->model->allowField(true)->save($params);
  73. Db::commit();
  74. } catch (ValidateException|PDOException|Exception $e) {
  75. Db::rollback();
  76. $this->error($e->getMessage());
  77. }
  78. if ($result === false) {
  79. $this->error(__('No rows were inserted'));
  80. }
  81. $this->success();
  82. }
  83. /**
  84. * 编辑
  85. *
  86. * @param $ids
  87. * @return string
  88. * @throws DbException
  89. * @throws \think\Exception
  90. */
  91. public function edit($ids = null)
  92. {
  93. $row = $this->model->get($ids);
  94. if (!$row) {
  95. $this->error(__('No Results were found'));
  96. }
  97. $adminIds = $this->getDataLimitAdminIds();
  98. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  99. $this->error(__('You have no permission'));
  100. }
  101. if (false === $this->request->isPost()) {
  102. $this->view->assign('row', $row);
  103. return $this->view->fetch();
  104. }
  105. $params = $this->request->post('row/a');
  106. if (empty($params)) {
  107. $this->error(__('Parameter %s can not be empty', ''));
  108. }
  109. $params = $this->preExcludeFields($params);
  110. $result = false;
  111. Db::startTrans();
  112. try {
  113. //是否采用模型验证
  114. if ($this->modelValidate) {
  115. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  116. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  117. $row->validateFailException()->validate($validate);
  118. }
  119. $result = $row->allowField(true)->save($params);
  120. Db::commit();
  121. } catch (ValidateException|PDOException|Exception $e) {
  122. Db::rollback();
  123. $this->error($e->getMessage());
  124. }
  125. if (false === $result) {
  126. $this->error(__('No rows were updated'));
  127. }
  128. $this->success();
  129. }
  130. }