InfoController.php 13 KB

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