InfoController.php 14 KB

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