438 lines
16 KiB
PHP
438 lines
16 KiB
PHP
<?php
|
|
|
|
namespace app\api\service;
|
|
|
|
use app\api\exception\ApiException;
|
|
use app\common\exception\EnumCode;
|
|
use app\common\repository\SkitsLibraryReposotpry;
|
|
use app\common\repository\SkitsVideoReposotpry;
|
|
use app\common\repository\SubscriptionReposotpry;
|
|
use app\common\repository\UserReposotpry;
|
|
use app\common\repository\BookCaseReposotpry;
|
|
use app\common\repository\HistoryReposotpry;
|
|
use think\Db;
|
|
|
|
class PlayService
|
|
{
|
|
|
|
protected $skitsLibraryReposotpry;
|
|
protected $skitsVideoReposotpry;
|
|
protected $subscriptionReposotpry;
|
|
protected $userReposotpry;
|
|
protected $bookCaseReposotpry;
|
|
protected $historyReposotpry;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->skitsLibraryReposotpry = new SkitsLibraryReposotpry;
|
|
$this->skitsVideoReposotpry = new SkitsVideoReposotpry;
|
|
$this->subscriptionReposotpry = new SubscriptionReposotpry;
|
|
$this->userReposotpry = new UserReposotpry;
|
|
$this->bookCaseReposotpry = new BookCaseReposotpry;
|
|
$this->historyReposotpry = new HistoryReposotpry;
|
|
}
|
|
|
|
/**
|
|
* 视频列表过程 function
|
|
*
|
|
* @author dotdotdot <6383846@qq.com>
|
|
* @date 2022-08-15
|
|
* @param Request $request
|
|
* @return array
|
|
*/
|
|
public function list(array $params)
|
|
{
|
|
$sid = $params['sid'];
|
|
$uid = $params['uid'];
|
|
$limit = $params['limit'];
|
|
$page = ($params['page'] - 1) * $params['limit'];
|
|
|
|
$skitsLibraryFields = ['name', 'buytype'];
|
|
|
|
$videoInfo = $this->skitsLibraryReposotpry->selectById($skitsLibraryFields, $sid);
|
|
|
|
if (empty($videoInfo)) {
|
|
throw new ApiException('获取视频信息失败!', EnumCode::VideoNotExist);
|
|
}
|
|
|
|
$skitsVideoFields = ['id', 'order', 'isvip'];
|
|
|
|
$skitsVideoData = $this->skitsVideoReposotpry->selectList($skitsVideoFields, $sid, $page, $limit);
|
|
|
|
$skitsVideoCount = $this->skitsVideoReposotpry->selectListCount($skitsVideoFields, $sid);
|
|
|
|
$userFields = ['is_vip', 'vip_enddate'];
|
|
|
|
$userInfo = $this->userReposotpry->selectByUid($userFields, $uid);
|
|
|
|
$playAuth = $userInfo['is_vip'] && $userInfo['vip_enddate'] >= time() ? 1 : -1;
|
|
|
|
if ($playAuth == -1) {
|
|
switch ($videoInfo['buytype']) {
|
|
case 0:
|
|
$playAuth = 2;
|
|
|
|
$videoId = array_column($skitsVideoData, 'id');
|
|
|
|
$subscriptionFields = ['chapterid', 'count(id) `unlock`', 'buytype'];
|
|
|
|
$subscriptionData = $this->subscriptionReposotpry->selectByUidAndArticleidInChapterid($subscriptionFields, $uid, $sid, $videoId);
|
|
|
|
$tmpSubscriptionData = array_columns($subscriptionData, 'chapterid', ['unlock', 'buytype']);
|
|
case 1:
|
|
if ($this->subscriptionReposotpry->selectUnlockAll($uid, $sid)) {
|
|
$playAuth = 3;
|
|
}
|
|
break;
|
|
default:
|
|
throw new ApiException('暂不支持当前类型!', EnumCode::BuyTypeNotExist);
|
|
}
|
|
}
|
|
|
|
foreach ($skitsVideoData as $skitsVideoKey => $skitsVideo) {
|
|
switch ($playAuth) {
|
|
case 1:
|
|
$skitsVideoData[$skitsVideoKey]['unlock'] = 1;
|
|
$skitsVideoData[$skitsVideoKey]['buytype'] = 3;
|
|
break;
|
|
case 2:
|
|
$skitsVideoData[$skitsVideoKey]['unlock'] = 0;
|
|
$skitsVideoData[$skitsVideoKey]['buytype'] = 0;
|
|
|
|
if (isset($tmpSubscriptionData[$skitsVideo['id']])) {
|
|
$skitsVideoData[$skitsVideoKey]['unlock'] = 1;
|
|
$skitsVideoData[$skitsVideoKey]['buytype'] = 1;
|
|
}
|
|
|
|
if ($skitsVideo['isvip'] == 0) {
|
|
$skitsVideoData[$skitsVideoKey]['unlock'] = 1;
|
|
$skitsVideoData[$skitsVideoKey]['buytype'] = 0;
|
|
}
|
|
break;
|
|
case 3:
|
|
$skitsVideoData[$skitsVideoKey]['unlock'] = 1;
|
|
$skitsVideoData[$skitsVideoKey]['buytype'] = 2;
|
|
break;
|
|
default:
|
|
$skitsVideoData[$skitsVideoKey]['unlock'] = 0;
|
|
$skitsVideoData[$skitsVideoKey]['buytype'] = 0;
|
|
|
|
if ($skitsVideo['isvip'] == 0) {
|
|
$skitsVideoData[$skitsVideoKey]['unlock'] = 1;
|
|
$skitsVideoData[$skitsVideoKey]['buytype'] = 0;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'video_name' => $videoInfo['name'],
|
|
'video_count' => $skitsVideoCount,
|
|
'video_data' => $skitsVideoData
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 视频解锁单集或全集过程 function
|
|
*
|
|
* @author dotdotdot <6383846@qq.com>
|
|
* @date 2022-08-16
|
|
* @param array $params
|
|
* @return array
|
|
*/
|
|
public function videoUnlock(array $params)
|
|
{
|
|
$uid = $params['uid'];
|
|
$id = $params['id'];
|
|
$sid = $params['sid'];
|
|
|
|
$skitsLibraryFields = ['id', 'buytype', 'allprice'];
|
|
|
|
$videoInfo = $this->skitsLibraryReposotpry->selectById($skitsLibraryFields, $sid);
|
|
|
|
if (empty($videoInfo)) {
|
|
throw new ApiException('获取视频信息失败!', EnumCode::VideoNotExist);
|
|
}
|
|
|
|
$userFields = ['egold', 'is_vip', 'vip_enddate'];
|
|
|
|
$userInfo = $this->userReposotpry->selectByUid($userFields, $uid);
|
|
|
|
if ($userInfo['is_vip'] && $userInfo['vip_enddate'] >= time()) {
|
|
throw new ApiException('当前已是VIP,无需解锁', EnumCode::VipIsExist);
|
|
}
|
|
|
|
switch ($videoInfo['buytype']) {
|
|
case 0:
|
|
if (empty($id)) {
|
|
throw new ApiException('剧Id不能为空!', EnumCode::VideoNotExist);
|
|
}
|
|
|
|
if ($this->subscriptionReposotpry->selectUnlockOnce($uid, $sid, $id)) {
|
|
throw new ApiException('当前剧已解锁,无需解锁', EnumCode::UnlockIsExist);
|
|
}
|
|
|
|
$skitsLibraryFields = ['isvip', 'price'];
|
|
|
|
$libraryVideoInfo = $this->skitsVideoReposotpry->selectByIdAndSid($skitsLibraryFields, $id, $sid);
|
|
|
|
if (empty($libraryVideoInfo)) {
|
|
throw new ApiException('获取剧Id失败!', EnumCode::VideoNotExist);
|
|
}
|
|
|
|
if ($libraryVideoInfo['is_vip'] == 0) {
|
|
throw new ApiException('当前剧为免费剧,无需解锁!', EnumCode::UnlockIsExist);
|
|
}
|
|
|
|
if (($userInfo['egold'] - $libraryVideoInfo['price']) < 0) {
|
|
throw new ApiException('余额不足', EnumCode::AmountInsufficient);
|
|
}
|
|
|
|
$subscriptionInsertData = [
|
|
'uid' => $uid,
|
|
'create_time' => time(),
|
|
'articleid' => $sid,
|
|
'price' => $libraryVideoInfo['price'],
|
|
'buytype' => 1,
|
|
'chapterid' => $id
|
|
];
|
|
$userUpdateData = [
|
|
'egold' => $userInfo['egold'] - $libraryVideoInfo['price']
|
|
];
|
|
break;
|
|
case 1:
|
|
if ($this->subscriptionReposotpry->selectUnlockAll($uid, $sid)) {
|
|
throw new ApiException('当前剧已全部解锁,无需解锁', EnumCode::UnlockIsExist);
|
|
}
|
|
|
|
if (($userInfo['egold'] - $videoInfo['allprice']) < 0) {
|
|
throw new ApiException('余额不足', EnumCode::AmountInsufficient);
|
|
}
|
|
|
|
$subscriptionInsertData = [
|
|
'uid' => $uid,
|
|
'create_time' => time(),
|
|
'articleid' => $sid,
|
|
'price' => $videoInfo['allprice'],
|
|
'buytype' => 2,
|
|
'chapterid' => 0
|
|
];
|
|
|
|
$userUpdateData = [
|
|
'egold' => $userInfo['egold'] - $videoInfo['allprice']
|
|
];
|
|
break;
|
|
default:
|
|
throw new ApiException('暂不支持当前类型购买!', EnumCode::BuyTypeNotExist);
|
|
}
|
|
|
|
Db::startTrans();
|
|
try {
|
|
$this->subscriptionReposotpry->insert($subscriptionInsertData);
|
|
$this->userReposotpry->updateData($userUpdateData, $uid);
|
|
Db::commit();
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
throw new ApiException('购买失败,请稍后再试!', EnumCode::ServiceError);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 播放过程 function
|
|
*
|
|
* @author dotdotdot <6383846@qq.com>
|
|
* @date 2022-08-16
|
|
* @param array $params
|
|
* @return array
|
|
*/
|
|
public function videoPlay(array $params)
|
|
{
|
|
$uid = $params['uid'];
|
|
$sid = $params['sid'];
|
|
$id = $params['id'];
|
|
$playAuth = 0;
|
|
$insertUnlockOnce = false;
|
|
$returnData = [];
|
|
$isAuthData = [];
|
|
|
|
$skitsLibraryFields = ['id', 'name', 'cover', 'buytype', 'allprice'];
|
|
|
|
$videoInfo = $this->skitsLibraryReposotpry->selectById($skitsLibraryFields, $sid);
|
|
|
|
if (empty($videoInfo)) {
|
|
throw new ApiException('获取视频信息失败!', EnumCode::VideoNotExist);
|
|
}
|
|
|
|
if (empty($id)) {
|
|
$historyFields = ['chapterid'];
|
|
$historyData = $this->historyReposotpry->selectByUidAndArticleid($historyFields, $uid, $sid);
|
|
$id = $historyData['chapterid'];
|
|
if (empty($historyData['chapterid'])) {
|
|
$firstVideoFields = ['id'];
|
|
$firstVideoData = $this->skitsVideoReposotpry->selectBySidOnOrderAsc($firstVideoFields, $sid);
|
|
$id = $firstVideoData['id'];
|
|
}
|
|
}
|
|
|
|
$skitsVideoFields = ['title', 'isvip', 'order', 'price', 'video_url'];
|
|
|
|
$libraryVideoInfo = $this->skitsVideoReposotpry->selectByIdAndSid($skitsVideoFields, $id, $sid);
|
|
|
|
if (empty($libraryVideoInfo)) {
|
|
throw new ApiException('获取剧Id失败!', EnumCode::VideoNotExist);
|
|
}
|
|
|
|
switch ($libraryVideoInfo['isvip']) {
|
|
case 0:
|
|
$playAuth = 1;
|
|
$isAuthData = ['video_url' => $libraryVideoInfo['video_url']];
|
|
break;
|
|
case 1:
|
|
$userFields = ['egold', 'is_vip', 'vip_enddate'];
|
|
|
|
$userInfo = $this->userReposotpry->selectByUid($userFields, $uid);
|
|
|
|
$playAuth = $userInfo['is_vip'] && $userInfo['vip_enddate'] >= time() ? 1 : -1;
|
|
|
|
if ($playAuth == 1) {
|
|
$playAuth = 1;
|
|
$isAuthData = ['video_url' => $libraryVideoInfo['video_url']];
|
|
break;
|
|
}
|
|
|
|
if ($playAuth == -1 && $videoInfo['buytype'] == 0) {
|
|
if ($this->subscriptionReposotpry->selectUnlockOnce($uid, $sid, $id)) {
|
|
$playAuth = 1;
|
|
$isAuthData = ['video_url' => $libraryVideoInfo['video_url']];
|
|
break;
|
|
}
|
|
|
|
if (($userInfo['egold'] - $libraryVideoInfo['price']) >= 0) {
|
|
$insertUnlockOnce = true;
|
|
$playAuth = 1;
|
|
$isAuthData = ['video_url' => $libraryVideoInfo['video_url']];
|
|
break;
|
|
}
|
|
$playAuth = 0;
|
|
break;
|
|
}
|
|
|
|
if ($playAuth == -1 && $videoInfo['buytype'] == 1) {
|
|
if ($this->subscriptionReposotpry->selectUnlockAll($uid, $sid)) {
|
|
$playAuth = 1;
|
|
$isAuthData = ['video_url' => $libraryVideoInfo['video_url']];
|
|
break;
|
|
}
|
|
$playAuth = 0;
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
throw new ApiException('暂不支持当前收费模式!', EnumCode::IsVipNotExist);
|
|
}
|
|
|
|
Db::startTrans();
|
|
try {
|
|
if ($insertUnlockOnce) {
|
|
$subscriptionInsertData = [
|
|
'uid' => $uid,
|
|
'create_time' => time(),
|
|
'articleid' => $sid,
|
|
'price' => $libraryVideoInfo['price'],
|
|
'buytype' => 1,
|
|
'chapterid' => $id
|
|
];
|
|
$userUpdateData = [
|
|
'egold' => $userInfo['egold'] - $libraryVideoInfo['price']
|
|
];
|
|
$this->subscriptionReposotpry->insert($subscriptionInsertData);
|
|
$this->userReposotpry->updateData($userUpdateData, $uid);
|
|
}
|
|
|
|
$historyFields = ['id'];
|
|
$historyData = $this->historyReposotpry->selectByUidAndArticleid($historyFields, $uid, $sid);
|
|
if ($historyData) {
|
|
$historyId = $historyData['id'];
|
|
$updataHistoryData = [
|
|
'chapterid' => $id,
|
|
'chaptername' => $libraryVideoInfo['title'],
|
|
'order' => $libraryVideoInfo['order'],
|
|
'update_time' => time()
|
|
];
|
|
$this->historyReposotpry->updateData($updataHistoryData, $historyId);
|
|
} else {
|
|
$insertHistoryData = [
|
|
'uid' => $uid,
|
|
'articleid' => $sid,
|
|
'chapterid' => $id,
|
|
'chaptername' => $libraryVideoInfo['title'],
|
|
'model' => 1,
|
|
'name' => $videoInfo['name'],
|
|
'order' => $libraryVideoInfo['order'],
|
|
'create_time' => time(),
|
|
'update_time' => time()
|
|
];
|
|
$this->historyReposotpry->insert($insertHistoryData);
|
|
}
|
|
Db::commit();
|
|
|
|
$order = $libraryVideoInfo['order'];
|
|
|
|
$beforeId = $this->skitsVideoReposotpry->selectBeforeBySidAndOrder(['id'], $sid, $order);
|
|
|
|
$afterId = $this->skitsVideoReposotpry->selectAfterBySidAndOrder(['id'], $sid, $order);
|
|
|
|
$returnData = array_merge([
|
|
'id' => $id,
|
|
'sid' => $sid,
|
|
'name' => $videoInfo['name'],
|
|
'title' => $libraryVideoInfo['title'],
|
|
'order' => $order,
|
|
'play_auth' => $playAuth,
|
|
'buytype' => $videoInfo['buytype'],
|
|
'cover' => $videoInfo['cover'],
|
|
'isvip' => $libraryVideoInfo['isvip'],
|
|
'before_id' => $beforeId['id'],
|
|
'after_id' => $afterId['id']
|
|
], $isAuthData);
|
|
|
|
return $returnData;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
throw new ApiException('播放失败,请稍后再试!', EnumCode::ServiceError);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 追剧过程 function
|
|
*
|
|
* @author dotdotdot <6383846@qq.com>
|
|
* @date 2022-08-22
|
|
* @param array $params
|
|
* @return array
|
|
*/
|
|
public function bingeWatching(array $params)
|
|
{
|
|
$uid = $params['uid'];
|
|
$sid = $params['sid'];
|
|
|
|
$bookCaseFields = ['id'];
|
|
$bookCaseData = $this->bookCaseReposotpry->selectByUidAndArticleid($bookCaseFields, $uid, $sid);
|
|
|
|
if (!empty($bookCaseData)) {
|
|
$deleteId = $bookCaseData['id'];
|
|
$this->bookCaseReposotpry->deleteById($deleteId);
|
|
return;
|
|
}
|
|
|
|
$insertData = [
|
|
'uid' => $uid,
|
|
'create_time' => time(),
|
|
'articleid' => $sid
|
|
];
|
|
|
|
$this->bookCaseReposotpry->insert($insertData);
|
|
return;
|
|
}
|
|
}
|