SocialLogic.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/29 0029
  6. * Time: 17:16
  7. */
  8. namespace App\Http\Logics\Admin;
  9. use App\Http\Models\ILog;
  10. use App\Http\Models\Site;
  11. use App\Http\Models\Social;
  12. use App\Http\Models\SocialPublish;
  13. use DirkGroenen\Pinterest\Pinterest;
  14. use Facebook\Facebook;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\File;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Storage;
  19. use Illuminate\Support\Str;
  20. use Lightit\LinkedinShare\Facades\LinkedinShare;
  21. use Thujohn\Twitter\Facades\Twitter;
  22. class SocialLogic
  23. {
  24. public function pin($imgPath, $url, $content, $board, $socialInfo)
  25. {
  26. if (!$board) { //本意不想发
  27. return [SocialPublish::STATUS_NO_NEED, 'pin board不存在'];
  28. }
  29. $err = '';
  30. if (empty($socialInfo['token'])) {
  31. $err .= 'pin token 不存在';
  32. }
  33. if (!$imgPath) {
  34. $err .= '图片不存在或不合法';
  35. }
  36. if ($err) {
  37. return [SocialPublish::STATUS_FAILURE, $err];
  38. }
  39. $content = mb_substr($content, 0, 450);
  40. //pin绝对路径居然不行?
  41. // https不行
  42. $imgPath = str_replace('https', 'http', sprintf('%s%s', config('app.url'), $imgPath));
  43. $pin = new Pinterest($socialInfo['app_id'], $socialInfo['app_secret']);
  44. $pin->auth->setOAuthToken($socialInfo['token']);
  45. try {
  46. $pin->pins->create([
  47. "note" => $content,
  48. "image_url" => $imgPath,
  49. "board" => $board,
  50. 'link' => $url
  51. ]);
  52. } catch (\Throwable $throwable) {
  53. return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)];
  54. }
  55. return [SocialPublish::STATUS_SUCCESS, ''];
  56. }
  57. public function linkedIn($content, $orgId)
  58. {
  59. if (empty(trim($orgId))) {
  60. return [SocialPublish::STATUS_NO_NEED, 'linkedIn orgId 不存在'];
  61. }
  62. $socialInfo = Social::query()->where(['type' => Social::LINKED, 'site_id' => 0])->first();
  63. if (empty($socialInfo->token)) {
  64. return [SocialPublish::STATUS_FAILURE, 'linkedIn token 不存在'];
  65. }
  66. $content = mb_substr($content, 0, 450);
  67. try {
  68. LinkedinShare::shareNoneCompany($socialInfo->token, $content, $orgId, 'accessToken');
  69. } catch (\Throwable $throwable) {
  70. Log::warning(sprintf('linedIn:%s', var_export($throwable->getMessage(), 1)));
  71. return [SocialPublish::STATUS_FAILURE, var_export(substr($throwable->getMessage(), 0, 200), 1)];
  72. }
  73. return [SocialPublish::STATUS_SUCCESS, ''];
  74. }
  75. public function twitter($imgPath, $url, $content, $socialInfo)
  76. {
  77. if (empty($socialInfo['token']) || empty($socialInfo['token_secret'])) {
  78. return [SocialPublish::STATUS_FAILURE, 'twitter的token或secret 不存在'];
  79. }
  80. $newContent = sprintf('%s %s', $url, $content);
  81. $content = mb_substr(trim($newContent), 0, 280);
  82. Twitter::reconfig([
  83. 'consumer_key' => config('services.twitter.client_id'),
  84. 'consumer_secret' => config('services.twitter.client_secret'),
  85. 'token' => $socialInfo['token'],
  86. 'secret' => $socialInfo['token_secret']
  87. ]);
  88. try {
  89. if ($imgPath) {
  90. $imgPath = public_path($imgPath);
  91. $uploaded_media = Twitter::uploadMedia(['media' => File::get($imgPath)]);
  92. $addition = ['media_ids' => $uploaded_media->media_id_string];
  93. }
  94. Twitter::postTweet(['status' => $content] + ($addition ?? []));
  95. // ILog::query()->create([
  96. // 'type' => 'twitter',
  97. // 'content' => var_export($result, 1)
  98. // ]);
  99. } catch (\Throwable $throwable) {
  100. return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)];
  101. }
  102. return [SocialPublish::STATUS_SUCCESS, ''];
  103. }
  104. public function facebookTrue($content, $url, $siteInfo)
  105. {
  106. if (empty($siteInfo['facebook_page']) || empty($siteInfo['facebook_page_token'])) {
  107. return [SocialPublish::STATUS_FAILURE, '主页信息不存在'];
  108. }
  109. try {
  110. $fb = new Facebook([
  111. 'app_id' => env('FACEBOOK_CLIENT_ID'),
  112. 'app_secret' => env('FACEBOOK_CLIENT_SECRET'),
  113. 'default_access_token' => $siteInfo['facebook_page_token'], // optional
  114. ]);
  115. $linkData = [
  116. 'link' => $url,
  117. 'message' => $content,
  118. ];
  119. // $pageId = '2822252084511458';
  120. $pageId = $siteInfo['facebook_page'];
  121. $response = $fb->post(sprintf('/%s/feed', $pageId), $linkData);
  122. ILog::query()->create([
  123. 'type' => 'facebook',
  124. 'content' => var_export($response->getBody(), 1)
  125. ]);
  126. } catch (\Throwable $throwable) {
  127. return [SocialPublish::STATUS_FAILURE, var_export($throwable->getMessage(), 1)];
  128. }
  129. return [SocialPublish::STATUS_SUCCESS, ''];
  130. }
  131. /**
  132. * 获取图片
  133. * @param $img
  134. * @return string
  135. */
  136. public function getImg($img)
  137. {
  138. if (!$img) {
  139. return false;
  140. }
  141. try {
  142. $pathInfo = pathinfo($img);
  143. $imgContent = file_get_contents($img);
  144. $filePath = sprintf('social/%s.%s', Str::random(40), $pathInfo['extension'] ?? '');
  145. Storage::disk('public')->put($filePath, $imgContent, 'public');
  146. return sprintf('/storage/%s', $filePath);
  147. } catch (\Throwable $throwable) {
  148. Log::warning('getImg:' . var_export($throwable->getMessage(), 1));
  149. return false;
  150. }
  151. }
  152. /**
  153. * 获取链接
  154. * @param $siteId
  155. * @param $remoteContentId
  156. * @return string
  157. */
  158. protected function getUrl($siteId, $remoteContentId)
  159. {
  160. $site = Site::query()->with('server')->select(['id', 'server_id', 'database', 'domain'])->where(['id' => $siteId])->first();
  161. if (empty($site->database) || empty($site->server)) {
  162. Log::warning('getUrl: 数据库或服务器信息不存在');
  163. return false;
  164. }
  165. $config = [
  166. 'connection_name' => sprintf('connection_name_%s', $site->id),
  167. 'host' => $site->server->server_ip,
  168. 'port' => 3306,
  169. 'database' => $site->database,
  170. 'username' => $site->server->mysql_user_name,
  171. 'password' => $site->server->mysql_passwd,
  172. ];
  173. config_connection($config);
  174. try {
  175. $record = DB::connection($config['connection_name'])->table('content')->where(['id' => $remoteContentId])->first();
  176. if ($record) {
  177. $url = sprintf('http://%s/%s.html', $site->domain, $record->uri ?? '');
  178. return $url;
  179. }
  180. Log::warning('getUrl: 远程记录信息不存在');
  181. } catch (\Throwable $throwable) {
  182. Log::warning('getUrl:' . var_export($throwable->getMessage(), 1));
  183. }
  184. return false;
  185. }
  186. // public function articlePublish($article, &$sitesMap)
  187. // {
  188. // $img = sprintf('%s%s', config('app.admin_url'), $article->thumb);
  189. // $imgPath = $this->getImg($img);
  190. //
  191. // $url = $this->getUrl($article->site_id, $article->remote_content_id);
  192. // if (!$url) {
  193. // return false;
  194. // }
  195. //
  196. // $content = str_replace('<br/>', PHP_EOL, html_entity_decode(strip_tags($article->translate_content, '<br/>')));
  197. //
  198. //// $linkedIn = $this->linkedIn($content, $url);
  199. // $pin = $this->pin($imgPath, $content);
  200. // $twitter = $this->twitter($imgPath, $content);
  201. //
  202. // $pageId = $sitesMap[$article->site_id]['facebook_page'] ?? null;
  203. // $facebook = $this->facebookTrue($content, $url, $pageId);
  204. // return compact('linkedIn', 'twitter', 'pin', 'facebook');
  205. // }
  206. public function publish($record, $sitesMap)
  207. {
  208. $socialInfo = Social::query()->where(['site_id' => $record->site_id])->get()->keyBy('type')->toArray();
  209. $imgPath = $this->getImg($record->media);
  210. $content = str_replace('<br/>', PHP_EOL, html_entity_decode(strip_tags($record->content, '<br/>')));
  211. list($linkedInStatus, $linkedErr) = $this->linkedIn($content, $record->linkedin_orgid);
  212. list($pinStatus, $pinErr) = $this->pin($imgPath, $record->url, $content, $record->pin_board, ($socialInfo[Social::PIN] ?? null));
  213. list($twStatus, $twErr) = $this->twitter($imgPath, $record->url, $content, ($socialInfo[Social::TWITTER] ?? null));
  214. // $siteInfo = $sitesMap[$record->site_id] ?? null;
  215. // list($fbStatus, $fbErr) = $this->facebookTrue($content, $record->url, $siteInfo);
  216. return [
  217. ['pin' => $pinErr, 'twitter' => $twErr, 'linkedIn' => $linkedErr],
  218. ['pin' => $pinStatus, 'twitter' => $twStatus,'linkedIn' => $linkedInStatus]
  219. ];
  220. }
  221. }