Page.php 6.9 KB

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