123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Model;
- use app\common\library\Redis;
- class BookCategory extends BaseRwModel
- {
- // 表名
- protected $table = 'book_category';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'sex_text',
- 'status_text',
- ];
- protected $bc = [];
- protected static function init()
- {
- self::afterInsert(function ($row) {
- $pk = $row->getPk();
- $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
- });
- }
- public function getCategoryList($sex = '')
- {
- //设个缓存
- $key = 'BCA:'.$sex;
- $data = Redis::instance()->get($key);
- if ($data) {
- $list = json_decode($data, true);
- } else {
- if ($sex) {
- $list = $this->where("sex", $sex)->column('id,name');
- } else {
- $list = $this->column('id,name');
- }
- Redis::instance()->set($key, json_encode($list, JSON_UNESCAPED_UNICODE), 600);
- }
- return $list;
- }
- public function getSexList()
- {
- return ['1' => '男频', '2' => '女频'];
- }
- public function getStatusList()
- {
- return ['normal' => '显示', 'hidden' => '隐藏'];
- }
- public function getSexTextAttr($value, $data)
- {
- $value = $value ? $value : $data['sex'];
- $list = $this->getSexList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- /**
- * 分类信息 7天有效期
- */
- public function info($id)
- {
- if (!$id || !intval($id)) {
- return null;
- }
- if (isset($this->bc[$id])) {
- return $this->bc[$id];
- }
- $key = 'BCT:' . $id;
- $c = Redis::instance()->hgetall($key);
- if (empty($c)) {
- $c = model('BookCategory')::get($id);
- if (!$c) {
- return null;
- }
- $c = $c->toArray();
- if ($c) {
- Redis::instance()->hmset($key, $c);
- Redis::instance()->expire($key, 7 * 86400);
- }
- }
- $this->bc[$id] = $c;
- return $c;
- }
- /**
- * 男女频道下各自的分类数据
- * @param int $sex 频道
- * @return array
- */
- public static function channelCategories($sex)
- {
- $redis = Redis::instance();
- $key = 'CC:' . $sex;
- if ($redis->exists($key)) {
- $data = $redis->get($key);
- $data = json_decode($data, true);
- } else {
- $where["status"] = "normal";
- $where["sex"] = $sex;
- $data = model('BookCategory')->where($where)->order('weigh', 'desc')->select();
- $json = json_encode($data,JSON_UNESCAPED_UNICODE);
- $redis->setex($key, 86400, $json);
- }
- return $data;
- }
- /**
- * 分类下的书籍列表数据
- * @param int $book_category_id 书籍分类id
- * @param string $type 类别标识
- * @param int $page 页码数
- * @param int $is_water 是否清水
- * @return array
- */
- public static function booklist($book_category_id, $type = 'all', $page = 1, $is_water=false)
- {
- $redis = Redis::instance();
- if (!$is_water) {
- $key = 'CBL:' . $book_category_id . ':' . $type;
- } else {
- $key = 'CBL:W:' . $book_category_id . ':' . $type;
- }
- if(!$redis->exists($key)){
- //清水
- if ($is_water) {
- $map['book.classify_white_list'] = 1;
- }
- $map['book_category_id'] = $book_category_id;
- $map['state'] = 1; //上架
- if ($type == 'serial') {//连载
- $map['is_finish'] = 0;
- } elseif ($type == 'finish') {//完结
- $map['is_finish'] = 1;
- } elseif ($type == 'free') {//免费
- $now = time();
- $map['free_stime'] = array('lt', $now);
- $map['free_etime'] = array('gt', $now);
- } else {
- //全部,无限制条件。
- }
- $allbooks = Db::name('book')->field('id,idx')->where($map)->select();
- foreach($allbooks as $b){
- $redis->zadd($key,$b['idx'],'B:'.$b['id']);
- }
- $redis->expire($key,1800);
- }
- $start = ($page-1)*10;
- $books = $redis->zrevrangebyscore($key,'100','0',array('withscores'=>false,'limit'=>array($start,10)));
- $categoryArr = model("BookCategory")->getCategoryList();
- $data = array();
- foreach ($books as $bookkey) {
- if ($redis->exists($bookkey)) {
- //$data[] = $redis->hmget($bookkey,['id','name','description','author','image','read_num']);
- $b = $redis->hgetall($bookkey);
- } else {
- $res = explode(':', $bookkey);
- $b = model('Book')->bookInfo($res[1]);
- }
- $bookItem = [
- 'id' => $b['id'],
- 'name' => $b['name'],
- 'description' => $b['description'],
- 'author' => $b['author'],
- 'image' => $b['image'],
- 'read_num' => $b['read_num'],
- 'is_finish' => $b['is_finish']
- ];
- if ($type == 'free') {
- $from_date = date('m月d日', $b['free_stime']);
- $end_date = date('m月d日', $b['free_etime']);
- $bookItem['free_period'] = $from_date . '至' . $end_date;
- } else {
- $bookItem['free_period'] = '';
- }
- $data[] = $bookItem;
- }
- return $data;
- }
- }
|