SmmPostController.php 11 KB

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