抖音小程序

UserService.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Service;
  3. use App\Models\Album;
  4. use App\Models\Episode;
  5. use App\Models\Material;
  6. use App\Models\UserBehaviorLog;
  7. use App\Models\UserCollect;
  8. use App\Models\Users;
  9. use App\Support\DouYinApi;
  10. use App\User;
  11. class UserService
  12. {
  13. public static function loginCode($appId, $code) {
  14. # 调用抖音api获取用户信息
  15. $responseData = DouYinApi::code2Session($appId, $code);
  16. # 查询是否为新用户
  17. $res = Users::saveUser($appId, $responseData);
  18. # 返回用户信息
  19. return [['openid' => $responseData['openid']], 0];
  20. }
  21. public static function likeList($appId, $openId, $source, $page, $pageSize) {
  22. switch($source) {
  23. case 1: // 追剧
  24. return self::chasingDramasList($appId, $openId, $page, $pageSize);
  25. break;
  26. case 2: // 浏览历史(要区分开今日以及更早)
  27. return self::browsingHistoryList($appId, $openId, $page, $pageSize);
  28. break;
  29. case 3: // 收藏
  30. return self::collectList($appId, $openId, $page, $pageSize);
  31. break;
  32. }
  33. }
  34. # 追剧列表
  35. public static function chasingDramasList($appId, $openId, $page, $pageSize, $startDate = null, $endDate = null) {
  36. # 获取列表
  37. list($data, $count) = UserCollect::getUserCollectList($appId, $openId, null, 1, $startDate, $endDate
  38. , $page, $pageSize);
  39. # 批量查询短剧信息
  40. $albumIdList = array_column($data->toArray(), 'album_id');
  41. $albumList = Album::getListById($appId, $albumIdList);
  42. foreach($data as $info) {
  43. $albumInfo = $albumList->where('album_id', $info->album_id)->first();
  44. # 短剧基础信息
  45. $info->title = $albumInfo->title ?? '';
  46. $info->seq_num = $albumInfo->seq_num ?? 0;
  47. $coverList = Material::getPicList($appId, explode(',', $albumInfo->cover_list));
  48. $coverInfo = $coverList->first();
  49. $info->cover_url = $coverInfo->oss_url ?? '';
  50. # 播放信息
  51. $info->video_url = null;
  52. $episodeInfo = Episode::getInfo($appId, $info->album_id, $info->seq);
  53. $openVideoId = $episodeInfo->open_video_id ?? null;
  54. if(!empty($openVideoId)) {
  55. $videoInfoList = Material::getVideoList($appId, [$openVideoId]);
  56. $videoInfo = $videoInfoList->where('open_video_id', $openVideoId)->first();
  57. $info->video_url = $videoInfo->oss_url ?? null;
  58. }
  59. }
  60. return [$data, $count];
  61. }
  62. # 浏览历史列表
  63. public static function browsingHistoryList($appId, $openId, $page, $pageSize) {
  64. # 今日数据
  65. $today = date('Y-m-d');
  66. list($todayData, $todayCount) = self::chasingDramasList($appId, $openId, null, null, $today, $today);
  67. # 更早数据
  68. $yesterday = date('Y-m-d', strtotime('-1 days'));
  69. list($lastData, $lastCount) = self::chasingDramasList($appId, $openId, $page, $pageSize, null, $yesterday);
  70. return [['today' => $todayData, 'last' => $lastData], $lastCount];
  71. }
  72. # 收藏列表
  73. public static function collectList($appId, $openId, $page, $pageSize) {
  74. # 获取列表
  75. list($data, $count) = UserCollect::getUserCollectList($appId, $openId, null, 2, null, null
  76. , $page, $pageSize);
  77. # 批量查询短剧信息
  78. $albumIdList = array_column($data->toArray(), 'album_id');
  79. $albumList = Album::getListById($appId, $albumIdList);
  80. # 查询用户观看信息
  81. // 提取短剧id
  82. $albumIdList = array_column($data->toArray(), 'album_id');
  83. list($collectList, $collectCount) = UserCollect::getUserCollectList($appId, $openId, $albumIdList);
  84. foreach($data as $info) {
  85. $albumInfo = $albumList->where('album_id', $info->album_id)->first();
  86. # 短剧基础信息
  87. $info->title = $albumInfo->title ?? '';
  88. $info->seq_num = $albumInfo->seq_num ?? 0;
  89. $cover_list = empty($albumInfo->cover_list) ? '' : $albumInfo->cover_list;
  90. $coverList = Material::getPicList($appId, explode(',', $cover_list));
  91. $coverInfo = $coverList->first();
  92. $info->cover_url = $coverInfo->oss_url ?? '';
  93. # 播放信息查询
  94. $playInfo = $collectList->where('album_id', $info->album_id)->where('type', 1)->first();
  95. $info->seq = $playInfo->seq ?? 1;
  96. $info->video_url = null;
  97. $episodeInfo = Episode::getInfo($appId, $info->album_id, $info->seq);
  98. $openVideoId = $episodeInfo->open_video_id ?? null;
  99. $info->episode_id = $episodeInfo->episode_id ?? null;
  100. if(!empty($openVideoId)) {
  101. $videoInfoList = Material::getVideoList($appId, [$openVideoId]);
  102. $videoInfo = $videoInfoList->where('open_video_id', $openVideoId)->first();
  103. $info->video_url = $videoInfo->oss_url ?? null;
  104. }
  105. }
  106. return [$data, $count];
  107. }
  108. public static function like($appId, $openId, $type, $albumId, $episodeId, $status, $seq) {
  109. switch($status) {
  110. case 1: // 浏览 收藏
  111. $res = UserCollect::addRecord($appId, $openId, $type, $albumId, $episodeId, $seq);
  112. break;
  113. case 0: // 取消浏览 取消收藏
  114. $res = UserCollect::delRecord($appId, $openId, $type, $albumId);
  115. break;
  116. }
  117. return [$res, 0];
  118. }
  119. public static function watchAd($appId, $openId, $albumId, $episodeId, $adPlacementId) {
  120. $res = UserBehaviorLog::saveData($appId, $openId, $albumId, $episodeId, $adPlacementId);
  121. if(!$res) {
  122. return 400;
  123. }
  124. return 0;
  125. }
  126. public static function updateUserByOpenId($userId, $updateData) {
  127. $user = Users::updateData($userId, $updateData);
  128. if(empty($user)) {
  129. return [[], 500];
  130. }
  131. return [$user, 0];
  132. }
  133. }