InfoController.php 9.3 KB

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