Payer.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Controller;
  5. use think\Request;
  6. /**
  7. *
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Payer extends Backend
  12. {
  13. /**
  14. * Payer模型对象
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('Payer');
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  26. */
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags']);
  34. if ($this->request->isAjax())
  35. {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('pkey_name'))
  38. {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $total = $this->model
  43. ->where($where)
  44. ->order($sort, $order)
  45. ->count();
  46. $list = $this->model
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->limit($offset, $limit)
  50. ->select();
  51. $result = array("total" => $total, "rows" => $list);
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 添加
  58. */
  59. public function add()
  60. {
  61. if ($this->request->isPost())
  62. {
  63. $params = $this->request->post("row/a");
  64. if ($params)
  65. {
  66. if ($this->dataLimit)
  67. {
  68. $params[$this->dataLimitField] = $this->auth->id;
  69. }
  70. try
  71. {
  72. //是否采用模型验证
  73. if ($this->modelValidate)
  74. {
  75. $name = basename(str_replace('\\', '/', get_class($this->model)));
  76. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  77. $this->model->validate($validate);
  78. }
  79. $result = $this->model->allowField(true)->save($params);
  80. if ($result !== false)
  81. {
  82. $this->success();
  83. }
  84. else
  85. {
  86. $this->error($this->model->getError());
  87. }
  88. }
  89. catch (\think\exception\PDOException $e)
  90. {
  91. $this->error($e->getMessage());
  92. }
  93. }
  94. $this->error(__('Parameter %s can not be empty', ''));
  95. }
  96. return $this->view->fetch();
  97. }
  98. /**
  99. * 编辑
  100. */
  101. public function edit($ids = NULL)
  102. {
  103. $row = $this->model->get($ids);
  104. if (!$row)
  105. $this->error(__('No Results were found'));
  106. $adminIds = $this->getDataLimitAdminIds();
  107. if (is_array($adminIds))
  108. {
  109. if (!in_array($row[$this->dataLimitField], $adminIds))
  110. {
  111. $this->error(__('You have no permission'));
  112. }
  113. }
  114. if ($this->request->isPost())
  115. {
  116. $params = $this->request->post("row/a");
  117. if ($params)
  118. {
  119. /*
  120. * 已经弃用,如果为了兼容老版可取消注释
  121. foreach ($params as $k => &$v)
  122. {
  123. $v = is_array($v) ? implode(',', $v) : $v;
  124. }
  125. */
  126. try
  127. {
  128. //是否采用模型验证
  129. if ($this->modelValidate)
  130. {
  131. $name = basename(str_replace('\\', '/', get_class($this->model)));
  132. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  133. $row->validate($validate);
  134. }
  135. $result = $row->allowField(true)->save($params);
  136. if ($result !== false)
  137. {
  138. $this->success();
  139. }
  140. else
  141. {
  142. $this->error($row->getError());
  143. }
  144. }
  145. catch (\think\exception\PDOException $e)
  146. {
  147. $this->error($e->getMessage());
  148. }
  149. }
  150. $this->error(__('Parameter %s can not be empty', ''));
  151. }
  152. $this->view->assign("row", $row);
  153. return $this->view->fetch();
  154. }
  155. /**
  156. * 删除
  157. */
  158. public function del($ids = "")
  159. {
  160. if ($ids)
  161. {
  162. $pk = $this->model->getPk();
  163. $adminIds = $this->getDataLimitAdminIds();
  164. if (is_array($adminIds))
  165. {
  166. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  167. }
  168. $list = $this->model->where($pk, 'in', $ids)->select();
  169. $count = 0;
  170. foreach ($list as $k => $v)
  171. {
  172. $has_payer = model('AdminExtend')->where(['payerid'=>$v->id])->count();
  173. if(!$has_payer){
  174. $count += $v->delete();
  175. }
  176. }
  177. if ($count)
  178. {
  179. $this->success();
  180. }
  181. else
  182. {
  183. $this->error(__('正在使用中,不能删除'));
  184. }
  185. }
  186. $this->error(__('Parameter %s can not be empty', 'ids'));
  187. }
  188. }