SmmPostController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Admin\Repositories\BaseProductImage;
  4. use App\Distributor\Repositories\SmmPost;
  5. use App\Distributor\Repositories\SmmPostLog;
  6. use App\Distributor\Repositories\SmmUserAccount;
  7. use App\Services\SmmService;
  8. use Carbon\Carbon;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Grid\Model;
  12. use Dcat\Admin\Show;
  13. use Dcat\Admin\Http\Controllers\AdminController;
  14. use Dcat\Admin\Layout\Content;
  15. use Dcat\Admin\Admin;
  16. use Dcat\Admin\FormStep\Form as StepForm;
  17. use Dcat\Admin\Traits\HasUploadedFile;
  18. use Dcat\Admin\Widgets\Alert;
  19. use App\Console\Commands\TimerSsmPost;
  20. use phpseclib3\Crypt\EC\BaseCurves\Montgomery;
  21. class SmmPostController extends AdminDistController
  22. {
  23. use HasUploadedFile;
  24. /**
  25. * page index
  26. */
  27. public function index(Content $content)
  28. {
  29. return $content
  30. ->header(admin_trans_label('send_post'))
  31. ->body($this->form());
  32. }
  33. protected function form()
  34. {
  35. return Form::make(new SmmPost(), function (Form $form) {
  36. $form->title(admin_trans_label('local_materials'));
  37. $form->action('ssm-post');
  38. $form->disableListButton();
  39. $form->radio('post_type',admin_trans_label('post_type'))
  40. ->when([0], function ($step) {
  41. $step->textarea('image_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  42. $step->multipleImage('image_url', '<span style="color:#bd4147;">*</span> '.admin_trans_label('images'))
  43. ->retainable()//禁止删OSS图
  44. ->sortable() // 可拖动排序
  45. ->removable() // 可移除图片
  46. ->autoUpload() // 自动上传
  47. ->uniqueName()
  48. ->limit(4)
  49. ->accept('jpg,png')
  50. ->help('jpg,png (Max 3M)')
  51. ->maxSize(3072);
  52. })
  53. ->when([1], function ($step) {
  54. $step->select('youtube_category','<span style="color:#bd4147;">*</span> '.admin_trans_label('youtube_category'))->setView('distributor.form_custom.select_hide')->options(SmmUserAccount::getYoutubeCategory())->default(22)->placeholder(' ');
  55. $step->text('yutube_title','<span style="color:#bd4147;">*</span> '.admin_trans_label('yutube_title'))->setView('distributor.form_custom.text_hide')->placeholder(' ');
  56. $step->textarea('video_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  57. $step->file('video_url','<span style="color:#bd4147;">*</span> '.admin_trans_label('video'))
  58. ->uniqueName()
  59. ->autoUpload()
  60. ->accept(config('admin.upload.oss_video.accept'))
  61. ->maxSize(20480)
  62. ->chunked()
  63. ->help('mp4 (Max 20M)')
  64. ->removable();
  65. //->chunked()
  66. })
  67. ->options([ 0=>admin_trans_label('graphic'), 1=>admin_trans_label('videos')])->default(0)->required();
  68. //当前时间
  69. $now = Carbon::now()->format('Y-m-d H:i:s');
  70. $form->datetime('send_time', admin_trans_label('send_time'))->placeholder(' ')->default($now)->required();
  71. $rootAccounts = SmmUserAccount::getUserAccounts();
  72. $listBoxOptions = [];
  73. foreach ($rootAccounts as $account) {
  74. if (SmmPostLog::getPostQuota($account->getParent->name) > 0) {
  75. //限额大于0才显示
  76. $listBoxOptions[$account->id] = $account->name . ' ('.$account->getParent->name.')';
  77. }
  78. }
  79. $form->checkbox('account_ids', admin_trans_label('accountsSelect'))->options($listBoxOptions)->required();
  80. $form->disableViewButton();
  81. $form->disableViewCheck();
  82. $form->disableDeleteButton();
  83. $form->disableEditingCheck();
  84. $form->disableCreatingCheck();
  85. $this->addJs();
  86. // $form->select('youtube_category')->setView('distributor.form_custom.select_hide')->options(SmmUserAccount::getYoutubeCategory())->default(22)->required();
  87. /*
  88. $form->multipleSteps()
  89. ->remember()
  90. ->width('950px')
  91. ->add(admin_trans_label('select_local_media'), function ($step) {
  92. $step->radio('send_type',admin_trans_label('send_type'))
  93. ->when([1], function ($step) {
  94. $step->datetime('send_time', '<span style="color:#bd4147;">*</span> '.admin_trans_label('send_time'))->placeholder(' ');
  95. })
  96. ->options([ 0=>admin_trans_label('immediate'), 1=>admin_trans_label('timing')])->default(0);
  97. $step->radio('post_type',admin_trans_label('media_type'))
  98. ->when([0], function ($step) {
  99. $step->textarea('image_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  100. $step->multipleImage('image_url', '<span style="color:#bd4147;">*</span> '.admin_trans_label('images'))
  101. ->retainable()//禁止删OSS图
  102. ->sortable() // 可拖动排序
  103. ->removable() // 可移除图片
  104. ->autoUpload() // 自动上传
  105. ->uniqueName()
  106. ->limit(4)
  107. ->accept('jpg,png')
  108. ->help('jpg,png (Max 3M)')
  109. ->maxSize(3072);
  110. })
  111. ->when([1], function ($step) {
  112. $step->textarea('video_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  113. $step->file('video_url','<span style="color:#bd4147;">*</span> '.admin_trans_label('video'))
  114. ->uniqueName()
  115. ->autoUpload()
  116. ->accept(config('admin.upload.oss_video.accept'))
  117. ->maxSize(20480)
  118. ->chunked()
  119. ->help('mp4 (Max 20M)')
  120. ->removable();
  121. //->chunked()
  122. })
  123. ->options([ 0=>admin_trans_label('images'), 1=>admin_trans_label('videos')])->default(0);
  124. $this->stepLeaving($step,0);
  125. })
  126. ->add(admin_trans_label('choose_platforms'), function ($step) {
  127. $rootAccounts = SmmUserAccount::getUserAccounts();
  128. $listBoxOptions = [];
  129. foreach ($rootAccounts as $account) {
  130. if (SmmPostLog::getPostQuota($account->getParent->name) > 0) {
  131. //限额大于0才显示
  132. $listBoxOptions[$account->id] = $account->name . ' ('.$account->getParent->name.')';
  133. }
  134. }
  135. $step->listbox('account_ids', '<span style="color:#bd4147;">*</span> '.admin_trans_label('accountsSelect'))
  136. ->options($listBoxOptions);
  137. $step->select('youtube_category')->setView('distributor.form_custom.select_hide')->options(SmmUserAccount::getYoutubeCategory())->default(22)->required();
  138. $this->stepLeaving($step,1);
  139. });
  140. $this->addJs();
  141. */
  142. });
  143. }
  144. /*
  145. * 保存数据
  146. */
  147. public function store() {
  148. $post = $_POST;
  149. if (isset($post['_file_del_'])) {
  150. // 删除上传的文件
  151. header('Content-Type: application/json');
  152. echo json_encode(['status' => true, 'data' => []]);
  153. exit;
  154. }
  155. if (isset($post['upload_column']) && ($post['upload_column'] == 'image_url' || $post['upload_column'] == 'video_url')) {
  156. // 上传图片或视频
  157. return $this->upload();
  158. }
  159. //保存数据
  160. if (isset($post['post_type'])) {
  161. if ($post['account_ids'] == [] || count($post['account_ids']) == 0 || $post['account_ids'] == '' || empty($post['account_ids'][0])) {
  162. return Admin::json()->error(admin_trans_label('choose_account'));
  163. }
  164. if (SmmUserAccount::findYoutubeAccount($post['account_ids'])->count() > 0) {
  165. if (empty($post['yutube_title']) || empty($post['youtube_category'])) {
  166. return Admin::json()->error(admin_trans_label('input_yutube_fields'));
  167. }
  168. $charCount = mb_strlen($post['yutube_title'], 'UTF-8');
  169. if ($charCount > 70) {
  170. return Admin::json()->error(admin_trans_label('yutube_title_limit'));
  171. }
  172. }
  173. $post_type = $post['post_type'];
  174. if ($post_type == 0) {
  175. $image_video_url = $post['image_url'];
  176. $post['message'] = $post['image_message'];
  177. } else {
  178. $image_video_url = $post['video_url'];
  179. $post['message'] = $post['video_message'];
  180. }
  181. if (empty($post['message'])) {
  182. return Admin::json()->error(admin_trans_label('fill_post_message'));
  183. }
  184. $messageCount = mb_strlen($post['message'], 'UTF-8');
  185. if ($messageCount > 2000) {
  186. return Admin::json()->error(admin_trans_label('post_message_limit'));
  187. }
  188. if ($this->checkStoragePath($image_video_url) === false) {
  189. return Admin::json()->error(admin_trans_label('check_upload'));
  190. }
  191. if ($post['send_time'] == '' || $post['send_time'] == null) {
  192. return Admin::json()->error(admin_trans_label('select_send_time'));
  193. }
  194. // 從北京時間字符串創建 Carbon 對象
  195. $sendTime = Carbon::createFromFormat('Y-m-d H:i:s', $post['send_time'], 'Asia/Shanghai');
  196. // 轉換為 UTC 時間
  197. $send_time = $sendTime->setTimezone('UTC');
  198. //保存数据
  199. SmmPost::create($post,$send_time,$image_video_url);
  200. // 生成发送记录
  201. $timer = new TimerSsmPost();
  202. $timer->createLog();
  203. return Admin::json()->success(admin_trans_label('operation_successful'))->refresh();
  204. //最后一步
  205. // $data = [
  206. // 'title' => admin_trans_label('operation_successful'),
  207. // 'description' => admin_trans_label('send_post_description'),
  208. // 'continue_publishing' => admin_trans_label('continue_publishing'),
  209. // ];
  210. // return view('distributor.form_custom.completion-page', $data);
  211. }
  212. }
  213. /**
  214. * 上传图片到本地
  215. */
  216. public function upload() {
  217. try {
  218. //保存到本地
  219. $disk = $this->disk('local');
  220. // 判断是否是删除文件请求
  221. if ($this->isDeleteRequest()) {
  222. // 删除文件并响应
  223. return $this->deleteFileAndResponse($disk);
  224. }
  225. // 获取上传的文件
  226. $file = $this->file();
  227. $dir = 'ssm/'.getDistributorId();
  228. $newName = md5(uniqid() . mt_rand()) .'.'.$file->getClientOriginalExtension();
  229. //保存在本地
  230. $result = $disk->putFileAs($dir, $file, $newName);
  231. return $result
  232. ? $this->responseUploaded($result, $disk->url($result))
  233. : $this->responseErrorMessage(admin_trans_label('upload_failed'));
  234. } catch (\Exception $e) {
  235. return $this->responseErrorMessage($e->getMessage());
  236. }
  237. }
  238. /*
  239. * 判断路径是否正确
  240. */
  241. public function checkStoragePath ($filePath) {
  242. $storagePath = 'ssm/'.getDistributorId();
  243. if (strpos($filePath, $storagePath) === 0) {
  244. return true;
  245. }
  246. return false;
  247. }
  248. public function addJs()
  249. {
  250. Admin::script(
  251. <<<JS
  252. var hideYouTubeAccount = function(postType) {
  253. // 查找所有 account_ids 的 checkbox
  254. $('input[name="account_ids[]"]').each(function() {
  255. var label = $(this).closest('.vs-checkbox-con').find('span').last().text();
  256. // 检查是否包含 YouTube
  257. if (label.includes('YouTube')) {
  258. // 如果 post_type 值为 1(视频),显示 YouTube 选项;否则隐藏
  259. if (postType === '1') {
  260. $(this).closest('.vs-checkbox-con').show();
  261. //
  262. const youtubeElements = ['#select_hide_youtube_category', '#text_hide_yutube_title'];
  263. youtubeElements.forEach(id => $(id).toggle(false));
  264. } else {
  265. $(this).closest('.vs-checkbox-con').hide();
  266. // 可选择取消选中状态
  267. $(this).prop('checked', false);
  268. }
  269. }
  270. });
  271. }
  272. var changeCount = 0
  273. $('input[name="post_type"]').on('change', function() {
  274. changeCount++;
  275. console.log(changeCount);
  276. if (changeCount > 2) {
  277. // 获取当前选中的 post_type 值
  278. var postType = $(this).val();
  279. hideYouTubeAccount(postType);
  280. } else {
  281. hideYouTubeAccount(0);
  282. }
  283. });
  284. // 定义需要控制的表单元素ID
  285. const youtubeElements = ['#select_hide_youtube_category', '#text_hide_yutube_title'];
  286. // 检查是否包含YouTube账号的函数
  287. function checkYouTubeSelection() {
  288. let hasYouTube = false;
  289. // 遍历所有选中的复选框
  290. $('input[name="account_ids[]"]:checked').each(function() {
  291. // 获取父容器内的账号名称文本
  292. const labelText = $(this).closest('.vs-checkbox-con').find('span:last').text().trim();
  293. if (labelText.includes('YouTube')) {
  294. hasYouTube = true;
  295. return false; // 发现后提前终止循环
  296. }
  297. });
  298. // 统一控制元素的显隐
  299. youtubeElements.forEach(id => $(id).toggle(hasYouTube));
  300. }
  301. // 绑定所有账号复选框的change事件
  302. $('input[name="account_ids[]"]').change(checkYouTubeSelection);
  303. // 初始化执行一次
  304. checkYouTubeSelection();
  305. JS
  306. );
  307. }
  308. }