ProductController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * 产品列表
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-05-14 13:29:14
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Album;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\AlbumCatModel;
  12. use App\Models\AlbumProductModel;
  13. use App\Models\AlbumProductPriceModel;
  14. use App\Models\AlbumProductStyleModel;
  15. use App\Repositories\Album\Criteria\ProductWhere;
  16. use Illuminate\Http\Request;
  17. use App\Repositories\Base\Criteria\OrderBy;
  18. use App\Repositories\Album\Criteria\MultiWhere;
  19. use App\Repositories\Album\ProductRepository;
  20. class ProductController extends Controller
  21. {
  22. private $repository;
  23. /**
  24. * ProductController constructor.
  25. * @param ProductRepository $repository
  26. */
  27. public function __construct(ProductRepository $repository)
  28. {
  29. if (!$this->repository) {
  30. $this->repository = $repository;
  31. }
  32. }
  33. function index(Request $request)
  34. {
  35. $search['keyword'] = $request->input('keyword');
  36. $query = $this->repository->pushCriteria(new ProductWhere($search, $this->getStoreId()));
  37. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  38. $query = $query->pushCriteria(new OrderBy($request['sort_field'], $request['sort_field_by']));
  39. } else {
  40. $query = $query->pushCriteria(new OrderBy('id', 'DESC'));
  41. }
  42. $list = $query->paginate();
  43. foreach ($list as $key => $item) {
  44. $cat = AlbumCatModel::where('id', $item->cat_id)->first();
  45. if ($cat) {
  46. $catParent = AlbumCatModel::where('id', $cat->parent_id)->first();
  47. }
  48. $item->cat_name = ($catParent->name ?? '暂无') . '>' . ($cat->name ?? '暂无');
  49. $upload_img = json_decode($item->upload_img);
  50. $attr = json_decode($item->attr);
  51. $detail = json_decode($item->detail);
  52. $list[$key]['install_img'] = $upload_img[0];
  53. $list[$key]['specifications_img'] = $attr[0];
  54. $list[$key]['detail'] = $detail[0];
  55. }
  56. //dd($list);
  57. return view('admin.album.product.index', compact('list'));
  58. }
  59. /**
  60. * @param Request $request
  61. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  62. */
  63. function check(Request $request)
  64. {
  65. $request = $request->all();
  66. $search['keyword'] = $request->input('keyword');
  67. $orderby = array();
  68. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  69. $orderby[$request['sort_field']] = $request['sort_field_by'];
  70. }
  71. $list = $this->repository->search($search, $orderby);
  72. return view('admin.album.product.check', compact('list'));
  73. }
  74. /**
  75. * @param Request $request
  76. * @return \Illuminate\Http\JsonResponse
  77. */
  78. public function getSecondCategory(Request $request)
  79. {
  80. $cat_id = $request->input('cat_id');
  81. if (!$cat_id) {
  82. return response()->json(['message' => '参数不合法', 'code' => 1]);
  83. }
  84. $cat = AlbumCatModel::where([
  85. ['parent_id',$cat_id],['store_id',$this->getStoreId()]
  86. ])->orderByDesc('sort')->get()->toArray();
  87. if (empty($cat)) {
  88. $cat[] = [
  89. 'name' => '请先添加二级分类!',
  90. 'id' => 0
  91. ];
  92. }
  93. return response()->json([
  94. 'data' => $cat,
  95. 'code' => 0
  96. ]);
  97. }
  98. /**
  99. * 添加
  100. *
  101. */
  102. public function create(Request $request)
  103. {
  104. if ($request->method() == 'POST') {
  105. return $this->_createSave();
  106. }
  107. $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id','!=',0]])->get();
  108. $data['cat_id'] = null;
  109. return view('admin.album.product.edit', compact('data', 'cat', 'style'));
  110. }
  111. /**
  112. * 保存修改
  113. */
  114. private function _createSave()
  115. {
  116. $data = (array) request('data');
  117. $data['store_id'] = $this->getStoreId();
  118. if (!empty($data['specifications_img']['url'])) {
  119. foreach ($data['specifications_img']['url'] as $key => $val) {
  120. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  121. }
  122. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  123. }
  124. if (!empty($data['install_img']['url'])) {
  125. foreach ($data['install_img']['url'] as $key => $val) {
  126. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  127. }
  128. $data['install_img'] = json_encode($data['install_img']['url']);
  129. }
  130. if (!empty($data['cover_pic'])) {
  131. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  132. }
  133. if (!empty($data['thumb'])) {
  134. $data['thumb'] = $this->formatImgUrl($data['thumb']);
  135. }
  136. if (!empty($data['detail']['url'])) {
  137. foreach ($data['detail']['url'] as $key => $val) {
  138. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  139. }
  140. $data['detail'] = json_encode($data['detail']['url']);
  141. }
  142. if (!empty($data['detail_pic'])) {
  143. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  144. }
  145. $id = $this->repository->create($data);
  146. if ($id) {
  147. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  148. $url[] = array('url' => U('Album/Product/create'), 'title' => '继续添加');
  149. $this->showMessage('添加成功', $url);
  150. } else {
  151. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  152. return $this->showWarning('添加失败', $url);
  153. }
  154. }
  155. /**
  156. *
  157. * 修改
  158. *
  159. *
  160. */
  161. public function update(Request $request)
  162. {
  163. if ($request->method() == 'POST') {
  164. return $this->_updateSave();
  165. }
  166. $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id','!=',0]])->get();
  167. $data = $this->repository->find($request->get('id'));
  168. $data['install_img'] = json_decode($data['install_img']);
  169. $data['specifications_img'] = json_decode($data['specifications_img']);
  170. $data['detail'] = json_decode($data['detail']);
  171. return view('admin.album.product.edit', compact('data', 'cat', 'parent'));
  172. }
  173. /**
  174. * 保存修改
  175. */
  176. private function _updateSave()
  177. {
  178. $data = (array) request('data');
  179. if (!empty($data['specifications_img']['url'])) {
  180. foreach ($data['specifications_img']['url'] as $key => $val) {
  181. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  182. }
  183. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  184. }
  185. if (!empty($data['install_img']['url'])) {
  186. foreach ($data['install_img']['url'] as $key => $val) {
  187. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  188. }
  189. $data['install_img'] = json_encode($data['install_img']['url']);
  190. }
  191. if (!empty($data['cover_pic'])) {
  192. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  193. }
  194. if (!empty($data['thumb'])) {
  195. $data['thumb'] = $this->formatImgUrl($data['thumb']);
  196. }
  197. if (!empty($data['detail']['url'])) {
  198. foreach ($data['detail']['url'] as $key => $val) {
  199. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  200. }
  201. $data['detail'] = json_encode($data['detail']['url']);
  202. }
  203. if (!empty($data['detail_pic'])) {
  204. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  205. }
  206. $price = AlbumProductPriceModel::where('product_id', request('id'))->get();
  207. foreach ($price as $p) {
  208. $p->cat_id = $data['cat_id'];
  209. $p->name = $data['name'];
  210. }
  211. $ok = $this->repository->update(request('id'), $data);
  212. if ($ok) {
  213. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  214. return $this->showMessage('操作成功', urldecode(request('_referer')));
  215. } else {
  216. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  217. return $this->showWarning('操作失败', $url);
  218. }
  219. }
  220. public function view(Request $request)
  221. {
  222. $data = $this->repository->find(request('id'));
  223. return view('admin.album.product.view', compact('data'));
  224. }
  225. /**
  226. *
  227. * 状态改变
  228. *
  229. */
  230. public function status(Request $request)
  231. {
  232. $ok = $this->repository->updateStatus(request('id'), request('status'));
  233. if ($ok) {
  234. return $this->showMessage('操作成功');
  235. } else {
  236. return $this->showWarning('操作失败');
  237. }
  238. }
  239. /**
  240. * 删除
  241. */
  242. public function destroy(Request $request)
  243. {
  244. //$bool = $this->repository->destroy($request->get('id'));
  245. $cat = AlbumProductModel::find($request->get('id'));
  246. $ok = $cat->delete();
  247. if ($ok) {
  248. return $this->showMessage('操作成功');
  249. } else {
  250. return $this->showWarning("操作失败");
  251. }
  252. }
  253. }