CatController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. public function createFirst(Request $request)
  76. {
  77. if ($request->method() == 'POST') {
  78. return $this->_createFirst();
  79. }
  80. return view('admin.album.cat.edit-first');
  81. }
  82. /**
  83. * 保存修改
  84. */
  85. private function _createFirst()
  86. {
  87. $data = (array) request('data');
  88. $ids = request('id');
  89. $data['store_id'] = $this->getStoreId();
  90. $data['parent_id'] = 0;
  91. $id = $this->repository->create($data);
  92. if ($ids == 0) {
  93. $url = U('Album/Cat/create');
  94. } else {
  95. $url = U('Album/Cat/update', ['id' => $ids]);
  96. }
  97. if ($id) {
  98. $urls[] = array('url' => $url, 'title' => '返回');
  99. $this->showMessage('添加成功', $urls);
  100. } else {
  101. $urls[] = array('url' => $url, 'title' => '返回列表');
  102. return $this->showWarning('添加失败', $urls);
  103. }
  104. }
  105. /**
  106. * 保存修改
  107. */
  108. private function _createSave(){
  109. $data = (array) request('data');
  110. $data['store_id'] = $this->getStoreId();
  111. if (!empty($data['pic_url'])) {
  112. $data['pic_url'] = $this->formatImgUrl($data['pic_url']);
  113. }
  114. $id = $this->repository->create($data);
  115. if ($id) {
  116. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  117. $url[] = array('url' => U('Album/Cat/create'), 'title' => '继续添加');
  118. $this->showMessage('添加成功', $url);
  119. } else {
  120. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  121. return $this->showWarning('添加失败', $url);
  122. }
  123. }
  124. /**
  125. *
  126. * 修改
  127. *
  128. *
  129. */
  130. public function update(Request $request)
  131. {
  132. if ($request->method() == 'POST') {
  133. return $this->_updateSave();
  134. }
  135. $data = $this->repository->find($request->get('id'));
  136. $cat = AlbumCatModel::where([['parent_id',0],['store_id',$this->getStoreId()]])->get();
  137. return view('admin.album.cat.edit', compact('data', 'cat'));
  138. }
  139. public function updateFirst(Request $request)
  140. {
  141. if ($request->method() == 'POST') {
  142. return $this->_updateSave();
  143. }
  144. $data = $this->repository->find($request->get('id'));
  145. return view('admin.album.cat.edit-first', compact('data'));
  146. }
  147. /**
  148. * 保存修改
  149. */
  150. private function _updateSave()
  151. {
  152. $data = (array) request('data');
  153. if (!empty($data['pic_url'])) {
  154. $data['pic_url'] = $this->formatImgUrl($data['pic_url']);
  155. }
  156. $ok = $this->repository->update(request('id'), $data);
  157. if ($ok) {
  158. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  159. return $this->showMessage('操作成功', urldecode(request('_referer')));
  160. } else {
  161. $url[] = array('url' => U('Album/Cat/index'), 'title' => '返回列表');
  162. return $this->showWarning('操作失败', $url);
  163. }
  164. }
  165. public function view(Request $request)
  166. {
  167. $data = $this->repository->find(request('id'));
  168. return view('admin.album.cat.view', compact('data'));
  169. }
  170. /**
  171. *
  172. * 状态改变
  173. *
  174. */
  175. public function status(Request $request)
  176. {
  177. $ok = $this->repository->updateStatus(request('id'), request('status'));
  178. if ($ok) {
  179. return $this->showMessage('操作成功');
  180. } else {
  181. return $this->showWarning('操作失败');
  182. }
  183. }
  184. /**
  185. * 删除
  186. */
  187. public function destroy(Request $request)
  188. {
  189. $cat = AlbumCatModel::find($request->get('id'));
  190. $son_cat = AlbumCatModel::where('parent_id', $cat->id)->delete();
  191. if ($son_cat) {
  192. $son_cat->delete();
  193. }
  194. $ok = $cat->delete();
  195. if ($ok) {
  196. return $this->showMessage('操作成功');
  197. } else {
  198. return $this->showWarning("操作失败");
  199. }
  200. }
  201. }