Page.php 4.4 KB

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