123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/4/15 0015
- * Time: 14:44
- */
- namespace App\Http\Logics\Admin;
- use App\Http\Models\CustomerLog;
- use App\Http\Models\DemoLog;
- use App\Http\Models\HootsuiteHistory;
- use App\Http\Models\HootsuiteUser;
- use App\Http\Models\Role;
- use App\Http\Models\Site;
- use App\Http\Models\SiteAddition;
- use App\Http\Models\SiteEnterprise;
- use App\Http\Models\SiteInfo;
- use App\Http\Models\SiteMarketer;
- use App\Http\Models\SitePayment;
- use App\Http\Models\SiteSns;
- use App\Http\Models\SiteStation;
- use App\Http\Models\Social;
- use App\Http\Models\SocialPublish;
- use App\Http\Models\User;
- use App\Http\Services\TemplateLibraryApiService;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use \Illuminate\Database\ConnectionInterface;
- use GuzzleHttp\Client;
- class SiteLogic
- {
- public function updateOrCreate($validated, $id)
- {
- if (strpos($validated['site']['domain'], 'www') !== false) {
- return response()->json(['message' => '域名不要包含www'], 400);
- }
- if ($existsSite = Site::query()->where(['domain' => $validated['site']['domain']])->first()) {
- if ($existsSite->id != $id) {
- return response()->json(['message' => '域名已存在'], 400);
- }
- }
- if ($id == 0) {
- $validated['site']['identifier'] = (new Site)->identifier();
- if (empty($validated['site']['old_id'])) { //如果填写了说明是之前 rank上有 这里只是把它关联起来
- try { //如果没有的话那就是默认要创建 并且关联
- $siteSync = new SiteSyncLogic();
- $oldId = $siteSync->insertGetId($siteSync->fieldMap($validated['site']));
- $validated['site']['old_id'] = $oldId;
- } catch (\Throwable $throwable) {
- Log::error(var_export($throwable->getMessage(), 1));
- return response()->json(['message' => '远程添加项目出错'], 400);
- }
- }
- $validated['site']['status'] = 1; //建站期
- $validated['site']['server_id'] = 1; //20服务器
- $site = Site::query()->create($validated['site']);
- $siteId = $site->id;
- SiteInfo::query()->create([
- 'site_id' => $siteId,
- ]);
- DB::table('user_has_sites')->insert([
- 'site_id' => $siteId,
- 'user_id' => Auth::id()
- ]);
- $validated['payment']['site_id'] = $siteId;
- SitePayment::query()->create($validated['payment']);
- (new ProcessLogic)->initProcess($siteId); //生成初始项目流程
- } else {
- $site = Site::query()->where(['id' => $id])->first();
- if (!$site) return response()->json(['message' => '数据不存在']);
- $site->update($validated['site']);
- SitePayment::query()->where(['site_id' => $id])->update($validated['payment']);
- // 同步老rank平台的数据
- $siteSync = new SiteSyncLogic();
- $siteSync->update($site->old_id, $siteSync->fieldMap($site->toArray()));
- }
- return response()->json(['message' => '操作成功']);
- }
- /**
- * 客户完善处理
- * @param $validated
- * @param $act
- * @param $source
- * @param $siteId
- * @return \Illuminate\Http\JsonResponse
- */
- public function customerHandle($validated, $act, $source, $siteId)
- {
- // $process = Process::query()->where(['identifier' => 'know_customer_info'])->first();
- // if (!$process) return response()->json(['message' => '总流程信息不存在'], 400);
- // SiteProcess::query()->where(['site_id' => $siteId, 'process_id' => $process->id])->update(['active' => 1]);
- $user = auth()->user();
- $site = Site::query()->where(['id' => $siteId])->first();
- if (!$site) {
- return response()->json(['message' => '站点信息不存在'], 400);
- }
- $siteMap = ['site_id' => $siteId];
- // $source = 'customer';
- switch ($act) {
- case 'addition':
- if ($source == 'customer') {
- $addition = SiteAddition::query()->where($siteMap)->first();
- $additionArr = $addition ? $addition->toArray() : [];
- $diff = array_diff_assoc($validated['addition'], $additionArr);
- if ($diff) {
- $oldData = [];
- $diffKeys = array_keys($diff);
- foreach ($diffKeys as $inx) {
- $oldData[$inx] = $additionArr[$inx] ?? '';
- }
- CustomerLog::query()->create([
- 'site_id' => $siteId,
- 'title' => '网站信息',
- 'old_content' => serialize($oldData),
- 'content' => serialize($diff),
- 'operator_id' => $user->id,
- 'operator_name' => $user->nickname,
- 'site_name' => $site->cn_title,
- 'type' => $act
- ]);
- }
- }
- SiteAddition::query()->updateOrCreate($siteMap, $validated['addition'] + $siteMap);
- break;
- case 'keyword':
- if ($source == 'customer') {
- $keyword = SiteInfo::query()->where($siteMap)->first();
- $keywordArr = $keyword ? $keyword->keyword : [];
- $inputKeyword = $validated['keyword'] ?? [];
- $diff = array_diff_assoc($inputKeyword, $keywordArr);
- if ($diff) {
- $oldData = [];
- $diffKeys = array_keys($diff);
- foreach ($diffKeys as $inx) {
- $oldData[$inx] = $additionArr[$inx] ?? '';
- }
- CustomerLog::query()->create([
- 'site_id' => $siteId,
- 'title' => '核心关键词',
- 'old_content' => serialize($oldData),
- 'content' => serialize($diff),
- 'operator_id' => $user->id,
- 'operator_name' => $user->nickname,
- 'site_name' => $site->cn_title,
- 'type' => $act
- ]);
- }
- }
- SiteInfo::query()->updateOrCreate($siteMap, ['keyword' => ($validated['keyword'] ?? [])] + $siteMap);
- break;
- case 'station':
- if ($source == 'customer') {
- // $record = SiteStation::query()->where($siteMap)->first();
- // $recordArr = $record ? $record->toArray() : [];
- // dump($validated['station'],$recordArr);
- // $diff = array_diff_assoc($validated['station'], $recordArr);
- // if ($diff) {
- //
- // $oldData = [];
- // $diffKeys = array_keys($diff);
- // foreach ($diffKeys as $inx) {
- // $oldData[$inx] = $additionArr[$inx] ?? '';
- // }
- //
- // CustomerLog::query()->create([
- // 'site_id' => $siteId,
- // 'title' => '其他项目信息',
- // 'old_content' => serialize($oldData),
- // 'content' => serialize($diff),
- //
- // 'operator_id' => $user->id,
- // 'operator_name' => $user->nickname,
- // 'site_name' => $site->cn_title,
- // 'type' => $act
- // ]);
- // }
- }
- $validated['station']['compete_sites'] = $validated['station']['compete_sites'] ?? null;
- SiteStation::query()->updateOrCreate($siteMap, $validated['station'] + $siteMap);
- break;
- case 'enterprise':
- if ($source == 'customer') {
- $record = SiteEnterprise::query()->where($siteMap)->first();
- $recordArr = $record ? $record->toArray() : [];
- $diff = array_diff_assoc($validated['enterprise'], $recordArr);
- if ($diff) {
- $oldData = [];
- $diffKeys = array_keys($diff);
- foreach ($diffKeys as $inx) {
- $oldData[$inx] = $additionArr[$inx] ?? '';
- }
- CustomerLog::query()->create([
- 'site_id' => $siteId,
- 'title' => '项目基础信息',
- 'old_content' => serialize($oldData),
- 'content' => serialize($diff),
- 'operator_id' => $user->id,
- 'operator_name' => $user->nickname,
- 'site_name' => $site->cn_title,
- 'type' => $act
- ]);
- }
- }
- SiteEnterprise::query()->updateOrCreate($siteMap, $validated['enterprise'] + $siteMap);
- break;
- case 'marketer':
- if ($source == 'customer') {
- $record = SiteMarketer::query()->where($siteMap)->first();
- $recordArr = $record ? $record->toArray() : [];
- $diff = array_diff_assoc($validated['marketer'], $recordArr);
- if ($diff) {
- $oldData = [];
- $diffKeys = array_keys($diff);
- foreach ($diffKeys as $inx) {
- $oldData[$inx] = $additionArr[$inx] ?? '';
- }
- CustomerLog::query()->create([
- 'site_id' => $siteId,
- 'title' => '营销发布人信息',
- 'old_content' => serialize($oldData),
- 'content' => serialize($diff),
- 'operator_id' => $user->id,
- 'operator_name' => $user->nickname,
- 'site_name' => $site->cn_title,
- 'type' => $act
- ]);
- }
- }
- SiteMarketer::query()->updateOrCreate($siteMap, $validated['marketer'] + $siteMap);
- break;
- case 'sns':
- if ($source == 'customer') {
- $record = SiteSns::query()->where($siteMap)->first();
- $recordArr = $record ? $record->toArray() : [];
- $diff = array_diff_assoc($validated['sns'], $recordArr);
- if ($diff) {
- $oldData = [];
- $diffKeys = array_keys($diff);
- foreach ($diffKeys as $inx) {
- $oldData[$inx] = $additionArr[$inx] ?? '';
- }
- CustomerLog::query()->create([
- 'site_id' => $siteId,
- 'title' => 'SNS 社交资料',
- 'old_content' => serialize($oldData),
- 'content' => serialize($diff),
- 'operator_id' => $user->id,
- 'operator_name' => $user->nickname,
- 'site_name' => $site->cn_title,
- 'type' => $act
- ]);
- }
- }
- SiteSns::query()->updateOrCreate($siteMap, $validated['sns'] + $siteMap);
- break;
- default:
- return response()->json(['message' => '参数提交错误'], 400);
- }
- return response()->json(['message' => '操作成功']);
- }
- public function overview($siteId, $validated, $type)
- {
- if ($type == 1) { //基本信息
- if (strpos($validated['site']['domain'], 'www') !== false) {
- return response()->json(['message' => '域名不要包含www'], 400);
- }
- if ($existsSite = Site::query()->where(['domain' => $validated['site']['domain']])->first()) {
- if ($existsSite->id != $siteId) {
- return response()->json(['message' => '域名已存在'], 400);
- }
- }
- $site = Site::query()->with(['server'])->where(['id' => $siteId])->first();
- if (!$site) return response()->json(['message' => '数据不存在'], 400);
- if ($validated['site']['keyword_goal'] != $site->keyword_goal && $site->old_id) {
- $homepageReachCount = DB::connection('rank')->table('project_history')->select('top10')
- ->where([
- ['project_id', '=', (int)$site->old_id],
- ['create_time', '<', time()]
- ])->orderByDesc('create_time')->first();
- if ($validated['site']['keyword_goal'] > ($homepageReachCount->top10 ?? 0)) {
- $rankUpdate = [
- 'reach_days' => 0, //达标天数
- 'expire_time' => 0, //预期到期时间
- ];
- }
- }
- $site->update($validated['site']);
- //同步老rank平台的数据
- $siteSync = new SiteSyncLogic();
- if (!empty($site->server)) {
- $server = ['server_ip' => $site->server->server_ip];
- }
- $siteSync->update($site->old_id, $siteSync->fieldMap($site->toArray() + ($server ?? [])) + ($rankUpdate ?? []));
- if ($site->status == 7) {
- DB::connection('rank')->table('project')->where(['id' => $site->old_id])->update(['allow_googlerank' => 0]);
- }
- //同步到模版站
- if ($site->is_stencil == 1) {
- $connection = DB::connection('template');
- $update = [
- 'site_id' => $siteId,
- 'cn_title' => $site->cn_title ?? '',
- 'en_title' => $site->en_title ?? '',
- 'site_logo' => $site->site_logo ?? '',
- 'domain' => $site->domain ?? '',
- 'brand' => $site->brand ?? '',
- 'ip' => $site->server->server_ip ?? '',
- 'database' => $site->database ?? '',
- 'username' => $site->server->mysql_user_name ?? '',
- 'password' => $site->server->mysql_passwd ?? '',
- 'create_time' => date('Y-m-d')
- ];
- $result = $connection->table('template_library_project_config')->where('site_id', $siteId)->first();
- if (empty($result)) {
- $connection->table('template_library_project_config')->insert($update);
- } else {
- $connection->table('template_library_project_config')->where('site_id', $siteId)->update($update);
- }
- }
- return response()->json(['message' => '操作成功']);
- } elseif ($type == 2) {
- //详细信息
- $site = Site::query()->with(['users'])->where(['id' => $siteId])->first();
- if (!$site) return response()->json(['message' => '数据不存在'], 400);
- $validated['payment']['google_bid'] = json_encode($validated['payment']['google_bid']);
- SitePayment::query()->where(['site_id' => $siteId])->update($validated['payment']);
- return response()->json(['message' => '操作成功']);
- } elseif ($type == 3) {
- //详细信息
- $site = Site::query()->with(['users'])->where(['id' => $siteId])->first();
- if (!$site) return response()->json(['message' => '数据不存在'], 400);
- $userIds = array_merge(
- $validated['allot']['editor_ids'] ?? [],
- $validated['allot']['server_ids'] ?? [],
- $validated['allot']['article_ids'] ?? [],
- $validated['allot']['manager_ids'] ?? [],
- $validated['allot']['manage_helper_ids'] ?? [],
- $validated['allot']['planner_ids'] ?? [],
- $validated['allot']['optimizer_ids'] ?? [],
- $validated['allot']['bid_user_ids'] ?? [],
- $validated['allot']['seller_ids'] ?? [],
- $validated['allot']['web_ids'] ?? [],
- $validated['allot']['designer_ids'] ?? [],
- $validated['allot']['article_edit_ids'] ?? [],
- $validated['allot']['external_link_ids'] ?? [],
- $validated['allot']['chongqing_link_ids'] ?? [],
- $validated['allot']['optimization_editing_ids'] ?? []
- );
- foreach ($userIds as $key => $value) {
- if (empty($value)) {
- unset($userIds[$key]);
- }
- }
- $beforeUserIds = DB::table('user_has_sites')->where(['site_id' => $site->id])->pluck('user_id')->toArray();
- $updateUserIds = User::query()->whereIn('role_id', [
- Role::TYPE_AE,
- Role::TYPE_ARTICLE,
- Role::TYPE_SERVER,
- Role::TYPE_MANAGER,
- Role::TYPE_MANAGE_HELPER,
- Role::TYPE_OPTIMIZER,
- Role::TYPE_SELLER,
- Role::TYPE_BID,
- Role::TYPE_WEB,
- Role::TYPE_DESIGNER,
- Role::TYPE_PROPAGANDA,
- Role::TYPE_PLANNER,
- Role::TYPE_LINK,
- Role::TYPE_OPTIMIZATION_EDITING,
- Role::TYPE_SOFT_TEXT_SEARCH,
- Role::TYPE_LINK_PART_CHONGQING,
- ])->whereIn('id', $beforeUserIds)->pluck('id')->toArray();
- $keepUserIds = [];
- foreach ($beforeUserIds as $beforeUserId) {
- if (!in_array($beforeUserId, $updateUserIds)) {
- $keepUserIds[] = $beforeUserId;
- }
- }
- $saveUserIds = array_unique(array_merge($userIds, $keepUserIds));
- $site->users()->sync($saveUserIds);
- return response()->json(['message' => '操作成功']);
- } elseif ($type == 4) {
- $site = Site::query()->with(['server'])->where(['id' => $siteId])->first();
- if (!$site) return response()->json(['message' => '数据不存在'], 400);
- //更改为实施期去掉当月上线目标
- if ($validated['site']['status'] == 2) {
- $validated['site']['is_current_month_online'] = 0;
- }
- $site->update($validated['site']);
- if ($site->status == 7 || $site->status == 9 || $site->status == 10) {
- DB::connection('rank')->table('project')->where(['id' => $site->old_id])->update(['allow_googlerank' => 0]);
- }
- return response()->json(['message' => '操作成功']);
- } elseif ($type == 5) {
- $site = Site::query()->with(['server'])->where(['id' => $siteId])->first();
- if (!$site) return response()->json(['message' => '数据不存在'], 400);
- $site->update($validated['site']);
- //同步老rank平台的数据
- $siteSync = new SiteSyncLogic();
- if (!empty($site->server)) {
- $server = ['server_ip' => $site->server->server_ip];
- }
- $siteSync->update($site->old_id, $siteSync->fieldMap($site->toArray() + ($server ?? [])) + ($rankUpdate ?? []));
- return response()->json(['message' => '操作成功']);
- }
- }
- public function socialPublishBak($validated, ConnectionInterface $connection, $site)
- {
- $contentRecords = $connection->table('content')->whereIn('id', $validated['checkIds'])->get();
- $queues = [];
- $socialData = Social::query()->where(['site_id' => $site->id, 'type' => Social::LINKED])->first();
- foreach ($contentRecords as $inx => $record) {
- if (empty($record->uri) || empty($record->summary)) {
- continue;
- }
- $thumb = trim($record->thumb);
- $url = sprintf('http://%s/%s', $site->domain, $record->uri);
- if (!$connection->table('content')->where(['parent_id' => $record->id])->exists()) {
- $url .= '.html';
- }
- $queues[] = [
- 'site_id' => $site->id,
- 'remote_content_id' => $record->id,
- 'media' => $thumb ? sprintf('http://%s%s', $site->domain, $record->thumb) : null,
- 'url' => $url,
- 'content' => str_replace('<br/>', PHP_EOL, html_entity_decode(strip_tags($record->summary, '<br/>'))),
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- 'publish_at' => date('Y-m-d H:i:s', strtotime('+' . ($validated['release_interval'] * $inx) . ' day', strtotime($validated['release_at']))),
- 'pin_board' => $validated['pin_board'],
- 'linkedin_orgid' => $socialData->linkedin_orgid ?? ''
- ];
- }
- SocialPublish::query()->insert($queues);
- return response()->json(['message' => sprintf('操作成功!%s条记录符合!', count($queues))]);
- }
- public function socialPublish($validated, ConnectionInterface $connection, $site,$hootsuiteUser_id)
- {
- $label = '';
- if (!empty($validated['label_id'])) {
- $labelList = DB::table('social_label')
- ->where('site_id', $site->id)
- ->whereIn('id', $validated['label_id'])
- ->pluck('label');
- foreach ($labelList as $value) {
- $label .= '#' . $value . ' ';
- }
- }
- $contentRecords = $connection->table('content')->whereIn('id', $validated['checkIds'])->get();
- $queues = [];
- $client = new Client();
- //$hootsuiteUser = HootsuiteUser::query()->where(['id' => $site->id])->first();
- if(empty($hootsuiteUser_id)){
- $hootsuiteUser = HootsuiteUser::query()->where(['site_id' => $site->id])->first();
- }else{
- $hootsuiteUser = HootsuiteUser::query()->where(['id' => $hootsuiteUser_id])->first();
- }
- $socialProfiles = $hootsuiteUser->social_profiles ?? [];
- $mapSocialProfiles = array_column($socialProfiles, null, 'id');
- foreach ($contentRecords as $inx => $record) {
- if (empty($record->uri) || empty($record->summary)) {
- continue;
- }
- $thumb = trim($record->thumb);
- $string = substr($site->webmaster_domain, -1, 1);
- if ($string != '/') {
- $remoteImg = $thumb ? $site->webmaster_domain . $record->thumb : null;
- } else {
- $remoteImg = $thumb ? substr($site->webmaster_domain, 0, -1) . $record->thumb : null;
- }
- $publishAt = date('Y-m-d H:i:s', strtotime('+' . ($validated['release_interval'] * $inx) . ' day', strtotime($validated['release_at'])));
- $content = str_replace('<br/>', PHP_EOL, html_entity_decode(strip_tags($record->summary, '<br/>')));
- if (strtotime($publishAt) < (time() + 60 * 10)) {
- return response()->json(['message' => '发布时间距离当前时间过近或小于当前时间'], 400);
- }
- if (!$connection->table('content')->where(['parent_id' => $record->id])->exists()) {
- $webmasterDomain = $site->webmaster_domain . $record->uri . '.html';
- } else {
- $webmasterDomain = $site->webmaster_domain . $record->uri;
- }
- $newContent = $content . ' ' . $webmasterDomain . ' ' . $label;
- $lens = mb_strlen($newContent);
- if ($lens > 280) {
- $content = mb_substr($content, 0, 277 - $lens) . '...';
- $newContent = $content . ' ' . $webmasterDomain . ' ' . $label;
- }
- $tempSocialProfiles = [];
- foreach ($mapSocialProfiles as $key => $item) {
- if (in_array($key, $validated['social_ids'])) {
- $tempSocialProfiles[] = $item;
- }
- }
- $url = sprintf('%s/admin/hootsuite/schedule-message', config('app.wall_url'));
- try {
- $response = $client->request('POST', $url, [
- 'form_params' => [
- 'siteId' => $site->id,
- 'socialIds' => $validated['social_ids'],
- 'remoteImg' => $remoteImg,
- 'content' => $newContent,
- 'sendAt' => $publishAt
- ]
- ]);
- if ($response->getStatusCode() != 200) {
- return response()->json(['message' => "发送失败"], 400);
- }
- $result = json_decode($response->getBody()->getContents(), true);
- if (!empty($result['errors'])) {
- return response()->json(['message' => ($result['errors'][0]['message'] ?? 'schedule error')], 400);
- }
- $queues = [
- 'site_id' => $site->id,
- 'remote_content_id' => $record->id,
- 'media' => $remoteImg,
- 'url' => $webmasterDomain,
- 'content' => $newContent,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- 'publish_at' => $publishAt,
- 'social_ids' => json_encode($validated['social_ids'] ?? []),
- 'social_profiles' => json_encode($tempSocialProfiles ?? []),
- 'result' => json_encode($result['data'] ?? []),
- ];
- HootsuiteHistory::query()->insert($queues);
- } catch (\GuzzleHttp\Exception\GuzzleException $exception) {
- Log::error(var_export($exception->getMessage(), 1));
- return response()->json(['message' => '发布失败'], 400);
- }
- }
- return response()->json(['message' => sprintf('操作成功!%s条记录符合!', count($queues))]);
- }
- }
|