Page.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\store\controller\wxapp;
  3. use app\store\controller\Controller;
  4. use app\store\model\Category as CategoryModel;
  5. use app\store\model\sharing\Category as SharingCategoryModel;
  6. use app\store\model\article\Category as ArticleCategoryModel;
  7. use app\store\model\WxappPage as WxappPageModel;
  8. use app\store\model\WxappCategory as WxappCategoryModel;
  9. /**
  10. * 小程序页面管理
  11. * Class Page
  12. * @package app\store\controller\wxapp
  13. */
  14. class Page extends Controller
  15. {
  16. /**
  17. * 页面列表
  18. * @return mixed
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function index()
  24. {
  25. $model = new WxappPageModel;
  26. $list = $model->getList();
  27. return $this->fetch('index', compact('list'));
  28. }
  29. /**
  30. * 新增页面
  31. * @return array|mixed
  32. */
  33. public function add()
  34. {
  35. $model = new WxappPageModel;
  36. if (!$this->request->isAjax()) {
  37. return $this->fetch('edit', [
  38. 'defaultData' => json_encode($model->getDefaultItems()),
  39. 'jsonData' => json_encode(['page' => $model->getDefaultPage(), 'items' => []]),
  40. 'opts' => json_encode([
  41. 'catgory' => CategoryModel::getCacheTree(),
  42. 'sharingCatgory' => SharingCategoryModel::getCacheTree(),
  43. 'articleCatgory' => ArticleCategoryModel::getALL(),
  44. ])
  45. ]);
  46. }
  47. // 接收post数据
  48. $post = $this->request->post('data', null, null);
  49. if (!$model->add(json_decode($post, true))) {
  50. return $this->renderError('添加失败');
  51. }
  52. return $this->renderSuccess('添加成功', url('wxapp.page/index'));
  53. }
  54. /**
  55. * 编辑页面
  56. * @param $page_id
  57. * @return array|mixed
  58. * @throws \think\exception\DbException
  59. */
  60. public function edit($page_id)
  61. {
  62. $model = WxappPageModel::detail($page_id);
  63. if (!$this->request->isAjax()) {
  64. return $this->fetch('edit', [
  65. 'defaultData' => json_encode($model->getDefaultItems()),
  66. 'jsonData' => json_encode($model['page_data']),
  67. 'opts' => json_encode([
  68. 'catgory' => CategoryModel::getCacheTree(),
  69. 'sharingCatgory' => SharingCategoryModel::getCacheTree(),
  70. 'articleCatgory' => ArticleCategoryModel::getALL(),
  71. ])
  72. ]);
  73. }
  74. // 接收post数据
  75. $post = $this->request->post('data', null, null);
  76. if (!$model->edit(json_decode($post, true))) {
  77. return $this->renderError('更新失败');
  78. }
  79. return $this->renderSuccess('更新成功');
  80. }
  81. /**
  82. * 删除页面
  83. * @param $page_id
  84. * @return array
  85. * @throws \think\exception\DbException
  86. */
  87. public function delete($page_id)
  88. {
  89. // 帮助详情
  90. $model = WxappPageModel::detail($page_id);
  91. if (!$model->setDelete()) {
  92. return $this->renderError($model->getError() ?: '删除失败');
  93. }
  94. return $this->renderSuccess('删除成功');
  95. }
  96. /**
  97. * 设置默认首页
  98. * @param $page_id
  99. * @return array
  100. * @throws \think\exception\DbException
  101. */
  102. public function setHome($page_id)
  103. {
  104. // 帮助详情
  105. $model = WxappPageModel::detail($page_id);
  106. if (!$model->setHome()) {
  107. return $this->renderError($model->getError() ?: '设置失败');
  108. }
  109. return $this->renderSuccess('设置成功');
  110. }
  111. /**
  112. * 分类模板
  113. * @return array|mixed
  114. * @throws \think\exception\DbException
  115. */
  116. public function category()
  117. {
  118. $model = WxappCategoryModel::detail();
  119. if ($this->request->isAjax()) {
  120. if ($model->edit($this->postData('category'))) {
  121. return $this->renderSuccess('更新成功');
  122. }
  123. return $this->renderError($model->getError() ?: '更新失败');
  124. }
  125. return $this->fetch('category', compact('model'));
  126. }
  127. /**
  128. * 页面链接
  129. * @return mixed
  130. */
  131. public function links()
  132. {
  133. return $this->fetch('links');
  134. }
  135. }