InfoController.php 9.3 KB

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