Service.php 6.0 KB

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