InfoController.php 10 KB

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