123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- /**
- * 梦想列表
- * @author system
- * @version 1.0
- * @date 2017-06-28 14:50:22
- *
- */
- namespace App\Http\Controllers\Admin\Dream;
- use App\Http\Controllers\Admin\Controller;
- use App\Models\Attachment;
- use App\Models\BaseAttachmentModel;
- use App\Models\BaseSettingsModel;
- use App\Models\DreamImages;
- use App\Models\DreamInfoModel;
- use App\Models\SupportDreamModel;
- use App\Models\SystemInfoModel;
- use App\Models\UserCareDream;
- use App\Widget\Tools\VideoUpload;
- use Illuminate\Http\Request;
- use App\Repositories\Base\Criteria\OrderBy;
- use App\Repositories\Dream\Criteria\MultiWhere;
- use App\Repositories\Dream\InfoRepository;
- use App\Helper\JpushHelper;
- class InfoController extends Controller
- {
- use JpushHelper;
- private $repository;
- public function __construct(InfoRepository $repository) {
- if(!$this->repository) $this->repository = $repository;
- }
- function index(Request $reqeust) {
- $search['keyword'] = $reqeust->input('keyword');
- $query = $this->repository->pushCriteria(new MultiWhere($search));
- $request = $reqeust->all();
- if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
- }else{
- $query = $query->pushCriteria(new OrderBy('id','desc'));
- }
- $list = $query->with(['meet'])->paginate();
- $setting = BaseSettingsModel::where('category','paihang')->first();
- $a = $setting?$setting->key:1;
- $x = $setting?$setting->sort:10;
- if($x==0) $x = 10;
- $setting2 = BaseSettingsModel::where('category','score')->select('key','value','sort')->first();
- $a2 = count($setting2) > 0 ? $setting2->key : 0;
- $c2 = count($setting2) > 0 ? $setting2->value : 0;
- $n2 = count($setting2) > 0 ? $setting2->sort : 0;
- foreach ($list as $item) {
- $t = ceil((time()-strtotime($item->created_at))/60); //梦想发布分钟数
- $care_num = UserCareDream::where('dream_id',$item->id)->get()->count();
- if ($care_num==0) $care_num=1;
- $item->score = (log($care_num,$x) + ($a/$t) + $item->parameter)*100000000000000 ;
- $item->save();
- $item->x = log($care_num,$x);
- $item->y = $a/$t;
- $item->score = $item->score/100000000000000;
- $b2 = ceil((time()-strtotime($item->created_at))/60); //梦想发布分钟数
- $number =round( $n2*exp(-$a2*(pow($b2,1/$c2)))+1,2);
- $item->number = $number;
- }
- return view('admin.dream.info.index',compact('list'));
- }
- function check(Request $reqeust) {
- $request = $reqeust->all();
- $search['keyword'] = $reqeust->input('keyword');
- $orderby = array();
- if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $orderby[$request['sort_field']] = $request['sort_field_by'];
- }
- $list = $this->repository->search($search,$orderby);
- return view('admin.dream.info.check',compact('list'));
- }
- /**
- * 添加
- *
- */
- public function create(Request $reqeust)
- {
- if($reqeust->method() == 'POST') {
- return $this->_createSave();
- }
- $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
- return view('admin.dream.info.edit',compact('signs'));
- }
- /**
- * 保存修改
- */
- private function _createSave(){
- $data = (array) request('data');
- $care_num = 1; //梦想收藏人数 新建梦想时默认为1
- $setting = BaseSettingsModel::where('category','paihang')->first();
- $a = $setting?$setting->key:2;
- $b = $data['parameter'];
- $x = $setting?$setting->sort:10;
- $t = 1; //创建梦想时梦想发布时间最小为一分钟
- if ($care_num == 0) {
- $data['score'] = (($a/$t) + $b)*100000000000000 ;
- }else{
- $data['score'] = (log($care_num,$x) + ($a/$t) + $b)*100000000000000 ;
- }
- if (is_array($data['sign'])) {
- $data['sign'] = join(',',$data['sign']);
- }
- $pics = (array) request('pic');
- if (!empty(request("video"))) {
- $file = request("video");
- $fileSize = $file->getSize();
- $size = 200 * 1024 * 1024;
- if ($fileSize > $size) {
- return back()->with('error','请上传小于200MB的文件!');
- }
- $mimeType = [
- 'video/mp4',
- ];
- $fileMimeType = $file->getMimeType();
- if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
- return back()->with('error','File type allow MP4!');
- }
- if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
- $data["video"] = $file;
- }
- $data['created_at'] = date('Y-m-d H:i:s');
- $data['updated_at'] = date('Y-m-d H:i:s');
- $id = DreamInfoModel::insertGetId($data);
- // 生成二维码
- /* $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
- $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
- $code_url = env('APP_URL').'/user/meet?dream_id='.$id;
- $code_path = public_path('qrcodes/'.$info['code'].'.png');
- \QrCode::format('png')->size(500)->generate($code_url,$code_path);
- $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
- DreamInfoModel::where('id',$id)->update(compact('code'));*/
- if($id) {
- $arr = [];
- if (!empty($pics)) {
- foreach ($pics['url'] as $pic) {
- $arr[] = [
- 'pic'=>$pic,
- 'dream_id'=>$id,
- 'created_at'=>date("Y-m-d H:i:s"),
- 'updated_at'=>date("Y-m-d H:i:s"),
- ];
- }
- DreamImages::insert($arr);
- }
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- $url[] = array('url'=>U( 'Dream/Info/create'),'title'=>'继续添加');
- $this->showMessage('添加成功',$url);
- }else{
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- return $this->showWarning('添加失败',$url);
- }
- }
-
- /**
- *
- * 修改
- *
- *
- */
- public function update(Request $reqeust) {
- if($reqeust->method() == 'POST') {
- return $this->_updateSave();
- }
- $data = $this->repository->find($reqeust->get('id'));
- if (empty($data->video)) {
- $data->is_video = 0;
- }else{
- $data->is_video = 1;
- }
- $imgs = $data->imgs;
- $arr = [];
- foreach ($imgs as $pic) {
- $arr[] = $pic['pic'];
- }
- $data->imgs = $arr;
- $data['sign'] = explode(',',$data['sign']);
- $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
- // dd($data);
- return view('admin.dream.info.edit',compact('data','signs'));
- }
- /**
- * 保存修改
- */
- private function _updateSave() {
- $data = (array) request('data');
- $dream = DreamInfoModel::find(request('id'));
- if ($data['status']==2) { //审核不过
- $info = [
- 'to_user_id' => $data['user_id'],
- 'message' => ' 你的梦想被暂停,客服会在短时间内联络你',
- 'type_id' => 1,
- 'attr_id' => 5,
- ];
- SystemInfoModel::create($info);
- }
- if (array_key_exists('sign',$data)) {
- $data['sign'] = join(',',$data['sign']);
- }
- if (!array_key_exists('status',$data)) {
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- return $this->showWarning('请选择梦想状态',$url);
- }
- // 后台修改排行参数时,排行分数也改变
- $dream_id = request('id');
- $data2 = UserCareDream::where('dream_id',$dream_id)->get();
- $care_num = count($data2);
- if ($care_num==0) $care_num = 1; //梦想收藏人数 梦想时默认为1
- $setting = BaseSettingsModel::where('category','paihang')->first();
- $a = $setting?$setting->key:2;
- $b = $data['parameter'];
- $x = $setting?$setting->sort:10;
- if(empty($x)) $x=10;
- $t = ceil((time()-strtotime($dream->created_at))/60); //梦想发布分钟数
- if ($care_num == 0) {
- $data['score'] = (($a/$t) + $b)*100000000000000 ;
- }else{
- $data['score'] = (log($care_num,$x) + ($a/$t) + $b)*100000000000000 ;
- }
- $pics = (array) request('pic');
- if (!empty(request("video"))) {
- $md5 = $this->getarea($dream->video);
- if (!empty($md5)) {
- $attache = new \App\Services\Base\Attachment();
- $attache->deleteAttachment($md5);
- }
- $file = request("video");
- $fileSize = $file->getSize();
- $size = 200 * 1024 * 1024;
- if ($fileSize > $size) {
- return back()->with('error','请上传小于200 MB的文件!');
- }
- $mimeType = [
- 'video/mp4',
- ];
- $fileMimeType = $file->getMimeType();
- if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
- return back()->with('error','File type allow MP4!');
- }
- if (!$file = VideoUpload::mvFile('video')) return back()->with('error','上传失败');
- $data["video"] = $file;
- }
- if (!empty($pics)) {
- // 图片不为空
- $old_data_pics = $this->repository->find(request('id'))->imgs->toArray();
- $old_pics = array_column($old_data_pics,'pic');
- //操作成功,删除原来的图片
- foreach ($old_pics as $pic) {
- if (!in_array($pic, $pics['url'])) {
- $md5 = $this->getarea($pic);
- $attache = new \App\Services\Base\Attachment();
- $attache->deleteAttachment($md5);
- }
- }
- DreamImages::where('dream_id',request('id'))->delete();
- $arr = []; //插入新的图片
- foreach ($pics['url'] as $pic) {
- $arr[] = [
- 'pic'=>$pic,
- 'dream_id'=>request('id'),
- 'created_at'=>date('Y-m-d H:i:s'),
- 'updated_at'=>date('Y-m-d H:i:s'),
- ];
- }
- DreamImages::insert($arr);
- }
- $ok = $this->repository->update(request('id'),$data);
- if ($data['status'] == 1) { //审核通过
- $message = '你的梦想《'.$data['name'].'》已被批准,离你实现梦想又更进一步啦~ ';
- $info = [
- 'user_id' => $data['user_id'],
- 'message' => $message,
- ];
- SystemInfoModel::create($info);
- // 长连接
- $this->jPush($message,'',$data['user_id']);
- }
- if($ok) {
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- return $this->showMessage('操作成功',urldecode(request('_referer')));
- }else{
- $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
- return $this->showWarning('操作失败',$url);
- }
- }
- public function view(Request $reqeust) {
- $data = $this->repository->find(request('id'));
- return view('admin.dream.info.view',compact('data'));
- }
- /**
- *
- * 状态改变
- *
- */
- public function status(Request $reqeust) {
- $ok = $this->repository->updateStatus(request('id'),request('status'));
- if($ok) {
- return $this->showMessage('操作成功');
- }else{
- return $this->showWarning('操作失败');
- }
- }
-
- /**
- * 删除
- */
- public function destroy(Request $reqeust) {
- $dream = DreamInfoModel::find($reqeust->get('id'));
- if($dream) {
- $old_data_pics = $dream->imgs->toArray();
- $old_pics = array_column($old_data_pics,'pic');
- if (!empty($old_pics)) {
- foreach ($old_pics as $old_pic){
- if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
- unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
- }
- BaseAttachmentModel::where('url',$old_pic)->delete();
- }
- BaseAttachmentModel::where('url',$old_pic)->delete();
- }
- DreamImages::where('dream_id',$reqeust->get('id'))->delete();
- $this->repository->destroy($reqeust->get('id'));
- return $this->showMessage('操作成功');
- }else{
- return $this->showWarning("操作失败");
- }
- }
- public function show_code(Request $request)
- {
- $code = $request->code;
- return view('admin.dream.info.show_code',compact('code'));
- }
- // 查看支持记录
- public function supportInfo(Request $request)
- {
- $dream_id = $request->input('dream_id');
- $request = $request->all();
- if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $query = SupportDreamModel::orderBy($request['sort_field'],$request['sort_field_by']);
- }else{
- $query = SupportDreamModel::orderBy('id','desc');
- }
- $list = $query->with('user')->where('dream_id',$dream_id)->paginate();
- return view('admin.dream.support_dream.index',compact('list'));
- }
- // 获取视频图片后缀码
- public function getarea($str)
- {
- $start = strripos($str, '/');
- $first = substr($str, $start + 1);
- $end = strripos($first, '.');
- $last = substr($first, 0, $end);
- return $last;
- }
- }
|