Archives.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace app\admin\model\cms;
  3. use addons\cms\library\FulltextSearch;
  4. use addons\cms\library\Service;
  5. use app\common\model\User;
  6. use think\Model;
  7. use traits\model\SoftDelete;
  8. class Archives extends Model
  9. {
  10. use SoftDelete;
  11. // 表名
  12. protected $name = 'cms_archives';
  13. // 自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. // 追加属性
  20. protected $append = [
  21. 'flag_text',
  22. 'status_text',
  23. 'publishtime_text',
  24. 'url',
  25. 'fullurl',
  26. 'style_bold',
  27. 'style_color',
  28. ];
  29. protected static $config = [];
  30. public function getUrlAttr($value, $data)
  31. {
  32. return $this->buildUrl($value, $data);
  33. }
  34. public function getFullurlAttr($value, $data)
  35. {
  36. return $this->buildUrl($value, $data, true);
  37. }
  38. private function buildUrl($value, $data, $domain = false)
  39. {
  40. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  41. $catename = isset($this->channel) && $this->channel ? $this->channel->diyname : 'all';
  42. $cateid = isset($this->channel) && $this->channel ? $this->channel->id : 0;
  43. $time = $data['publishtime'] ?? time();
  44. $vars = [
  45. ':id' => $data['id'],
  46. ':diyname' => $diyname,
  47. ':channel' => $data['channel_id'],
  48. ':catename' => $catename,
  49. ':cateid' => $cateid,
  50. ':year' => date("Y", $time),
  51. ':month' => date("m", $time),
  52. ':day' => date("d", $time),
  53. ];
  54. return addon_url('cms/archives/index', $vars, static::$config['urlsuffix'], true);
  55. }
  56. public function getOriginData()
  57. {
  58. return $this->origin;
  59. }
  60. /**
  61. * 批量设置数据
  62. * @param $data
  63. * @return $this
  64. */
  65. public function setData($data)
  66. {
  67. if (is_object($data)) {
  68. $data = get_object_vars($data);
  69. }
  70. $this->data = array_merge($this->data, $data);
  71. return $this;
  72. }
  73. protected static function init()
  74. {
  75. self::$config = $config = get_addon_config('cms');
  76. self::beforeInsert(function ($row) {
  77. if (!isset($row['admin_id']) || !$row['admin_id']) {
  78. $admin_id = session('admin.id');
  79. $row['admin_id'] = $admin_id ? $admin_id : 0;
  80. }
  81. });
  82. self::afterInsert(function ($row) {
  83. $pk = $row->getPk();
  84. $channel = Channel::get($row['channel_id']);
  85. $row->getQuery()->where($pk, $row[$pk])->update(['model_id' => $channel ? $channel['model_id'] : 0]);
  86. Channel::where('id', $row['channel_id'])->setInc('items');
  87. });
  88. self::beforeWrite(function ($row) {
  89. $changedData = $row->getChangedData();
  90. if (isset($changedData['flag'])) {
  91. $row['weigh'] = is_array($changedData['flag']) && in_array('top', $changedData['flag'])
  92. ? ($row['weigh'] == 0 ? 9999 : $row['weigh'])
  93. : ($row['weigh'] == 9999 ? 0 : $row['weigh']);
  94. }
  95. if (isset($row['content'])) {
  96. $row['content'] = Service::autolinks($row['content']);
  97. }
  98. //在更新之前对数组进行处理
  99. foreach ($row->getData() as $k => $value) {
  100. if (is_array($value) && is_array(reset($value))) {
  101. $value = json_encode(self::getArrayData($value), JSON_UNESCAPED_UNICODE);
  102. } else {
  103. $value = is_array($value) ? implode(',', $value) : $value;
  104. }
  105. $row->$k = $value;
  106. }
  107. });
  108. self::afterWrite(function ($row) use ($config) {
  109. if (isset($row['channel_id'])) {
  110. //在更新成功后刷新副表、TAGS表数据、栏目表
  111. $channel = Channel::get($row->channel_id);
  112. if ($channel) {
  113. $model = Modelx::get($channel['model_id']);
  114. if ($model) {
  115. $values = array_intersect_key($row->getData(), array_flip($model->fields));
  116. $values['id'] = $row['id'];
  117. if (isset($row['content'])) {
  118. $values['content'] = $row['content'];
  119. }
  120. //更新副表
  121. $addonTbl = \think\Db::name($model['table']);
  122. if ($addonTbl->find($row['id'])) {
  123. $addonTbl->update($values);
  124. } else {
  125. $addonTbl->insert($values, true);
  126. }
  127. }
  128. }
  129. }
  130. if (isset($row['tags'])) {
  131. \addons\cms\model\Tag::refresh($row['tags'], $row['id']);
  132. }
  133. $changedData = $row->getChangedData();
  134. if (isset($changedData['status']) && $changedData['status'] == 'normal') {
  135. //增加积分
  136. User::score($config['score']['postarchives'], $row['user_id'], '发布文章');
  137. //推送到熊掌号和百度站长
  138. if ($config['baidupush']) {
  139. $urls = [$row->fullurl];
  140. \think\Hook::listen("baidupush", $urls);
  141. }
  142. }
  143. if ($config['searchtype'] == 'xunsearch') {
  144. //更新全文搜索
  145. FulltextSearch::update($row->id);
  146. }
  147. });
  148. self::afterDelete(function ($row) use ($config) {
  149. $data = Archives::withTrashed()->find($row['id']);
  150. if ($data) {
  151. if ($row['status'] == 'normal') {
  152. User::score(-$config['score']['postarchives'], $row['user_id'], '删除文章');
  153. }
  154. if ($config['searchtype'] == 'xunsearch') {
  155. FulltextSearch::del($row);
  156. }
  157. } else {
  158. //删除相关TAG
  159. \addons\cms\model\Tag::refresh('', $row['id']);
  160. }
  161. //删除评论
  162. Comment::deleteByType('archives', $row['id'], !$data);
  163. });
  164. }
  165. public function getFlagList()
  166. {
  167. $config = get_addon_config('cms');
  168. return $config['flagtype'];
  169. }
  170. public function getStatusList()
  171. {
  172. return ['normal' => __('Normal'), 'hidden' => __('Hidden'), 'rejected' => __('Status rejected'), 'pulloff' => __('Status pulloff')];
  173. }
  174. public function getStyleBoldAttr($value, $data)
  175. {
  176. return in_array('b', explode('|', $data['style']));
  177. }
  178. public function getStyleColorAttr($value, $data)
  179. {
  180. $result = preg_match("/(#([0-9a-z]{6}))/i", $data['style'], $matches);
  181. return $result ? $matches[1] : '';
  182. }
  183. public function getFlagTextAttr($value, $data)
  184. {
  185. $value = $value ? $value : $data['flag'];
  186. $valueArr = $value ? explode(',', $value) : [];
  187. $list = $this->getFlagList();
  188. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  189. }
  190. public function getStatusTextAttr($value, $data)
  191. {
  192. $value = $value ? $value : $data['status'];
  193. $list = $this->getStatusList();
  194. return isset($list[$value]) ? $list[$value] : '';
  195. }
  196. public function getPublishtimeTextAttr($value, $data)
  197. {
  198. $value = $value ? $value : $data['publishtime'];
  199. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  200. }
  201. protected function setPublishtimeAttr($value)
  202. {
  203. return $value && !is_numeric($value) ? strtotime($value) : ($value ? $value : null);
  204. }
  205. protected function setCreatetimeAttr($value)
  206. {
  207. return $value && !is_numeric($value) ? strtotime($value) : ($value ? $value : null);
  208. }
  209. public static function getArrayData($data)
  210. {
  211. if (!isset($data['value'])) {
  212. $result = [];
  213. foreach ($data as $index => $datum) {
  214. $result['field'][$index] = $datum['key'];
  215. $result['value'][$index] = $datum['value'];
  216. }
  217. $data = $result;
  218. }
  219. $fieldarr = $valuearr = [];
  220. $field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []);
  221. $value = isset($data['value']) ? $data['value'] : [];
  222. foreach ($field as $m => $n) {
  223. if ($n != '') {
  224. $fieldarr[] = $field[$m];
  225. $valuearr[] = $value[$m];
  226. }
  227. }
  228. return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
  229. }
  230. public function channel()
  231. {
  232. return $this->belongsTo('Channel', 'channel_id', '', [], 'LEFT')->setEagerlyType(0);
  233. }
  234. public function special()
  235. {
  236. return $this->belongsTo('Special', 'special_id', '', [], 'LEFT')->setEagerlyType(0);
  237. }
  238. /**
  239. * 关联模型
  240. */
  241. public function model()
  242. {
  243. return $this->belongsTo("Modelx", 'model_id')->setEagerlyType(1);
  244. }
  245. /**
  246. * 关联模型
  247. */
  248. public function user()
  249. {
  250. return $this->belongsTo("\\app\\common\\model\\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(1);
  251. }
  252. /**
  253. * 关联模型
  254. */
  255. public function admin()
  256. {
  257. return $this->belongsTo("\\app\\admin\\model\\Admin", 'admin_id', 'id', [], 'LEFT')->setEagerlyType(1);
  258. }
  259. }