controller-normal.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare (strict_types = 1);
  3. namespace <#namespace#>;
  4. use app\common\controller\Backend;
  5. <#if table || form#>
  6. use app\admin\traits\Actions;
  7. <#endif#>
  8. use think\annotation\route\Group;
  9. use think\annotation\route\Route;
  10. use <#model#> as <#modelName#>Model;
  11. <#if isTree#>
  12. use app\common\library\Tree;
  13. <#endif#>
  14. #[Group("<#group#>")]
  15. class <#controllerName#> extends Backend
  16. {
  17. <#if table || form#>
  18. use Actions{
  19. <#if table && isset(actionList['index'])#>
  20. index as private _index;
  21. <#endif#>
  22. <#if form && isset(actionList['add'])#>
  23. add as private _add;
  24. <#endif#>
  25. <#if form && isset(actionList['edit'])#>
  26. edit as private _edit;
  27. <#endif#>
  28. <#if isset(actionList['del'])#>
  29. del as private _del;
  30. <#endif#>
  31. <#if isset(actionList['multi'])#>
  32. multi as private _multi;
  33. <#endif#>
  34. <#if isset(actionList['import'])#>
  35. import as private _import;
  36. <#endif#>
  37. <#if isset(actionList['download'])#>
  38. download as private _download;
  39. <#endif#>
  40. <#if isset(actionList['recyclebin'])#>
  41. recyclebin as private _recyclebin;
  42. <#endif#>
  43. }
  44. <#endif#>
  45. protected function _initialize()
  46. {
  47. parent::_initialize();
  48. $this->model = new <#modelName#>Model();
  49. }
  50. <#if table && isset(actionList['index'])#>
  51. //查看
  52. #[Route("GET,JSON","index")]
  53. public function index()
  54. {
  55. <#if relation#>
  56. $this->relationField=<#relation#>;
  57. <#endif#>
  58. <#if isTree || summary#>
  59. if (false === $this->request->isAjax()) {
  60. return $this->fetch();
  61. }
  62. if($this->request->post('selectpage')){
  63. return $this->selectpage();
  64. }
  65. [$where, $order, $limit, $with] = $this->buildparams();
  66. $list = $this->model
  67. ->withJoin($with,'left')
  68. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  69. //->with($with)
  70. ->where($where)
  71. ->order($order)
  72. ->paginate($limit);
  73. $result = [
  74. <#if summary#>
  75. 'summary' => '自定义统计信息',
  76. <#endif#>
  77. 'total' => $list->total(),
  78. <#if isTree#>
  79. 'rows' => $this->toTree($list->items())
  80. <#endif#>
  81. <#if !isTree#>
  82. 'rows' => $list->items()
  83. <#endif#>
  84. ];
  85. return json($result);
  86. <#endif#>
  87. <#if !isTree && !summary#>
  88. return $this->_index();
  89. <#endif#>
  90. }
  91. <#endif#>
  92. <#if isTree#>
  93. private function toTree($list)
  94. {
  95. $tree = Tree::instance();
  96. $tree->init($list, 'pid');
  97. return $tree->getTreeList($tree->getTreeArray(0), '<#treeTitle#>');
  98. }
  99. <#endif#>
  100. <#if form && isset(actionList['add'])#>
  101. //添加
  102. #[Route("GET,POST","add")]
  103. public function add()
  104. {
  105. //通过定义postParams来增加或覆盖post提交的表单
  106. $this->postParams=[];
  107. //通过定义callback回调函数来执行添加后的操作
  108. $this->callback=function ($model){};
  109. <#if isTree#>
  110. if(!$this->request->isPost()){
  111. $list=$this->model->select()->toArray();
  112. $parentList=$this->toTree($list);
  113. $this->assign('parentList',$parentList);
  114. }
  115. <#endif#>
  116. return $this->_add();
  117. }
  118. <#endif#>
  119. <#if form && isset(actionList['edit'])#>
  120. //修改
  121. #[Route("GET,POST","edit")]
  122. public function edit()
  123. {
  124. //通过定义postParams来增加或覆盖post提交的表单
  125. $this->postParams=[];
  126. //通过定义callback回调函数来执行修改后的操作
  127. $this->callback=function ($model){};
  128. <#if isTree#>
  129. if(!$this->request->isPost()){
  130. $list=$this->model->select()->toArray();
  131. $parentList=$this->toTree($list);
  132. $this->assign('parentList',$parentList);
  133. }
  134. <#endif#>
  135. return $this->_edit();
  136. }
  137. <#endif#>
  138. <#if table && isset(actionList['del'])#>
  139. //删除
  140. #[Route("GET,POST","del")]
  141. public function del()
  142. {
  143. //通过定义callback回调函数来执行删除后的操作
  144. $this->callback=function ($ids){};
  145. return $this->_del();
  146. }
  147. <#endif#>
  148. <#if table && isset(actionList['multi'])#>
  149. //更新
  150. #[Route("GET,POST","multi")]
  151. public function multi()
  152. {
  153. //通过定义callback回调函数来执行更新后的操作
  154. $this->callback=function ($ids,$field,$value){};
  155. return $this->_multi();
  156. }
  157. <#endif#>
  158. <#if table && isset(actionList['import'])#>
  159. //导入
  160. #[Route("GET,POST","import")]
  161. public function import()
  162. {
  163. //通过定义callback回调函数来处理导入的数据
  164. $this->callback=function ($inserData){
  165. return $inserData;
  166. };
  167. return $this->_import();
  168. }
  169. <#endif#>
  170. <#if table && isset(actionList['recyclebin'])#>
  171. //回收站
  172. #[Route("GET,POST,JSON","recyclebin")]
  173. public function recyclebin($action)
  174. {
  175. $this->recyclebinColumns=[
  176. <#recyclebinField#>
  177. ];
  178. $this->recyclebinColumnsType=[
  179. <#recyclebinType#>
  180. ];
  181. return $this->_recyclebin($action);
  182. }
  183. <#endif#>
  184. <#if table && isset(actionList['download'])#>
  185. //下载
  186. #[Route("GET,POST","download")]
  187. public function download()
  188. {
  189. //通过定义callback回调函数来处理下载的数据
  190. $this->callback=function ($downloadData){
  191. return $downloadData;
  192. };
  193. return $this->_download();
  194. }
  195. <#endif#>
  196. <#if methods#>
  197. <#methods#>
  198. <#endif#>
  199. }