123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2020/4/23
- * Time: 13:58
- */
- namespace app\common\service;
- use app\common\library\Redis;
- use app\main\constants\UserConstants;
- class VipCpService
- {
- public static $self;
- protected $cp_book_redis = UserConstants::USER_VIP_CP_BOOK;
- /**
- * @return VipCpService
- */
- public static function instance()
- {
- if(self::$self == NULL){
- self::$self = new self();
- }
- return self::$self;
- }
- public function getCpVipListModel(){
- return model('CpVipList');
- }
- /**
- * 判断书籍是否 VIP-CP书籍
- * @param $book_id
- * @return bool
- */
- public function isVipCpBook($book_id)
- {
- $redisKey = $this->cp_book_redis . $book_id;
- $data = false;
- if ($rst = Redis::instance()->get($redisKey)) {
- if ($rst == 1) {
- $data = true;
- }
- } else {
- $obj = $this->getCpVipListModel()->where('book_id', 'eq', $book_id)->find();
- if ($obj) {
- $this->saveCpRedis($book_id);
- $data = true;
- } else {
- $this->saveCpRedis($book_id, -1);
- }
- }
- return $data;
- }
- /**
- * 判断书籍是否CP书籍,对VIP收费
- * 如果是VIP,才能调用这个方法,其他用户调用无效
- * @param $book_id
- * @param $user_id
- * @param $vip_endtime
- * @return bool true => 免费 false=>收费
- */
- public function isFreeCpBookForVip($book_id, $user_id, $vip_endtime)
- {
- $rst = true;
- if ($vip_endtime < time()) {
- return false;
- }
- // 判断书籍是否是 VIP-CP
- if ($this->isVipCpBook($book_id)) {
- $cp_obj = $this->getCpVipListModel()->where('book_id', 'eq', $book_id)->find();
- $cp_createtime = $cp_obj['createtime'] ?? 0;
- // vip 扩展表中读取 vip_starttime
- if ($vip_starttime = UserVipExtendService::instance()->getVipStartTime($user_id)) {
- if ($vip_starttime > $cp_createtime) {
- // 收费
- $rst = false;
- }
- }
- }
- return $rst;
- }
- /**
- * @param $book_id
- * @param int $value
- */
- public function saveCpRedis($book_id, $value = 1)
- {
- Redis::instance()->set($this->cp_book_redis . $book_id, $value, 864000);
- }
- /**
- * @param $book_id
- */
- public function delCpRedis($book_id)
- {
- Redis::instance()->del($this->cp_book_redis . $book_id);
- }
- }
|