Company.php 5.3 KB

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