ProductController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. function check(Request $request) {
  50. $request = $request->all();
  51. $search['keyword'] = $request->input('keyword');
  52. $orderby = array();
  53. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  54. $orderby[$request['sort_field']] = $request['sort_field_by'];
  55. }
  56. $list = $this->repository->search($search,$orderby);
  57. return view('admin.album.product.check',compact('list'));
  58. }
  59. /**
  60. * 添加
  61. *
  62. */
  63. public function create(Request $request)
  64. {
  65. if($request->method() == 'POST') {
  66. return $this->_createSave();
  67. }
  68. $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id',0]])->get();
  69. $style = AlbumProductStyleModel::Where('store_id',$this->getStoreId())->get();
  70. $data['cat_id']=null;
  71. return view('admin.album.product.edit',compact('data','cat','style'));
  72. }
  73. /**
  74. * 保存修改
  75. */
  76. private function _createSave(){
  77. $data = (array) request('data');
  78. $data['store_id'] = $this->getStoreId();
  79. if(!empty($data['specifications_img']['url'])) {
  80. foreach ($data['specifications_img']['url'] as $key=>$val) {
  81. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  82. }
  83. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  84. }
  85. if(!empty($data['install_img']['url'])){
  86. foreach ($data['install_img']['url'] as $key=>$val) {
  87. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  88. }
  89. $data['install_img'] = json_encode($data['install_img']['url']);
  90. }
  91. if(!empty($data['cover_pic']))
  92. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  93. if(!empty($data['detail']['url'])) {
  94. foreach ($data['detail']['url'] as $key=>$val) {
  95. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  96. }
  97. $data['detail'] = json_encode($data['detail']['url']);
  98. }
  99. if(!empty($data['detail_pic']))
  100. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  101. $id = $this->repository->create($data);
  102. if($id) {
  103. $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
  104. $url[] = array('url'=>U( 'Album/Product/create'),'title'=>'继续添加');
  105. $this->showMessage('添加成功',$url);
  106. }else{
  107. $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
  108. return $this->showWarning('添加失败',$url);
  109. }
  110. }
  111. /**
  112. *
  113. * 修改
  114. *
  115. *
  116. */
  117. public function update(Request $request) {
  118. if($request->method() == 'POST') {
  119. return $this->_updateSave();
  120. }
  121. $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id',0]])->get();
  122. $style = AlbumProductStyleModel::Where('store_id',$this->getStoreId())->get();
  123. $data = $this->repository->find($request->get('id'));
  124. $data['install_img'] = json_decode($data['install_img']);
  125. $data['specifications_img'] = json_decode($data['specifications_img']);
  126. $data['detail'] = json_decode($data['detail']);
  127. return view('admin.album.product.edit',compact('data','cat','style'));
  128. }
  129. /**
  130. * 保存修改
  131. */
  132. private function _updateSave() {
  133. $data = (array) request('data');
  134. if(!empty($data['specifications_img']['url'])) {
  135. foreach ($data['specifications_img']['url'] as $key=>$val) {
  136. $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
  137. }
  138. $data['specifications_img'] = json_encode($data['specifications_img']['url']);
  139. }
  140. if(!empty($data['install_img']['url'])){
  141. foreach ($data['install_img']['url'] as $key=>$val) {
  142. $data['install_img']['url'][$key] = $this->formatImgUrl($val);
  143. }
  144. $data['install_img'] = json_encode($data['install_img']['url']);
  145. }
  146. if(!empty($data['cover_pic']))
  147. $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
  148. if(!empty($data['detail']['url'])) {
  149. foreach ($data['detail']['url'] as $key=>$val) {
  150. $data['detail']['url'][$key] = $this->formatImgUrl($val);
  151. }
  152. $data['detail'] = json_encode($data['detail']['url']);
  153. }
  154. if(!empty($data['detail_pic']))
  155. $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
  156. // dd($data);
  157. $ok = $this->repository->update(request('id'),$data);
  158. if($ok) {
  159. $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
  160. return $this->showMessage('操作成功',urldecode(request('_referer')));
  161. }else{
  162. $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
  163. return $this->showWarning('操作失败',$url);
  164. }
  165. }
  166. public function view(Request $request) {
  167. $data = $this->repository->find(request('id'));
  168. return view('admin.album.product.view',compact('data'));
  169. }
  170. /**
  171. *
  172. * 状态改变
  173. *
  174. */
  175. public function status(Request $request) {
  176. $ok = $this->repository->updateStatus(request('id'),request('status'));
  177. if($ok) {
  178. return $this->showMessage('操作成功');
  179. }else{
  180. return $this->showWarning('操作失败');
  181. }
  182. }
  183. /**
  184. * 删除
  185. */
  186. public function destroy(Request $request) {
  187. //$bool = $this->repository->destroy($request->get('id'));
  188. $cat = AlbumProductModel::find($request->get('id'));
  189. $ok = $cat->delete();
  190. if($ok) {
  191. return $this->showMessage('操作成功');
  192. }else{
  193. return $this->showWarning("操作失败");
  194. }
  195. }
  196. }