控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Category.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 栏目模型
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\model\cms;
  15. use app\admin\model\cms\CategoryPriv;
  16. use think\Db;
  17. use think\facade\Cache;
  18. use think\Model;
  19. /**
  20. * 模型
  21. */
  22. class Category extends Model
  23. {
  24. protected static $arrparent;
  25. protected static $arrchild;
  26. protected $auto = ['setting'];
  27. public function setSettingAttr($value)
  28. {
  29. return is_array($value) ? serialize($value) : $value;
  30. }
  31. public static function init()
  32. {
  33. $cmsConfig = get_addon_config("cms");
  34. self::afterWrite(function ($row) use ($cmsConfig) {
  35. if (isset($cmsConfig['web_site_weburlpush']) && $cmsConfig['web_site_weburlpush']) {
  36. hook("weburlpush", buildCatUrl($row->id, '', true, true));
  37. }
  38. if (isset($row['priv_groupid'])) {
  39. CategoryPriv::update_priv($row->id, $row->priv_groupid, 0);
  40. }
  41. });
  42. self::afterInsert(function ($row) {
  43. cache('Category', null);
  44. if ($row['type'] == 1) {
  45. //增加默认单页
  46. (new Page)->savePage([
  47. 'catid' => $row->id,
  48. 'title' => $row->catname,
  49. ]);
  50. }
  51. });
  52. self::beforeUpdate(function ($row) {
  53. //应用模板到所有子栏目
  54. if (isset($row['template_child']) && $row['template_child']) {
  55. $idstr = self::get_arrchildid($row->id);
  56. $data = $setting = unserialize($row->setting);
  57. $setting['category_template'] = $data['category_template'] ?? '';
  58. $setting['list_template'] = $data['list_template'] ?? '';
  59. $setting['show_template'] = $data['show_template'] ?? '';
  60. $setting['page_template'] = $data['page_template'] ?? '';
  61. Db::name('category')->where('id', 'in', $idstr)->update(['setting' => serialize($setting)]);
  62. }
  63. });
  64. self::afterUpdate(function ($row) {
  65. $changedData = $row->getChangedData();
  66. //其他类型改为单页也要新增单页
  67. if ($row['type'] == 1 && isset($changedData['type'])) {
  68. //增加默认单页
  69. (new Page)->savePage([
  70. 'catid' => $row->id,
  71. 'title' => $row->catname,
  72. ]);
  73. }
  74. //更新栏目缓存
  75. cache('Category', null);
  76. getCategory($row->id, '', true);
  77. });
  78. self::beforeDelete(function ($row) {
  79. //是否存在子栏目
  80. if (self::where('parentid', $row->id)->find()) {
  81. throw new \Exception("栏目含有子栏目,不得删除!");
  82. }
  83. $catInfo = self::get($row->id);
  84. //检查是否存在数据,存在数据不执行删除
  85. if ($catInfo['modelid'] && $catInfo['type'] == 2) {
  86. $tbname = ucwords(getModel($catInfo['modelid'], 'tablename'));
  87. if ($tbname && Db::name($tbname)->where(['catid' => $row->id])->find()) {
  88. throw new \Exception("栏目含有信息,不得删除!");
  89. }
  90. } elseif ($catInfo['type'] == 1) {
  91. Db::name('page')->where(['catid' => $row->id])->delete();
  92. }
  93. });
  94. }
  95. /**
  96. *
  97. * 获取父栏目ID列表
  98. * @param integer $catid 栏目ID
  99. * @param array $arrparentid 父目录ID
  100. * @param integer $n 查找的层次
  101. */
  102. public static function get_arrparentid($catid, $arrparentid = '', $n = 1)
  103. {
  104. if (empty(self::$arrparent)) {
  105. self::$arrparent = cache('Category');
  106. }
  107. if ($n > 10 || !is_array(self::$arrparent) || !isset(self::$arrparent[$catid])) {
  108. return false;
  109. }
  110. //获取当前栏目的上级栏目ID
  111. $parentid = self::$arrparent[$catid]['parentid'];
  112. //所有父ID
  113. $arrparentid = $arrparentid ? $parentid . ',' . $arrparentid : $parentid;
  114. if ($parentid) {
  115. $arrparentid = self::get_arrparentid($parentid, $arrparentid, ++$n);
  116. } else {
  117. self::$arrparent[$catid]['arrparentid'] = $arrparentid;
  118. }
  119. return (string) $arrparentid;
  120. }
  121. /**
  122. *
  123. * 获取子栏目ID列表
  124. * @param $catid 栏目ID
  125. */
  126. public static function get_arrchildid($catid)
  127. {
  128. if (!self::$arrchild) {
  129. self::$arrchild = cache('Category');
  130. }
  131. $arrchildid = $catid;
  132. if (is_array(self::$arrchild)) {
  133. foreach (self::$arrchild as $id => $cat) {
  134. if ($cat['parentid'] && $id != $catid && $cat['parentid'] == $catid) {
  135. $arrchildid .= ',' . self::get_arrchildid($id);
  136. }
  137. }
  138. }
  139. return (string) $arrchildid;
  140. }
  141. }