LandmarkController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * 地标
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-10-26 08:38:42
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Map;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\MapBannerModel;
  12. use App\Repositories\Map\Criteria\MarkWhere;
  13. use Illuminate\Http\Request;
  14. use App\Repositories\Base\Criteria\OrderBy;
  15. use App\Repositories\Map\Criteria\MultiWhere;
  16. use App\Repositories\Map\LandmarkRepository;
  17. class LandmarkController extends Controller
  18. {
  19. private $repository;
  20. public function __construct(LandmarkRepository $repository) {
  21. if(!$this->repository) $this->repository = $repository;
  22. }
  23. function index(Request $request) {
  24. $search['keyword'] = $request->input('keyword');
  25. $query = $this->repository->pushCriteria(new MarkWhere($search));
  26. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  27. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  28. }else{
  29. $query = $query->pushCriteria(new OrderBy('id','DESC'));
  30. }
  31. $list = $query->paginate();
  32. return view('admin.map.landmark.index',compact('list'));
  33. }
  34. function check(Request $request) {
  35. $request = $request->all();
  36. $search['keyword'] = $request->input('keyword');
  37. $orderby = array();
  38. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  39. $orderby[$request['sort_field']] = $request['sort_field_by'];
  40. }
  41. $list = $this->repository->search($search,$orderby);
  42. return view('admin.map.landmark.check',compact('list'));
  43. }
  44. /**
  45. * 添加
  46. *
  47. */
  48. public function create(Request $request)
  49. {
  50. if($request->method() == 'POST') {
  51. return $this->_createSave();
  52. }
  53. return view('admin.map.landmark.edit');
  54. }
  55. /**
  56. * 保存修改
  57. */
  58. private function _createSave(){
  59. $data = (array) request('data');
  60. if(!empty($data['banner']['url'])){
  61. $banner = $data['banner']['url'];
  62. unset($data['banner']);
  63. }
  64. // $data['cover'] = $this->formatImgUrl($data['cover']);
  65. $id = $this->repository->create($data);
  66. if(!empty($banner)){
  67. foreach($banner as $key=>$val){
  68. $add['image'] = $this->formatImgUrl($val);
  69. $add['mark_id'] = $id;
  70. $add['sort'] = 0;
  71. MapBannerModel::create($add);
  72. }
  73. }
  74. if($id) {
  75. $url[] = array('url'=>U( 'Map/Landmark/index'),'title'=>'返回列表');
  76. $url[] = array('url'=>U( 'Map/Landmark/create'),'title'=>'继续添加');
  77. $this->showMessage('添加成功',$url);
  78. }else{
  79. $url[] = array('url'=>U( 'Map/Landmark/index'),'title'=>'返回列表');
  80. return $this->showWarning('添加失败',$url);
  81. }
  82. }
  83. /**
  84. *
  85. * 修改
  86. *
  87. *
  88. */
  89. public function update(Request $request) {
  90. if($request->method() == 'POST') {
  91. return $this->_updateSave();
  92. }
  93. $data = $this->repository->find($request->get('id'));
  94. $banner = MapBannerModel::where('mark_id',$data['id'])->get();
  95. $imgs = array();
  96. foreach($banner as $key=>$val){
  97. $imgs[] = $val['image'];
  98. }
  99. $data->banner = $imgs;
  100. return view('admin.map.landmark.edit',compact('data'));
  101. }
  102. /**
  103. * 保存修改
  104. */
  105. private function _updateSave() {
  106. $data = (array) request('data');
  107. $id = request('id');
  108. MapBannerModel::where('mark_id',$id)->delete();
  109. foreach($data['banner']['url'] as $key=>$val){
  110. $add['image'] = $this->formatImgUrl($val);
  111. $add['mark_id'] = $id;
  112. $add['sort'] = 0;
  113. MapBannerModel::create($add);
  114. }
  115. unset($data['banner']);
  116. // $data['cover'] = $this->formatImgUrl($data['cover']);
  117. // dd($data);
  118. $ok = $this->repository->update(request('id'),$data);
  119. if($ok) {
  120. $url[] = array('url'=>U( 'Map/Landmark/index'),'title'=>'返回列表');
  121. return $this->showMessage('操作成功',urldecode(request('_referer')));
  122. }else{
  123. $url[] = array('url'=>U( 'Map/Landmark/index'),'title'=>'返回列表');
  124. return $this->showWarning('操作失败',$url);
  125. }
  126. }
  127. public function view(Request $request) {
  128. $data = $this->repository->find(request('id'));
  129. return view('admin.map.landmark.view',compact('data'));
  130. }
  131. /**
  132. *
  133. * 状态改变
  134. *
  135. */
  136. public function status(Request $request) {
  137. $ok = $this->repository->updateStatus(request('id'),request('status'));
  138. if($ok) {
  139. return $this->showMessage('操作成功');
  140. }else{
  141. return $this->showWarning('操作失败');
  142. }
  143. }
  144. /**
  145. * 删除
  146. */
  147. public function destroy(Request $request) {
  148. $bool = $this->repository->destroy($request->get('id'));
  149. if($bool) {
  150. return $this->showMessage('操作成功');
  151. }else{
  152. return $this->showWarning("操作失败");
  153. }
  154. }
  155. }