ProductController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. foreach ($cat as $key => $val) {
  109. $son = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id', $val['id']]])->get();
  110. $category[] = [
  111. 'id' => 0,
  112. 'name' => $val['name'],
  113. 'son' => $son
  114. ];
  115. }
  116. $data['cat_id'] = null;
  117. return view('admin.album.product.edit', compact('data', 'category', 'style'));
  118. }
  119. /**
  120. * 保存修改
  121. */
  122. private function _createSave()
  123. {
  124. $data = (array) request('data');
  125. $data['store_id'] = $this->getStoreId();
  126. if (!empty($data['specifications_img']['url'])) {
  127. foreach ($data['specifications_img']['url'] as $key => $val) {
  128. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  129. }
  130. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  131. }
  132. if (!empty($data['install_img']['url'])) {
  133. foreach ($data['install_img']['url'] as $key => $val) {
  134. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  135. }
  136. $data['install_img'] = json_encode($data['install_img']['url']);
  137. }
  138. if (!empty($data['cover_pic'])) {
  139. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  140. }
  141. if (!empty($data['thumb'])) {
  142. $data['thumb'] = $this->formatImgUrl($data['thumb']);
  143. }
  144. if (!empty($data['detail']['url'])) {
  145. foreach ($data['detail']['url'] as $key => $val) {
  146. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  147. }
  148. $data['detail'] = json_encode($data['detail']['url']);
  149. }
  150. if (!empty($data['detail_pic'])) {
  151. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  152. }
  153. $id = $this->repository->create($data);
  154. if ($id) {
  155. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  156. $url[] = array('url' => U('Album/Product/create'), 'title' => '继续添加');
  157. $this->showMessage('添加成功', $url);
  158. } else {
  159. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  160. return $this->showWarning('添加失败', $url);
  161. }
  162. }
  163. /**
  164. *
  165. * 修改
  166. *
  167. *
  168. */
  169. public function update(Request $request)
  170. {
  171. if ($request->method() == 'POST') {
  172. return $this->_updateSave();
  173. }
  174. $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id', 0]])->get();
  175. foreach ($cat as $key => $val) {
  176. $son = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id', $val['id']]])->get();
  177. $category[] = [
  178. 'id' => 0,
  179. 'name' => $val['name'],
  180. 'son' => $son
  181. ];
  182. }
  183. $data = $this->repository->find($request->get('id'));
  184. $data['install_img'] = json_decode($data['install_img']);
  185. $data['specifications_img'] = json_decode($data['specifications_img']);
  186. $data['detail'] = json_decode($data['detail']);
  187. return view('admin.album.product.edit', compact('data', 'category', 'parent'));
  188. }
  189. /**
  190. * 保存修改
  191. */
  192. private function _updateSave()
  193. {
  194. $data = (array) request('data');
  195. if (!empty($data['specifications_img']['url'])) {
  196. foreach ($data['specifications_img']['url'] as $key => $val) {
  197. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  198. }
  199. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  200. }
  201. if (!empty($data['install_img']['url'])) {
  202. foreach ($data['install_img']['url'] as $key => $val) {
  203. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  204. }
  205. $data['install_img'] = json_encode($data['install_img']['url']);
  206. }
  207. if (!empty($data['cover_pic'])) {
  208. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  209. }
  210. if (!empty($data['thumb'])) {
  211. $data['thumb'] = $this->formatImgUrl($data['thumb']);
  212. }
  213. if (!empty($data['detail']['url'])) {
  214. foreach ($data['detail']['url'] as $key => $val) {
  215. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  216. }
  217. $data['detail'] = json_encode($data['detail']['url']);
  218. }
  219. if (!empty($data['detail_pic'])) {
  220. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  221. }
  222. $price = AlbumProductPriceModel::where('product_id', request('id'))->get();
  223. foreach ($price as $p) {
  224. $p->cat_id = $data['cat_id'];
  225. $p->name = $data['name'];
  226. }
  227. $ok = $this->repository->update(request('id'), $data);
  228. if ($ok) {
  229. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  230. return $this->showMessage('操作成功', urldecode(request('_referer')));
  231. } else {
  232. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  233. return $this->showWarning('操作失败', $url);
  234. }
  235. }
  236. public function view(Request $request)
  237. {
  238. $data = $this->repository->find(request('id'));
  239. return view('admin.album.product.view', compact('data'));
  240. }
  241. /**
  242. *
  243. * 状态改变
  244. *
  245. */
  246. public function status(Request $request)
  247. {
  248. $ok = $this->repository->updateStatus(request('id'), request('status'));
  249. if ($ok) {
  250. return $this->showMessage('操作成功');
  251. } else {
  252. return $this->showWarning('操作失败');
  253. }
  254. }
  255. /**
  256. * 删除
  257. */
  258. public function destroy(Request $request)
  259. {
  260. //$bool = $this->repository->destroy($request->get('id'));
  261. $cat = AlbumProductModel::find($request->get('id'));
  262. $ok = $cat->delete();
  263. if ($ok) {
  264. return $this->showMessage('操作成功');
  265. } else {
  266. return $this->showWarning("操作失败");
  267. }
  268. }
  269. }