Page.php 7.5 KB

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