SmmPostController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. //查看发送次数是否超过限制
  81. // if (SmmPostLog::getPostQuota($account->getParent->name) > 0) {
  82. //限额大于0才显示
  83. $listBoxOptions[$account->id] = $account->name . ' ('.$account->getParent->name.')';
  84. // }
  85. }
  86. $form->checkbox('account_ids', admin_trans_label('accountsSelect'))->options($listBoxOptions)->required();
  87. $form->disableViewButton();
  88. $form->disableViewCheck();
  89. $form->disableDeleteButton();
  90. $form->disableEditingCheck();
  91. $form->disableCreatingCheck();
  92. $this->addJs();
  93. });
  94. }
  95. /*
  96. * 保存数据
  97. */
  98. public function store() {
  99. $post = $_POST;
  100. if (isset($post['_file_del_'])) {
  101. // 删除上传的文件
  102. header('Content-Type: application/json');
  103. echo json_encode(['status' => true, 'data' => []]);
  104. exit;
  105. }
  106. if (isset($post['upload_column']) && ($post['upload_column'] == 'image_url' || $post['upload_column'] == 'video_url')) {
  107. // 上传图片或视频
  108. return $this->upload();
  109. }
  110. //保存数据
  111. if (isset($post['post_type'])) {
  112. if ($post['account_ids'] == [] || count($post['account_ids']) == 0 || $post['account_ids'] == '' || empty($post['account_ids'][0])) {
  113. return Admin::json()->error(admin_trans_label('choose_account'));
  114. }
  115. if (SmmUserAccount::findYoutubeAccount($post['account_ids'],getDistributorId())->count() > 0) {
  116. if (empty($post['yutube_title']) || empty($post['youtube_category'])) {
  117. return Admin::json()->error(admin_trans_label('input_yutube_fields'));
  118. }
  119. $charCount = mb_strlen($post['yutube_title'], 'UTF-8');
  120. if ($charCount > 90) {
  121. return Admin::json()->error(admin_trans_label('yutube_title_limit'));
  122. }
  123. }
  124. $post_type = $post['post_type'];
  125. if ($post_type == 0) {
  126. $image_video_url = $post['image_url'];
  127. $post['message'] = $post['image_message'];
  128. } else {
  129. $image_video_url = $post['video_url'];
  130. $post['message'] = $post['video_message'];
  131. }
  132. if (empty($post['message'])) {
  133. return Admin::json()->error(admin_trans_label('fill_post_message'));
  134. }
  135. $messageCount = mb_strlen($post['message'], 'UTF-8');
  136. if ($messageCount > 500) {
  137. return Admin::json()->error(admin_trans_label('post_message_limit'));
  138. }
  139. if ($this->checkStoragePath($image_video_url) === false) {
  140. return Admin::json()->error(admin_trans_label('check_upload'));
  141. }
  142. if ($post['send_time'] == '' || $post['send_time'] == null) {
  143. return Admin::json()->error(admin_trans_label('select_send_time'));
  144. }
  145. //判断是否超过发送限制
  146. $checkResult = SmmUserAccount::checkAccountCanSendPost($post['account_ids'],getDistributorId());
  147. if ($checkResult['status'] == false) {
  148. return Admin::json()->error($checkResult['mediaName'].' '.admin_trans_label('account_send_limit'));
  149. }
  150. // 從北京時間字符串創建 Carbon 對象
  151. $sendTime = Carbon::createFromFormat('Y-m-d H:i:s', $post['send_time'], 'Asia/Shanghai');
  152. // 轉換為 UTC 時間
  153. $send_time = $sendTime->setTimezone('UTC');
  154. //保存数据
  155. SmmPost::create($post,$send_time,$image_video_url,getDistributorId());
  156. // 生成发送记录
  157. $timer = new TimerSsmPost();
  158. $timer->createLog();
  159. return Admin::json()->success(admin_trans_label('operation_successful'))->refresh();
  160. //最后一步
  161. // $data = [
  162. // 'title' => admin_trans_label('operation_successful'),
  163. // 'description' => admin_trans_label('send_post_description'),
  164. // 'continue_publishing' => admin_trans_label('continue_publishing'),
  165. // ];
  166. // return view('distributor.form_custom.completion-page', $data);
  167. }
  168. }
  169. /**
  170. * 上传图片到本地
  171. */
  172. public function upload() {
  173. try {
  174. //保存到本地
  175. $disk = $this->disk('local');
  176. // 判断是否是删除文件请求
  177. if ($this->isDeleteRequest()) {
  178. // 删除文件并响应
  179. return $this->deleteFileAndResponse($disk);
  180. }
  181. // 获取上传的文件
  182. $file = $this->file();
  183. $dir = 'ssm/'.getDistributorId();
  184. $newName = md5(uniqid() . mt_rand()) .'.'.$file->getClientOriginalExtension();
  185. //保存在本地
  186. $result = $disk->putFileAs($dir, $file, $newName);
  187. return $result
  188. ? $this->responseUploaded($result, $disk->url($result))
  189. : $this->responseErrorMessage(admin_trans_label('upload_failed'));
  190. } catch (\Exception $e) {
  191. return $this->responseErrorMessage($e->getMessage());
  192. }
  193. }
  194. /*
  195. * 判断路径是否正确
  196. */
  197. public function checkStoragePath ($filePath) {
  198. $storagePath = 'ssm/'.getDistributorId();
  199. if (strpos($filePath, $storagePath) === 0) {
  200. return true;
  201. }
  202. return false;
  203. }
  204. public function addJs()
  205. {
  206. Admin::script(
  207. <<<JS
  208. var hideYouTubeAccount = function(postType) {
  209. // 查找所有 account_ids 的 checkbox
  210. $('input[name="account_ids[]"]').each(function() {
  211. var label = $(this).closest('.vs-checkbox-con').find('span').last().text();
  212. // 检查是否包含 YouTube
  213. if (label.includes('YouTube')) {
  214. // 如果 post_type 值为 1(视频),显示 YouTube 选项;否则隐藏
  215. if (postType === '1') {
  216. $(this).closest('.vs-checkbox-con').show();
  217. //
  218. const youtubeElements = ['#select_hide_youtube_category', '#text_hide_yutube_title'];
  219. youtubeElements.forEach(id => $(id).toggle(false));
  220. } else {
  221. $(this).closest('.vs-checkbox-con').hide();
  222. // 可选择取消选中状态
  223. $(this).prop('checked', false);
  224. }
  225. }
  226. });
  227. }
  228. var changeCount = 0
  229. $('input[name="post_type"]').on('change', function() {
  230. changeCount++;
  231. console.log(changeCount);
  232. if (changeCount > 2) {
  233. // 获取当前选中的 post_type 值
  234. var postType = $(this).val();
  235. hideYouTubeAccount(postType);
  236. } else {
  237. hideYouTubeAccount(0);
  238. }
  239. });
  240. // 定义需要控制的表单元素ID
  241. const youtubeElements = ['#select_hide_youtube_category', '#text_hide_yutube_title'];
  242. // 检查是否包含YouTube账号的函数
  243. function checkYouTubeSelection() {
  244. let hasYouTube = false;
  245. // 遍历所有选中的复选框
  246. $('input[name="account_ids[]"]:checked').each(function() {
  247. // 获取父容器内的账号名称文本
  248. const labelText = $(this).closest('.vs-checkbox-con').find('span:last').text().trim();
  249. if (labelText.includes('YouTube')) {
  250. hasYouTube = true;
  251. return false; // 发现后提前终止循环
  252. }
  253. });
  254. // 统一控制元素的显隐
  255. youtubeElements.forEach(id => $(id).toggle(hasYouTube));
  256. }
  257. // 绑定所有账号复选框的change事件
  258. $('input[name="account_ids[]"]').change(checkYouTubeSelection);
  259. // 初始化执行一次
  260. checkYouTubeSelection();
  261. JS
  262. );
  263. }
  264. }