Content.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php namespace Psource\Lib;
  2. use Dever;
  3. class Content
  4. {
  5. # 获取内容分类以及默认分类
  6. public function getCate($source_id, $channel_id)
  7. {
  8. $cate = Dever::db('psource/content_cate')->select(['info_id' => $source_id, 'channel_id' => $channel_id, 'status' => 1], ['col' => 'id,name']);
  9. # 获取当前的默认分类
  10. $default = Dever::input('cate_id', '', '', 0);
  11. if (!$default && $cate) {
  12. $default = $cate[0]['id'];
  13. }
  14. return [$cate, $default];
  15. }
  16. # 获取内容列表
  17. public function getList($uid, $source_id, $channel_id, $cate_id = 0)
  18. {
  19. $channel = Dever::db('psource/content_channel')->find($channel_id);
  20. $where['info_id'] = $source_id;
  21. $where['channel_id'] = $channel_id;
  22. if ($cate_id > 0) {
  23. $where['cate_id'] = $cate_id;
  24. }
  25. $search = Dever::input('search');
  26. if ($search) {
  27. $where['name'] = ['like', $search];
  28. }
  29. $score_time = Dever::db('psource/content')->config['struct']['score_time']['value'];
  30. $data = Dever::db('psource/content')->select($where, ['col' => 'id,name,info,info_id,sku_id,score_id,score_num,score_time,content,udate', 'num' => 10]);
  31. if ($data) {
  32. foreach ($data as &$v) {
  33. if (!$v['info'] && $v['content']) {
  34. $v['content'] = strip_tags(htmlspecialchars_decode($v['content']));
  35. $v['info'] = mb_substr($v['content'], 0, 50, 'UTF-8');
  36. }
  37. unset($v['content']);
  38. $v['price'] = '';
  39. $v['look'] = $this->getLook($uid, $v);
  40. if ($v['look'] == 2) {
  41. $v['price'] = '需解锁';
  42. if ($v['info']) {
  43. $v['info'] = $v['price'] . ',' . $v['info'];
  44. } else {
  45. $v['info'] = $v['price'];
  46. }
  47. } elseif ($v['look'] >= 3) {
  48. $v['price'] = Dever::load(\Pscore\Lib\Info::class)->getText($v['score_num'], $v['score_id']);
  49. if ($v['info']) {
  50. $v['info'] = '需付费,' . $v['info'];
  51. } else {
  52. $v['info'] = '需付费';
  53. }
  54. }
  55. $v['cate_id'] = Dever::db('psource/info')->column($v['info_id'], 'cate_id');
  56. $v['buy'] = '您需要解锁后才能继续访问,点击确定将跳转至解锁页面';
  57. if ($v['price']) {
  58. $v['score_time'] = $score_time[$v['score_time']];
  59. $v['score'] = '您需要付费'.$v['price'].'才能继续访问,付费后' . $v['score_time'] . '内免费访问';
  60. }
  61. }
  62. }
  63. return $data;
  64. }
  65. # 获取内容信息
  66. public function getInfo($uid, $content_id, $kou = false)
  67. {
  68. $info = Dever::db('psource/content')->find($content_id, ['col' => 'id,name,info_id,sku_id,score_id,score_num,score_time,info,content']);
  69. if (!$info) {
  70. Dever::error('内容不存在');
  71. }
  72. $info['look'] = $this->getLook($uid, $info, $kou);
  73. if ($info['look'] == 2) {
  74. Dever::error('请先解锁', -1);
  75. }
  76. if ($info['look'] == 4) {
  77. Dever::error('积分不足', -2);
  78. }
  79. if ($kou) {
  80. return $info;
  81. }
  82. $info['content'] = htmlspecialchars_decode($info['content']);
  83. $info['media'] = Dever::db('psource/content_media')->select(['content_id' => $info['id']], ['col' => 'id,name as title,file as url']);
  84. $info['data'] = Dever::db('psource/content_data')->select(['content_id' => $info['id']], ['col' => 'id,name,file']);
  85. $info['link'] = Dever::db('psource/content_link')->select(['content_id' => $info['id']], ['col' => 'id,name,link']);
  86. return $info;
  87. }
  88. # 获取内容是否可以查看 1是可以看,2是需要购买规格,3是需要购买积分
  89. public function getLook($uid, $info, $kou = false)
  90. {
  91. $state = 2;
  92. # 先验证是否需要解锁
  93. if ($info['sku_id'] == -1) {
  94. # 免费
  95. $state = 1;
  96. } else {
  97. # 检测是否解锁
  98. if ($uid) {
  99. $check = Dever::db('porder/source_detail')->find(['uid' => $uid, 'sku_id' => $info['sku_id']]);
  100. if (!$check) {
  101. # 没解锁,需要购买规格
  102. $state = 2;
  103. } elseif ($check && $check['status'] >= 4) {
  104. # 退款了,也需要重新购买
  105. $state = 2;
  106. } else {
  107. $state = 1;
  108. }
  109. } else {
  110. $state = 2;
  111. }
  112. }
  113. # 购买了,需要消耗积分
  114. if ($state == 1 && $info['score_id'] && $info['score_num'] && $uid) {
  115. $state = 2;
  116. # 检测是否消费过
  117. $check = Dever::db('psource/user_content')->find(['uid' => $uid, 'content_id' => $info['id']]);
  118. if ($check) {
  119. $score_time = $info['score_time']*3600;
  120. $time = time() - $check['cdate'];
  121. if ($time <= $score_time) {
  122. $state = 1;
  123. }
  124. }
  125. if ($state == 2) {
  126. $user = Dever::load(\Pscore\Lib\User::class)->get($uid, $info['score_id']);
  127. if ($user && $user['balance'] >= $info['score_num']) {
  128. # 积分足够
  129. $state = 3;
  130. # 扣积分
  131. if ($kou) {
  132. Dever::load(\Pscore\Lib\Log::class)->action('访问内容', $info['score_id'])->add($uid, $info['name'], $info['score_num']);
  133. Dever::db('psource/user_content')->insert(['uid' => $uid, 'content_id' => $info['id'], 'score_id' => $info['score_id'], 'score_num' => $info['score_num']]);
  134. }
  135. } else {
  136. # 积分不够
  137. $state = 4;
  138. }
  139. }
  140. }
  141. return $state;
  142. }
  143. }