BookCategory.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. use app\common\library\Redis;
  6. class BookCategory extends BaseRwModel
  7. {
  8. // 表名
  9. protected $table = 'book_category';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // 追加属性
  16. protected $append = [
  17. 'sex_text',
  18. 'status_text',
  19. ];
  20. protected $bc = [];
  21. protected static function init()
  22. {
  23. self::afterInsert(function ($row) {
  24. $pk = $row->getPk();
  25. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  26. });
  27. }
  28. public function getCategoryList($sex = '')
  29. {
  30. //设个缓存
  31. $key = 'BCA:'.$sex;
  32. $data = Redis::instance()->get($key);
  33. if ($data) {
  34. $list = json_decode($data, true);
  35. } else {
  36. if ($sex) {
  37. $list = $this->where("sex", $sex)->column('id,name');
  38. } else {
  39. $list = $this->column('id,name');
  40. }
  41. Redis::instance()->set($key, json_encode($list, JSON_UNESCAPED_UNICODE), 600);
  42. }
  43. return $list;
  44. }
  45. public function getSexList()
  46. {
  47. return ['1' => '男频', '2' => '女频'];
  48. }
  49. public function getStatusList()
  50. {
  51. return ['normal' => '显示', 'hidden' => '隐藏'];
  52. }
  53. public function getSexTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : $data['sex'];
  56. $list = $this->getSexList();
  57. return isset($list[$value]) ? $list[$value] : '';
  58. }
  59. public function getStatusTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : $data['status'];
  62. $list = $this->getStatusList();
  63. return isset($list[$value]) ? $list[$value] : '';
  64. }
  65. /**
  66. * 分类信息 7天有效期
  67. */
  68. public function info($id)
  69. {
  70. if (!$id || !intval($id)) {
  71. return null;
  72. }
  73. if (isset($this->bc[$id])) {
  74. return $this->bc[$id];
  75. }
  76. $key = 'BCT:' . $id;
  77. $c = Redis::instance()->hgetall($key);
  78. if (empty($c)) {
  79. $c = model('BookCategory')::get($id);
  80. if (!$c) {
  81. return null;
  82. }
  83. $c = $c->toArray();
  84. if ($c) {
  85. Redis::instance()->hmset($key, $c);
  86. Redis::instance()->expire($key, 7 * 86400);
  87. }
  88. }
  89. $this->bc[$id] = $c;
  90. return $c;
  91. }
  92. /**
  93. * 男女频道下各自的分类数据
  94. * @param int $sex 频道
  95. * @return array
  96. */
  97. public static function channelCategories($sex)
  98. {
  99. $redis = Redis::instance();
  100. $key = 'CC:' . $sex;
  101. if ($redis->exists($key)) {
  102. $data = $redis->get($key);
  103. $data = json_decode($data, true);
  104. } else {
  105. $where["status"] = "normal";
  106. $where["sex"] = $sex;
  107. $data = model('BookCategory')->where($where)->order('weigh', 'desc')->select();
  108. $json = json_encode($data,JSON_UNESCAPED_UNICODE);
  109. $redis->setex($key, 86400, $json);
  110. }
  111. return $data;
  112. }
  113. /**
  114. * 分类下的书籍列表数据
  115. * @param int $book_category_id 书籍分类id
  116. * @param string $type 类别标识
  117. * @param int $page 页码数
  118. * @param int $is_water 是否清水
  119. * @return array
  120. */
  121. public static function booklist($book_category_id, $type = 'all', $page = 1, $is_water=false)
  122. {
  123. $redis = Redis::instance();
  124. if (!$is_water) {
  125. $key = 'CBL:' . $book_category_id . ':' . $type;
  126. } else {
  127. $key = 'CBL:W:' . $book_category_id . ':' . $type;
  128. }
  129. if(!$redis->exists($key)){
  130. //清水
  131. if ($is_water) {
  132. $map['book.classify_white_list'] = 1;
  133. }
  134. $map['book_category_id'] = $book_category_id;
  135. $map['state'] = 1; //上架
  136. if ($type == 'serial') {//连载
  137. $map['is_finish'] = 0;
  138. } elseif ($type == 'finish') {//完结
  139. $map['is_finish'] = 1;
  140. } elseif ($type == 'free') {//免费
  141. $now = time();
  142. $map['free_stime'] = array('lt', $now);
  143. $map['free_etime'] = array('gt', $now);
  144. } else {
  145. //全部,无限制条件。
  146. }
  147. $allbooks = Db::name('book')->field('id,idx')->where($map)->select();
  148. foreach($allbooks as $b){
  149. $redis->zadd($key,$b['idx'],'B:'.$b['id']);
  150. }
  151. $redis->expire($key,1800);
  152. }
  153. $start = ($page-1)*10;
  154. $books = $redis->zrevrangebyscore($key,'100','0',array('withscores'=>false,'limit'=>array($start,10)));
  155. $categoryArr = model("BookCategory")->getCategoryList();
  156. $data = array();
  157. foreach ($books as $bookkey) {
  158. if ($redis->exists($bookkey)) {
  159. //$data[] = $redis->hmget($bookkey,['id','name','description','author','image','read_num']);
  160. $b = $redis->hgetall($bookkey);
  161. } else {
  162. $res = explode(':', $bookkey);
  163. $b = model('Book')->bookInfo($res[1]);
  164. }
  165. $bookItem = [
  166. 'id' => $b['id'],
  167. 'name' => $b['name'],
  168. 'description' => $b['description'],
  169. 'author' => $b['author'],
  170. 'image' => $b['image'],
  171. 'read_num' => $b['read_num'],
  172. 'is_finish' => $b['is_finish']
  173. ];
  174. if ($type == 'free') {
  175. $from_date = date('m月d日', $b['free_stime']);
  176. $end_date = date('m月d日', $b['free_etime']);
  177. $bookItem['free_period'] = $from_date . '至' . $end_date;
  178. } else {
  179. $bookItem['free_period'] = '';
  180. }
  181. $data[] = $bookItem;
  182. }
  183. return $data;
  184. }
  185. }