123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace addons\cms\model;
- use addons\cms\library\Service;
- use think\Cache;
- use think\Db;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 专题模型
- */
- class Special extends Model
- {
- use SoftDelete;
- protected $name = "cms_special";
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'url',
- 'fullurl',
- 'create_date',
- ];
- protected static $config = [];
- protected static $tagCount = 0;
- /**
- * 批量设置数据
- * @param $data
- * @return $this
- */
- public function setData($data)
- {
- if (is_object($data)) {
- $data = get_object_vars($data);
- }
- $this->data = array_merge($this->data, $data);
- return $this;
- }
- protected static function init()
- {
- $config = get_addon_config('cms');
- self::$config = $config;
- }
- public function getCreateDateAttr($value, $data)
- {
- return human_date($data['createtime']);
- }
- public function getIscommentAttr($value, $data)
- {
- //优先判断全局评论开关
- $iscomment = self::$config['iscomment'] ?? 1;
- if ($iscomment) {
- $iscomment = $value ? $value : self::$config['iscomment'];
- }
- return $iscomment;
- }
- public function getImageAttr($value, $data)
- {
- $value = $value ? $value : self::$config['default_special_img'];
- return cdnurl($value, true);
- }
- public function getUrlAttr($value, $data)
- {
- return $this->buildUrl($value, $data);
- }
- public function getFullurlAttr($value, $data)
- {
- return $this->buildUrl($value, $data, true);
- }
- private function buildUrl($value, $data, $domain = false)
- {
- $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
- $time = $data['createtime'] ?? time();
- $vars = [
- ':id' => $data['id'],
- ':diyname' => $diyname,
- ':year' => date("Y", $time),
- ':month' => date("m", $time),
- ':day' => date("d", $time)
- ];
- $suffix = static::$config['moduleurlsuffix']['special'] ?? static::$config['urlsuffix'];
- return addon_url('cms/special/index', $vars, $suffix, $domain);
- }
- public function getHasimageAttr($value, $data)
- {
- return $this->getData("image") ? true : false;
- }
- /**
- * 获取专题列表
- * @param $tag
- * @return array|false|\PDOStatement|string|\think\Collection
- */
- public static function getSpecialList($tag)
- {
- $config = get_addon_config('cms');
- $condition = empty($tag['condition']) ? '' : $tag['condition'];
- $field = empty($tag['field']) ? '*' : $tag['field'];
- $flag = empty($tag['flag']) ? '' : $tag['flag'];
- $row = empty($tag['row']) ? 10 : (int)$tag['row'];
- $orderby = empty($tag['orderby']) ? 'createtime' : $tag['orderby'];
- $orderway = empty($tag['orderway']) ? 'desc' : strtolower($tag['orderway']);
- $limit = empty($tag['limit']) ? $row : $tag['limit'];
- $cache = !isset($tag['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$tag['cache'];
- $imgwidth = empty($tag['imgwidth']) ? '' : $tag['imgwidth'];
- $imgheight = empty($tag['imgheight']) ? '' : $tag['imgheight'];
- $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
- $paginate = !isset($tag['paginate']) ? false : $tag['paginate'];
- $cache = !$cache ? false : $cache;
- $where = ['status' => 'normal'];
- self::$tagCount++;
- //如果有设置标志,则拆分标志信息并构造condition条件
- if ($flag !== '') {
- if (stripos($flag, '&') !== false) {
- $arr = [];
- foreach (explode('&', $flag) as $k => $v) {
- $arr[] = "FIND_IN_SET('{$v}', flag)";
- }
- if ($arr) {
- $condition .= "(" . implode(' AND ', $arr) . ")";
- }
- } else {
- $condition .= ($condition ? ' AND ' : '');
- $arr = [];
- foreach (explode(',', str_replace('|', ',', $flag)) as $k => $v) {
- $arr[] = "FIND_IN_SET('{$v}', flag)";
- }
- if ($arr) {
- $condition .= "(" . implode(' OR ', $arr) . ")";
- }
- }
- }
- $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
- $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
- $specialModel = self::where($where)
- ->where($condition)
- ->field($field)
- ->orderRaw($order);
- if ($paginate) {
- $paginateArr = explode(',', $paginate);
- $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
- $config = [];
- $config['var_page'] = isset($paginateArr[2]) ? $paginateArr[2] : 'spage' . self::$tagCount;
- $config['path'] = isset($paginateArr[3]) ? $paginateArr[3] : '';
- $config['fragment'] = isset($paginateArr[4]) ? $paginateArr[4] : '';
- $config['query'] = request()->get();
- $list = $specialModel->paginate($listRows, (isset($paginateArr[1]) ? $paginateArr[1] : false), $config);
- } else {
- $list = $specialModel->limit($limit)->cache($cache)->select();
- }
- $fieldsContentList = Fields::getFieldsContentList('special');
- foreach ($list as $index => $item) {
- Service::appendTextAttr($fieldsContentList, $item);
- }
- self::render($list, $imgwidth, $imgheight);
- return $list;
- }
- /**
- * 渲染数据
- * @param array $list
- * @param int $imgwidth
- * @param int $imgheight
- * @return array
- */
- public static function render(&$list, $imgwidth, $imgheight)
- {
- $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
- $height = $imgheight ? 'height="' . $imgheight . '"' : '';
- foreach ($list as $k => &$v) {
- $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
- $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
- $v['img'] = '<img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' />';
- }
- return $list;
- }
- /**
- * 获取专题文档集合
- */
- public static function getArchivesIds($special_id)
- {
- $ids = Archives::whereRaw("FIND_IN_SET('{$special_id}', `special_ids`)")->cache(86400)->column('id');
- return $ids;
- }
- }
|