Page.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace addons\cms\model;
  3. use addons\cms\library\Service;
  4. use think\Db;
  5. use think\Model;
  6. use think\View;
  7. use traits\model\SoftDelete;
  8. /**
  9. * 单页模型
  10. */
  11. class Page extends Model
  12. {
  13. use SoftDelete;
  14. protected $name = "cms_page";
  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. ];
  26. protected static $config = [];
  27. protected static $tagCount = 0;
  28. protected static function init()
  29. {
  30. $config = get_addon_config('cms');
  31. self::$config = $config;
  32. }
  33. public function getAttr($name)
  34. {
  35. //获取自定义字段关联表数据
  36. if (!isset($this->data[$name]) && preg_match("/(.*)_value\$/i", $name, $matches)) {
  37. $key = $this->data[$matches[1]] ?? '';
  38. if (!$key) {
  39. return '';
  40. }
  41. return Service::getRelationFieldValue('page', 0, $matches[1], $key);
  42. }
  43. return parent::getAttr($name);
  44. }
  45. public function getIscommentAttr($value, $data)
  46. {
  47. //优先判断全局评论开关
  48. $iscomment = self::$config['iscomment'] ?? 1;
  49. if ($iscomment) {
  50. $iscomment = $value ? $value : 0;
  51. }
  52. return $iscomment;
  53. }
  54. public function getImageAttr($value, $data)
  55. {
  56. $value = $value ? $value : self::$config['default_page_img'];
  57. return cdnurl($value);
  58. }
  59. public function getUrlAttr($value, $data)
  60. {
  61. return $this->buildUrl($value, $data);
  62. }
  63. public function getFullurlAttr($value, $data)
  64. {
  65. return $this->buildUrl($value, $data, true);
  66. }
  67. private function buildUrl($value, $data, $domain = false)
  68. {
  69. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  70. $time = $data['createtime'] ?? time();
  71. $vars = [
  72. ':id' => $data['id'],
  73. ':diyname' => $diyname,
  74. ':year' => date("Y", $time),
  75. ':month' => date("m", $time),
  76. ':day' => date("d", $time)
  77. ];
  78. $suffix = static::$config['moduleurlsuffix']['page'] ?? static::$config['urlsuffix'];
  79. return addon_url('cms/page/index', $vars, $suffix, $domain);
  80. }
  81. public function getContentAttr($value, $data)
  82. {
  83. if (isset($data['parsetpl']) && $data['parsetpl']) {
  84. $view = View::instance();
  85. $view->engine->layout(false);
  86. return $view->display($data['content']);
  87. }
  88. return $data['content'];
  89. }
  90. public function getHasimageAttr($value, $data)
  91. {
  92. return $this->getData("image") ? true : false;
  93. }
  94. public function getLikeratioAttr($value, $data)
  95. {
  96. return ($data['dislikes'] > 0 ? min(1, $data['likes'] / ($data['dislikes'] + $data['likes'])) : ($data['likes'] ? 1 : 0.5)) * 100;
  97. }
  98. /**
  99. * 获取单页列表
  100. * @param $params
  101. * @return false|\PDOStatement|string|\think\Collection
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. * @throws \think\exception\DbException
  105. */
  106. public static function getPageList($params)
  107. {
  108. $type = empty($params['type']) ? '' : $params['type'];
  109. $condition = empty($params['condition']) ? '' : $params['condition'];
  110. $field = empty($params['field']) ? '*' : $params['field'];
  111. $row = empty($params['row']) ? 10 : (int)$params['row'];
  112. $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
  113. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  114. $limit = empty($params['limit']) ? $row : $params['limit'];
  115. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  116. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  117. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  118. $paginate = !isset($params['paginate']) ? false : $params['paginate'];
  119. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('pagelist', $params);
  120. self::$tagCount++;
  121. $where = ['status' => 'normal'];
  122. if ($type !== '') {
  123. $where['type'] = $type;
  124. }
  125. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  126. $pageModel = self::where($where)
  127. ->where($condition)
  128. ->field($field)
  129. ->orderRaw($order);
  130. if ($paginate) {
  131. list($listRows, $simple, $config) = Service::getPaginateParams('ppage' . self::$tagCount, $params);
  132. $list = $pageModel->paginate($listRows, $simple, $config);
  133. } else {
  134. $list = $pageModel->limit($limit)->cache($cacheKey, $cacheExpire)->select();
  135. }
  136. Service::appendTextAndList('page', 0, $list, true);
  137. self::render($list, $imgwidth, $imgheight);
  138. return $list;
  139. }
  140. public static function getPageInfo($params)
  141. {
  142. $config = get_addon_config('cms');
  143. $sid = empty($params['sid']) ? '' : $params['sid'];
  144. $condition = empty($params['condition']) ? '' : $params['condition'];
  145. $field = empty($params['field']) ? '*' : $params['field'];
  146. $row = empty($params['row']) ? 10 : (int)$params['row'];
  147. $orderby = empty($params['orderby']) ? 'weigh' : $params['orderby'];
  148. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  149. $limit = empty($params['limit']) ? $row : $params['limit'];
  150. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  151. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  152. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  153. $where = [];
  154. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('pageinfo', $params);
  155. if ($sid !== '') {
  156. $where['id'] = $sid;
  157. }
  158. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  159. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  160. $data = self::where($where)
  161. ->where($condition)
  162. ->field($field)
  163. ->order($order)
  164. ->limit($limit)
  165. ->cache($cacheKey, $cacheExpire)
  166. ->find();
  167. if ($data) {
  168. $list = [$data];
  169. self::render($list, $imgwidth, $imgheight);
  170. return reset($list);
  171. } else {
  172. return false;
  173. }
  174. }
  175. public static function render(&$list, $imgwidth, $imgheight)
  176. {
  177. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  178. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  179. foreach ($list as $k => &$v) {
  180. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
  181. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" ' . $width . ' ' . $height . ' /></a>';
  182. $v['img'] = '<img src="' . $v['image'] . '" ' . $width . ' ' . $height . ' />';
  183. }
  184. return $list;
  185. }
  186. }