Template.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace app\admin\controller\template;
  3. use app\admin\model\template\TemplateModel;
  4. use app\admin\service\template\TemplateService;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\Session;
  11. /**
  12. * 模板
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Template extends Backend
  17. {
  18. protected $model = null;
  19. protected $service = null;
  20. /**
  21. * 无需登录的方法,同时也就不需要鉴权了
  22. * @var array
  23. */
  24. protected $noNeedLogin = ['publicparentindex','publicchildindex'];
  25. /**
  26. * 检查类型
  27. * @var array
  28. */
  29. protected $classList = ['CT' => 'CT', 'CR' => 'CR', 'DR' => 'DR', 'MR' => 'MR'];
  30. /**
  31. * 快速搜索时执行查找的字段
  32. */
  33. protected $searchFields = ['title'];
  34. public function _initialize()
  35. {
  36. parent::_initialize();
  37. $this->model = new TemplateModel();
  38. $this->service = new TemplateService();
  39. }
  40. /**
  41. * 公有模板列表
  42. */
  43. public function index()
  44. { //设置过滤方法
  45. $this->request->filter(['strip_tags']);
  46. return $this->view->fetch('public_index');
  47. }
  48. /**
  49. * 公有模板父级列表
  50. * @return string|\think\response\Json
  51. * @throws \think\Exception
  52. * @author matielong
  53. */
  54. public function publicParentIndex()
  55. {
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags']);
  58. if ($this->request->isAjax()) {
  59. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  60. $my_where = [
  61. 'is_public' => 1,
  62. 'parent_id' => 0
  63. ];
  64. $total = $this->model
  65. ->where($where)
  66. ->where($my_where)
  67. ->order($sort, $order)
  68. ->count();
  69. $list = $this->model
  70. ->where($where)
  71. ->where($my_where)
  72. ->order($sort, $order)
  73. ->limit($offset, $limit)
  74. ->select();
  75. $list = collection($list)->toArray();
  76. $result = array("total" => $total, "rows" => $list);
  77. return json($result);
  78. }
  79. $siteList = [['id' => '1', 'title' => '公有模板', 'active' => 1], ['id' => '2', 'title' => '私有模板',],];
  80. $this->view->assign('siteList',$siteList);
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 公有模板父级添加
  85. * @return string
  86. * @throws Exception
  87. * @author matielong
  88. */
  89. public function publicAddParent()
  90. {
  91. if ($this->request->isPost()) {
  92. $params = $this->request->post("row/a");
  93. if ($params) {
  94. $params = $this->preExcludeFields($params);
  95. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  96. $params[$this->dataLimitField] = $this->auth->id;
  97. }
  98. $result = false;
  99. Db::startTrans();
  100. try {
  101. //是否采用模型验证
  102. if ($this->modelValidate) {
  103. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  104. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  105. $this->model->validateFailException(true)->validate($validate);
  106. }
  107. $user = Session::get('admin');
  108. $replenish = [
  109. 'id' => makeNew16Uid(),
  110. 'parent_id' => 0,
  111. 'is_public' => 1,
  112. 'createdAt' => date('Y-m-d H:i:s'),
  113. 'create_user' => $user['id']
  114. ];
  115. $params = array_merge($replenish,$params);
  116. $result = $this->model->allowField(true)->save($params);
  117. Db::commit();
  118. } catch (ValidateException $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. } catch (PDOException $e) {
  122. Db::rollback();
  123. $this->error($e->getMessage());
  124. } catch (Exception $e) {
  125. Db::rollback();
  126. $this->error($e->getMessage());
  127. }
  128. if ($result !== false) {
  129. $this->success();
  130. } else {
  131. $this->error(__('No rows were inserted'));
  132. }
  133. }
  134. $this->error(__('Parameter %s can not be empty', ''));
  135. }
  136. $this->view->assign('classList',$this->classList);
  137. return $this->view->fetch();
  138. }
  139. /**
  140. * 公有模板父级编辑
  141. * @param null $ids
  142. * @return string
  143. * @throws Exception
  144. * @author matielong
  145. */
  146. public function publicEditParent($ids = null)
  147. {
  148. $row = $this->model->get($ids);
  149. if (!$row) {
  150. $this->error(__('No Results were found'));
  151. }
  152. $adminIds = $this->getDataLimitAdminIds();
  153. if (is_array($adminIds)) {
  154. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  155. $this->error(__('You have no permission'));
  156. }
  157. }
  158. if ($this->request->isPost()) {
  159. $params = $this->request->post("row/a");
  160. if ($params) {
  161. $params = $this->preExcludeFields($params);
  162. $result = false;
  163. Db::startTrans();
  164. try {
  165. //是否采用模型验证
  166. if ($this->modelValidate) {
  167. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  168. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  169. $row->validateFailException(true)->validate($validate);
  170. }
  171. $result = $row->allowField(true)->save($params);
  172. Db::commit();
  173. } catch (ValidateException $e) {
  174. Db::rollback();
  175. $this->error($e->getMessage());
  176. } catch (PDOException $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. } catch (Exception $e) {
  180. Db::rollback();
  181. $this->error($e->getMessage());
  182. }
  183. if ($result !== false) {
  184. $this->success();
  185. } else {
  186. $this->error(__('No rows were updated'));
  187. }
  188. }
  189. $this->error(__('Parameter %s can not be empty', ''));
  190. }
  191. $this->view->assign('classList',$this->classList);
  192. $this->view->assign("row", $row);
  193. return $this->view->fetch();
  194. }
  195. /**
  196. * 公有模板父级删除
  197. * @param string $ids
  198. * @author matielong
  199. */
  200. public function publicDelParent($ids = "")
  201. {
  202. if ($ids) {
  203. $pk = $this->model->getPk();
  204. $adminIds = $this->getDataLimitAdminIds();
  205. if (is_array($adminIds)) {
  206. $this->model->where($this->dataLimitField, 'in', $adminIds);
  207. }
  208. // 是否存在子级
  209. $child = $this->model
  210. ->where($pk, 'in', $ids)
  211. ->where('parent_id','in',$ids)
  212. ->select();
  213. if($child){
  214. $this->error('存在子级模板,无法删除');
  215. }
  216. $list = $this->model->where($pk, 'in', $ids)->select();
  217. $count = 0;
  218. Db::startTrans();
  219. try {
  220. foreach ($list as $k => $v) {
  221. $count += $v->delete();
  222. }
  223. Db::commit();
  224. } catch (PDOException $e) {
  225. Db::rollback();
  226. $this->error($e->getMessage());
  227. } catch (Exception $e) {
  228. Db::rollback();
  229. $this->error($e->getMessage());
  230. }
  231. if ($count) {
  232. $this->success();
  233. } else {
  234. $this->error(__('No rows were deleted'));
  235. }
  236. }
  237. $this->error(__('Parameter %s can not be empty', 'ids'));
  238. }
  239. /**
  240. * 公有模板子级列表
  241. * @return string|\think\response\Json
  242. * @throws \think\Exception
  243. * @author matielong
  244. */
  245. public function publicChildIndex()
  246. {
  247. $pid = $this->request->param('pid');
  248. //设置过滤方法
  249. $this->request->filter(['strip_tags']);
  250. if ($this->request->isAjax()) {
  251. list($where, $sort, $order, $offset, $limit) = $this->buildparams($this->searchFields, true);
  252. $my_where = [
  253. 'is_public' => 1,
  254. 'parent_id' => $pid
  255. ];
  256. $total = $this->model->alias('tmp')
  257. ->where($where)
  258. ->where($my_where)
  259. ->order($sort, $order)
  260. ->count();
  261. $list = $this->model->alias('tmp')
  262. ->where($where)
  263. ->where($my_where)
  264. ->order($sort, $order)
  265. ->limit($offset, $limit)
  266. ->select();
  267. $list = collection($list)->toArray();
  268. $result = array("total" => $total, "rows" => $list);
  269. return json($result);
  270. }
  271. return $this->view->fetch();
  272. }
  273. /**
  274. * 公有模板子级添加
  275. * @param $ids
  276. * @return string
  277. * @throws Exception
  278. * @author matielong
  279. */
  280. public function publicAddChild($ids)
  281. {
  282. $pid = $ids;
  283. if ($this->request->isPost()) {
  284. $params = $this->request->post("row/a");
  285. if ($params) {
  286. $params = $this->preExcludeFields($params);
  287. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  288. $params[$this->dataLimitField] = $this->auth->id;
  289. }
  290. $result = false;
  291. Db::startTrans();
  292. try {
  293. //是否采用模型验证
  294. if ($this->modelValidate) {
  295. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  296. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  297. $this->model->validateFailException(true)->validate($validate);
  298. }
  299. $user = Session::get('admin');
  300. $replenish = [
  301. 'id' => makeNew16Uid(),
  302. 'parent_id' => $pid,
  303. 'is_public' => 1,
  304. 'createdAt' => date('Y-m-d H:i:s'),
  305. 'create_user' => $user['id']
  306. ];
  307. $params = array_merge($replenish,$params);
  308. $result = $this->model->allowField(true)->save($params);
  309. Db::commit();
  310. } catch (ValidateException $e) {
  311. Db::rollback();
  312. $this->error($e->getMessage());
  313. } catch (PDOException $e) {
  314. Db::rollback();
  315. $this->error($e->getMessage());
  316. } catch (Exception $e) {
  317. Db::rollback();
  318. $this->error($e->getMessage());
  319. }
  320. if ($result !== false) {
  321. $this->success();
  322. } else {
  323. $this->error(__('No rows were inserted'));
  324. }
  325. }
  326. $this->error(__('Parameter %s can not be empty', ''));
  327. }
  328. $this->view->assign('classList',$this->classList);
  329. return $this->view->fetch();
  330. }
  331. /**
  332. * 公有模板子级修改
  333. * @param null $ids
  334. * @return string
  335. * @throws Exception
  336. * @author matielong
  337. */
  338. public function publicEditChild($ids = null)
  339. {
  340. $row = $this->model->get($ids);
  341. if (!$row) {
  342. $this->error(__('No Results were found'));
  343. }
  344. $adminIds = $this->getDataLimitAdminIds();
  345. if (is_array($adminIds)) {
  346. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  347. $this->error(__('You have no permission'));
  348. }
  349. }
  350. if ($this->request->isPost()) {
  351. $params = $this->request->post("row/a");
  352. if ($params) {
  353. $params = $this->preExcludeFields($params);
  354. $result = false;
  355. Db::startTrans();
  356. try {
  357. //是否采用模型验证
  358. if ($this->modelValidate) {
  359. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  360. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  361. $row->validateFailException(true)->validate($validate);
  362. }
  363. $result = $row->allowField(true)->save($params);
  364. Db::commit();
  365. } catch (ValidateException $e) {
  366. Db::rollback();
  367. $this->error($e->getMessage());
  368. } catch (PDOException $e) {
  369. Db::rollback();
  370. $this->error($e->getMessage());
  371. } catch (Exception $e) {
  372. Db::rollback();
  373. $this->error($e->getMessage());
  374. }
  375. if ($result !== false) {
  376. $this->success();
  377. } else {
  378. $this->error(__('No rows were updated'));
  379. }
  380. }
  381. $this->error(__('Parameter %s can not be empty', ''));
  382. }
  383. $this->view->assign('classList',$this->classList);
  384. $this->view->assign("row", $row);
  385. return $this->view->fetch();
  386. }
  387. }