Special.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\admin\model\cms;
  3. use addons\cms\library\Service;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. class Special extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $name = 'cms_special';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = 'deletetime';
  17. // 追加属性
  18. protected $append = [
  19. 'url',
  20. 'fullurl',
  21. 'flag_text',
  22. 'status_text'
  23. ];
  24. protected static $config = [];
  25. protected static function init()
  26. {
  27. self::$config = $config = get_addon_config('cms');
  28. self::beforeInsert(function ($row) {
  29. if (!isset($row['admin_id']) || !$row['admin_id']) {
  30. $admin_id = session('admin.id');
  31. $row['admin_id'] = $admin_id ? $admin_id : 0;
  32. }
  33. });
  34. self::beforeWrite(function ($row) {
  35. //在更新之前对数组进行处理
  36. foreach ($row->getData() as $k => $value) {
  37. if (is_array($value) && is_array(reset($value))) {
  38. $value = json_encode(self::getArrayData($value), JSON_UNESCAPED_UNICODE);
  39. } else {
  40. $value = is_array($value) ? implode(',', $value) : $value;
  41. }
  42. $row->setAttr($k, $value);
  43. }
  44. if (isset($row['tag_ids'])) {
  45. $tagIds = array_filter(explode(',', $row['tag_ids']));
  46. if ($tagIds) {
  47. foreach ($tagIds as $index => &$tagId) {
  48. $tag = Tag::get($tagId);
  49. if (!$tag && !is_numeric($tagId)) {
  50. $data = [
  51. 'name' => $tagId,
  52. ];
  53. $tag = Tag::create($data);
  54. $tagId = $tag->id;
  55. }
  56. }
  57. $row->tag_ids = implode(',', $tagIds);
  58. }
  59. }
  60. });
  61. self::afterWrite(function ($row) use ($config) {
  62. $changedData = $row->getChangedData();
  63. if (isset($changedData['status']) && $changedData['status'] == 'normal') {
  64. //推送到熊掌号+百度站长
  65. if ($config['baidupush']) {
  66. $urls = [$row->fullurl];
  67. \think\Hook::listen("baidupush", $urls);
  68. }
  69. }
  70. $oldArchivesIds = self::getArchivesIds($row['id']);
  71. if (isset($row['archives_ids'])) {
  72. $newArchivesIds = explode(",", $row['archives_ids']);
  73. $remainIds = array_diff($newArchivesIds, $oldArchivesIds);
  74. $removeIds = array_diff($oldArchivesIds, $newArchivesIds);
  75. $archivesList = \addons\cms\model\Archives::where('id', 'in', array_merge($remainIds, $removeIds))->select();
  76. foreach ($archivesList as $index => $item) {
  77. $ids = explode(',', $item['special_ids']);
  78. if (in_array($item['id'], $remainIds)) {
  79. $ids[] = $row['id'];
  80. }
  81. if (in_array($item['id'], $removeIds)) {
  82. $ids = array_diff($ids, [$row['id']]);
  83. }
  84. $item->save(['special_ids' => implode(',', array_unique(array_filter($ids)))]);
  85. }
  86. }
  87. });
  88. self::afterDelete(function ($row) {
  89. $data = Special::withTrashed()->find($row['id']);
  90. //删除评论
  91. Comment::deleteByType('special', $row['id'], !$data);
  92. });
  93. }
  94. public function getUrlAttr($value, $data)
  95. {
  96. return $this->buildUrl($value, $data);
  97. }
  98. public function getFullurlAttr($value, $data)
  99. {
  100. return $this->buildUrl($value, $data, true);
  101. }
  102. private function buildUrl($value, $data, $domain = false)
  103. {
  104. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  105. $time = $data['createtime'] ?? time();
  106. $vars = [
  107. ':id' => $data['id'],
  108. ':diyname' => $diyname,
  109. ':year' => date("Y", $time),
  110. ':month' => date("m", $time),
  111. ':day' => date("d", $time)
  112. ];
  113. return addon_url('cms/special/index', $vars, static::$config['urlsuffix'], $domain);
  114. }
  115. public function getFlagList()
  116. {
  117. $config = get_addon_config('cms');
  118. return $config['flagtype'];
  119. }
  120. public function getStatusList()
  121. {
  122. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  123. }
  124. public function getFlagTextAttr($value, $data)
  125. {
  126. $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
  127. $valueArr = explode(',', $value);
  128. $list = $this->getFlagList();
  129. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  130. }
  131. public function getStatusTextAttr($value, $data)
  132. {
  133. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  134. $list = $this->getStatusList();
  135. return isset($list[$value]) ? $list[$value] : '';
  136. }
  137. protected function setFlagAttr($value)
  138. {
  139. return is_array($value) ? implode(',', $value) : $value;
  140. }
  141. protected function setKeywordsAttr($value)
  142. {
  143. return str_replace(["&nbsp;", "\r\n", "\r", "\n"], "", strip_tags($value));
  144. }
  145. protected function setDescriptionAttr($value)
  146. {
  147. return str_replace(["&nbsp;", "\r\n", "\r", "\n"], "", strip_tags($value));
  148. }
  149. /**
  150. * 获取专题文档集合
  151. */
  152. public static function getArchivesIds($special_id)
  153. {
  154. $ids = Archives::whereRaw("FIND_IN_SET('{$special_id}', `special_ids`)")->column('id');
  155. return $ids;
  156. }
  157. /**
  158. * 获取专题的文档ID集合
  159. */
  160. public function getArchivesIdsAttr($value, $data)
  161. {
  162. if (isset($data['archives_ids'])) {
  163. return $data['archives_ids'];
  164. }
  165. $ids = Special::getArchivesIds($data['id']);
  166. return implode(',', $ids);
  167. }
  168. }