| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- /**
- * 产品列表
- * @author system
- * @version 1.0
- * @date 2018-05-14 13:29:14
- *
- */
- namespace App\Http\Controllers\Admin\Album;
- use App\Http\Controllers\Admin\Controller;
- use App\Models\AlbumCatModel;
- use App\Models\AlbumProductModel;
- use App\Models\AlbumProductStyleModel;
- use App\Repositories\Album\Criteria\ProductWhere;
- use Illuminate\Http\Request;
- use App\Repositories\Base\Criteria\OrderBy;
- use App\Repositories\Album\Criteria\MultiWhere;
- use App\Repositories\Album\ProductRepository;
- class ProductController extends Controller
- {
- private $repository;
- public function __construct(ProductRepository $repository) {
- if(!$this->repository) $this->repository = $repository;
- }
- function index(Request $request) {
- $search['keyword'] = $request->input('keyword');
- $query = $this->repository->pushCriteria(new ProductWhere($search,$this->getStoreId()));
- if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
- }else{
- $query = $query->pushCriteria(new OrderBy('id','DESC'));
- }
- $list = $query->paginate();
- foreach ($list as $key => $item) {
- $cat = AlbumCatModel::where('id',$item->cat_id)->first();
- $style = AlbumProductStyleModel::where('id',$item->style)->first();
- $item->cat_name = $cat['name'];
- $item->style_name = $style['name'];
- $upload_img = json_decode($item->upload_img);
- $attr = json_decode($item->attr);
- $detail = json_decode($item->detail);
- $list[$key]['install_img'] = $upload_img[0];
- $list[$key]['specifications_img'] = $attr[0];
- $list[$key]['detail'] = $detail[0];
- }
- //dd($list);
- return view('admin.album.product.index',compact('list'));
- }
- function check(Request $request) {
- $request = $request->all();
- $search['keyword'] = $request->input('keyword');
- $orderby = array();
- if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
- $orderby[$request['sort_field']] = $request['sort_field_by'];
- }
- $list = $this->repository->search($search,$orderby);
- return view('admin.album.product.check',compact('list'));
- }
- /**
- * 添加
- *
- */
- public function create(Request $request)
- {
- if($request->method() == 'POST') {
- return $this->_createSave();
- }
- $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id',0]])->get();
- $style = AlbumProductStyleModel::Where('store_id',$this->getStoreId())->get();
- $data['cat_id']=null;
- return view('admin.album.product.edit',compact('data','cat','style'));
- }
- /**
- * 保存修改
- */
- private function _createSave(){
- $data = (array) request('data');
- $data['store_id'] = $this->getStoreId();
- if(!empty($data['specifications_img']['url'])) {
- foreach ($data['specifications_img']['url'] as $key=>$val) {
- $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
- }
- $data['specifications_img'] = json_encode($data['specifications_img']['url']);
- }
- if(!empty($data['install_img']['url'])){
- foreach ($data['install_img']['url'] as $key=>$val) {
- $data['install_img']['url'][$key] = $this->formatImgUrl($val);
- }
- $data['install_img'] = json_encode($data['install_img']['url']);
- }
- if(!empty($data['cover_pic']))
- $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
- if(!empty($data['detail']['url'])) {
- foreach ($data['detail']['url'] as $key=>$val) {
- $data['detail']['url'][$key] = $this->formatImgUrl($val);
- }
- $data['detail'] = json_encode($data['detail']['url']);
- }
- if(!empty($data['detail_pic']))
- $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
- $id = $this->repository->create($data);
- if($id) {
- $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
- $url[] = array('url'=>U( 'Album/Product/create'),'title'=>'继续添加');
- $this->showMessage('添加成功',$url);
- }else{
- $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
- return $this->showWarning('添加失败',$url);
- }
- }
-
- /**
- *
- * 修改
- *
- *
- */
- public function update(Request $request) {
- if($request->method() == 'POST') {
- return $this->_updateSave();
- }
- $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id',0]])->get();
- $style = AlbumProductStyleModel::Where('store_id',$this->getStoreId())->get();
- $data = $this->repository->find($request->get('id'));
- $data['install_img'] = json_decode($data['install_img']);
- $data['specifications_img'] = json_decode($data['specifications_img']);
- $data['detail'] = json_decode($data['detail']);
- return view('admin.album.product.edit',compact('data','cat','style'));
- }
- /**
- * 保存修改
- */
- private function _updateSave() {
- $data = (array) request('data');
- if(!empty($data['specifications_img']['url'])) {
- foreach ($data['specifications_img']['url'] as $key=>$val) {
- $data['specifications_img']['url'][$key] = $this->formatImgUrl($val);
- }
- $data['specifications_img'] = json_encode($data['specifications_img']['url']);
- }
- if(!empty($data['install_img']['url'])){
- foreach ($data['install_img']['url'] as $key=>$val) {
- $data['install_img']['url'][$key] = $this->formatImgUrl($val);
- }
- $data['install_img'] = json_encode($data['install_img']['url']);
- }
- if(!empty($data['cover_pic']))
- $data['cover_pic'] = $this->formatImgUrl($data['cover_pic']);
- if(!empty($data['detail']['url'])) {
- foreach ($data['detail']['url'] as $key=>$val) {
- $data['detail']['url'][$key] = $this->formatImgUrl($val);
- }
- $data['detail'] = json_encode($data['detail']['url']);
- }
- if(!empty($data['detail_pic']))
- $data['detail_pic'] = $this->formatImgUrl($data['detail_pic']);
- // dd($data);
- $ok = $this->repository->update(request('id'),$data);
- if($ok) {
- $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
- return $this->showMessage('操作成功',urldecode(request('_referer')));
- }else{
- $url[] = array('url'=>U( 'Album/Product/index'),'title'=>'返回列表');
- return $this->showWarning('操作失败',$url);
- }
- }
- public function view(Request $request) {
- $data = $this->repository->find(request('id'));
- return view('admin.album.product.view',compact('data'));
- }
- /**
- *
- * 状态改变
- *
- */
- public function status(Request $request) {
- $ok = $this->repository->updateStatus(request('id'),request('status'));
- if($ok) {
- return $this->showMessage('操作成功');
- }else{
- return $this->showWarning('操作失败');
- }
- }
-
- /**
- * 删除
- */
- public function destroy(Request $request) {
- //$bool = $this->repository->destroy($request->get('id'));
- $cat = AlbumProductModel::find($request->get('id'));
- $ok = $cat->delete();
- if($ok) {
- return $this->showMessage('操作成功');
- }else{
- return $this->showWarning("操作失败");
- }
- }
- }
|