ProductController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 App\Services\OSS;
  17. use Illuminate\Http\Request;
  18. use App\Repositories\Base\Criteria\OrderBy;
  19. use App\Repositories\Album\Criteria\MultiWhere;
  20. use App\Repositories\Album\ProductRepository;
  21. class ProductController extends Controller
  22. {
  23. private $repository;
  24. /**
  25. * ProductController constructor.
  26. * @param ProductRepository $repository
  27. */
  28. public function __construct(ProductRepository $repository)
  29. {
  30. if (!$this->repository) {
  31. $this->repository = $repository;
  32. }
  33. }
  34. function index(Request $request)
  35. {
  36. $search['keyword'] = $request->input('keyword');
  37. $query = $this->repository->pushCriteria(new ProductWhere($search, $this->getStoreId()));
  38. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  39. $query = $query->pushCriteria(new OrderBy($request['sort_field'], $request['sort_field_by']));
  40. } else {
  41. $query = $query->pushCriteria(new OrderBy('id', 'DESC'));
  42. }
  43. $list = $query->paginate();
  44. foreach ($list as $key => $item) {
  45. $cat = AlbumCatModel::where('id', $item->cat_id)->first();
  46. if ($cat) {
  47. $catParent = AlbumCatModel::where('id', $cat->parent_id)->first();
  48. }
  49. $item->cat_name = ($catParent->name ?? '暂无') . '>' . ($cat->name ?? '暂无');
  50. $upload_img = json_decode($item->upload_img);
  51. $attr = json_decode($item->attr);
  52. $detail = json_decode($item->detail);
  53. $list[$key]['install_img'] = $upload_img[0];
  54. $list[$key]['specifications_img'] = $attr[0];
  55. $list[$key]['detail'] = $detail[0];
  56. }
  57. //dd($list);
  58. return view('admin.album.product.index', compact('list'));
  59. }
  60. /**
  61. * @param Request $request
  62. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  63. */
  64. function check(Request $request)
  65. {
  66. $request = $request->all();
  67. $search['keyword'] = $request->input('keyword');
  68. $orderby = array();
  69. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  70. $orderby[$request['sort_field']] = $request['sort_field_by'];
  71. }
  72. $list = $this->repository->search($search, $orderby);
  73. return view('admin.album.product.check', compact('list'));
  74. }
  75. /**
  76. * @param Request $request
  77. * @return \Illuminate\Http\JsonResponse
  78. */
  79. public function getSecondCategory(Request $request)
  80. {
  81. $cat_id = $request->input('cat_id');
  82. if (!$cat_id) {
  83. return response()->json(['message' => '参数不合法', 'code' => 1]);
  84. }
  85. $cat = AlbumCatModel::where([
  86. ['parent_id',$cat_id],['store_id',$this->getStoreId()]
  87. ])->orderByDesc('sort')->get()->toArray();
  88. if (empty($cat)) {
  89. $cat[] = [
  90. 'name' => '请先添加二级分类!',
  91. 'id' => 0
  92. ];
  93. }
  94. return response()->json([
  95. 'data' => $cat,
  96. 'code' => 0
  97. ]);
  98. }
  99. /**
  100. * 添加
  101. *
  102. */
  103. public function create(Request $request)
  104. {
  105. if ($request->method() == 'POST') {
  106. return $this->_createSave();
  107. }
  108. $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id', 0]])->get();
  109. foreach ($cat as $key => $val) {
  110. $son = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id', $val['id']]])->get();
  111. $category[] = [
  112. 'id' => 0,
  113. 'name' => $val['name'],
  114. 'son' => $son
  115. ];
  116. }
  117. $data['cat_id'] = null;
  118. return view('admin.album.product.edit', compact('data', 'category', 'style'));
  119. }
  120. /**
  121. * 保存修改
  122. */
  123. private function _createSave()
  124. {
  125. $data = (array) request('data');
  126. // dd($data);
  127. if (isset($data['upload_video'])) {
  128. $data['upload_video'] = $this->uploadVideo($data['upload_video'], '');
  129. }
  130. $data['store_id'] = $this->getStoreId();
  131. if (!empty($data['specifications_img']['url'])) {
  132. foreach ($data['specifications_img']['url'] as $key => $val) {
  133. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  134. }
  135. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  136. }
  137. if (!empty($data['install_img']['url'])) {
  138. foreach ($data['install_img']['url'] as $key => $val) {
  139. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  140. }
  141. $data['install_img'] = json_encode($data['install_img']['url']);
  142. }
  143. if (!empty($data['cover_pic'])) {
  144. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  145. }
  146. if (!empty($data['thumb'])) {
  147. $data['thumb'] = $this->formatImgUrl($data['thumb']);
  148. }
  149. if (!empty($data['detail']['url'])) {
  150. foreach ($data['detail']['url'] as $key => $val) {
  151. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  152. }
  153. $data['detail'] = json_encode($data['detail']['url']);
  154. }
  155. if (!empty($data['detail_pic'])) {
  156. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  157. }
  158. $id = $this->repository->create($data);
  159. if ($id) {
  160. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  161. $url[] = array('url' => U('Album/Product/create'), 'title' => '继续添加');
  162. $this->showMessage('添加成功', $url);
  163. } else {
  164. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  165. return $this->showWarning('添加失败', $url);
  166. }
  167. }
  168. public function uploadVideo($video,$id)
  169. {
  170. // dd($video);
  171. // 判断图片有效性
  172. if (!$video) {
  173. return back()->withErrors('上传视频无效..');
  174. }
  175. if ($id) {
  176. $check = AlbumProductModel::where([['id',$id]])->first();
  177. if ($check) {
  178. $res = json_decode($check->upload_video, true);
  179. OSS::publicDeleteObject(config('alioss.BucketName'), $res['oss_key']);
  180. }
  181. }
  182. // 获取图片在临时文件中的地址
  183. $videoPath = $video->getRealPath();
  184. // / dd($video);
  185. // 制作文件名
  186. $ex = $video->getClientOriginalExtension();
  187. $key = time() . rand(10000, 99999999) . '.' . $ex;
  188. //阿里 OSS 文件上传
  189. $result = OSS::publicUpload(config('alioss.BucketName'), $key, $videoPath);
  190. if ($result) {
  191. $data['oss_key'] = $key;
  192. $data['url'] = config('alioss.FileUrl') . $key;
  193. $data = json_encode($data);
  194. return $data;
  195. } else {
  196. return false;
  197. }
  198. }
  199. /**
  200. *
  201. * 修改
  202. *
  203. *
  204. */
  205. public function update(Request $request)
  206. {
  207. if ($request->method() == 'POST') {
  208. return $this->_updateSave();
  209. }
  210. $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id', 0]])->get();
  211. foreach ($cat as $key => $val) {
  212. $son = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id', $val['id']]])->get();
  213. $category[] = [
  214. 'id' => 0,
  215. 'name' => $val['name'],
  216. 'son' => $son
  217. ];
  218. }
  219. $data = $this->repository->find($request->get('id'));
  220. $data['install_img'] = json_decode($data['install_img']);
  221. $data['specifications_img'] = json_decode($data['specifications_img']);
  222. $data['detail'] = json_decode($data['detail']);
  223. $data['upload_video'] = json_decode($data['upload_video'], true);
  224. $data['upload_video'] = $data['upload_video']['url'];
  225. return view('admin.album.product.edit', compact('data', 'category', 'parent'));
  226. }
  227. /**
  228. * 保存修改
  229. */
  230. private function _updateSave()
  231. {
  232. $data = (array) request('data');
  233. if (isset($data['upload_video'])) {
  234. $data['upload_video'] = $this->uploadVideo($data['upload_video'], request('id'));
  235. }
  236. if (!empty($data['specifications_img']['url'])) {
  237. foreach ($data['specifications_img']['url'] as $key => $val) {
  238. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  239. }
  240. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  241. }
  242. if (!empty($data['install_img']['url'])) {
  243. foreach ($data['install_img']['url'] as $key => $val) {
  244. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  245. }
  246. $data['install_img'] = json_encode($data['install_img']['url']);
  247. }
  248. if (!empty($data['cover_pic'])) {
  249. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  250. }
  251. if (!empty($data['thumb'])) {
  252. $data['thumb'] = $this->formatImgUrl($data['thumb']);
  253. }
  254. if (!empty($data['detail']['url'])) {
  255. foreach ($data['detail']['url'] as $key => $val) {
  256. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  257. }
  258. $data['detail'] = json_encode($data['detail']['url']);
  259. }
  260. if (!empty($data['detail_pic'])) {
  261. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  262. }
  263. $price = AlbumProductPriceModel::where('product_id', request('id'))->get();
  264. foreach ($price as $p) {
  265. $p->cat_id = $data['cat_id'];
  266. $p->name = $data['name'];
  267. }
  268. $ok = $this->repository->update(request('id'), $data);
  269. if ($ok) {
  270. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  271. return $this->showMessage('操作成功', urldecode(request('_referer')));
  272. } else {
  273. $url[] = array('url' => U('Album/Product/index'), 'title' => '返回列表');
  274. return $this->showWarning('操作失败', $url);
  275. }
  276. }
  277. public function view(Request $request)
  278. {
  279. $data = $this->repository->find(request('id'));
  280. return view('admin.album.product.view', compact('data'));
  281. }
  282. /**
  283. *
  284. * 状态改变
  285. *
  286. */
  287. public function status(Request $request)
  288. {
  289. $ok = $this->repository->updateStatus(request('id'), request('status'));
  290. if ($ok) {
  291. return $this->showMessage('操作成功');
  292. } else {
  293. return $this->showWarning('操作失败');
  294. }
  295. }
  296. /**
  297. * 删除
  298. */
  299. public function destroy(Request $request)
  300. {
  301. //$bool = $this->repository->destroy($request->get('id'));
  302. $cat = AlbumProductModel::find($request->get('id'));
  303. $ok = $cat->delete();
  304. if ($ok) {
  305. return $this->showMessage('操作成功');
  306. } else {
  307. return $this->showWarning("操作失败");
  308. }
  309. }
  310. }