InfoController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace App\Http\Controllers\Admin\Interaction;
  3. use App\Models\BaseAttachmentModel;
  4. use App\Models\CommentInfoModel;
  5. use App\Models\DreamInfoModel;
  6. use App\Models\InteractionInfo;
  7. use App\Models\UserInfoModel;
  8. use App\Widget\Tools\VideoUpload;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Admin\Controller;
  11. class InfoController extends Controller
  12. {
  13. public function view(Request $request)
  14. {
  15. $dream_id = $request->id;
  16. $dream = DreamInfoModel::where('id',$request->id)->with(['interactions'=> function ($query) {
  17. $query->orderBy('id','desc');
  18. }])->first();
  19. $list = $dream->interactions;
  20. foreach ($list as $item){
  21. $comments = CommentInfoModel::where('interaction_id',$item->id)->orderBy('created_at')->get();
  22. $item->comms = $comments;
  23. }
  24. return view('admin.dream.interaction.view',compact('list','dream_id'));
  25. }
  26. public function update(Request $request,$id)
  27. {
  28. $data = (array) request('data');
  29. $ok =CommentInfoModel::where('id',$id)->update($data);
  30. if($ok) {
  31. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  32. return $this->showMessage('操作成功',urldecode(request('_referer')));
  33. }else{
  34. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  35. return $this->showWarning('操作失败',$url);
  36. }
  37. }
  38. // 新增动态
  39. public function create(Request $reqeust)
  40. {
  41. if($reqeust->method() == 'POST') {
  42. return $this->_createSave();
  43. }
  44. $dream_id = $reqeust->dream_id;
  45. return view('admin.interaction.info.edit',compact('dream_id'));
  46. }
  47. /**
  48. * 保存修改
  49. */
  50. private function _createSave(){
  51. $data = (array) request('data');
  52. $pics = (array) request('pic');
  53. if($pics){
  54. foreach ($pics['url'] as $key => $pic) {
  55. $data['pic'.($key+1)] = $pic;
  56. }
  57. }
  58. if (request("video")) {
  59. $file = request("video");
  60. $fileSize = $file->getSize();
  61. $size = 200 * 1024 * 1024;
  62. if ($fileSize > $size) {
  63. return back()->with('error','请上传小于200MB的文件!');
  64. }
  65. $mimeType = [
  66. 'video/mp4',
  67. ];
  68. $fileMimeType = $file->getMimeType();
  69. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  70. return back()->with('error','File type allow MP4!');
  71. }
  72. if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
  73. $data["video"] = $file;
  74. }
  75. $id = InteractionInfo::create($data);
  76. if($id) {
  77. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  78. $this->showMessage('添加成功',$url);
  79. }else{
  80. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  81. return $this->showWarning('添加失败',$url);
  82. }
  83. }
  84. // 修改动态
  85. public function updateInteraction(Request $reqeust) {
  86. if($reqeust->method() == 'POST') {
  87. return $this->_updateSave();
  88. }
  89. $data = InteractionInfo::find($reqeust->get('id'));
  90. $arr = [];
  91. for ($i = 1; $i <=9; $i++) {
  92. if (!empty($data['pic'.($i)])) {
  93. $arr[] = $data['pic'.($i)];
  94. }
  95. }
  96. $data->imgs = $arr;
  97. if (empty($data->video)) {
  98. $data->is_video = 0;
  99. }else{
  100. $data->is_video = 1;
  101. }
  102. return view('admin.interaction.info.edit',compact('data'));
  103. }
  104. /**
  105. * 保存修改
  106. */
  107. private function _updateSave() {
  108. $data = (array) request('data');
  109. $interaction_id = request('id');
  110. $interaction = InteractionInfo::find($interaction_id)->toArray();
  111. $interaction['title'] = $data['title'];
  112. $old_pics = [];
  113. for ($i = 1; $i <=9; $i++) {
  114. if (!empty($interaction['pic'.($i)])) {
  115. $old_pics[$i] = $interaction['pic'.($i)];
  116. }
  117. }
  118. $new_pics = (array) request('pic');
  119. if (!empty($new_pics)) {
  120. // 图片不为空
  121. //操作成功,删除原来的图片
  122. foreach ($new_pics['url'] as $key => $pic) {
  123. $interaction['pic'.($key+1)] = $pic;
  124. }
  125. foreach ($old_pics as $k => $pic) {
  126. if (!in_array($pic, $new_pics['url'])) {
  127. $md5 = $this->getarea($pic);
  128. $attache = new \App\Services\Base\Attachment();
  129. $attache->deleteAttachment($md5);
  130. $interaction['pic'.($k+1)] = '';
  131. }
  132. }
  133. }
  134. if (!empty(request("video"))) {
  135. $file = request("video");
  136. $fileSize = $file->getSize();
  137. $size = 200 * 1024 * 1024;
  138. if ($fileSize > $size) {
  139. return back()->with('error','请上传小于200 MB的文件!');
  140. }
  141. $mimeType = [
  142. 'video/mp4',
  143. ];
  144. $fileMimeType = $file->getMimeType();
  145. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  146. return back()->with('error','File type allow MP4!');
  147. }
  148. if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
  149. $interaction["video"] = $file;
  150. }
  151. $ok = InteractionInfo::find(request('id'))->update($interaction);
  152. if($ok) {
  153. $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
  154. return $this->showMessage('操作成功',urldecode(request('_referer')));
  155. }else{
  156. $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
  157. return $this->showWarning('操作失败',$url);
  158. }
  159. }
  160. /**
  161. * 删除
  162. */
  163. public function destroy(Request $reqeust)
  164. {
  165. // 删除动态的图片 视频 评论...
  166. $data = InteractionInfo::find($reqeust->get('id'));
  167. $arr = [];
  168. for ($i = 1; $i <=9; $i++) {
  169. if (!empty($data['pic'.($i)])) {
  170. $arr[] = $data['pic'.($i)];
  171. }
  172. }
  173. // 删除图片
  174. if (!empty($arr)) {
  175. foreach ($arr as $old_pic){
  176. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  177. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  178. }
  179. BaseAttachmentModel::where('url',$old_pic)->delete();
  180. }
  181. }
  182. // 删除视频
  183. if (!empty($data->video)) {
  184. if (is_file('.'.str_replace(getenv('APP_URL'),'',$data->video))) {
  185. unlink('.'.str_replace(getenv('APP_URL'),'',$data->video));
  186. }
  187. }
  188. // 删除评论
  189. CommentInfoModel::where('interaction_id',$reqeust->get('id'))->delete();
  190. $ok = InteractionInfo::destroy($reqeust->get('id'));
  191. if ($ok) {
  192. return $this->showMessage('操作成功');
  193. } else {
  194. return $this->showWarning("操作失败");
  195. }
  196. }
  197. public function comment(Request $request)
  198. {
  199. $interaction_id = $request->input('id');
  200. $comment_id = $request->input('comment_id');
  201. if ($request->isMethod('POST')) {
  202. $comment = $request->input('comment');
  203. $interaction = InteractionInfo::find($interaction_id);
  204. $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
  205. $user = UserInfoModel::find($user_id);
  206. if(!empty($comment_id)){ //回复
  207. $comment_info = CommentInfoModel::find($comment_id);
  208. $interaction = InteractionInfo::find($comment_info->interaction_id);
  209. $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
  210. $user = UserInfoModel::find($user_id);
  211. $arr = [
  212. 'interaction_id'=>$comment_info->interaction_id,
  213. 'user_id'=>$comment_info->user_id,
  214. 'user_avatar'=>$comment_info->user_avatar,
  215. 'user_nickname'=>$comment_info->user_nickname,
  216. 'to_user_id'=>$user_id,
  217. 'content'=>$comment,
  218. 'to_user_avatar'=>empty($user) ? '' : $user->avatar ,
  219. 'to_user_nickname'=>empty($user) ? '' : $user->nickname ,
  220. ];
  221. $ok = CommentInfoModel::create($arr);
  222. }else{
  223. $arr = [
  224. 'interaction_id'=>$interaction_id,
  225. 'user_id'=>$user_id,
  226. 'content'=>$comment,
  227. 'user_avatar'=>empty($user) ? '' : $user->avatar ,
  228. 'user_nickname'=>empty($user) ? '' : $user->nickname ,
  229. ];
  230. $ok = CommentInfoModel::create($arr);
  231. }
  232. if ($ok) {
  233. return $this->showMessage('操作成功');
  234. } else {
  235. return $this->showWarning("操作失败");
  236. }
  237. }
  238. return view('admin.comment.edit',compact('interaction_id'));
  239. }
  240. // 获取视频图片后缀码
  241. public function getarea($str)
  242. {
  243. $start = strripos($str, '/');
  244. $first = substr($str, $start + 1);
  245. $end = strripos($first, '.');
  246. $last = substr($first, 0, $end);
  247. return $last;
  248. }
  249. }