Special.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace addons\cms\model;
  3. use addons\cms\library\Service;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Model;
  7. use traits\model\SoftDelete;
  8. /**
  9. * 专题模型
  10. */
  11. class Special extends Model
  12. {
  13. use SoftDelete;
  14. protected $name = "cms_special";
  15. // 开启自动写入时间戳字段
  16. protected $autoWriteTimestamp = 'int';
  17. // 定义时间戳字段名
  18. protected $createTime = 'createtime';
  19. protected $updateTime = 'updatetime';
  20. protected $deleteTime = 'deletetime';
  21. // 追加属性
  22. protected $append = [
  23. 'url',
  24. 'fullurl',
  25. 'create_date',
  26. ];
  27. protected static $config = [];
  28. protected static $tagCount = 0;
  29. /**
  30. * 批量设置数据
  31. * @param $data
  32. * @return $this
  33. */
  34. public function setData($data)
  35. {
  36. if (is_object($data)) {
  37. $data = get_object_vars($data);
  38. }
  39. $this->data = array_merge($this->data, $data);
  40. return $this;
  41. }
  42. protected static function init()
  43. {
  44. $config = get_addon_config('cms');
  45. self::$config = $config;
  46. }
  47. public function getCreateDateAttr($value, $data)
  48. {
  49. return human_date($data['createtime']);
  50. }
  51. public function getIscommentAttr($value, $data)
  52. {
  53. //优先判断全局评论开关
  54. $iscomment = self::$config['iscomment'] ?? 1;
  55. if ($iscomment) {
  56. $iscomment = $value ? $value : self::$config['iscomment'];
  57. }
  58. return $iscomment;
  59. }
  60. public function getImageAttr($value, $data)
  61. {
  62. $value = $value ? $value : self::$config['default_special_img'];
  63. return cdnurl($value, true);
  64. }
  65. public function getUrlAttr($value, $data)
  66. {
  67. return $this->buildUrl($value, $data);
  68. }
  69. public function getFullurlAttr($value, $data)
  70. {
  71. return $this->buildUrl($value, $data, true);
  72. }
  73. private function buildUrl($value, $data, $domain = false)
  74. {
  75. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  76. $time = $data['createtime'] ?? time();
  77. $vars = [
  78. ':id' => $data['id'],
  79. ':diyname' => $diyname,
  80. ':year' => date("Y", $time),
  81. ':month' => date("m", $time),
  82. ':day' => date("d", $time)
  83. ];
  84. $suffix = static::$config['moduleurlsuffix']['special'] ?? static::$config['urlsuffix'];
  85. return addon_url('cms/special/index', $vars, $suffix, $domain);
  86. }
  87. public function getHasimageAttr($value, $data)
  88. {
  89. return $this->getData("image") ? true : false;
  90. }
  91. /**
  92. * 获取专题列表
  93. * @param $tag
  94. * @return array|false|\PDOStatement|string|\think\Collection
  95. */
  96. public static function getSpecialList($tag)
  97. {
  98. $config = get_addon_config('cms');
  99. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  100. $field = empty($tag['field']) ? '*' : $tag['field'];
  101. $flag = empty($tag['flag']) ? '' : $tag['flag'];
  102. $row = empty($tag['row']) ? 10 : (int)$tag['row'];
  103. $orderby = empty($tag['orderby']) ? 'createtime' : $tag['orderby'];
  104. $orderway = empty($tag['orderway']) ? 'desc' : strtolower($tag['orderway']);
  105. $limit = empty($tag['limit']) ? $row : $tag['limit'];
  106. $cache = !isset($tag['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$tag['cache'];
  107. $imgwidth = empty($tag['imgwidth']) ? '' : $tag['imgwidth'];
  108. $imgheight = empty($tag['imgheight']) ? '' : $tag['imgheight'];
  109. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  110. $paginate = !isset($tag['paginate']) ? false : $tag['paginate'];
  111. $cache = !$cache ? false : $cache;
  112. $where = ['status' => 'normal'];
  113. self::$tagCount++;
  114. //如果有设置标志,则拆分标志信息并构造condition条件
  115. if ($flag !== '') {
  116. if (stripos($flag, '&') !== false) {
  117. $arr = [];
  118. foreach (explode('&', $flag) as $k => $v) {
  119. $arr[] = "FIND_IN_SET('{$v}', flag)";
  120. }
  121. if ($arr) {
  122. $condition .= "(" . implode(' AND ', $arr) . ")";
  123. }
  124. } else {
  125. $condition .= ($condition ? ' AND ' : '');
  126. $arr = [];
  127. foreach (explode(',', str_replace('|', ',', $flag)) as $k => $v) {
  128. $arr[] = "FIND_IN_SET('{$v}', flag)";
  129. }
  130. if ($arr) {
  131. $condition .= "(" . implode(' OR ', $arr) . ")";
  132. }
  133. }
  134. }
  135. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  136. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  137. $specialModel = self::where($where)
  138. ->where($condition)
  139. ->field($field)
  140. ->orderRaw($order);
  141. if ($paginate) {
  142. $paginateArr = explode(',', $paginate);
  143. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  144. $config = [];
  145. $config['var_page'] = isset($paginateArr[2]) ? $paginateArr[2] : 'spage' . self::$tagCount;
  146. $config['path'] = isset($paginateArr[3]) ? $paginateArr[3] : '';
  147. $config['fragment'] = isset($paginateArr[4]) ? $paginateArr[4] : '';
  148. $config['query'] = request()->get();
  149. $list = $specialModel->paginate($listRows, (isset($paginateArr[1]) ? $paginateArr[1] : false), $config);
  150. } else {
  151. $list = $specialModel->limit($limit)->cache($cache)->select();
  152. }
  153. $fieldsContentList = Fields::getFieldsContentList('special');
  154. foreach ($list as $index => $item) {
  155. Service::appendTextAttr($fieldsContentList, $item);
  156. }
  157. self::render($list, $imgwidth, $imgheight);
  158. return $list;
  159. }
  160. /**
  161. * 渲染数据
  162. * @param array $list
  163. * @param int $imgwidth
  164. * @param int $imgheight
  165. * @return array
  166. */
  167. public static function render(&$list, $imgwidth, $imgheight)
  168. {
  169. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  170. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  171. foreach ($list as $k => &$v) {
  172. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
  173. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
  174. $v['img'] = '<img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' />';
  175. }
  176. return $list;
  177. }
  178. /**
  179. * 获取专题文档集合
  180. */
  181. public static function getArchivesIds($special_id)
  182. {
  183. $ids = Archives::whereRaw("FIND_IN_SET('{$special_id}', `special_ids`)")->cache(86400)->column('id');
  184. return $ids;
  185. }
  186. }