InfoController.php 13 KB

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