InfoController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * 梦想列表
  4. * @author system
  5. * @version 1.0
  6. * @date 2017-06-28 14:50:22
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Dream;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\BaseAttachmentModel;
  12. use App\Models\BaseSettingsModel;
  13. use App\Models\DreamImages;
  14. use App\Models\DreamInfoModel;
  15. use App\Models\SupportDreamModel;
  16. use App\Models\SystemInfoModel;
  17. use App\Models\UserCareDream;
  18. use App\Widget\Tools\VideoUpload;
  19. use Illuminate\Http\Request;
  20. use App\Repositories\Base\Criteria\OrderBy;
  21. use App\Repositories\Dream\Criteria\MultiWhere;
  22. use App\Repositories\Dream\InfoRepository;
  23. use App\Helper\JpushHelper;
  24. class InfoController extends Controller
  25. {
  26. use JpushHelper;
  27. private $repository;
  28. public function __construct(InfoRepository $repository) {
  29. if(!$this->repository) $this->repository = $repository;
  30. }
  31. function index(Request $reqeust) {
  32. $search['keyword'] = $reqeust->input('keyword');
  33. $query = $this->repository->pushCriteria(new MultiWhere($search));
  34. $request = $reqeust->all();
  35. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  36. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  37. }else{
  38. $query = $query->pushCriteria(new OrderBy('id','desc'));
  39. }
  40. $list = $query->with(['meet'])->paginate();
  41. $ab_value = BaseSettingsModel::where('category','paihang')->first();
  42. return view('admin.dream.info.index',compact('list','ab_value'));
  43. }
  44. function check(Request $reqeust) {
  45. $request = $reqeust->all();
  46. $search['keyword'] = $reqeust->input('keyword');
  47. $orderby = array();
  48. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  49. $orderby[$request['sort_field']] = $request['sort_field_by'];
  50. }
  51. $list = $this->repository->search($search,$orderby);
  52. return view('admin.dream.info.check',compact('list'));
  53. }
  54. /**
  55. * 添加
  56. *
  57. */
  58. public function create(Request $reqeust)
  59. {
  60. if($reqeust->method() == 'POST') {
  61. return $this->_createSave();
  62. }
  63. $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
  64. return view('admin.dream.info.edit',compact('signs'));
  65. }
  66. /**
  67. * 保存修改
  68. */
  69. private function _createSave(){
  70. $data = (array) request('data');
  71. if (is_array($data['sign'])) {
  72. $data['sign'] = join(',',$data['sign']);
  73. }
  74. $pics = (array) request('pic');
  75. if (request("file")) {
  76. $file = request("file");
  77. $fileSize = $file->getSize();
  78. $size = 200 * 1024 * 1024;
  79. if ($fileSize > $size) {
  80. return back()->with('error','请上传小于200MB的文件!');
  81. }
  82. $mimeType = [
  83. 'video/mp4',
  84. ];
  85. $fileMimeType = $file->getMimeType();
  86. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  87. return back()->with('error','File type allow MP4!');
  88. }
  89. if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
  90. $data["video"] = $file;
  91. }
  92. $data['created_at'] = date('Y-m-d H:i:s');
  93. $data['updated_at'] = date('Y-m-d H:i:s');
  94. $id = DreamInfoModel::insertGetId($data);
  95. // 生成二维码
  96. /* $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  97. $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
  98. $code_url = env('APP_URL').'/user/meet?dream_id='.$id;
  99. $code_path = public_path('qrcodes/'.$info['code'].'.png');
  100. \QrCode::format('png')->size(500)->generate($code_url,$code_path);
  101. $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
  102. DreamInfoModel::where('id',$id)->update(compact('code'));*/
  103. if($id) {
  104. $arr = [];
  105. if (!empty($pics)) {
  106. foreach ($pics['url'] as $pic) {
  107. $arr[] = [
  108. 'pic'=>getenv('APP_URL').$pic,
  109. 'dream_id'=>$id,
  110. 'created_at'=>date("Y-m-d H:i:s"),
  111. 'updated_at'=>date("Y-m-d H:i:s"),
  112. ];
  113. }
  114. DreamImages::insert($arr);
  115. }
  116. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  117. $url[] = array('url'=>U( 'Dream/Info/create'),'title'=>'继续添加');
  118. $this->showMessage('添加成功',$url);
  119. }else{
  120. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  121. return $this->showWarning('添加失败',$url);
  122. }
  123. }
  124. /**
  125. *
  126. * 修改
  127. *
  128. *
  129. */
  130. public function update(Request $reqeust) {
  131. if($reqeust->method() == 'POST') {
  132. return $this->_updateSave();
  133. }
  134. $data = $this->repository->find($reqeust->get('id'));
  135. if (empty($data->video)) {
  136. $data->is_video = 0;
  137. }else{
  138. $data->is_video = 1;
  139. }
  140. $imgs = $data->imgs;
  141. $arr = [];
  142. foreach ($imgs as $pic) {
  143. $arr[] = $pic['pic'];
  144. }
  145. $data->imgs = $arr;
  146. // dd($data);
  147. $data['sign'] = explode(',',$data['sign']);
  148. $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
  149. return view('admin.dream.info.edit',compact('data','signs'));
  150. }
  151. /**
  152. * 保存修改
  153. */
  154. private function _updateSave() {
  155. $data = (array) request('data');
  156. if ($data['status']==2) { //审核不过
  157. $info = [
  158. 'to_user_id' => $data['user_id'],
  159. 'message' => ' 你的梦想被暂停,客服会在短时间内联络你',
  160. 'type_id' => 1,
  161. 'attr_id' => 5,
  162. ];
  163. SystemInfoModel::create($info);
  164. }
  165. if (array_key_exists('sign',$data)) {
  166. $data['sign'] = join(',',$data['sign']);
  167. }
  168. if (!array_key_exists('status',$data)) {
  169. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  170. return $this->showWarning('请选择梦想状态',$url);
  171. }
  172. // 后台修改排行参数时,梦想分数也改变
  173. $dream_id = request('id');
  174. $data2 = UserCareDream::where('dream_id',$dream_id)->get();
  175. $care_num = count($data2);
  176. $setting = BaseSettingsModel::where('category','paihang')->first();
  177. $a = $data['parameter'];
  178. $b = $setting?$setting->value:1;
  179. $t = 21*3600 / 60;
  180. \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  181. if ($care_num == 0) {
  182. $data['score'] = (($a/$t) + $b)*100000000 ;
  183. }else{
  184. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  185. }
  186. $pics = (array) request('pic');
  187. if (request("file")) {
  188. $file = request("file");
  189. $fileSize = $file->getSize();
  190. $size = 200 * 1024 * 1024;
  191. if ($fileSize > $size) {
  192. return back()->with('error','请上传小于200 MB的文件!');
  193. }
  194. $mimeType = [
  195. 'video/mp4',
  196. ];
  197. $fileMimeType = $file->getMimeType();
  198. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  199. return back()->with('error','File type allow MP4!');
  200. }
  201. if (!$file = VideoUpload::mvFile('file')) return back()->with('error','上传失败');
  202. $data["video"] = $file;
  203. }else{
  204. // 更新是视频不存在则删除
  205. $dream = $this->repository->find(request('id'));
  206. if (is_file('.'.str_replace(getenv('APP_URL'),'',$dream->video))) {
  207. unlink('.'.str_replace(getenv('APP_URL'),'',$dream->video));
  208. }
  209. $dream->video = '';
  210. $dream->save();
  211. }
  212. if (!empty($pics)) {
  213. // 图片不为空
  214. $old_data_pics = $this->repository->find(request('id'))->imgs->toArray();
  215. $old_pics = array_column($old_data_pics,'pic');
  216. $a = array_diff($pics['url'],$old_pics);
  217. $b = array_diff($old_pics,$pics['url']);
  218. if (!empty($a) || !empty($b)) { //有图片变化执行
  219. // 删除以前图片 重新插入
  220. if (!empty($b)) {
  221. foreach ($b as $old_pic){
  222. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  223. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  224. }
  225. BaseAttachmentModel::where('url',$old_pic)->delete();
  226. DreamImages::where('pic',$old_pic)->delete();
  227. }
  228. }
  229. $arr = []; //插入新的图片
  230. foreach ($a as $pic) {
  231. $arr[] = [
  232. 'pic'=>getenv('APP_URL').$pic,
  233. 'dream_id'=>request('id'),
  234. 'created_at'=>date('Y-m-d H:i:s'),
  235. 'updated_at'=>date('Y-m-d H:i:s'),
  236. ];
  237. }
  238. DreamImages::insert($arr);
  239. }
  240. }
  241. $ok = $this->repository->update(request('id'),$data);
  242. if ($data['status'] == 1) { //审核通过
  243. $message = '你的梦想《'.$data['name'].'》已被批准,离你实现梦想又更进一步啦~ ';
  244. $info = [
  245. 'user_id' => $data['user_id'],
  246. 'message' => $message,
  247. ];
  248. SystemInfoModel::create($info);
  249. // 长连接
  250. // $this->jPush($message,'',$data['user_id']);
  251. }
  252. if($ok) {
  253. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  254. return $this->showMessage('操作成功',urldecode(request('_referer')));
  255. }else{
  256. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  257. return $this->showWarning('操作失败',$url);
  258. }
  259. }
  260. public function view(Request $reqeust) {
  261. $data = $this->repository->find(request('id'));
  262. return view('admin.dream.info.view',compact('data'));
  263. }
  264. /**
  265. *
  266. * 状态改变
  267. *
  268. */
  269. public function status(Request $reqeust) {
  270. $ok = $this->repository->updateStatus(request('id'),request('status'));
  271. if($ok) {
  272. return $this->showMessage('操作成功');
  273. }else{
  274. return $this->showWarning('操作失败');
  275. }
  276. }
  277. /**
  278. * 删除
  279. */
  280. public function destroy(Request $reqeust) {
  281. $dream = DreamInfoModel::find($reqeust->get('id'));
  282. if($dream) {
  283. $old_data_pics = $dream->imgs->toArray();
  284. $old_pics = array_column($old_data_pics,'pic');
  285. if (!empty($old_pics)) {
  286. foreach ($old_pics as $old_pic){
  287. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  288. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  289. }
  290. BaseAttachmentModel::where('url',$old_pic)->delete();
  291. }
  292. BaseAttachmentModel::where('url',$old_pic)->delete();
  293. }
  294. DreamImages::where('dream_id',$reqeust->get('id'))->delete();
  295. $this->repository->destroy($reqeust->get('id'));
  296. return $this->showMessage('操作成功');
  297. }else{
  298. return $this->showWarning("操作失败");
  299. }
  300. }
  301. public function show_code(Request $request)
  302. {
  303. $code = $request->code;
  304. return view('admin.dream.info.show_code',compact('code'));
  305. }
  306. // 查看支持记录
  307. public function supportInfo(Request $request)
  308. {
  309. $dream_id = $request->input('dream_id');
  310. $request = $request->all();
  311. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  312. $query = SupportDreamModel::orderBy($request['sort_field'],$request['sort_field_by']);
  313. }else{
  314. $query = SupportDreamModel::orderBy('id','desc');
  315. }
  316. $list = $query->with('user')->where('dream_id',$dream_id)->paginate();
  317. return view('admin.dream.support_dream.index',compact('list'));
  318. }
  319. }