Special.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 getAttr($name)
  48. {
  49. //获取自定义字段关联表数据
  50. if (!isset($this->data[$name]) && preg_match("/(.*)_value\$/i", $name, $matches)) {
  51. $key = $this->data[$matches[1]] ?? '';
  52. if (!$key) {
  53. return '';
  54. }
  55. return Service::getRelationFieldValue('special', 0, $matches[1], $key);
  56. }
  57. return parent::getAttr($name);
  58. }
  59. public function getCreateDateAttr($value, $data)
  60. {
  61. return human_date($data['createtime']);
  62. }
  63. public function getIscommentAttr($value, $data)
  64. {
  65. //优先判断全局评论开关
  66. $iscomment = self::$config['iscomment'] ?? 1;
  67. if ($iscomment) {
  68. $iscomment = $value ? $value : 0;
  69. }
  70. return $iscomment;
  71. }
  72. public function getImageAttr($value, $data)
  73. {
  74. $value = $value ? $value : self::$config['default_special_img'];
  75. return cdnurl($value, true);
  76. }
  77. public function getBannerAttr($value, $data)
  78. {
  79. return cdnurl($value, true);
  80. }
  81. public function getUrlAttr($value, $data)
  82. {
  83. return $this->buildUrl($value, $data);
  84. }
  85. public function getFullurlAttr($value, $data)
  86. {
  87. return $this->buildUrl($value, $data, true);
  88. }
  89. private function buildUrl($value, $data, $domain = false)
  90. {
  91. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  92. $time = $data['createtime'] ?? time();
  93. $vars = [
  94. ':id' => $data['id'],
  95. ':diyname' => $diyname,
  96. ':year' => date("Y", $time),
  97. ':month' => date("m", $time),
  98. ':day' => date("d", $time)
  99. ];
  100. $suffix = static::$config['moduleurlsuffix']['special'] ?? static::$config['urlsuffix'];
  101. return addon_url('cms/special/index', $vars, $suffix, $domain);
  102. }
  103. public function getHasimageAttr($value, $data)
  104. {
  105. return $this->getData("image") ? true : false;
  106. }
  107. /**
  108. * 获取专题列表
  109. * @param $params
  110. * @return array|false|\PDOStatement|string|\think\Collection
  111. */
  112. public static function getSpecialList($params)
  113. {
  114. $condition = empty($params['condition']) ? '' : $params['condition'];
  115. $field = empty($params['field']) ? '*' : $params['field'];
  116. $flag = empty($params['flag']) ? '' : $params['flag'];
  117. $row = empty($params['row']) ? 10 : (int)$params['row'];
  118. $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
  119. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  120. $limit = empty($params['limit']) ? $row : $params['limit'];
  121. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  122. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  123. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  124. $paginate = !isset($params['paginate']) ? false : $params['paginate'];
  125. $where = ['status' => 'normal'];
  126. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('speciallist', $params);
  127. self::$tagCount++;
  128. //如果有设置标志,则拆分标志信息并构造condition条件
  129. if ($flag !== '') {
  130. if (stripos($flag, '&') !== false) {
  131. $arr = [];
  132. foreach (explode('&', $flag) as $k => $v) {
  133. $arr[] = "FIND_IN_SET('{$v}', flag)";
  134. }
  135. if ($arr) {
  136. $condition .= "(" . implode(' AND ', $arr) . ")";
  137. }
  138. } else {
  139. $condition .= ($condition ? ' AND ' : '');
  140. $arr = [];
  141. foreach (explode(',', str_replace('|', ',', $flag)) as $k => $v) {
  142. $arr[] = "FIND_IN_SET('{$v}', flag)";
  143. }
  144. if ($arr) {
  145. $condition .= "(" . implode(' OR ', $arr) . ")";
  146. }
  147. }
  148. }
  149. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  150. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  151. $specialModel = self::where($where)
  152. ->where($condition)
  153. ->field($field)
  154. ->orderRaw($order);
  155. if ($paginate) {
  156. list($listRows, $simple, $config) = Service::getPaginateParams('spage' . self::$tagCount, $params);
  157. $list = $specialModel->paginate($listRows, $simple, $config);
  158. } else {
  159. $list = $specialModel->limit($limit)->cache($cacheKey, $cacheExpire)->select();
  160. }
  161. Service::appendTextAndList('special', 0, $list, true);
  162. self::render($list, $imgwidth, $imgheight);
  163. return $list;
  164. }
  165. /**
  166. * 渲染数据
  167. * @param array $list
  168. * @param int $imgwidth
  169. * @param int $imgheight
  170. * @return array
  171. */
  172. public static function render(&$list, $imgwidth, $imgheight)
  173. {
  174. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  175. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  176. foreach ($list as $k => &$v) {
  177. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
  178. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" ' . $width . ' ' . $height . ' /></a>';
  179. $v['img'] = '<img src="' . $v['image'] . '" ' . $width . ' ' . $height . ' />';
  180. }
  181. return $list;
  182. }
  183. /**
  184. * 获取专题文档集合
  185. */
  186. public static function getArchivesIds($special_id)
  187. {
  188. $ids = Archives::whereRaw("FIND_IN_SET('{$special_id}', `special_ids`)")->cache(86400)->column('id');
  189. return $ids;
  190. }
  191. }