Curd.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\traits;
  12. use EasyAdmin\annotation\NodeAnotation;
  13. use EasyAdmin\tool\CommonTool;
  14. use jianyan\excel\Excel;
  15. use think\facade\Db;
  16. /**
  17. * 后台CURD复用
  18. * Trait Curd
  19. * @package app\admin\traits
  20. */
  21. trait Curd
  22. {
  23. /**
  24. * @NodeAnotation(title="列表")
  25. */
  26. public function index()
  27. {
  28. if ($this->request->isAjax()) {
  29. if (input('selectFields')) {
  30. return $this->selectList();
  31. }
  32. list($page, $limit, $where) = $this->buildTableParames();
  33. $count = $this->model
  34. ->where($where)
  35. ->count();
  36. $list = $this->model
  37. ->where($where)
  38. ->page($page, $limit)
  39. ->order($this->sort)
  40. ->select();
  41. $data = [
  42. 'code' => 0,
  43. 'msg' => '',
  44. 'count' => $count,
  45. 'data' => $list,
  46. ];
  47. return json($data);
  48. }
  49. return $this->fetch();
  50. }
  51. /**
  52. * @NodeAnotation(title="添加")
  53. */
  54. public function add()
  55. {
  56. if ($this->request->isPost()) {
  57. $post = $this->request->post();
  58. $rule = [];
  59. $this->validate($post, $rule);
  60. try {
  61. $save = $this->model->save($post);
  62. } catch (\Exception $e) {
  63. $this->error('保存失败:'.$e->getMessage());
  64. }
  65. $save ? $this->success('保存成功') : $this->error('保存失败');
  66. }
  67. return $this->fetch();
  68. }
  69. /**
  70. * @NodeAnotation(title="编辑")
  71. */
  72. public function edit($id)
  73. {
  74. $row = $this->model->find($id);
  75. empty($row) && $this->error('数据不存在');
  76. if ($this->request->isPost()) {
  77. $post = $this->request->post();
  78. $rule = [];
  79. $this->validate($post, $rule);
  80. try {
  81. $save = $row->save($post);
  82. } catch (\Exception $e) {
  83. $this->error('保存失败');
  84. }
  85. $save ? $this->success('保存成功') : $this->error('保存失败');
  86. }
  87. $this->assign('row', $row);
  88. return $this->fetch();
  89. }
  90. /**
  91. * @NodeAnotation(title="删除")
  92. */
  93. public function delete($id)
  94. {
  95. $this->checkPostRequest();
  96. $row = $this->model->whereIn('id', $id)->select();
  97. $row->isEmpty() && $this->error('数据不存在');
  98. try {
  99. $save = $row->delete();
  100. } catch (\Exception $e) {
  101. $this->error('删除失败');
  102. }
  103. $save ? $this->success('删除成功') : $this->error('删除失败');
  104. }
  105. /**
  106. * @NodeAnotation(title="导出")
  107. */
  108. public function export()
  109. {
  110. list($page, $limit, $where) = $this->buildTableParames();
  111. $header = getExportHeader($modelName);
  112. $list = $this->model
  113. ->where($where)
  114. ->limit(100000)
  115. ->order('id', 'desc')
  116. ->select()
  117. ->toArray();
  118. $fileName = time();
  119. return Excel::exportData($list, $header, $fileName, 'xlsx');
  120. }
  121. /**
  122. * @NodeAnotation(title="属性修改")
  123. */
  124. public function modify()
  125. {
  126. $this->checkPostRequest();
  127. $post = $this->request->post();
  128. $rule = [
  129. 'id|ID' => 'require',
  130. 'field|字段' => 'require',
  131. 'value|值' => 'require',
  132. ];
  133. $this->validate($post, $rule);
  134. $row = $this->model->find($post['id']);
  135. if (!$row) {
  136. $this->error('数据不存在');
  137. }
  138. if (!in_array($post['field'], $this->allowModifyFields)) {
  139. $this->error('该字段不允许修改:' . $post['field']);
  140. }
  141. try {
  142. $row->save([
  143. $post['field'] => $post['value'],
  144. ]);
  145. } catch (\Exception $e) {
  146. $this->error($e->getMessage());
  147. }
  148. $this->success('保存成功');
  149. }
  150. }