controller-normal.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. <#if isset(actionList['settlement'])#>
  44. settlement as private _settlement;
  45. <#endif#>
  46. }
  47. <#endif#>
  48. protected function _initialize()
  49. {
  50. parent::_initialize();
  51. $this->model = new <#modelName#>Model();
  52. }
  53. <#if table && isset(actionList['index'])#>
  54. //查看
  55. #[Route("GET,JSON","index")]
  56. public function index()
  57. {
  58. <#if relation#>
  59. $this->relationField=<#relation#>;
  60. <#endif#>
  61. <#if isTree || summary#>
  62. if (false === $this->request->isAjax()) {
  63. return $this->fetch();
  64. }
  65. if($this->request->post('selectpage')){
  66. return $this->selectpage();
  67. }
  68. [$where, $order, $limit, $with] = $this->buildparams();
  69. $list = $this->model
  70. ->withJoin($with,'left')
  71. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  72. //->with($with)
  73. ->where($where)
  74. ->order($order)
  75. ->paginate($limit);
  76. $result = [
  77. <#if summary#>
  78. 'summary' => '自定义统计信息',
  79. <#endif#>
  80. 'total' => $list->total(),
  81. <#if isTree#>
  82. 'rows' => $this->toTree($list->items())
  83. <#endif#>
  84. <#if !isTree#>
  85. 'rows' => $list->items()
  86. <#endif#>
  87. ];
  88. return json($result);
  89. <#endif#>
  90. <#if !isTree && !summary#>
  91. return $this->_index();
  92. <#endif#>
  93. }
  94. <#endif#>
  95. <#if isTree#>
  96. private function toTree($list)
  97. {
  98. $tree = Tree::instance();
  99. $tree->init($list, 'pid');
  100. return $tree->getTreeList($tree->getTreeArray(0), '<#treeTitle#>');
  101. }
  102. <#endif#>
  103. <#if form && isset(actionList['add'])#>
  104. //添加
  105. #[Route("GET,POST","add")]
  106. public function add()
  107. {
  108. //通过定义postParams来增加或覆盖post提交的表单
  109. $this->postParams=[];
  110. //通过定义callback回调函数来执行添加后的操作
  111. $this->callback=function ($model){};
  112. <#if isTree#>
  113. if(!$this->request->isPost()){
  114. $list=$this->model->select()->toArray();
  115. $parentList=$this->toTree($list);
  116. $this->assign('parentList',$parentList);
  117. }
  118. <#endif#>
  119. return $this->_add();
  120. }
  121. <#endif#>
  122. <#if form && isset(actionList['edit'])#>
  123. //修改
  124. #[Route("GET,POST","edit")]
  125. public function edit()
  126. {
  127. //通过定义postParams来增加或覆盖post提交的表单
  128. $this->postParams=[];
  129. //通过定义callback回调函数来执行修改后的操作
  130. $this->callback=function ($model){};
  131. <#if isTree#>
  132. if(!$this->request->isPost()){
  133. $list=$this->model->select()->toArray();
  134. $parentList=$this->toTree($list);
  135. $this->assign('parentList',$parentList);
  136. }
  137. <#endif#>
  138. return $this->_edit();
  139. }
  140. <#endif#>
  141. <#if table && isset(actionList['del'])#>
  142. //删除
  143. #[Route("GET,POST","del")]
  144. public function del()
  145. {
  146. //通过定义callback回调函数来执行删除后的操作
  147. $this->callback=function ($ids){};
  148. return $this->_del();
  149. }
  150. <#endif#>
  151. <#if table && isset(actionList['multi'])#>
  152. //更新
  153. #[Route("GET,POST","multi")]
  154. public function multi()
  155. {
  156. //通过定义callback回调函数来执行更新后的操作
  157. $this->callback=function ($ids,$field,$value){};
  158. return $this->_multi();
  159. }
  160. <#endif#>
  161. <#if table && isset(actionList['import'])#>
  162. //导入
  163. #[Route("GET,POST","import")]
  164. public function import()
  165. {
  166. //通过定义callback回调函数来处理导入的数据
  167. $this->callback=function ($inserData){
  168. return $inserData;
  169. };
  170. return $this->_import();
  171. }
  172. <#endif#>
  173. <#if table && isset(actionList['recyclebin'])#>
  174. //回收站
  175. #[Route("GET,POST,JSON","recyclebin")]
  176. public function recyclebin($action)
  177. {
  178. $this->recyclebinColumns=[
  179. <#recyclebinField#>
  180. ];
  181. $this->recyclebinColumnsType=[
  182. <#recyclebinType#>
  183. ];
  184. return $this->_recyclebin($action);
  185. }
  186. <#endif#>
  187. <#if table && isset(actionList['download'])#>
  188. //下载
  189. #[Route("GET,POST","download")]
  190. public function download()
  191. {
  192. //通过定义callback回调函数来处理下载的数据
  193. $this->callback=function ($downloadData){
  194. return $downloadData;
  195. };
  196. return $this->_download();
  197. }
  198. <#endif#>
  199. <#if methods#>
  200. <#if table && isset(actionList['settlement'])#>
  201. //结算
  202. #[Route("GET,POST","settlement")]
  203. public function settlement()
  204. {
  205. //通过定义callback回调函数来执行删除后的操作
  206. $this->callback=function ($ids){};
  207. return $this->_settlement();
  208. }
  209. <#endif#>
  210. <#methods#>
  211. <#endif#>
  212. }