InfoController.php 14 KB

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