CatController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * 产品分类
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-05-14 13:27:48
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Album;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\AlbumCatModel;
  12. use App\Repositories\Album\Criteria\CatWhere;
  13. use Dingo\Blueprint\Annotation\Method\Post;
  14. use Illuminate\Http\Request;
  15. use App\Repositories\Base\Criteria\OrderBy;
  16. use App\Repositories\Album\Criteria\MultiWhere;
  17. use App\Repositories\Album\CatRepository;
  18. class CatController extends Controller
  19. {
  20. private $repository;
  21. public function __construct(CatRepository $repository) {
  22. if(!$this->repository) $this->repository = $repository;
  23. }
  24. function index(Request $request) {
  25. //AlbumCatModel::where()->get();
  26. $search['keyword'] = $request->input('keyword');
  27. $query = $this->repository->pushCriteria(new CatWhere($search,0,$this->getStoreId()));
  28. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  29. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  30. }else{
  31. $query = $query->pushCriteria(new OrderBy('id','DESC'));
  32. }
  33. $list = $query->paginate();
  34. foreach ($list as $key => $item) {
  35. //dump($item);
  36. $item->sonlist = AlbumCatModel::where('parent_id',$item->id)->get();
  37. }
  38. // dump($list);
  39. return view('admin.album.cat.index',compact('list'));
  40. }
  41. function check(Request $request) {
  42. $request = $request->all();
  43. $search['keyword'] = $request->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.album.cat.check',compact('list'));
  50. }
  51. /**
  52. * 添加
  53. *
  54. */
  55. public function create(Request $request)
  56. {
  57. if($request->method() == 'POST') {
  58. return $this->_createSave();
  59. }
  60. $cat = AlbumCatModel::where('parent_id',0)->get();
  61. $data['parent_id'] = null;
  62. return view('admin.album.cat.edit',compact('data','cat'));
  63. }
  64. /**
  65. * 保存修改
  66. */
  67. private function _createSave(){
  68. $data = (array) request('data');
  69. $data['store_id'] = $this->getStoreId();
  70. if(!empty($data['pic_url']))
  71. $data['pic_url'] = $this->formatImgUrl($data['pic_url']);
  72. $id = $this->repository->create($data);
  73. if($id) {
  74. $url[] = array('url'=>U( 'Album/Cat/index'),'title'=>'返回列表');
  75. $url[] = array('url'=>U( 'Album/Cat/create'),'title'=>'继续添加');
  76. $this->showMessage('添加成功',$url);
  77. }else{
  78. $url[] = array('url'=>U( 'Album/Cat/index'),'title'=>'返回列表');
  79. return $this->showWarning('添加失败',$url);
  80. }
  81. }
  82. /**
  83. *
  84. * 修改
  85. *
  86. *
  87. */
  88. public function update(Request $request) {
  89. if($request->method() == 'POST') {
  90. return $this->_updateSave();
  91. }
  92. $data = $this->repository->find($request->get('id'));
  93. $cat = AlbumCatModel::where('parent_id',0)->get();
  94. return view('admin.album.cat.edit',compact('data','cat'));
  95. }
  96. /**
  97. * 保存修改
  98. */
  99. private function _updateSave() {
  100. $data = (array) request('data');
  101. if(!empty($data['pic_url']))
  102. $data['pic_url'] = $this->formatImgUrl($data['pic_url']);
  103. $ok = $this->repository->update(request('id'),$data);
  104. if($ok) {
  105. $url[] = array('url'=>U( 'Album/Cat/index'),'title'=>'返回列表');
  106. return $this->showMessage('操作成功',urldecode(request('_referer')));
  107. }else{
  108. $url[] = array('url'=>U( 'Album/Cat/index'),'title'=>'返回列表');
  109. return $this->showWarning('操作失败',$url);
  110. }
  111. }
  112. public function view(Request $request) {
  113. $data = $this->repository->find(request('id'));
  114. return view('admin.album.cat.view',compact('data'));
  115. }
  116. /**
  117. *
  118. * 状态改变
  119. *
  120. */
  121. public function status(Request $request) {
  122. $ok = $this->repository->updateStatus(request('id'),request('status'));
  123. if($ok) {
  124. return $this->showMessage('操作成功');
  125. }else{
  126. return $this->showWarning('操作失败');
  127. }
  128. }
  129. /**
  130. * 删除
  131. */
  132. public function destroy(Request $request) {
  133. //$bool = $this->repository->destroy($request->get('id'));
  134. $cat = AlbumCatModel::find($request->get('id'));
  135. $son_cat = AlbumCatModel::where('parent_id',$cat->id)->delete();
  136. if($son_cat){
  137. //$son_cat->delete();
  138. }
  139. $ok = $cat->delete();
  140. if($ok) {
  141. return $this->showMessage('操作成功');
  142. }else{
  143. return $this->showWarning("操作失败");
  144. }
  145. }
  146. }