Sitemap.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\Sitemap as SitemapInfo;
  11. use app\admin\model\SitemapCate;
  12. use app\admin\validate\SitemapCateCheck;
  13. use app\admin\validate\SitemapCheck;
  14. use think\exception\ValidateException;
  15. use think\facade\Db;
  16. use think\facade\View;
  17. class Sitemap extends BaseController
  18. {
  19. public function index()
  20. {
  21. if (request()->isAjax()) {
  22. $param = get_params();
  23. $where = array();
  24. if (!empty($param['keywords'])) {
  25. $where[] = ['id|name', 'like', '%' . $param['keywords'] . '%'];
  26. }
  27. $where[] = ['status', '=', 1];
  28. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  29. $SitemapCate = SitemapCate::where($where)
  30. ->order('sort desc, id desc')
  31. ->paginate($rows, false, ['query' => $param]);
  32. return table_assign(0, '', $SitemapCate);
  33. } else {
  34. return view();
  35. }
  36. }
  37. //添加
  38. public function add()
  39. {
  40. $param = get_params();
  41. if (request()->isAjax()) {
  42. if (!empty($param['id']) && $param['id'] > 0) {
  43. try {
  44. validate(SitemapCateCheck::class)->scene('edit')->check($param);
  45. } catch (ValidateException $e) {
  46. // 验证失败 输出错误信息
  47. return to_assign(1, $e->getError());
  48. }
  49. $param['update_time'] = time();
  50. $res = SitemapCate::where('id', $param['id'])->strict(false)->field(true)->update($param);
  51. if ($res) {
  52. add_log('edit', $param['id'], $param);
  53. }
  54. // 删除菜单缓存
  55. clear_cache('homeSitemap');
  56. return to_assign();
  57. } else {
  58. try {
  59. validate(SitemapCateCheck::class)->scene('add')->check($param);
  60. } catch (ValidateException $e) {
  61. // 验证失败 输出错误信息
  62. return to_assign(1, $e->getError());
  63. }
  64. $param['create_time'] = time();
  65. $sid = SitemapCate::strict(false)->field(true)->insertGetId($param);
  66. if ($sid) {
  67. add_log('add', $sid, $param);
  68. }
  69. // 删除菜单缓存
  70. clear_cache('homeSitemap');
  71. return to_assign();
  72. }
  73. }
  74. else{
  75. $id = isset($param['id']) ? $param['id'] : 0;
  76. if ($id > 0) {
  77. $cate = Db::name('SitemapCate')->where(['id' => $id])->find();
  78. View::assign('cate', $cate);
  79. }
  80. View::assign('id', $id);
  81. return view();
  82. }
  83. }
  84. //删除
  85. public function delete()
  86. {
  87. $id = get_params("id");
  88. $where[] = ['sitemap_cate_id', '=', $id];
  89. $where[] = ['status', '>=', 0];
  90. $count = Db::name('Sitemap')->where($where)->count();
  91. if ($count > 0) {
  92. return to_assign(1, '该分类下还有数据,无法删除');
  93. }
  94. $data['status'] = '-1';
  95. $data['id'] = $id;
  96. $data['update_time'] = time();
  97. if (Db::name('SitemapCate')->update($data) !== false) {
  98. add_log('delete', $id);
  99. return to_assign(0, "删除成功");
  100. } else {
  101. return to_assign(1, "删除失败");
  102. }
  103. }
  104. //管理网站地图
  105. public function sitemap_info()
  106. {
  107. $param = get_params();
  108. if (request()->isAjax()) {
  109. $where = array();
  110. $where[] = ['sitemap_cate_id', '=', $param['id']];
  111. $where[] = ['status', '>=', 0];
  112. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  113. $sitemap = SitemapInfo::where($where)
  114. ->order('sort desc, id desc')
  115. ->paginate($rows, false, ['query' => $param]);
  116. return table_assign(0, '', $sitemap);
  117. } else {
  118. return view('', [
  119. 'sitemap_cate_id' => $param['id'],
  120. ]);
  121. }
  122. }
  123. //添加网站地图
  124. public function sitemap_info_add()
  125. {
  126. $param = get_params();
  127. if (request()->isAjax()) {
  128. if (!empty($param['id']) && $param['id'] > 0) {
  129. try {
  130. validate(SitemapCheck::class)->scene('edit')->check($param);
  131. } catch (ValidateException $e) {
  132. // 验证失败 输出错误信息
  133. return to_assign(1, $e->getError());
  134. }
  135. $param['update_time'] = time();
  136. $param['name'] = htmlspecialchars($param['name']);
  137. $res = SitemapInfo::where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  138. if ($res) {
  139. add_log('edit', $param['id'], $param);
  140. }
  141. // 删除导航缓存
  142. clear_cache('homeSitemap');
  143. return to_assign();
  144. } else {
  145. try {
  146. validate(SitemapCheck::class)->scene('add')->check($param);
  147. } catch (ValidateException $e) {
  148. // 验证失败 输出错误信息
  149. return to_assign(1, $e->getError());
  150. }
  151. $param['name'] = htmlspecialchars($param['name']);
  152. $param['create_time'] = time();
  153. $sid = SitemapInfo::strict(false)->field(true)->insertGetId($param);
  154. if ($sid) {
  155. add_log('add', $sid, $param);
  156. }
  157. // 删除导航缓存
  158. clear_cache('homeSitemap');
  159. return to_assign();
  160. }
  161. }
  162. else{
  163. $id = isset($param['id']) ? $param['id'] : 0;
  164. $sitemap_cate_id = isset($param['cid']) ? $param['cid'] : 0;
  165. if ($id > 0) {
  166. $sitemap = Db::name('Sitemap')->where(['id' => $id])->find();
  167. View::assign('sitemap', $sitemap);
  168. }
  169. View::assign('id', $id);
  170. View::assign('sitemap_cate_id', $sitemap_cate_id);
  171. return view();
  172. }
  173. }
  174. //删除网站地图
  175. public function sitemap_info_delete()
  176. {
  177. $id = get_params("id");
  178. $data['status'] = '-1';
  179. $data['id'] = $id;
  180. $data['update_time'] = time();
  181. if (Db::name('Sitemap')->update($data) !== false) {
  182. add_log('delete', $id);
  183. return to_assign(0, "删除成功");
  184. } else {
  185. return to_assign(1, "删除失败");
  186. }
  187. }
  188. }