pluck('service_provider_id'); list($list, $count) = PlatformPlaylet::getPlayletList($platformId, $keyword, $accountList, $page, $pageSize); # 平台数据获取 $platformList = config('platform.platform'); foreach($list as $item) { # 平台名称 $item->platform_name = $platformList[$item->platform_id]['platform_name'] ?? '未知'; # 账号名称 $accountInfo = $accountData->where('service_provider_id', $item->partner_id)->first(); $item->account = $accountInfo->account ?? '-'; unset($item->partner_id); } return [$list, $count]; } /* * 创建推广链接 * */ public static function createLink($params, $platformId, $accountId, $sysGroupId, $adminId, &$errno) { try { # 检验小程序app_id if($platformId != 4 && empty($params['mini_app_id'])){ $errno = 4105; return []; } switch($platformId) { case 4: // 嘉书H5(东方小剧场) list($pagePath, $pid) = self::createJiaShuLink($params, $platformId, $accountId, $sysGroupId, $adminId,$errno); break; case 3: // 迈步 list($pagePath, $pid) = self::createMaibuLink($params, $platformId, $accountId, $sysGroupId, $adminId, $errno); break; default: $errno = 4910; break; } } catch (\Exception $e) { EmailQueue::rPush('生成推广链接过程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com', 'song.shen@kuxuan-inc.com'], '生成推广链接过程发生异常'); Log::logError('迈步推广链接存储失败', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'params' => $params, 'platformId' => $platformId, 'accountId' => $accountId, 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId ], 'CreateLink'); $errno = 4914; } if($errno || !$pagePath) return []; # 查询剧封面和标题 $playletInfo = PlatformPlaylet::query()->select('name', 'description', 'cover_url') ->where('platform_id', $platformId)->where('playlet_id', $params['playlet_id']) ->where('enable', 1)->where('state', 1) ->first(); $desc = empty($playletInfo->description) ? (empty($playletInfo->name) ? null : $playletInfo->name) : $playletInfo->description; $name = $playletInfo->name ?? null; $coverUrl = $playletInfo->cover_url ?? ''; return ['path' => $pagePath, 'desc' => $desc, 'cover' => $coverUrl, 'pid' => $pid, 'playlet_name' => $name]; } public static function createJiaShuLink($params, $platformId, $accountId, $sysGroupId, $adminId, &$errno) { # 查询渠道商id $channelInfo = IntelligentMassSendingAccount::query()->where('id', $accountId) ->where('platform_id', $platformId)->where('status', 1) ->where('active_status', 1)->where('enable', 1)->first(); if(empty($channelInfo)) { $errno = 4912; return [null, null]; } $customerId = $channelInfo->service_provider_id; $apiKey = $channelInfo->api_key; # 查询第三方平台app_id $platformAppId = PlatformApp::getAppExit($platformId, $customerId, $params['app_id']); if(empty($platformAppId)) { $errno = 4915; return [null, null]; } # 查询剧集 $sectionCount = PlatformPlaylet::query() ->where('platform_id', $platformId) ->where('playlet_id', $params['playlet_id']) ->where('enable', 1) ->value('section_count'); if(empty($sectionCount) || $params['episode_num'] > $sectionCount) { $errno = 4911; return [null, null]; } # 生成推广链接 $promotionData = JiaShuService::savePromotion( $apiKey, $customerId, $params['app_id'], $params['playlet_id'], $params['episode_num'], $params['name'] ); $code = $promotionData['code'] ?? 1; if(1008 == $code) { $errno = 4916;// 该剧素材库不完整,暂时不能创建 return [null, null]; } else if($code) { $msg = $promotionData['message'] ?? '未标识的错误'; EmailQueue::rPush('生成嘉书H5推广链接失败', $msg, ['song.shen@kuxuan-inc.com'], '生成推广链接失败'); $errno = 4913; return [false, null]; } $pid = $promotionData['data']['article'] ?? null; $linkInfo = $promotionData['data'] ?? null; if(empty($pid) || empty($linkInfo)) { $errno = 4913; return [null, null]; } # 检出链接信息 $insertData = [ 'platform_id' => $platformId, 'account' => $accountId, 'sys_group_id' => $sysGroupId, 'app_id' => $params['app_id'], 'playlet_id' => $params['playlet_id'], 'episode_id' => $params['episode_num'], 'admin_id' => $adminId, 'corpid' => $params['corpid'], 'mini_app_id' => $params['mini_app_id'] ?? null, 'name' => $params['name'], 'force_subscribe' => $params['force_subscribe'], 'force_subscribe_sectionId' => $params['force_subscribe_sectionId'], 'type' => $params['type'], 'h5' => $linkInfo['mini_url'], 'pid' => $pid ]; $result = PromoteLink::query()->insert($insertData); if(!$result) { Log::logError('嘉书H5推广链接存储失败', ['data' => $insertData], 'CreateLink'); } $pagePath = $insertData['h5'] ?? null; return [$pagePath, $pid]; } public static function createMaibuLink($params, $platformId, $accountId, $sysGroupId, $adminId, &$errno) { # 查询渠道商id $channelId = IntelligentMassSendingAccount::query()->where('id', $accountId) ->where('platform_id', $platformId)->where('status', 1) ->where('active_status', 1)->where('enable', 1) ->value('service_provider_id'); if(empty($channelId)) { $errno = 4912; return [null, null]; } # 查询第三方平台app_id $platformAppId = PlatformApp::getPlatformAppId($platformId, $channelId, $params['app_id'], $params['mini_app_id']); if(empty($platformAppId)) { $errno = 4915; return [null, null]; } # 查询剧集id $episodeId = PlatformEpisode::query()->where('platform_id', $platformId) ->where('playlet_id', $params['playlet_id']) ->where('name', '第'.$params['episode_num'].'集')->where('enable', 1) ->value('episode_id'); if(empty($episodeId)) { $errno = 4911; return [null, null]; } # 生成推广链接 $responseData = MaiBuService::savePromotion( $channelId, $platformAppId, $params['name'], $params['playlet_id'], $episodeId ); $code = $responseData['code'] ?? 1; if($code) { $msg = $responseData['msg'] ?? '未标识的错误'; EmailQueue::rPush('生成推广链接失败', $msg, ['xiaohua.hou@kuxuan-inc.com'], '生成推广链接失败'); return false; } $linkInfo = $responseData['data'] ?? []; $pid = $linkInfo['id'] ?? null; if(empty($linkInfo) || !$pid) { $errno = 4913; return [null, null]; } # 检出链接信息 $insertData = [ 'platform_id' => $platformId, 'account' => $accountId, 'sys_group_id' => $sysGroupId, 'app_id' => $params['app_id'], 'playlet_id' => $params['playlet_id'], 'episode_id' => $episodeId, 'admin_id' => $adminId, 'corpid' => $params['corpid'], 'mini_app_id' => $params['mini_app_id'], 'name' => $params['name'], 'force_subscribe' => $params['force_subscribe'], 'force_subscribe_sectionId' => $params['force_subscribe_sectionId'], 'type' => $params['type'], 'page_path' => $linkInfo['pagePath'], 'quick_h5' => $linkInfo['quickH5'], 'h5' => $linkInfo['h5'], 'hap' => $linkInfo['hap'], 'https_hap' => $linkInfo['httpsHap'], 'content_share_image' => $linkInfo['contentShareImage'], 'media_id' => $linkInfo['mediaId'], 'wx_share_description' => $linkInfo['wxShareDescription'], 'pid' => $pid ]; $result = PromoteLink::query()->insert($insertData); if(!$result) { Log::logError('迈步推广链接存储失败', ['data' => $insertData], 'CreateLink'); } if(0 == $params['link_type']) { $pagePath = $insertData['page_path'] ?? null; } else { $pagePath = $insertData['h5'] ?? null; } return [$pagePath, $pid]; } public static function setPlayletGroup($sysGroupId, $corpid, $adminId, $groupId, $name, $playletList, &$errno) { if(empty($playletList)) { $errno = 4101; // 短剧信息不能为空 return false; } if(!is_array($playletList)){ $playletList = json_decode($playletList, 1); } try{ \DB::begintransaction(); // 更新短剧组 if($groupId) { $res1 = PlatformPlayletGroup::query()->where('id', $groupId)->update([ 'name' => $name, 'corpid' => $corpid, 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId, ]); // 将现有短剧组enable更新为0 $res2 = PlatformPlayletGroupDetail::query()->where('group_id', $groupId) ->where('enable', 1)->update(['enable' => 0]); RedisModel::lPush(MassMsgLinkRecord::WELCOME_MSG_PLAYLET_GROUP_UPDATE_RDS, $groupId); } else { $res1 = $groupId = PlatformPlayletGroup::query()->insertGetId([ 'name' => $name, 'corpid' => $corpid, 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId, ]); if(!$res1) { \DB::rollBack(); $errno = 4103; Log::logError('设置剧集组接口异常状态码',[ 'sys_group_id' => $sysGroupId, 'corpid' => $corpid, 'admin_id' => $adminId, 'group_id' => $groupId, 'name' => $name, 'playlet_list' => $playletList, 'errno' => $errno, ], 'interface'); return false; } } // 循环处理剧集详情信息 foreach($playletList as $key => &$playletInfo) { $dataId = $playletInfo['data_id'] ?? 0; $item['sort_order'] = $key + 1;// 补完排序值 $item['group_id'] = $groupId;// 短剧组ID $item['enable'] = 1;// 有效性 $item['corpid'] = $corpid; $item['force_subscribe'] = 0; $item['force_subscribe_sectionId'] = 0; $item['type'] = 0; $item['name'] = $playletInfo['name']; $item['episode_num'] = $playletInfo['episode_num']; $item['app_id'] = $playletInfo['app_id']; $item['mini_app_id'] = $playletInfo['mini_app_id'] ?? null; $item['account_id'] = $playletInfo['account_id']; $item['link_type'] = $playletInfo['link_type']; $item['platform_id'] = $playletInfo['platform_id']; $item['playlet_id'] = $playletInfo['playlet_id']; $item['playlet_unique_id'] = $playletInfo['playlet_unique_id']; // 处理好的短剧信息入库 if(empty($dataId)) { $detailRes = PlatformPlayletGroupDetail::query()->insert($item); } else { $detailRes = PlatformPlayletGroupDetail::query()->where('id', $dataId)->update($item); } if(!$detailRes) { \DB::rollBack(); $errno = 4102; Log::logError('设置剧集组接口异常状态码',[ 'sys_group_id' => $sysGroupId, 'corpid' => $corpid, 'admin_id' => $adminId, 'group_id' => $groupId, 'name' => $name, 'playlet_list' => $playletList, 'errno' => $errno, ], 'interface'); return false; } } \DB::commit(); return true; } catch (\Exception $exception) { Log::logError('设置剧集组接口异常',[ 'params' => [ 'sys_group_id' => $sysGroupId, 'corpid' => $corpid, 'admin_id' => $adminId, 'group_id' => $groupId, 'name' => $name, 'playlet_list' => $playletList, ], 'exception' => [ 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ] ], 'interface'); $errno = 500; return false; } } public static function playletGroupList($sysGroupId, $page, $pageSize) { $groupQuery = PlatformPlayletGroup::query()->where('sys_group_id', $sysGroupId) ->where('enable', 1); $total = $groupQuery->count(); $offset = ($page - 1) * $pageSize; $list = $groupQuery->selectRaw('id as group_id, name')->offset($offset)->limit($pageSize) ->get(); // 提取短剧组ID $groupIdList = array_column($list->toArray(), 'group_id'); // 查询对应的短剧列表 $groupDetailList = PlatformPlayletGroupDetail::query()->whereIn('group_id', $groupIdList) ->where('enable', 1)->orderBy('sort_order', 'asc')->get(); # 查询剧封面和标题 $playletInfoList = PlatformPlaylet::query()->select('name', 'description', 'cover_url', 'playlet_id', 'platform_id')->where('enable', 1)->where('state', 1) ->get(); foreach($list as &$item) { $groupDetail = $groupDetailList->where('group_id', $item->group_id)->all(); $playletArr = []; foreach($groupDetail as $value) { $playletInfo = $playletInfoList->where('platform_id', $value->platform_id) ->where('playlet_id', $value->playlet_id)->first(); if(!empty($playletInfo)){ $playletArr[] = $playletInfo; } } $item->playlet_list = $playletArr; } return [$list, $total]; } public static function addPlayletToGroup($params, &$errno) { // 判断该剧是否已经加入短剧组 $row = PlatformPlayletGroupDetail::query()->where('platform_id', $params['platform_id']) ->where('enable', 1)->where('group_id', $params['group_id']) ->where('playlet_id', $params['playlet_id'])->first(); if($row) { $res = PlatformPlayletGroupDetail::query()->where('id', $row->id)->update($params); } else { $res = PlatformPlayletGroupDetail::query()->insert($params); } if(!$res){ $errno = 400; return false; } return true; } public static function playletGroupDetail($groupId) { $groupInfo = PlatformPlayletGroup::query()->selectRaw('id as group_id, name') ->where('id', $groupId)->first(); if(empty($groupInfo)){ return [[], 4104]; } // 查询对应的短剧列表 $groupDetailList = PlatformPlayletGroupDetail::query()->where('group_id', $groupId) ->where('enable', 1)->selectRaw('id as data_id, corpid, platform_id, '. 'account_id, app_id, mini_app_id, name, playlet_id, episode_num, link_type, playlet_unique_id') ->orderBy('sort_order', 'asc')->get(); $groupDetailList = $groupDetailList->isNotEmpty() ? $groupDetailList->toArray() : []; # 查询剧封面和标题 $playletInfoList = PlatformPlaylet::query()->select('name', 'description', 'cover_url', 'playlet_id', 'platform_id')->where('enable', 1)->where('state', 1) ->get(); foreach($groupDetailList as $key => $value) { $playletInfo = $playletInfoList->where('platform_id', $value['platform_id']) ->where('playlet_id', $value['playlet_id'])->first(); if(empty($playletInfo)) { unset($groupDetailList[$key]); continue; } $groupDetailList[$key]['playlet_name'] = $playletInfo->name ?? ''; $groupDetailList[$key]['playlet_description'] = $playletInfo->description ?? ''; $groupDetailList[$key]['playlet_cover'] = $playletInfo->cover_url; } $groupInfo->playlet_list = $groupDetailList; return [$groupInfo, 0]; } public static function playletGroupH5Detail($groupId, $sender, $ruleId, $msgType) { $list = MassMsgLinkRecord::query()->where('playlet_group_id', $groupId)->where('enable', 1) ->where('sender', $sender)->where('rule_id', $ruleId)->where('msg_type', $msgType) ->select(['playlet_id', 'platform_id', 'link_type', 'link', 'desc'])->orderBy('sort', 'asc') ->get(); $list = $list->isNotEmpty() ? $list->toArray() : []; # 查询剧封面和标题 $playletInfoList = PlatformPlaylet::query()->select('name', 'description', 'cover_url', 'platform_id', 'playlet_id')->where('enable', 1)->where('state', 1) ->get(); foreach($list as $key=>$value){ $playletInfo = $playletInfoList->where('platform_id', $value['platform_id']) ->where('playlet_id', $value['playlet_id'])->first(); if(empty($playletInfo)) { unset($list[$key]); continue; } $list[$key]['playlet_name'] = $playletInfo->name ?? ''; $list[$key]['playlet_description'] = $playletInfo->description ?? ''; $list[$key]['playlet_cover'] = $playletInfo->cover_url ?? ''; } return $list; } /* * 创建推广链接 * */ public static function paramsValidate($params, $platformId, $accountId, $sysGroupId, $adminId, &$errno) { try { # 检验小程序app_id if($platformId != 4 && empty($params['mini_app_id'])){ $errno = 4105; return ['小程序app_id必填']; } if($platformId == 4 && 0 == $params['link_type']){ $errno = 4106; return ['嘉书平台只能够创建H5类型的推广链接']; } switch($platformId) { case 4: // 嘉书H5(东方小剧场) $pagePath = self::validateJiaShuLink($params, $platformId, $accountId, $sysGroupId, $adminId,$errno); break; case 3: // 迈步 $pagePath = self::validateMaibuLink($params, $platformId, $accountId, $sysGroupId, $adminId, $errno); break; default: $errno = 4910; break; } } catch (\Exception $e) { EmailQueue::rPush('生成推广链接过程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com', 'song.shen@kuxuan-inc.com'], '生成推广链接过程发生异常'); Log::logError('迈步推广链接存储失败', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'params' => $params, 'platformId' => $platformId, 'accountId' => $accountId, 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId ], 'CreateLink'); $errno = 4914; } if($errno || !$pagePath) return []; return []; } public static function validateJiaShuLink($params, $platformId, $accountId, $sysGroupId, $adminId, &$errno) { # 查询渠道商id $channelInfo = IntelligentMassSendingAccount::query()->where('id', $accountId) ->where('platform_id', $platformId)->where('status', 1) ->where('active_status', 1)->where('enable', 1)->first(); if(empty($channelInfo)) { $errno = 4912; return []; } $customerId = $channelInfo->service_provider_id; $apiKey = $channelInfo->api_key; # 查询第三方平台app_id $platformAppId = PlatformApp::getAppExit($platformId, $customerId, $params['app_id']); if(empty($platformAppId)) { $errno = 4915; return []; } # 查询剧集 $sectionCount = PlatformPlaylet::query() ->where('platform_id', $platformId) ->where('playlet_id', $params['playlet_id']) ->where('enable', 1) ->value('section_count'); if(empty($sectionCount) || $params['episode_num'] > $sectionCount) { $errno = 4911; return []; } return true; } public static function validateMaibuLink($params, $platformId, $accountId, $sysGroupId, $adminId, &$errno) { # 查询渠道商id $channelId = IntelligentMassSendingAccount::query()->where('id', $accountId) ->where('platform_id', $platformId)->where('status', 1) ->where('active_status', 1)->where('enable', 1) ->value('service_provider_id'); if(empty($channelId)) { $errno = 4912; return []; } # 查询第三方平台app_id $platformAppId = PlatformApp::getPlatformAppId($platformId, $channelId, $params['app_id'], $params['mini_app_id']); if(empty($platformAppId)) { $errno = 4915; return []; } # 查询剧集id $episodeId = PlatformEpisode::query()->where('platform_id', $platformId) ->where('playlet_id', $params['playlet_id']) ->where('name', '第'.$params['episode_num'].'集')->where('enable', 1) ->value('episode_id'); if(empty($episodeId)) { $errno = 4911; return []; } return true; } public static function batchCreateLink($sysGroupId, $corpid, $adminId, $playletList) { if(empty($playletList)) { return ['短剧信息不能为空', 4101]; } if(!is_array($playletList)){ $playletList = json_decode($playletList, 1); } if(count($playletList) > 9) { return ['一次最多只能创建9条推广链接', 4107]; } $linkData = []; try { // 循环处理剧集详情信息 foreach($playletList as $key => $playletInfo) { # 判断必要参数 if( !isset($playletInfo['platform_id']) || !isset($playletInfo['account_id']) || !isset($playletInfo['app_id']) || !isset($playletInfo['name']) || !isset($playletInfo['playlet_id']) || !isset($playletInfo['episode_num']) || !isset($playletInfo['link_type'])) { $errno = 1103; $linkData[] = [ 'link_data' => $playletInfo, 'err_no' => $errno, 'err_msg' => Error::getError($errno) ]; continue; } if($playletInfo['platform_id'] == 4 && 0 == $playletInfo['link_type']){ $errno = 4106; $linkData[] = [ 'link_data' => $playletInfo, 'err_no' => $errno, 'err_msg' => Error::getError($errno) ]; continue; } if(!isset($playletInfo['page']) || empty($playletInfo['page'])) { $playletInfo['corpid'] = $corpid; $playletInfo['force_subscribe'] = 0; $playletInfo['force_subscribe_sectionId'] = 0; $playletInfo['type'] = 0; $errno = 0; $linkInfo = PlatformService::createLink( $playletInfo, $playletInfo['platform_id'], $playletInfo['account_id'], $sysGroupId, $adminId, $errno ); if(empty($linkInfo) || $errno > 0) { $linkData[] = [ 'link_data' => $playletInfo, 'err_no' => $errno, 'err_msg' => Error::getError($errno) ]; } else { $linkInfo = array_merge($linkInfo, $playletInfo); $linkData[] = [ 'link_data' => $linkInfo, 'err_no' => 0, 'err_msg' => '' ]; } if(in_array($linkInfo, [4])) {// 嘉书平台限制1秒钟请求一次接口 sleep(1); } } else { $linkData[] = [ 'link_data' => $playletInfo, 'err_no' => 0, 'err_msg' => '' ]; } } return [$linkData, 0]; } catch (\Exception $e) { EmailQueue::rPush('批量创建推广链接过程发生异常', $e->getTraceAsString(), [ 'song.shen@kuxuan-inc.com'], '批量创建推广链接过程发生异常'); Log::logError('批量创建推广链接过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'playlet_list' => $playletList, 'corpid' => $corpid, 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId ], 'CreateLink'); $errno = 4914; return [$linkData, $errno]; } } }