ProductController.php 8.8 KB

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