Company.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = $this->whereop('company.id');
  46. $list = $this->model
  47. ->with(['wallet'])
  48. ->where($where)
  49. ->where($where_op)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->getRelation('wallet')->visible(['money']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 关联
  62. */
  63. public function guanlian($ids = null){
  64. $row = $this->model->get($ids);
  65. if (!$row) {
  66. $this->error(__('No Results were found'));
  67. }
  68. $adminIds = $this->getDataLimitAdminIds();
  69. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  70. $this->error(__('You have no permission'));
  71. }
  72. if (false === $this->request->isPost()) {
  73. $this->view->assign('row', $row);
  74. return $this->view->fetch();
  75. }
  76. $params = $this->request->post('row/a');
  77. if (empty($params)) {
  78. $this->error(__('Parameter %s can not be empty', ''));
  79. }
  80. $params = $this->preExcludeFields($params);
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. //是否采用模型验证
  85. if ($this->modelValidate) {
  86. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  87. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  88. $row->validateFailException()->validate($validate);
  89. }
  90. $result = $row->allowField(true)->save($params);
  91. Db::commit();
  92. } catch (ValidateException|PDOException|Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if (false === $result) {
  97. $this->error(__('No rows were updated'));
  98. }
  99. $this->success();
  100. }
  101. /**
  102. * 审核
  103. */
  104. public function audit($ids = null){
  105. $row = $this->model->get($ids);
  106. if (!$row) {
  107. $this->error(__('No Results were found'));
  108. }
  109. $adminIds = $this->getDataLimitAdminIds();
  110. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  111. $this->error(__('You have no permission'));
  112. }
  113. if (false === $this->request->isPost()) {
  114. $this->view->assign('row', $row);
  115. return $this->view->fetch();
  116. }
  117. $params = $this->request->post('row/a');
  118. if (empty($params)) {
  119. $this->error(__('Parameter %s can not be empty', ''));
  120. }
  121. $params = $this->preExcludeFields($params);
  122. $result = false;
  123. Db::startTrans();
  124. try {
  125. //是否采用模型验证
  126. if ($this->modelValidate) {
  127. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  128. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  129. $row->validateFailException()->validate($validate);
  130. }
  131. $result = $row->allowField(true)->save($params);
  132. Db::commit();
  133. } catch (ValidateException|PDOException|Exception $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. }
  137. if (false === $result) {
  138. $this->error(__('No rows were updated'));
  139. }
  140. $this->success();
  141. }
  142. }