AdminController.php.tp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. namespace app\admin\controller;
  2. use app\admin\controller\base\Permissions;
  3. use think\Db;
  4. class {$humpName} extends Permissions
  5. {
  6. private function getModel()
  7. {
  8. return new \app\common\model\{$humpName}();
  9. }
  10. public function index()
  11. {
  12. if ($this->request->isAjax()) {
  13. $post = $this->request->param();
  14. $where = [];
  15. {volist name="$fieldsInfo" id="vo"}
  16. {php}
  17. if(empty($vo['ShowSearch'])){
  18. continue;
  19. }
  20. {/php}
  21. {if condition="$vo['Field'] == 'id'"}
  22. if (isset($post['ids']) and !empty($post['ids'])) {
  23. $where['id'] = ['in', $post['ids']];
  24. }
  25. {php}continue;{/php}
  26. {/if}
  27. {if condition="in_array($vo['Component'],[0,3,4,7])"}
  28. {php}//输入框,文章段落,图片,复选框{/php}
  29. if (!empty($post["{$vo['Field']}"])) {
  30. $where["{$vo['Field']}"] = ['like', '%' . $post["{$vo['Field']}"] . '%'];
  31. }
  32. {php}continue;{/php}
  33. {/if}
  34. {if condition="in_array($vo['Component'],[1,5,6,8])"}
  35. {php}//数字,开关,下拉框,单选{/php}
  36. if (isset($post["{$vo['Field']}"]) and ""!=$post["{$vo['Field']}"]) {
  37. $where["{$vo['Field']}"] = $post["{$vo['Field']}"];
  38. }
  39. {php}continue;{/php}
  40. {/if}
  41. {if condition="$vo['Component'] == 2"}
  42. {php}//时间{/php}
  43. if (isset($post["{$vo['Field']}"]) and !empty($post["{$vo['Field']}"])) {
  44. $timerang = explode(' - ', $post["{$vo['Field']}"]);
  45. $min_time = strtotime($timerang[0]);
  46. $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
  47. $where["{$vo['Field']}"] = [['>=', $min_time], ['<=', $max_time]];
  48. }
  49. {php}continue;{/php}
  50. {/if}
  51. {/volist}
  52. $model = $this->getModel();
  53. $count = $model->where($where)->count();
  54. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order({if condition="array_key_exists('sort',$fieldsInfo)"}'sort desc'{else/}'id desc'{/if})->select();
  55. {volist name="$fieldsInfo" id="vo"}
  56. {if condition="$vo['Component'] == 4"}
  57. foreach ($data as $key => $value) {
  58. $value['{$vo['Field']}_url'] = geturl($value['{$vo['Field']}']);
  59. $data[$key] = $value;
  60. }
  61. {/if}
  62. {/volist}
  63. return array('code' => 0, 'count' => $count, 'data' => $data);
  64. } else {
  65. return $this->fetch();
  66. }
  67. }
  68. {if condition="array_key_exists('update',$crud) or array_key_exists('create',$crud)"}
  69. public function publish()
  70. {
  71. $id = $this->request->param('id', 0, 'intval');
  72. $model = $this->getModel();
  73. $post = $this->request->post();
  74. if ($this->request->isPost()) {
  75. {volist name="$fieldsInfo" id="vo"}
  76. {php}
  77. if(empty($vo['ShowEdit'])){
  78. continue;
  79. }
  80. if(in_array($vo['Field'],['id'])){
  81. continue;
  82. }
  83. {/php}
  84. {if condition="$vo['Component'] == 2"}
  85. if (isset($post['{$vo['Field']}']) && !empty($post['{$vo['Field']}'])) {
  86. $post['{$vo['Field']}'] = strtotime($post['{$vo['Field']}']);
  87. }
  88. {/if}
  89. {if condition="$vo['Component'] == 7"}
  90. if (isset($post['{$vo['Field']}']) && !empty($post['{$vo['Field']}'])) {
  91. $post['{$vo['Field']}'] = implode(',',$post['{$vo['Field']}']);
  92. }
  93. {/if}
  94. {/volist}
  95. //验证
  96. $validate = new \think\Validate([
  97. {volist name="$fieldsInfo" id="vo"}
  98. {php}
  99. if(empty($vo['ShowEdit'])){
  100. continue;
  101. }
  102. $field_key = $vo['Field'];
  103. if(in_array($field_key,['id','create_time','update_time'])){
  104. continue;
  105. }
  106. if(!empty($vo['Comment'])){
  107. $comment = $vo['Comment'];
  108. if(in_array($vo['Component'],[5,6,7,8])){
  109. $arr = explode(':',$vo['Comment']);
  110. $comment = count($arr)==2 ? $arr[0] : $vo['Field'];
  111. }
  112. $field_key.= "|" . $comment;
  113. }
  114. $field_val = "";
  115. if($vo['Default'] === null){
  116. $field_val.= "require";
  117. }
  118. if(startWith($vo["Type"],'int') || startWith($vo["Type"],'tinyint')){
  119. $field_val .= empty($field_val) ? "number" : "|number";
  120. }
  121. if(startWith($vo["Type"],'varchar')){
  122. $maxLen = str_replace(['varchar(',')'],['',''],$vo["Type"]);
  123. $field_val .= empty($field_val) ? "max:".$maxLen : "|max:".$maxLen;
  124. }
  125. {/php}
  126. {notempty name="$field_val"}
  127. ['{$field_key}', '{$field_val}'],
  128. {/notempty}
  129. {/volist}
  130. ]);
  131. if (!$validate->check($post)) {
  132. $this->error('提交失败:' . $validate->getError());
  133. }
  134. }
  135. if ($id > 0) {
  136. {if condition="array_key_exists('update',$crud)"}
  137. //修改
  138. $data = $model->where('id', $id)->find();
  139. if (empty($data)) {
  140. $this->error('id不正确');
  141. }
  142. if ($this->request->isPost()) {
  143. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  144. $this->error('修改失败');
  145. } else {
  146. $this->success('修改成功');
  147. }
  148. } else {
  149. $this->assign('data', $data);
  150. return $this->fetch();
  151. }
  152. {/if}
  153. } else {
  154. {if condition="array_key_exists('create',$crud)"}
  155. //新增
  156. if ($this->request->isPost()) {
  157. if (false == $model->allowField(true)->save($post)) {
  158. $this->error('添加失败');
  159. } else {
  160. $this->success('添加成功', 'index');
  161. }
  162. } else {
  163. return $this->fetch();
  164. }
  165. {/if}
  166. }
  167. }
  168. {/if}
  169. {if condition="array_key_exists('delete',$crud)"}
  170. public function delete()
  171. {
  172. if ($this->request->isAjax()) {
  173. $id = $this->request->param('id', 0, 'intval');
  174. if (false == $this->getModel()->where('id', $id)->delete()) {
  175. $this->error('删除失败');
  176. } else {
  177. $this->success('删除成功', 'index');
  178. }
  179. }
  180. }
  181. public function deletes()
  182. {
  183. if ($this->request->isAjax()) {
  184. $post = $this->request->param();
  185. $ids = $post['ids'];
  186. if ($this->getModel()->where('id', 'in', $ids)->delete()) {
  187. $this->success('删除成功');
  188. }
  189. }
  190. }
  191. {/if}
  192. {volist name="$fieldsInfo" id="vo"}
  193. {if condition="$vo['Component'] == 5"}
  194. public function {$vo['Field']}()
  195. {
  196. if ($this->request->isPost()) {
  197. $post = $this->request->post();
  198. if (false == $this->getModel()->where('id', $post['id'])->update(['{$vo['Field']}' => $post['status']])) {
  199. $this->error('设置失败');
  200. } else {
  201. $this->success('设置成功', 'index');
  202. }
  203. }
  204. }
  205. {/if}
  206. {/volist}
  207. {if condition="array_key_exists('sort',$fieldsInfo)"}
  208. public function sort()
  209. {
  210. if ($this->request->isPost()) {
  211. $post = $this->request->post();
  212. $i = 0;
  213. foreach ($post['ids'] as $k => $id) {
  214. $sort = Db::name('{$underLineName}')->where('id', $id)->value('sort');
  215. $newsort = $post['sorts'][$k]??$sort;
  216. if ($sort != $newsort) {
  217. if (false == Db::name('{$underLineName}')->where('id', $id)->update(['sort' => $newsort])) {
  218. $this->error('更新失败');
  219. } else {
  220. $i++;
  221. }
  222. }
  223. }
  224. $this->success('成功更新' . $i . '个数据', 'index');
  225. }
  226. }
  227. {/if}
  228. }