webman_duanju/app/controller/IndexController.php

204 lines
6.5 KiB
PHP

<?php
namespace app\controller;
use app\api\model\CourseCollect;
use app\api\model\CourseDetails;
use app\api\model\TbUser;
use app\model\DatabaseRoute;
use app\model\Test;
use support\Request;
use think\facade\Db;
use think\facade\Log;
class IndexController
{
public function index(Request $request)
{
$get['courseId'] = $course_id = '1877654905222135809';
$user['user_id'] = $user_id = '14240';
$user = DatabaseRoute::getDb('tb_user', $user_id)->find();
try {
if(empty($get['courseId'])) {
return json('参数不完整');
}
$courseId = $get['courseId'];
// 获取短剧详情
$dd_b = Db::connect('duanju_slave');
$db_name = $dd_b->name('course');
$bean = $db_name->where(['course_id' => $courseId])->find();
if(!$bean) {
return json('短剧不存在');
}
$courseCollect = DatabaseRoute::getDb('course_collect', $user_id)
->where(['course_id' => $course_id])
->where(['user_id' => $user_id])
->where(['classify' => 3])
->limit(1)
->find();
// 是否追剧
$collect = DatabaseRoute::getDb('course_collect', $user_id)
->where(['course_id' => $course_id])
->where(['classify' => 1])
->limit(1)
->find();
$db = Db::connect(config('think-orm.search_library'));
$userVip = $db->name('user_vip')->where(['user_id' => $user['user_id']])->find();
if ($userVip) {
$user['member'] = $userVip['is_vip'];
$user['end_time'] = $userVip['end_time'];
}
$userInfo = $user;
if (!empty($userInfo['member']) && $userInfo['member'] == 2) {
$isVip = true;
}else{
$isVip = false;
}
// 查询用户是否购买了整集
$courseUser = DatabaseRoute::getDb('course_user', $user_id)
->where(['course_id' => $course_id])
->where(['classify' => 1])
->find();
// 每天购买超过上限,获得免费时间段资格
$freeWatch = Test::checkFreeWatchPayCount($user['user_id']);
$startSort = 0;
$endSort = 5;
$dn_course_details = DatabaseRoute::getDb('course_details', ['course_id' => $courseId]);
$sort = null;
if (is_null($sort)) {
if (!empty($courseCollect)) {
$courseDetails = $dn_course_details->field('sort')
->where('course_details_id', $courseCollect['course_details_id'])
->limit(1)
->find();
$sort = $courseDetails['sort'];
}
}
if ($freeWatch || !empty($courseUser)) {
$courseDetailsSetVos = Test::courseSets($courseId, 2, null);
} else {
$courseDetailsSetVos = Test::courseSets($courseId, 1, $bean['wholesale_price']);
}
// 调整集数范围
if (!is_null($sort) && $sort > 2) {
$startSort = $sort - 3;
$endSort = $sort + 3;
if (count($courseDetailsSetVos) < $endSort) {
$startSort = count($courseDetailsSetVos) - 5;
$endSort = count($courseDetailsSetVos) + 1;
}
}
// 已购买剧集ID集合
$detailsId = [];
if (!$freeWatch) {
$det_db = Db::connect(DatabaseRoute::getConnection('course_user', ['user_id' => $user['user_id']]));
$detailsId = $det_db->name('course_user')->where(['course_id' => $courseId, 'classify' => 2])->column('course_details_id');
$det_db->close();
$detailsId = array_flip(array_flip($detailsId)); // 去重
}
// 处理剧集列表
$current = null;
foreach ($courseDetailsSetVos as &$s) {
$s['wholesalePrice'] = (int) $s['wholesalePrice'];
// 当前播放集
if (!empty($courseCollect) && $s['courseDetailsId'] == $courseCollect['course_details_id']) {
$s['current'] = 1;
$current = &$s;
}
// 非免费用户的权限控制
if (
!$freeWatch &&
$s['sort'] > 3 &&
(empty($detailsId) || !in_array($s['courseDetailsId'], $detailsId)) &&
empty($courseUser) &&
!$isVip
) {
$s['videoUrl'] = null;
}
// 检查是否已点赞
if ($s['sort'] > $startSort && $s['sort'] < $endSort) {
$isGood_db = Db::connect(DatabaseRoute::getConnection('course_collect', ['user_id' => $user['user_id']]));
$isGood = $isGood_db->name('course_collect')
->where('course_details_id', $s['courseDetailsId'])
->where('classify', 2)
->limit(1)
->count();
$isGood_db->close();
$s['isGood'] = empty($isGood) || $isGood == 0 ? 0 : 1;
}
}
// 如果没有当前播放集,默认第一集
if (empty($current) && !empty($courseDetailsSetVos)) {
$courseDetailsSetVos[0]['current'] = 1;
$current = &$courseDetailsSetVos[0];
}
Test::setCourseView($bean);
$price = ($freeWatch ? 0 : ($bean['price'] ?? 0));
$price = bccomp($price, '0', 2) <= 0 ? 0 : $price;
// 返回结果
$map = [
'current' => $current,
'price' => $price,
'title' => $bean['title'],
'collect' => empty($collect) || $collect == 0 ? 0 : 1,
'list' => $courseDetailsSetVos
];
return json($map);
} catch (\Exception $e) {
return json($e->getMessage());
}
}
public function view(Request $request)
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
}