123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Service;
- use App\Models\Album;
- use App\Models\Episode;
- use App\Models\Material;
- use App\Models\UserBehaviorLog;
- use App\Models\UserCollect;
- use App\Models\Users;
- use App\Support\DouYinApi;
- use App\User;
- class UserService
- {
- public static function loginCode($appId, $code) {
- # 调用抖音api获取用户信息
- $responseData = DouYinApi::code2Session($appId, $code);
- # 查询是否为新用户
- $res = Users::saveUser($appId, $responseData);
- # 返回用户信息
- return [['openid' => $responseData['openid']], 0];
- }
- public static function likeList($appId, $openId, $source, $page, $pageSize) {
- switch($source) {
- case 1: // 追剧
- return self::chasingDramasList($appId, $openId, $page, $pageSize);
- break;
- case 2: // 浏览历史(要区分开今日以及更早)
- return self::browsingHistoryList($appId, $openId, $page, $pageSize);
- break;
- case 3: // 收藏
- return self::collectList($appId, $openId, $page, $pageSize);
- break;
- }
- }
- # 追剧列表
- public static function chasingDramasList($appId, $openId, $page, $pageSize, $startDate = null, $endDate = null) {
- # 获取列表
- list($data, $count) = UserCollect::getUserCollectList($appId, $openId, null, 1, $startDate, $endDate
- , $page, $pageSize);
- # 批量查询短剧信息
- $albumIdList = array_column($data->toArray(), 'album_id');
- $albumList = Album::getListById($appId, $albumIdList);
- foreach($data as $info) {
- $albumInfo = $albumList->where('album_id', $info->album_id)->first();
- # 短剧基础信息
- $info->title = $albumInfo->title ?? '';
- $info->seq_num = $albumInfo->seq_num ?? 0;
- $coverList = Material::getPicList($appId, explode(',', $albumInfo->cover_list));
- $coverInfo = $coverList->first();
- $info->cover_url = $coverInfo->oss_url ?? '';
- # 播放信息
- $info->video_url = null;
- $episodeInfo = Episode::getInfo($appId, $info->album_id, $info->seq);
- $openVideoId = $episodeInfo->open_video_id ?? null;
- if(!empty($openVideoId)) {
- $videoInfoList = Material::getVideoList($appId, [$openVideoId]);
- $videoInfo = $videoInfoList->where('open_video_id', $openVideoId)->first();
- $info->video_url = $videoInfo->oss_url ?? null;
- }
- }
- return [$data, $count];
- }
- # 浏览历史列表
- public static function browsingHistoryList($appId, $openId, $page, $pageSize) {
- # 今日数据
- $today = date('Y-m-d');
- list($todayData, $todayCount) = self::chasingDramasList($appId, $openId, null, null, $today, $today);
- # 更早数据
- $yesterday = date('Y-m-d', strtotime('-1 days'));
- list($lastData, $lastCount) = self::chasingDramasList($appId, $openId, $page, $pageSize, null, $yesterday);
- return [['today' => $todayData, 'last' => $lastData], $lastCount];
- }
- # 收藏列表
- public static function collectList($appId, $openId, $page, $pageSize) {
- # 获取列表
- list($data, $count) = UserCollect::getUserCollectList($appId, $openId, null, 2, null, null
- , $page, $pageSize);
- # 批量查询短剧信息
- $albumIdList = array_column($data->toArray(), 'album_id');
- $albumList = Album::getListById($appId, $albumIdList);
- # 查询用户观看信息
- // 提取短剧id
- $albumIdList = array_column($data->toArray(), 'album_id');
- list($collectList, $collectCount) = UserCollect::getUserCollectList($appId, $openId, $albumIdList);
- foreach($data as $info) {
- $albumInfo = $albumList->where('album_id', $info->album_id)->first();
- # 短剧基础信息
- $info->title = $albumInfo->title ?? '';
- $info->seq_num = $albumInfo->seq_num ?? 0;
- $cover_list = empty($albumInfo->cover_list) ? '' : $albumInfo->cover_list;
- $coverList = Material::getPicList($appId, explode(',', $cover_list));
- $coverInfo = $coverList->first();
- $info->cover_url = $coverInfo->oss_url ?? '';
- # 播放信息查询
- $playInfo = $collectList->where('album_id', $info->album_id)->where('type', 1)->first();
- $info->seq = $playInfo->seq ?? 1;
- $info->video_url = null;
- $episodeInfo = Episode::getInfo($appId, $info->album_id, $info->seq);
- $openVideoId = $episodeInfo->open_video_id ?? null;
- $info->episode_id = $episodeInfo->episode_id ?? null;
- if(!empty($openVideoId)) {
- $videoInfoList = Material::getVideoList($appId, [$openVideoId]);
- $videoInfo = $videoInfoList->where('open_video_id', $openVideoId)->first();
- $info->video_url = $videoInfo->oss_url ?? null;
- }
- }
- return [$data, $count];
- }
- public static function like($appId, $openId, $type, $albumId, $episodeId, $status, $seq) {
- switch($status) {
- case 1: // 浏览 收藏
- $res = UserCollect::addRecord($appId, $openId, $type, $albumId, $episodeId, $seq);
- break;
- case 0: // 取消浏览 取消收藏
- $res = UserCollect::delRecord($appId, $openId, $type, $albumId);
- break;
- }
- return [$res, 0];
- }
- public static function watchAd($appId, $openId, $albumId, $episodeId, $adPlacementId) {
- $res = UserBehaviorLog::saveData($appId, $openId, $albumId, $episodeId, $adPlacementId);
- if(!$res) {
- return 400;
- }
- return 0;
- }
- public static function updateUserByOpenId($userId, $updateData) {
- $user = Users::updateData($userId, $updateData);
- if(empty($user)) {
- return [[], 500];
- }
- return [$user, 0];
- }
- }
|