SpecialPage.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\common\model;
  3. use app\common\constants\SpecialPageConstants;
  4. use app\common\library\Redis;
  5. use app\main\service\ActivityService;
  6. use app\main\service\BookService;
  7. use think\Model;
  8. class SpecialPage extends Model
  9. {
  10. // 表名
  11. protected $table = 'special_page';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. // 追加属性
  18. protected $append = [
  19. 'type_text',
  20. 'status_text'
  21. ];
  22. public function getTypeList()
  23. {
  24. return ['1' => __('Type 1'),'2' => __('Type 2'),'3' => __('Type 3'),'4' => __('Type 4')];
  25. }
  26. public function getStatusList()
  27. {
  28. return ['normal' => __('Status normal'),'hidden' => __('Status hidden')];
  29. }
  30. public function getTypeTextAttr($value, $data)
  31. {
  32. $value = $value ? $value : $data['type'];
  33. $list = $this->getTypeList();
  34. return isset($list[$value]) ? $list[$value] : '';
  35. }
  36. public function getStatusTextAttr($value, $data)
  37. {
  38. $value = $value ? $value : $data['status'];
  39. $list = $this->getStatusList();
  40. return isset($list[$value]) ? $list[$value] : '';
  41. }
  42. /**
  43. * 返回详情
  44. * @param $id
  45. * @return array
  46. */
  47. public function getInfo($id)
  48. {
  49. $result = [];
  50. $redis = Redis::instance();
  51. $key = SpecialPageConstants::REDIS_KEY. $id; //书单信息缓存
  52. if ($redis->exists($key)) {
  53. $result = json_decode($redis->get($key), true);
  54. } else {
  55. $page = $this->where('id', 'EQ', $id)->find();
  56. if (!empty($page)) {
  57. $result = $page->toArray();
  58. //拉取资源
  59. $blocks = collection(model("SpecialBlock")->where('page_id', 'EQ', $id)->order("weigh desc")->select())->toArray();
  60. $result['items'] = $blocks;
  61. if (!empty($result['items'])) {
  62. foreach ($result['items'] as &$item) {
  63. $bookIds = explode(",", str_replace([" ", " ", "\s", "\r", "\n"], [",", ",", ",", ",", ","], trim($item['book_id'])));
  64. $bookIds = array_values(array_filter($bookIds));
  65. if ($bookIds) {
  66. $books = [];
  67. foreach ($bookIds as $bookId) {
  68. $bookInfo = BookService::instance()->getBookModel()->BookInfo($bookId);
  69. if(!empty($bookInfo)){
  70. $books[$bookId]['word_count'] = formatNumber($bookInfo['word_count']);
  71. $books[$bookId]['read_num'] = formatNumber($bookInfo['read_num']);
  72. }
  73. }
  74. $item['books'] = $books;
  75. }
  76. if ($item['activity_id']) {
  77. $res = ActivityService::instance()->getActivityModel()->alias('a')->join(['resource' => 'r'], 'r.activity_id = a.id')
  78. ->field(['r.id', 'a.id' => 'aid'])
  79. ->where('a.id', 'eq', $item['activity_id'])
  80. ->find();
  81. if ($res) {
  82. $item['activity_rid'] = $res['id'];
  83. }
  84. }
  85. }
  86. }
  87. $redis->set($key, json_encode($result, 256));
  88. }
  89. }
  90. return $result;
  91. }
  92. /**
  93. * 清除缓存
  94. * @param $id
  95. * @return bool
  96. */
  97. public function rmCache($id)
  98. {
  99. if (!empty($id)) {
  100. $redis = Redis::instance();
  101. $key = 'SPP:'. $id; //专题页缓存
  102. if ($redis->exists($key)) {
  103. $redis->del($key);
  104. }
  105. }
  106. return true;
  107. }
  108. }