123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php namespace Psource\Lib;
- use Dever;
- class Content
- {
- # 获取内容分类以及默认分类
- public function getCate($source_id, $channel_id)
- {
- $cate = Dever::db('psource/content_cate')->select(['info_id' => $source_id, 'channel_id' => $channel_id, 'status' => 1], ['col' => 'id,name']);
- # 获取当前的默认分类
- $default = Dever::input('cate_id', '', '', 0);
- if (!$default && $cate) {
- $default = $cate[0]['id'];
- }
- return [$cate, $default];
- }
- # 获取内容列表
- public function getList($uid, $source_id, $channel_id, $cate_id = 0)
- {
- $channel = Dever::db('psource/content_channel')->find($channel_id);
- $where['info_id'] = $source_id;
- $where['channel_id'] = $channel_id;
- if ($cate_id > 0) {
- $where['cate_id'] = $cate_id;
- }
- $search = Dever::input('search');
- if ($search) {
- $where['name'] = ['like', $search];
- }
- $score_time = Dever::db('psource/content')->config['struct']['score_time']['value'];
- $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]);
- if ($data) {
- foreach ($data as &$v) {
- if (!$v['info'] && $v['content']) {
- $v['content'] = strip_tags(htmlspecialchars_decode($v['content']));
- $v['info'] = mb_substr($v['content'], 0, 50, 'UTF-8');
- }
- unset($v['content']);
- $v['price'] = '';
- $v['look'] = $this->getLook($uid, $v);
- if ($v['look'] == 2) {
- $v['price'] = '需解锁';
- if ($v['info']) {
- $v['info'] = $v['price'] . ',' . $v['info'];
- } else {
- $v['info'] = $v['price'];
- }
- } elseif ($v['look'] >= 3) {
- $v['price'] = Dever::load(\Pscore\Lib\Info::class)->getText($v['score_num'], $v['score_id']);
- if ($v['info']) {
- $v['info'] = '需付费,' . $v['info'];
- } else {
- $v['info'] = '需付费';
- }
- }
- $v['cate_id'] = Dever::db('psource/info')->column($v['info_id'], 'cate_id');
- $v['buy'] = '您需要解锁后才能继续访问,点击确定将跳转至解锁页面';
- if ($v['price']) {
- $v['score_time'] = $score_time[$v['score_time']];
- $v['score'] = '您需要付费'.$v['price'].'才能继续访问,付费后' . $v['score_time'] . '内免费访问';
- }
- }
- }
- return $data;
- }
- # 获取内容信息
- public function getInfo($uid, $content_id, $kou = false)
- {
- $info = Dever::db('psource/content')->find($content_id, ['col' => 'id,name,info_id,sku_id,score_id,score_num,score_time,info,content']);
- if (!$info) {
- Dever::error('内容不存在');
- }
- $info['look'] = $this->getLook($uid, $info, $kou);
- if ($info['look'] == 2) {
- Dever::error('请先解锁', -1);
- }
- if ($info['look'] == 4) {
- Dever::error('积分不足', -2);
- }
- if ($kou) {
- return $info;
- }
- $info['content'] = htmlspecialchars_decode($info['content']);
- $info['media'] = Dever::db('psource/content_media')->select(['content_id' => $info['id']], ['col' => 'id,name as title,file as url']);
- $info['data'] = Dever::db('psource/content_data')->select(['content_id' => $info['id']], ['col' => 'id,name,file']);
- $info['link'] = Dever::db('psource/content_link')->select(['content_id' => $info['id']], ['col' => 'id,name,link']);
- return $info;
- }
- # 获取内容是否可以查看 1是可以看,2是需要购买规格,3是需要购买积分
- public function getLook($uid, $info, $kou = false)
- {
- $state = 2;
- # 先验证是否需要解锁
- if ($info['sku_id'] == -1) {
- # 免费
- $state = 1;
- } else {
- # 检测是否解锁
- if ($uid) {
- $check = Dever::db('porder/source_detail')->find(['uid' => $uid, 'sku_id' => $info['sku_id']]);
- if (!$check) {
- # 没解锁,需要购买规格
- $state = 2;
- } elseif ($check && $check['status'] >= 4) {
- # 退款了,也需要重新购买
- $state = 2;
- } else {
- $state = 1;
- }
- } else {
- $state = 2;
- }
- }
- # 购买了,需要消耗积分
- if ($state == 1 && $info['score_id'] && $info['score_num'] && $uid) {
- $state = 2;
- # 检测是否消费过
- $check = Dever::db('psource/user_content')->find(['uid' => $uid, 'content_id' => $info['id']]);
- if ($check) {
- $score_time = $info['score_time']*3600;
- $time = time() - $check['cdate'];
- if ($time <= $score_time) {
- $state = 1;
- }
- }
- if ($state == 2) {
- $user = Dever::load(\Pscore\Lib\User::class)->get($uid, $info['score_id']);
- if ($user && $user['balance'] >= $info['score_num']) {
- # 积分足够
- $state = 3;
- # 扣积分
- if ($kou) {
- Dever::load(\Pscore\Lib\Log::class)->action('访问内容', $info['score_id'])->add($uid, $info['name'], $info['score_num']);
- Dever::db('psource/user_content')->insert(['uid' => $uid, 'content_id' => $info['id'], 'score_id' => $info['score_id'], 'score_num' => $info['score_num']]);
- }
- } else {
- # 积分不够
- $state = 4;
- }
- }
- }
- return $state;
- }
- }
|