InfoController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 ($data['status']==2) { //审核不过
  135. $info = [
  136. 'user_id' => $data['user_id'],
  137. 'message' => ' 你的梦想被暂停,客服会在短时间内联络你',
  138. 'type_id' => 1,
  139. ];
  140. SystemInfoModel::create($info);
  141. }
  142. if (array_key_exists('sign',$data)) {
  143. $data['sign'] = join(',',$data['sign']);
  144. }
  145. if (!array_key_exists('status',$data)) {
  146. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  147. return $this->showWarning('请选择梦想状态',$url);
  148. }
  149. // 后台修改排行参数时,梦想分数也改变
  150. $dream_id = request('id');
  151. $data2 = UserCareDream::where('dream_id',$dream_id)->get();
  152. $care_num = count($data2);
  153. $setting = BaseSettingsModel::where('category','paihang')->first();
  154. $a = $data['parameter'];
  155. $b = $setting?$setting->value:1;
  156. $t = 21*3600 / 60;
  157. \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  158. if ($care_num == 0) {
  159. $data['score'] = (($a/$t) + $b)*100000000 ;
  160. }else{
  161. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  162. }
  163. $pics = (array) request('pic');
  164. if (empty($pics)) {
  165. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  166. return $this->showWarning('请添加图片',$url);
  167. }
  168. $old_data_pics = $this->repository->find(request('id'))->imgs->toArray();
  169. $old_pics = array_column($old_data_pics,'pic');
  170. $a = array_diff($pics['url'],$old_pics);
  171. $b = array_diff($old_pics,$pics['url']);
  172. if (!empty($a) || !empty($b)) { //有图片变化执行
  173. // 删除以前图片 重新插入
  174. if (!empty($b)) {
  175. foreach ($b as $old_pic){
  176. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  177. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  178. }
  179. BaseAttachmentModel::where('url',$old_pic)->delete();
  180. DreamImages::where('pic',$old_pic)->delete();
  181. }
  182. }
  183. $arr = []; //插入新的图片
  184. foreach ($a as $pic) {
  185. $arr[] = [
  186. 'pic'=>getenv('APP_URL').$pic,
  187. 'dream_id'=>request('id'),
  188. 'created_at'=>date('Y-m-d H:i:s'),
  189. 'updated_at'=>date('Y-m-d H:i:s'),
  190. ];
  191. }
  192. DreamImages::insert($arr);
  193. }
  194. $ok = $this->repository->update(request('id'),$data);
  195. if ($data['status'] == 1) { //审核通过
  196. $message = '你的梦想《'.$data['name'].'》已被批准,离你实现梦想又更进一步啦~ ';
  197. $info = [
  198. 'user_id' => $data['user_id'],
  199. 'message' => $message,
  200. ];
  201. SystemInfoModel::create($info);
  202. // 长连接
  203. $this->jPush($message,'',$data['user_id']);
  204. }
  205. if($ok) {
  206. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  207. return $this->showMessage('操作成功',urldecode(request('_referer')));
  208. }else{
  209. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  210. return $this->showWarning('操作失败',$url);
  211. }
  212. }
  213. public function view(Request $reqeust) {
  214. $data = $this->repository->find(request('id'));
  215. return view('admin.dream.info.view',compact('data'));
  216. }
  217. /**
  218. *
  219. * 状态改变
  220. *
  221. */
  222. public function status(Request $reqeust) {
  223. $ok = $this->repository->updateStatus(request('id'),request('status'));
  224. if($ok) {
  225. return $this->showMessage('操作成功');
  226. }else{
  227. return $this->showWarning('操作失败');
  228. }
  229. }
  230. /**
  231. * 删除
  232. */
  233. public function destroy(Request $reqeust) {
  234. $dream = DreamInfoModel::find($reqeust->get('id'));
  235. if($dream) {
  236. $old_data_pics = $dream->imgs->toArray();
  237. $old_pics = array_column($old_data_pics,'pic');
  238. if (!empty($old_pics)) {
  239. foreach ($old_pics as $old_pic){
  240. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  241. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  242. }
  243. BaseAttachmentModel::where('url',$old_pic)->delete();
  244. }
  245. BaseAttachmentModel::where('url',$old_pic)->delete();
  246. }
  247. DreamImages::where('dream_id',$reqeust->get('id'))->delete();
  248. $this->repository->destroy($reqeust->get('id'));
  249. return $this->showMessage('操作成功');
  250. }else{
  251. return $this->showWarning("操作失败");
  252. }
  253. }
  254. public function show_code(Request $request)
  255. {
  256. $code = $request->code;
  257. return view('admin.dream.info.show_code',compact('code'));
  258. }
  259. // 查看支持记录
  260. public function supportInfo(Request $request)
  261. {
  262. $dream_id = $request->input('dream_id');
  263. $request = $request->all();
  264. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  265. $query = SupportDreamModel::orderBy($request['sort_field'],$request['sort_field_by']);
  266. }else{
  267. $query = SupportDreamModel::orderBy('id','desc');
  268. }
  269. $list = $query->with('user')->where('dream_id',$dream_id)->paginate();
  270. return view('admin.dream.support_dream.index',compact('list'));
  271. }
  272. }