InfoController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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')->first();
  17. $list = $dream->interactions;
  18. foreach ($list as $item){
  19. $comments = CommentInfoModel::where('interaction_id',$item->id)->orderBy('created_at')->get();
  20. $item->comms = $comments;
  21. }
  22. return view('admin.dream.interaction.view',compact('list','dream_id'));
  23. }
  24. public function update(Request $request,$id)
  25. {
  26. $data = (array) request('data');
  27. $ok =CommentInfoModel::where('id',$id)->update($data);
  28. if($ok) {
  29. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  30. return $this->showMessage('操作成功',urldecode(request('_referer')));
  31. }else{
  32. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  33. return $this->showWarning('操作失败',$url);
  34. }
  35. }
  36. // 新增动态
  37. public function create(Request $reqeust)
  38. {
  39. if($reqeust->method() == 'POST') {
  40. return $this->_createSave();
  41. }
  42. $dream_id = $reqeust->dream_id;
  43. return view('admin.interaction.info.edit',compact('dream_id'));
  44. }
  45. /**
  46. * 保存修改
  47. */
  48. private function _createSave(){
  49. $data = (array) request('data');
  50. $pics = (array) request('pic');
  51. if($pics){
  52. foreach ($pics['url'] as $key => $pic) {
  53. $data['pic'.($key+1)] = getenv('APP_URL').$pic;
  54. }
  55. }
  56. if (request("file")) {
  57. $file = request("file");
  58. $fileSize = $file->getSize();
  59. $size = 200 * 1024 * 1024;
  60. if ($fileSize > $size) {
  61. return back()->with('error','请上传小于200MB的文件!');
  62. }
  63. $mimeType = [
  64. 'video/mp4',
  65. ];
  66. $fileMimeType = $file->getMimeType();
  67. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  68. return back()->with('error','File type allow MP4!');
  69. }
  70. if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
  71. $data["video"] = $file;
  72. }
  73. $id = InteractionInfo::create($data);
  74. if($id) {
  75. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  76. $this->showMessage('添加成功',$url);
  77. }else{
  78. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  79. return $this->showWarning('添加失败',$url);
  80. }
  81. }
  82. // 修改动态
  83. public function updateInteraction(Request $reqeust) {
  84. if($reqeust->method() == 'POST') {
  85. return $this->_updateSave();
  86. }
  87. $data = InteractionInfo::find($reqeust->get('id'));
  88. $arr = [];
  89. for ($i = 1; $i <=9; $i++) {
  90. if (!empty($data['pic'.($i)])) {
  91. $arr[] = $data['pic'.($i)];
  92. }
  93. }
  94. $data->imgs = $arr;
  95. return view('admin.interaction.info.edit',compact('data'));
  96. }
  97. /**
  98. * 保存修改
  99. */
  100. private function _updateSave() {
  101. $data = (array) request('data');
  102. $interaction_id = request('id');
  103. $interaction = InteractionInfo::find($interaction_id)->toArray();
  104. $old_pics = [];
  105. for ($i = 1; $i <=9; $i++) {
  106. if (!empty($interaction['pic'.($i)])) {
  107. $old_pics[] = $interaction['pic'.($i)];
  108. }
  109. }
  110. $new_pics = (array) request('pic');
  111. $a = array_diff($new_pics['url'],$old_pics);
  112. $b = array_diff($old_pics,$new_pics['url']);
  113. if (!empty($a) || !empty($b)) { //有图片变化执行
  114. // 删除以前图片 重新插入
  115. if (!empty($b)) {
  116. foreach ($b as $old_pic){
  117. $key = array_search($old_pic,$interaction);
  118. $interaction[$key] = '';
  119. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  120. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  121. }
  122. BaseAttachmentModel::where('url',$old_pic)->delete();
  123. InteractionInfo::find(request('id'))->update($interaction);
  124. }
  125. }
  126. $a = array_values($a);
  127. for ($i = 1; $i <=9; $i++) {
  128. if (empty($interaction['pic'.($i)])) {
  129. $c = -1;
  130. $c++;
  131. $interaction['pic'.($i)] = getenv('APP_URL').$a[$c];
  132. }
  133. }
  134. }
  135. $ok = InteractionInfo::find(request('id'))->update($interaction);
  136. if($ok) {
  137. $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
  138. return $this->showMessage('操作成功',urldecode(request('_referer')));
  139. }else{
  140. $url[] = array('url'=>U( 'Interaction/Info/index'),'title'=>'返回列表');
  141. return $this->showWarning('操作失败',$url);
  142. }
  143. }
  144. /**
  145. * 删除
  146. */
  147. public function destroy(Request $reqeust)
  148. {
  149. $ok = InteractionInfo::destroy($reqeust->get('id'));
  150. if ($ok) {
  151. return $this->showMessage('操作成功');
  152. } else {
  153. return $this->showWarning("操作失败");
  154. }
  155. }
  156. public function comment(Request $request)
  157. {
  158. $interaction_id = $request->input('id');
  159. $comment_id = $request->input('comment_id');
  160. if ($request->isMethod('POST')) {
  161. $comment = $request->input('comment');
  162. $interaction = InteractionInfo::find($interaction_id);
  163. $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
  164. $user = UserInfoModel::find($user_id);
  165. if(!empty($comment_id)){ //回复
  166. $comment_info = CommentInfoModel::find($comment_id);
  167. $interaction = InteractionInfo::find($comment_info->interaction_id);
  168. $user_id = empty($interaction->dream) ? 0 : $interaction->dream->user_id;
  169. $user = UserInfoModel::find($user_id);
  170. $arr = [
  171. 'interaction_id'=>$comment_info->interaction_id,
  172. 'user_id'=>$comment_info->user_id,
  173. 'user_avatar'=>$comment_info->user_avatar,
  174. 'user_nickname'=>$comment_info->user_nickname,
  175. 'to_user_id'=>$user_id,
  176. 'content'=>$comment,
  177. 'to_user_avatar'=>empty($user) ? '' : $user->avatar ,
  178. 'to_user_nickname'=>empty($user) ? '' : $user->nickname ,
  179. ];
  180. $ok = CommentInfoModel::create($arr);
  181. }else{
  182. $arr = [
  183. 'interaction_id'=>$interaction_id,
  184. 'user_id'=>$user_id,
  185. 'content'=>$comment,
  186. 'user_avatar'=>empty($user) ? '' : $user->avatar ,
  187. 'user_nickname'=>empty($user) ? '' : $user->nickname ,
  188. ];
  189. $ok = CommentInfoModel::create($arr);
  190. }
  191. if ($ok) {
  192. return $this->showMessage('操作成功');
  193. } else {
  194. return $this->showWarning("操作失败");
  195. }
  196. }
  197. return view('admin.comment.edit',compact('interaction_id'));
  198. }
  199. }