VipCpService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2020/4/23
  6. * Time: 13:58
  7. */
  8. namespace app\common\service;
  9. use app\common\library\Redis;
  10. use app\main\constants\UserConstants;
  11. class VipCpService
  12. {
  13. public static $self;
  14. protected $cp_book_redis = UserConstants::USER_VIP_CP_BOOK;
  15. /**
  16. * @return VipCpService
  17. */
  18. public static function instance()
  19. {
  20. if(self::$self == NULL){
  21. self::$self = new self();
  22. }
  23. return self::$self;
  24. }
  25. public function getCpVipListModel(){
  26. return model('CpVipList');
  27. }
  28. /**
  29. * 判断书籍是否 VIP-CP书籍
  30. * @param $book_id
  31. * @return bool
  32. */
  33. public function isVipCpBook($book_id)
  34. {
  35. $redisKey = $this->cp_book_redis . $book_id;
  36. $data = false;
  37. if ($rst = Redis::instance()->get($redisKey)) {
  38. if ($rst == 1) {
  39. $data = true;
  40. }
  41. } else {
  42. $obj = $this->getCpVipListModel()->where('book_id', 'eq', $book_id)->find();
  43. if ($obj) {
  44. $this->saveCpRedis($book_id);
  45. $data = true;
  46. } else {
  47. $this->saveCpRedis($book_id, -1);
  48. }
  49. }
  50. return $data;
  51. }
  52. /**
  53. * 判断书籍是否CP书籍,对VIP收费
  54. * 如果是VIP,才能调用这个方法,其他用户调用无效
  55. * @param $book_id
  56. * @param $user_id
  57. * @param $vip_endtime
  58. * @return bool true => 免费 false=>收费
  59. */
  60. public function isFreeCpBookForVip($book_id, $user_id, $vip_endtime)
  61. {
  62. $rst = true;
  63. if ($vip_endtime < time()) {
  64. return false;
  65. }
  66. // 判断书籍是否是 VIP-CP
  67. if ($this->isVipCpBook($book_id)) {
  68. $cp_obj = $this->getCpVipListModel()->where('book_id', 'eq', $book_id)->find();
  69. $cp_createtime = $cp_obj['createtime'] ?? 0;
  70. // vip 扩展表中读取 vip_starttime
  71. if ($vip_starttime = UserVipExtendService::instance()->getVipStartTime($user_id)) {
  72. if ($vip_starttime > $cp_createtime) {
  73. // 收费
  74. $rst = false;
  75. }
  76. }
  77. }
  78. return $rst;
  79. }
  80. /**
  81. * @param $book_id
  82. * @param int $value
  83. */
  84. public function saveCpRedis($book_id, $value = 1)
  85. {
  86. Redis::instance()->set($this->cp_book_redis . $book_id, $value, 864000);
  87. }
  88. /**
  89. * @param $book_id
  90. */
  91. public function delCpRedis($book_id)
  92. {
  93. Redis::instance()->del($this->cp_book_redis . $book_id);
  94. }
  95. }