Hotel.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Area;
  4. use app\api\service\BDService;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 酒店管理
  11. *
  12. * @icon fa fa-hotel
  13. */
  14. class Hotel extends Backend
  15. {
  16. /**
  17. * Hotel模型对象
  18. * @var \app\admin\model\Hotel
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Hotel;
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = false;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. $row->visible(['id', 'name', 'photo_images', 'description', 'tags', 'star', 'phone', 'address', 'updatetime']);
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. public function add()
  59. {
  60. if (false === $this->request->isPost()) {
  61. return $this->view->fetch();
  62. }
  63. $params = $this->request->post('row/a');
  64. if (empty($params)) {
  65. $this->error(__('Parameter %s can not be empty', ''));
  66. }
  67. $params = $this->preExcludeFields($params);
  68. $s_res = BDService::geocoding($params["address"] ?? $params["area_city"]);
  69. if ($s_res) {
  70. $params["lng"] = $s_res->data()["lng"];
  71. $params["lat"] = $s_res->data()["lat"];
  72. }
  73. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  74. $params[$this->dataLimitField] = $this->auth->id;
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException()->validate($validate);
  84. }
  85. $result = $this->model->allowField(true)->save($params);
  86. Db::commit();
  87. } catch (ValidateException | PDOException | Exception $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. }
  91. if ($result === false) {
  92. $this->error(__('No rows were inserted'));
  93. }
  94. $this->success();
  95. }
  96. public function edit($ids = null)
  97. {
  98. $row = $this->model->get($ids);
  99. if (!$row) {
  100. $this->error(__('No Results were found'));
  101. }
  102. $adminIds = $this->getDataLimitAdminIds();
  103. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  104. $this->error(__('You have no permission'));
  105. }
  106. if (false === $this->request->isPost()) {
  107. $this->view->assign('row', $row);
  108. return $this->view->fetch();
  109. }
  110. $params = $this->request->post('row/a');
  111. if (empty($params)) {
  112. $this->error(__('Parameter %s can not be empty', ''));
  113. }
  114. $params = $this->preExcludeFields($params);
  115. $s_res = BDService::geocoding($params["address"] ?? $params["area_city"]);
  116. if ($s_res) {
  117. $params["lng"] = $s_res->data()["lng"];
  118. $params["lat"] = $s_res->data()["lat"];
  119. }
  120. $result = false;
  121. Db::startTrans();
  122. try {
  123. //是否采用模型验证
  124. if ($this->modelValidate) {
  125. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  126. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  127. $row->validateFailException()->validate($validate);
  128. }
  129. $result = $row->allowField(true)->save($params);
  130. Db::commit();
  131. } catch (ValidateException | PDOException | Exception $e) {
  132. Db::rollback();
  133. $this->error($e->getMessage());
  134. }
  135. if (false === $result) {
  136. $this->error(__('No rows were updated'));
  137. }
  138. $this->success();
  139. }
  140. }