InfoController.php 14 KB

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