CatController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. {
  23. if (!$this->repository) {
  24. $this->repository = $repository;
  25. }
  26. }
  27. /**
  28. * @param Request $request
  29. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  30. */
  31. function index(Request $request)
  32. {
  33. $search['keyword'] = $request->input('keyword');
  34. $query = $this->repository->pushCriteria(new CatWhere($search, 0, $this->getStoreId()));
  35. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  36. $query = $query->pushCriteria(new OrderBy($request['sort_field'], $request['sort_field_by']));
  37. } else {
  38. $query = $query->pushCriteria(new OrderBy('sort', 'DESC'));
  39. }
  40. $list = $query->paginate();
  41. foreach ($list as $key => $item) {
  42. //dump($item);
  43. $item->sonlist = AlbumCatModel::where([['parent_id',$item->id],['store_id',$this->getStoreId()]])->orderByDesc('sort')->get();
  44. }
  45. return view('admin.album.cat.index', compact('list'));
  46. }
  47. /**
  48. * @param Request $request
  49. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  50. */
  51. function check(Request $request)
  52. {
  53. $request = $request->all();
  54. $search['keyword'] = $request->input('keyword');
  55. $orderby = array();
  56. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  57. $orderby[$request['sort_field']] = $request['sort_field_by'];
  58. }
  59. $list = $this->repository->search($search, $orderby);
  60. return view('admin.album.cat.check', compact('list'));
  61. }
  62. /**
  63. * 添加
  64. *
  65. */
  66. public function create(Request $request)
  67. {
  68. if ($request->method() == 'POST') {
  69. return $this->_createSave();
  70. }
  71. $cat = AlbumCatModel::where([['parent_id', 0],['store_id',$this->getStoreId()]])->get();
  72. $data['parent_id'] = null;
  73. return view('admin.album.cat.edit', compact('data', 'cat'));
  74. }
  75. /**
  76. * 保存修改
  77. */
  78. private function _createSave(){
  79. $data = (array) request('data');
  80. $data['store_id'] = $this->getStoreId();
  81. if (!empty($data['pic_url'])) {
  82. $data['pic_url'] = $this->formatImgUrl($data['pic_url']);
  83. }
  84. $id = $this->repository->create($data);
  85. if ($id) {
  86. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  87. $url[] = array('url' => U('Album/Cat/create'), 'title' => '继续添加');
  88. $this->showMessage('添加成功', $url);
  89. } else {
  90. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  91. return $this->showWarning('添加失败', $url);
  92. }
  93. }
  94. /**
  95. *
  96. * 修改
  97. *
  98. *
  99. */
  100. public function update(Request $request)
  101. {
  102. if ($request->method() == 'POST') {
  103. return $this->_updateSave();
  104. }
  105. $data = $this->repository->find($request->get('id'));
  106. $cat = AlbumCatModel::where([['parent_id',0],['store_id',$this->getStoreId()]])->get();
  107. return view('admin.album.cat.edit', compact('data', 'cat'));
  108. }
  109. /**
  110. * 保存修改
  111. */
  112. private function _updateSave()
  113. {
  114. $data = (array) request('data');
  115. if (!empty($data['pic_url'])) {
  116. $data['pic_url'] = $this->formatImgUrl($data['pic_url']);
  117. }
  118. $ok = $this->repository->update(request('id'), $data);
  119. if ($ok) {
  120. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  121. return $this->showMessage('操作成功', urldecode(request('_referer')));
  122. } else {
  123. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  124. return $this->showWarning('操作失败', $url);
  125. }
  126. }
  127. public function view(Request $request)
  128. {
  129. $data = $this->repository->find(request('id'));
  130. return view('admin.album.cat.view', compact('data'));
  131. }
  132. /**
  133. *
  134. * 状态改变
  135. *
  136. */
  137. public function status(Request $request)
  138. {
  139. $ok = $this->repository->updateStatus(request('id'), request('status'));
  140. if ($ok) {
  141. return $this->showMessage('操作成功');
  142. } else {
  143. return $this->showWarning('操作失败');
  144. }
  145. }
  146. /**
  147. * 删除
  148. */
  149. public function destroy(Request $request)
  150. {
  151. $cat = AlbumCatModel::find($request->get('id'));
  152. $son_cat = AlbumCatModel::where('parent_id', $cat->id)->delete();
  153. if ($son_cat) {
  154. $son_cat->delete();
  155. }
  156. $ok = $cat->delete();
  157. if ($ok) {
  158. return $this->showMessage('操作成功');
  159. } else {
  160. return $this->showWarning("操作失败");
  161. }
  162. }
  163. }