Backend.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace app\admin\library\traits;
  3. use Throwable;
  4. use think\facade\Config;
  5. /**
  6. * 后台控制器trait类
  7. * 已导入到 @see \app\common\controller\Backend 中
  8. * 若需修改此类方法:请复制方法至对应控制器后进行重写
  9. */
  10. trait Backend
  11. {
  12. /**
  13. * 排除入库字段
  14. * @param array $params
  15. * @return array
  16. */
  17. protected function excludeFields(array $params): array
  18. {
  19. if (!is_array($this->preExcludeFields)) {
  20. $this->preExcludeFields = explode(',', (string)$this->preExcludeFields);
  21. }
  22. foreach ($this->preExcludeFields as $field) {
  23. if (array_key_exists($field, $params)) {
  24. unset($params[$field]);
  25. }
  26. }
  27. return $params;
  28. }
  29. /**
  30. * 查看
  31. * @throws Throwable
  32. */
  33. public function index(): void
  34. {
  35. if ($this->request->param('select')) {
  36. $this->select();
  37. }
  38. list($where, $alias, $limit, $order) = $this->queryBuilder();
  39. $res = $this->model
  40. ->field($this->indexField)
  41. ->withJoin($this->withJoinTable, $this->withJoinType)
  42. ->alias($alias)
  43. ->where($where)
  44. ->order($order)
  45. ->paginate($limit);
  46. $this->success('', [
  47. 'list' => $res->items(),
  48. 'total' => $res->total(),
  49. 'remark' => get_route_remark(),
  50. ]);
  51. }
  52. /**
  53. * 添加
  54. */
  55. public function add(): void
  56. {
  57. if ($this->request->isPost()) {
  58. $data = $this->request->post();
  59. if (!$data) {
  60. $this->error(__('Parameter %s can not be empty', ['']));
  61. }
  62. $data = $this->excludeFields($data);
  63. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  64. $data[$this->dataLimitField] = $this->auth->id;
  65. }
  66. $result = false;
  67. $this->model->startTrans();
  68. try {
  69. // 模型验证
  70. if ($this->modelValidate) {
  71. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  72. if (class_exists($validate)) {
  73. $validate = new $validate;
  74. if ($this->modelSceneValidate) $validate->scene('add');
  75. $validate->check($data);
  76. }
  77. }
  78. $result = $this->model->save($data);
  79. $this->model->commit();
  80. } catch (Throwable $e) {
  81. $this->model->rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if ($result !== false) {
  85. $this->success(__('Added successfully'));
  86. } else {
  87. $this->error(__('No rows were added'));
  88. }
  89. }
  90. $this->error(__('Parameter error'));
  91. }
  92. /**
  93. * 编辑
  94. * @throws Throwable
  95. */
  96. public function edit(): void
  97. {
  98. $pk = $this->model->getPk();
  99. $id = $this->request->param($pk);
  100. $row = $this->model->find($id);
  101. if (!$row) {
  102. $this->error(__('Record not found'));
  103. }
  104. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  105. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  106. $this->error(__('You have no permission'));
  107. }
  108. if ($this->request->isPost()) {
  109. $data = $this->request->post();
  110. if (!$data) {
  111. $this->error(__('Parameter %s can not be empty', ['']));
  112. }
  113. $data = $this->excludeFields($data);
  114. $result = false;
  115. $this->model->startTrans();
  116. try {
  117. // 模型验证
  118. if ($this->modelValidate) {
  119. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  120. if (class_exists($validate)) {
  121. $validate = new $validate;
  122. if ($this->modelSceneValidate) $validate->scene('edit');
  123. $data[$pk] = $row[$pk];
  124. $validate->check($data);
  125. }
  126. }
  127. $result = $row->save($data);
  128. $this->model->commit();
  129. } catch (Throwable $e) {
  130. $this->model->rollback();
  131. $this->error($e->getMessage());
  132. }
  133. if ($result !== false) {
  134. $this->success(__('Update successful'));
  135. } else {
  136. $this->error(__('No rows updated'));
  137. }
  138. }
  139. $this->success('', [
  140. 'row' => $row
  141. ]);
  142. }
  143. /**
  144. * 删除
  145. * @param array $ids
  146. * @throws Throwable
  147. */
  148. public function del(array $ids = []): void
  149. {
  150. if (!$this->request->isDelete() || !$ids) {
  151. $this->error(__('Parameter error'));
  152. }
  153. $where = [];
  154. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  155. if ($dataLimitAdminIds) {
  156. $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
  157. }
  158. $pk = $this->model->getPk();
  159. $where[] = [$pk, 'in', $ids];
  160. $count = 0;
  161. $data = $this->model->where($where)->select();
  162. $this->model->startTrans();
  163. try {
  164. foreach ($data as $v) {
  165. $count += $v->delete();
  166. }
  167. $this->model->commit();
  168. } catch (Throwable $e) {
  169. $this->model->rollback();
  170. $this->error($e->getMessage());
  171. }
  172. if ($count) {
  173. $this->success(__('Deleted successfully'));
  174. } else {
  175. $this->error(__('No rows were deleted'));
  176. }
  177. }
  178. /**
  179. * 排序
  180. * @param int $id 排序主键值
  181. * @param int $targetId 排序位置主键值
  182. * @throws Throwable
  183. */
  184. public function sortable(int $id, int $targetId): void
  185. {
  186. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  187. if ($dataLimitAdminIds) {
  188. $this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
  189. }
  190. $row = $this->model->find($id);
  191. $target = $this->model->find($targetId);
  192. if (!$row || !$target) {
  193. $this->error(__('Record not found'));
  194. }
  195. if ($row[$this->weighField] == $target[$this->weighField]) {
  196. $autoSortEqWeight = is_null($this->autoSortEqWeight) ? Config::get('buildadmin.auto_sort_eq_weight') : $this->autoSortEqWeight;
  197. if (!$autoSortEqWeight) {
  198. $this->error(__('Invalid collation because the weights of the two targets are equal'));
  199. }
  200. // 自动重新整理排序
  201. $all = $this->model->select();
  202. foreach ($all as $item) {
  203. $item[$this->weighField] = $item[$this->model->getPk()];
  204. $item->save();
  205. }
  206. unset($all);
  207. // 重新获取
  208. $row = $this->model->find($id);
  209. $target = $this->model->find($targetId);
  210. }
  211. $backup = $target[$this->weighField];
  212. $target[$this->weighField] = $row[$this->weighField];
  213. $row[$this->weighField] = $backup;
  214. $row->save();
  215. $target->save();
  216. $this->success();
  217. }
  218. /**
  219. * 加载为select(远程下拉选择框)数据,默认还是走$this->index()方法
  220. * 必要时请在对应控制器类中重写
  221. */
  222. public function select(): void
  223. {
  224. }
  225. }