LandmarkController.php 5.4 KB

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