SmmPostController.php 15 KB

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