Page.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Service;
  4. use fast\Tree;
  5. use think\Db;
  6. use think\Model;
  7. use think\View;
  8. use traits\model\SoftDelete;
  9. /**
  10. * 模型
  11. */
  12. class Page extends Model
  13. {
  14. use SoftDelete;
  15. // 表名
  16. protected $name = 'shop_page';
  17. // 开启自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'createtime';
  21. protected $updateTime = 'updatetime';
  22. protected $deleteTime = 'deletetime';
  23. // 追加属性
  24. protected $append = [
  25. 'url'
  26. ];
  27. protected static $tagCount = 0;
  28. public function getImageAttr($value, $data)
  29. {
  30. $value = $value ? $value : '';
  31. return cdnurl($value, true);
  32. }
  33. public function getUrlAttr($value, $data)
  34. {
  35. return $this->buildUrl($value, $data);
  36. }
  37. public function getFullurlAttr($value, $data)
  38. {
  39. return $this->buildUrl($value, $data, true);
  40. }
  41. private function buildUrl($value, $data, $domain = false)
  42. {
  43. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  44. $time = $data['createtime'] ?? time();
  45. $vars = [
  46. ':id' => $data['id'],
  47. ':diyname' => $diyname,
  48. ':year' => date("Y", $time),
  49. ':month' => date("m", $time),
  50. ':day' => date("d", $time)
  51. ];
  52. $suffix = static::$config['moduleurlsuffix']['page'] ?? "";
  53. return addon_url('shop/page/index', $vars, $suffix, $domain);
  54. }
  55. public function getContentAttr($value, $data)
  56. {
  57. if (isset($data['parsetpl']) && $data['parsetpl']) {
  58. $view = View::instance();
  59. $view->engine->layout(false);
  60. return $view->display($data['content']);
  61. }
  62. //组装卡片信息
  63. return Service::formatSourceTpl($data['content']);
  64. }
  65. public function getHasimageAttr($value, $data)
  66. {
  67. return $this->getData("image") ? true : false;
  68. }
  69. public function getLikeratioAttr($value, $data)
  70. {
  71. return bcmul(($data['dislikes'] > 0 ? min(1, $data['likes'] / ($data['dislikes'] + $data['likes'])) : ($data['likes'] ? 1 : 0.5)), 100);
  72. }
  73. /**
  74. * 获取单页列表
  75. * @param $params
  76. * @return false|\PDOStatement|string|\think\Collection
  77. */
  78. public static function getPageList($params)
  79. {
  80. $type = empty($params['type']) ? '' : $params['type'];
  81. $condition = empty($params['condition']) ? '' : $params['condition'];
  82. $field = empty($params['field']) ? '*' : $params['field'];
  83. $row = empty($params['row']) ? 10 : (int)$params['row'];
  84. $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
  85. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  86. $limit = empty($params['limit']) ? $row : $params['limit'];
  87. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  88. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  89. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  90. $paginate = !isset($params['paginate']) ? false : $params['paginate'];
  91. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('pagelist', $params);
  92. self::$tagCount++;
  93. $where = ['status' => 'normal'];
  94. if ($type !== '') {
  95. $where['type'] = $type;
  96. }
  97. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  98. $pageModel = self::where($where)
  99. ->where($condition)
  100. ->field($field)
  101. ->orderRaw($order);
  102. if ($paginate) {
  103. $paginateArr = explode(',', $paginate);
  104. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  105. $config = [];
  106. $config['var_page'] = $paginateArr[2] ?? 'ppage' . self::$tagCount;
  107. $config['path'] = $paginateArr[3] ?? '';
  108. $config['fragment'] = $paginateArr[4] ?? '';
  109. $config['query'] = request()->get();
  110. $config['type'] = '\\addons\\shop\\library\\Bootstrap';
  111. $list = $pageModel->paginate($listRows, ($paginateArr[1] ?? false), $config);
  112. } else {
  113. $list = $pageModel->limit($limit)->cache($cacheKey, $cacheExpire, 'shop')->select();
  114. }
  115. self::render($list, $imgwidth, $imgheight);
  116. return $list;
  117. }
  118. public static function getPageInfo($params)
  119. {
  120. $sid = empty($params['sid']) ? '' : $params['sid'];
  121. $condition = empty($params['condition']) ? '' : $params['condition'];
  122. $field = empty($params['field']) ? '*' : $params['field'];
  123. $row = empty($params['row']) ? 10 : (int)$params['row'];
  124. $orderby = empty($params['orderby']) ? 'weigh' : $params['orderby'];
  125. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  126. $limit = empty($params['limit']) ? $row : $params['limit'];
  127. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  128. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  129. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  130. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('pageinfo', $params);
  131. $where = [];
  132. if ($sid !== '') {
  133. $where['id'] = $sid;
  134. }
  135. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  136. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  137. $data = self::where($where)
  138. ->where($condition)
  139. ->field($field)
  140. ->order($order)
  141. ->limit($limit)
  142. ->cache($cacheKey, $cacheExpire, 'shop')
  143. ->find();
  144. if ($data) {
  145. $list = [$data];
  146. self::render($list, $imgwidth, $imgheight);
  147. return reset($list);
  148. } else {
  149. return false;
  150. }
  151. }
  152. public static function render(&$list, $imgwidth, $imgheight)
  153. {
  154. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  155. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  156. foreach ($list as $k => &$v) {
  157. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
  158. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
  159. $v['img'] = '<img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' />';
  160. }
  161. return $list;
  162. }
  163. }