InfoController.php 11 KB

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