| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | <?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/4/29 0029 * Time: 17:16 */namespace App\Http\Logics\Admin;use App\Http\Models\ILog;use App\Http\Models\Site;use App\Http\Models\Social;use App\Http\Models\SocialPublish;use DirkGroenen\Pinterest\Pinterest;use Facebook\Facebook;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\File;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Storage;use Illuminate\Support\Str;use Lightit\LinkedinShare\Facades\LinkedinShare;use Thujohn\Twitter\Facades\Twitter;class SocialLogic{    public function pin($imgPath, $url, $content, $board, $socialInfo)    {        if (!$board) { //本意不想发            return [SocialPublish::STATUS_NO_NEED, 'pin board不存在'];        }        $err = '';        if (empty($socialInfo['token'])) {            $err .= 'pin token 不存在';        }        if (!$imgPath) {            $err .= '图片不存在或不合法';        }        if ($err) {            return [SocialPublish::STATUS_FAILURE, $err];        }        $content = mb_substr($content, 0, 450);        //pin绝对路径居然不行?        // https不行        $imgPath = str_replace('https', 'http', sprintf('%s%s', config('app.url'), $imgPath));        $pin = new Pinterest($socialInfo['app_id'], $socialInfo['app_secret']);        $pin->auth->setOAuthToken($socialInfo['token']);        try {            $pin->pins->create([                "note" => $content,                "image_url" => $imgPath,                "board" => $board,                'link' => $url            ]);        } catch (\Throwable $throwable) {            return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)];        }        return [SocialPublish::STATUS_SUCCESS, ''];    }    public function linkedIn($content, $orgId)    {        if (empty(trim($orgId))) {            return [SocialPublish::STATUS_NO_NEED, 'linkedIn orgId 不存在'];        }        $socialInfo = Social::query()->where(['type' => Social::LINKED, 'site_id' => 0])->first();        if (empty($socialInfo->token)) {            return [SocialPublish::STATUS_FAILURE, 'linkedIn token 不存在'];        }        $content = mb_substr($content, 0, 450);        try {            LinkedinShare::shareNoneCompany($socialInfo->token, $content, $orgId, 'accessToken');        } catch (\Throwable $throwable) {            Log::warning(sprintf('linedIn:%s', var_export($throwable->getMessage(), 1)));            return [SocialPublish::STATUS_FAILURE, var_export(substr($throwable->getMessage(), 0, 200), 1)];        }        return [SocialPublish::STATUS_SUCCESS, ''];    }    public function twitter($imgPath, $url, $content, $socialInfo)    {        if (empty($socialInfo['token']) || empty($socialInfo['token_secret'])) {            return [SocialPublish::STATUS_FAILURE, 'twitter的token或secret 不存在'];        }        $newContent = sprintf('%s %s', $url, $content);        $content = mb_substr(trim($newContent), 0, 280);        Twitter::reconfig([            'consumer_key' => config('services.twitter.client_id'),            'consumer_secret' => config('services.twitter.client_secret'),            'token' => $socialInfo['token'],            'secret' => $socialInfo['token_secret']        ]);        try {            if ($imgPath) {                $imgPath = public_path($imgPath);                $uploaded_media = Twitter::uploadMedia(['media' => File::get($imgPath)]);                $addition = ['media_ids' => $uploaded_media->media_id_string];            }            Twitter::postTweet(['status' => $content] + ($addition ?? []));//            ILog::query()->create([//                'type' => 'twitter',//                'content' => var_export($result, 1)//            ]);        } catch (\Throwable $throwable) {            return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)];        }        return [SocialPublish::STATUS_SUCCESS, ''];    }    public function facebookTrue($content, $url, $siteInfo)    {        if (empty($siteInfo['facebook_page']) || empty($siteInfo['facebook_page_token'])) {            return [SocialPublish::STATUS_FAILURE, '主页信息不存在'];        }        try {            $fb = new Facebook([                'app_id' => env('FACEBOOK_CLIENT_ID'),                'app_secret' => env('FACEBOOK_CLIENT_SECRET'),                'default_access_token' => $siteInfo['facebook_page_token'], // optional            ]);            $linkData = [                'link' => $url,                'message' => $content,            ];//            $pageId = '2822252084511458';            $pageId = $siteInfo['facebook_page'];            $response = $fb->post(sprintf('/%s/feed', $pageId), $linkData);            ILog::query()->create([                'type' => 'facebook',                'content' => var_export($response->getBody(), 1)            ]);        } catch (\Throwable $throwable) {            return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)];        }        return [SocialPublish::STATUS_SUCCESS, ''];    }    /**     * 获取图片     * @param $img     * @return string     */    public function getImg($img)    {        if (!$img) {            return false;        }        try {            $pathInfo = pathinfo($img);            $imgContent = file_get_contents($img);            $filePath = sprintf('social/%s.%s', Str::random(40), $pathInfo['extension'] ?? '');            Storage::disk('public')->put($filePath, $imgContent, 'public');            return sprintf('/storage/%s', $filePath);        } catch (\Throwable $throwable) {            Log::warning('getImg:' . var_export($throwable->getMessage(), 1));            return false;        }    }    /**     * 获取链接     * @param $siteId     * @param $remoteContentId     * @return string     */    protected function getUrl($siteId, $remoteContentId)    {        $site = Site::query()->with('server')->select(['id', 'server_id', 'database', 'domain'])->where(['id' => $siteId])->first();        if (empty($site->database) || empty($site->server)) {            Log::warning('getUrl: 数据库或服务器信息不存在');            return false;        }        $config = [            'connection_name' => sprintf('connection_name_%s', $site->id),            'host' => $site->server->server_ip,            'port' => 3306,            'database' => $site->database,            'username' => $site->server->mysql_user_name,            'password' => $site->server->mysql_passwd,        ];        config_connection($config);        try {            $record = DB::connection($config['connection_name'])->table('content')->where(['id' => $remoteContentId])->first();            if ($record) {                $url = sprintf('http://%s/%s.html', $site->domain, $record->uri ?? '');                return $url;            }            Log::warning('getUrl: 远程记录信息不存在');        } catch (\Throwable $throwable) {            Log::warning('getUrl:' . var_export($throwable->getMessage(), 1));        }        return false;    }//    public function articlePublish($article, &$sitesMap)//    {//        $img = sprintf('%s%s', config('app.admin_url'), $article->thumb);//        $imgPath = $this->getImg($img);////        $url = $this->getUrl($article->site_id, $article->remote_content_id);//        if (!$url) {//            return false;//        }////        $content = str_replace('<br/>', PHP_EOL, html_entity_decode(strip_tags($article->translate_content, '<br/>')));//////        $linkedIn = $this->linkedIn($content, $url);//        $pin = $this->pin($imgPath, $content);//        $twitter = $this->twitter($imgPath, $content);////        $pageId = $sitesMap[$article->site_id]['facebook_page'] ?? null;//        $facebook = $this->facebookTrue($content, $url, $pageId);//        return compact('linkedIn', 'twitter', 'pin', 'facebook');//    }    public function publish($record, $sitesMap)    {        $socialInfo = Social::query()->where(['site_id' => $record->site_id])->get()->keyBy('type')->toArray();        $imgPath = $this->getImg($record->media);        $content = str_replace('<br/>', PHP_EOL, html_entity_decode(strip_tags($record->content, '<br/>')));        list($linkedInStatus, $linkedErr) = $this->linkedIn($content, $record->linkedin_orgid);        list($pinStatus, $pinErr) = $this->pin($imgPath, $record->url, $content, $record->pin_board, ($socialInfo[Social::PIN] ?? null));        list($twStatus, $twErr) = $this->twitter($imgPath, $record->url, $content, ($socialInfo[Social::TWITTER] ?? null));//        $siteInfo = $sitesMap[$record->site_id] ?? null;//        list($fbStatus, $fbErr) = $this->facebookTrue($content, $record->url, $siteInfo);        return [            ['pin' => $pinErr, 'twitter' => $twErr, 'linkedIn' => $linkedErr],            ['pin' => $pinStatus, 'twitter' => $twStatus,'linkedIn' => $linkedInStatus]        ];    }}
 |