ProductController.php 11 KB

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