SmmPostController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Admin\Repositories\BaseProductImage;
  4. use App\Distributor\Repositories\SmmPost;
  5. use App\Distributor\Repositories\SmmUserAccount;
  6. use Carbon\Carbon;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Grid\Model;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Dcat\Admin\Layout\Content;
  13. use Dcat\Admin\Admin;
  14. use Dcat\Admin\FormStep\Form as StepForm;
  15. use Dcat\Admin\Traits\HasUploadedFile;
  16. use Dcat\Admin\Widgets\Alert;
  17. class SmmPostController extends AdminDistController
  18. {
  19. use HasUploadedFile;
  20. /**
  21. * page index
  22. */
  23. public function index(Content $content)
  24. {
  25. return $content
  26. ->header('发报帖子')
  27. ->body($this->form());
  28. }
  29. protected function form()
  30. {
  31. return Form::make(new SmmPost(), function (Form $form) {
  32. $form->title('本地素材发布');
  33. $form->action('ssm-post');
  34. $form->disableListButton();
  35. $form->multipleSteps()
  36. ->remember()
  37. ->width('950px')
  38. ->add('选择本地媒体', function ($step) {
  39. $step->radio('send_type',admin_trans_label('send_type'))
  40. ->when([1], function ($step) {
  41. $step->datetime('send_time', '<span style="color:#bd4147;">*</span> '.admin_trans_label('send_time'))->placeholder(' ');
  42. })
  43. ->options([ 0=>admin_trans_label('immediate'), 1=>admin_trans_label('timing')])->default(0);
  44. $step->radio('post_type',admin_trans_label('media_type'))
  45. ->when([0], function ($step) {
  46. $step->textarea('image_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  47. $step->multipleImage('image_url', '<span style="color:#bd4147;">*</span> '.admin_trans_label('images'))
  48. ->retainable()//禁止删OSS图
  49. ->sortable() // 可拖动排序
  50. ->removable() // 可移除图片
  51. ->autoUpload() // 自动上传
  52. ->uniqueName()
  53. ->limit(5)
  54. ->accept(config('admin.upload.oss_image.accept'))
  55. ->maxSize(config('admin.upload.oss_image.max_size'));
  56. })
  57. ->when([1], function ($step) {
  58. $step->textarea('video_message', '<span style="color:#bd4147;">*</span> '.admin_trans_label('post_message'))->rows(3)->placeholder(' ');
  59. $step->file('video_url','<span style="color:#bd4147;">*</span> '.admin_trans_label('video'))
  60. ->uniqueName()
  61. ->autoUpload()
  62. ->accept(config('admin.upload.oss_video.accept'))
  63. ->maxSize(config('admin.upload.oss_video.max_size'))
  64. ->chunkSize(1024);
  65. })
  66. ->options([ 0=>admin_trans_label('images'), 1=>admin_trans_label('videos')])->default(0);
  67. $this->stepLeaving($step,0);
  68. })
  69. ->add('选择传单社媒平台', function ($step) {
  70. $rootAccounts = SmmUserAccount::getUserAccounts();
  71. $listBoxOptions = [];
  72. foreach ($rootAccounts as $account) {
  73. $listBoxOptions[$account->id] = $account->name . ' ('.$account->getParent->name.')';
  74. }
  75. $step->listbox('account_ids', '<span style="color:#bd4147;">*</span> '.admin_trans_label('accountsSelect'))->options($listBoxOptions);
  76. $this->stepLeaving($step,1);
  77. });
  78. });
  79. }
  80. /*
  81. * 保存数据
  82. */
  83. public function store() {
  84. $post = $_POST;
  85. if (isset($post['upload_column']) && ($post['upload_column'] == 'image_url' || $post['upload_column'] == 'video_url')) {
  86. // 上传图片或视频
  87. return $this->upload();
  88. }
  89. if (isset($post['ALL_STEPS']) && $post['ALL_STEPS'] == '1') {
  90. $post_type = $post['post_type'];
  91. if ($post_type == 0) {
  92. $image_video_url = $post['image_url'];
  93. $post['message'] = $post['image_message'];
  94. } else {
  95. $image_video_url = $post['video_url'];
  96. $post['message'] = $post['video_message'];
  97. }
  98. if ($this->checkStoragePath($image_video_url) === false) {
  99. return '发送失败,请检查上传文件是否存在';
  100. }
  101. if ($post['send_type'] == 0) {
  102. $send_time = Carbon::now();
  103. } else {
  104. //转换时间格式
  105. $send_time = Carbon::createFromFormat('Y-m-d H:i:s', $post['send_time']);
  106. }
  107. //dd($post);
  108. //保存数据
  109. SmmPost::create($post,$send_time,$image_video_url);
  110. //最后一步
  111. $data = [
  112. 'title' => '操作成功',
  113. 'description' => '系统已生成发送队列,详情请查看日志。',
  114. ];
  115. return view('distributor.form_custom.completion-page', $data);
  116. }
  117. }
  118. /**
  119. * 上传图片到本地
  120. */
  121. public function upload() {
  122. $disk = $this->disk('oss');
  123. // 获取上传的文件
  124. $file = $this->file();
  125. $dir = 'ssm/'.getDistributorId();
  126. $newName = md5(uniqid() . mt_rand()) .'.'.$file->getClientOriginalExtension();
  127. $result = $disk->putFileAs($dir, $file, $newName);
  128. return $result
  129. ? $this->responseUploaded($result, $disk->url($result))
  130. : $this->responseErrorMessage(admin_trans_label('upload_failed'));
  131. }
  132. /*
  133. * 判断路径是否正确
  134. */
  135. public function checkStoragePath ($filePath) {
  136. $storagePath = 'ssm/'.getDistributorId();
  137. if (strpos($filePath, $storagePath) === 0) {
  138. return true;
  139. }
  140. return false;
  141. }
  142. private function stepLeaving($step,$index=0)
  143. {
  144. $lang = config('app.locale');//当前语言
  145. //JS 验证参数不能为空
  146. if ($index == 0) {
  147. $step->leaving(<<<JS
  148. function validateForm(formArray) {
  149. lang = '{$lang}';
  150. // 仅获取radio类型的post_type值
  151. let postType = formArray.find(item =>
  152. item.name === 'post_type' && item.type === 'radio'
  153. )?.value;
  154. // 仅获取radio类型的send_type值
  155. let sendType = formArray.find(item =>
  156. item.name === 'send_type' && item.type === 'radio'
  157. )?.value;
  158. // 验证post_type相关规则
  159. if (postType === '0') {
  160. const imageMessage = formArray.find(item => item.name === 'image_message')?.value;
  161. const imageUrl = formArray.find(item => item.name === 'image_url')?.value;
  162. if (!imageMessage || !imageUrl) {
  163. if (lang === 'en') {
  164. return 'Post message and images cannot be empty';
  165. } else {
  166. return '帖子留言和图片不能为空';
  167. }
  168. }
  169. } else if (postType === '1') {
  170. const videoMessage = formArray.find(item => item.name === 'video_message')?.value;
  171. const videoUrl = formArray.find(item => item.name === 'video_url')?.value;
  172. if (!videoMessage || !videoUrl) {
  173. if (lang === 'en') {
  174. return 'Post message and video cannot be empty';
  175. } else {
  176. return '帖子留言和视频不能为空';
  177. }
  178. }
  179. }
  180. // 验证send_type规则(仅处理radio类型的send_type)
  181. if (sendType === '1') {
  182. const sendTime = formArray.find(item => item.name === 'send_time')?.value;
  183. if (!sendTime) {
  184. if (lang === 'en') {
  185. return 'Send time cannot be empty';
  186. } else {
  187. return '定时发送时间不能为空';
  188. }
  189. }
  190. }
  191. return "";
  192. }
  193. var formArray = args.formArray;
  194. rs = validateForm(formArray);
  195. if (rs != "") {
  196. Dcat.error(rs);
  197. return false;
  198. }
  199. JS);
  200. } else {
  201. $step->leaving(<<<JS
  202. var formArray = args.formArray;
  203. var lang = '{$lang}';
  204. //找account_ids
  205. let accountIds = formArray.find(item => item.name === 'account_ids[]')?.value;
  206. if (!accountIds || accountIds.length === 0) {
  207. if (lang === 'en') {
  208. Dcat.error('Please select accounts');
  209. } else {
  210. Dcat.error('请选择社媒帐号');
  211. }
  212. return false;
  213. }
  214. JS);
  215. }
  216. }
  217. }