SmmPostController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 > 90) {
  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 > 500) {
  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. //判断是否超过发送限制
  201. if (SmmUserAccount::checkAccountCanSendPost($post['account_ids'],getDistributorId()) == false) {
  202. return Admin::json()->error(admin_trans_label('account_send_limit'));
  203. }
  204. // 從北京時間字符串創建 Carbon 對象
  205. $sendTime = Carbon::createFromFormat('Y-m-d H:i:s', $post['send_time'], 'Asia/Shanghai');
  206. // 轉換為 UTC 時間
  207. $send_time = $sendTime->setTimezone('UTC');
  208. //保存数据
  209. SmmPost::create($post,$send_time,$image_video_url,getDistributorId());
  210. // 生成发送记录
  211. $timer = new TimerSsmPost();
  212. $timer->createLog();
  213. return Admin::json()->success(admin_trans_label('operation_successful'))->refresh();
  214. //最后一步
  215. // $data = [
  216. // 'title' => admin_trans_label('operation_successful'),
  217. // 'description' => admin_trans_label('send_post_description'),
  218. // 'continue_publishing' => admin_trans_label('continue_publishing'),
  219. // ];
  220. // return view('distributor.form_custom.completion-page', $data);
  221. }
  222. }
  223. /**
  224. * 上传图片到本地
  225. */
  226. public function upload() {
  227. try {
  228. //保存到本地
  229. $disk = $this->disk('local');
  230. // 判断是否是删除文件请求
  231. if ($this->isDeleteRequest()) {
  232. // 删除文件并响应
  233. return $this->deleteFileAndResponse($disk);
  234. }
  235. // 获取上传的文件
  236. $file = $this->file();
  237. $dir = 'ssm/'.getDistributorId();
  238. $newName = md5(uniqid() . mt_rand()) .'.'.$file->getClientOriginalExtension();
  239. //保存在本地
  240. $result = $disk->putFileAs($dir, $file, $newName);
  241. return $result
  242. ? $this->responseUploaded($result, $disk->url($result))
  243. : $this->responseErrorMessage(admin_trans_label('upload_failed'));
  244. } catch (\Exception $e) {
  245. return $this->responseErrorMessage($e->getMessage());
  246. }
  247. }
  248. /*
  249. * 判断路径是否正确
  250. */
  251. public function checkStoragePath ($filePath) {
  252. $storagePath = 'ssm/'.getDistributorId();
  253. if (strpos($filePath, $storagePath) === 0) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. public function addJs()
  259. {
  260. Admin::script(
  261. <<<JS
  262. var hideYouTubeAccount = function(postType) {
  263. // 查找所有 account_ids 的 checkbox
  264. $('input[name="account_ids[]"]').each(function() {
  265. var label = $(this).closest('.vs-checkbox-con').find('span').last().text();
  266. // 检查是否包含 YouTube
  267. if (label.includes('YouTube')) {
  268. // 如果 post_type 值为 1(视频),显示 YouTube 选项;否则隐藏
  269. if (postType === '1') {
  270. $(this).closest('.vs-checkbox-con').show();
  271. //
  272. const youtubeElements = ['#select_hide_youtube_category', '#text_hide_yutube_title'];
  273. youtubeElements.forEach(id => $(id).toggle(false));
  274. } else {
  275. $(this).closest('.vs-checkbox-con').hide();
  276. // 可选择取消选中状态
  277. $(this).prop('checked', false);
  278. }
  279. }
  280. });
  281. }
  282. var changeCount = 0
  283. $('input[name="post_type"]').on('change', function() {
  284. changeCount++;
  285. console.log(changeCount);
  286. if (changeCount > 2) {
  287. // 获取当前选中的 post_type 值
  288. var postType = $(this).val();
  289. hideYouTubeAccount(postType);
  290. } else {
  291. hideYouTubeAccount(0);
  292. }
  293. });
  294. // 定义需要控制的表单元素ID
  295. const youtubeElements = ['#select_hide_youtube_category', '#text_hide_yutube_title'];
  296. // 检查是否包含YouTube账号的函数
  297. function checkYouTubeSelection() {
  298. let hasYouTube = false;
  299. // 遍历所有选中的复选框
  300. $('input[name="account_ids[]"]:checked').each(function() {
  301. // 获取父容器内的账号名称文本
  302. const labelText = $(this).closest('.vs-checkbox-con').find('span:last').text().trim();
  303. if (labelText.includes('YouTube')) {
  304. hasYouTube = true;
  305. return false; // 发现后提前终止循环
  306. }
  307. });
  308. // 统一控制元素的显隐
  309. youtubeElements.forEach(id => $(id).toggle(hasYouTube));
  310. }
  311. // 绑定所有账号复选框的change事件
  312. $('input[name="account_ids[]"]').change(checkYouTubeSelection);
  313. // 初始化执行一次
  314. checkYouTubeSelection();
  315. JS
  316. );
  317. }
  318. }