// +---------------------------------------------------------------------- namespace app\admin\controller; use think\Db; use think\Image; class Product extends Base { protected $table_name='product'; protected $controller_name='product'; protected $has_img=true; protected $maps=[]; protected $has_imgs=true; protected $has_source=true; public function _initialize() { parent::_initialize(); $this->assign('table_name',$this->table_name); $this->assign('controller',$this->controller_name); $this->assign('has_img',$this->has_img); $this->assign('has_imgs',$this->has_imgs); $this->assign('has_source',$this->has_source); $maps=[ '58'=>['name'=>'书画篆刻'], '59'=>['name'=>'工艺作品'], '60'=>['name'=>'造型艺术'], '61'=>['name'=>'玉翠珠宝'], '62'=>['name'=>'文玩杂项'], '63'=>['name'=>'其他'], ]; $types=Db::name('type')->where(['type_type'=>58])->select(); $maps['58']['childs']=$types; $types=Db::name('type')->where(['type_type'=>59])->select(); $maps['59']['childs']=$types; $types=Db::name('type')->where(['type_type'=>60])->select(); $maps['60']['childs']=$types; $types=Db::name('type')->where(['type_type'=>61])->select(); $maps['61']['childs']=$types; $types=Db::name('type')->where(['type_type'=>62])->select(); $maps['62']['childs']=$types; $types=Db::name('type')->where(['type_type'=>63])->select(); $maps['63']['childs']=$types; $this->maps=$maps; } public function show_list(){ $this->assign('table_name','show'); $page=input('page',1,'intval'); $postPage=input('post.page','','intval'); if($postPage)$page=$postPage; $this->assign('page',$page); $where=[]; $list=Db::name('proquarter')->where($where)->order(['sort'=>'asc','create_time'=>'desc'])->paginate(config('paginate.list_rows'),false,['query'=>get_query(),'page'=>$page]); $show = $list->render(); $this->assign('pageshow',$show); $this->assign('list',$list); if(request()->isAjax()){ return $this->fetch('product/ajax_show_list'); }else{ return $this->fetch('product/show_list'); } } /** * 排序 */ public function show_order() { $page=input('page','','intval'); $this->assign('page',$page); $jumpParam=['page'=>$page]; $jump=url('admin/'.$this->controller_name.'/show_list',$jumpParam); if (!request()->isAjax()){ $this->error('提交方式不正确',$jump); }else{ foreach (input('post.') as $n_id => $sort){ Db::name('proquarter')->update(['id'=>$n_id,'sort'=>$sort]); } $this->success('排序更新成功',$jump); } } /** * 删除(全选) */ public function show_del($id='') { $page=input('page','','intval'); $this->assign('page',$page); $p = input('p'); $ids = input('id/a'); $jumpParam=['p' => $p,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/show_list',$jumpParam); if(empty($ids)){ $this -> error("请选择待删除数据",$jump); } $rst=Db::name('proquarter')->where(['id'=>$id])->delete(); if($rst!==false){ $this->success("删除成功",$jump); }else{ $this -> error("删除失败!",$jump); } } /** * 删除(全选) */ public function show_alldel() { $page=input('page','','intval'); $this->assign('page',$page); $p = input('p'); $ids = input('id/a'); $jumpParam=['p' => $p,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/show_list',$jumpParam); if(empty($ids)){ $this -> error("请选择待删除数据",$jump); } if(is_array($ids)){ $where = 'id in('.implode(',',$ids).')'; }else{ $where = 'id='.$ids; } $rst=Db::name('proquarter')->where($where)->delete(); if($rst!==false){ $this->success("删除成功",$jump); }else{ $this -> error("删除失败!",$jump); } } public function category_list(){ $this->assign('table_name','category'); $page=input('page',1,'intval'); $postPage=input('post.page','','intval'); if($postPage)$page=$postPage; $this->assign('page',$page); $types=Db::name('proquarter')->field('id,title')->select(); $map=[]; if(count($types)){ $ids=array_search_key('id',$types); $titles=array_search_key('title',$types); $map=array_combine($ids,$titles); } $this->assign('types',$map); $where=[]; $list=Db::name('procategory')->where($where)->order(['sort'=>'desc','create_time'=>'desc'])->paginate(config('paginate.list_rows'),false,['query'=>get_query(),'page'=>$page]); $show = $list->render(); $this->assign('pageshow',$show); $this->assign('list',$list); if(request()->isAjax()){ return $this->fetch('product/ajax_category_list'); }else{ return $this->fetch('product/category_list'); } } public function category_alldel() { $page=input('page','','intval'); $this->assign('page',$page); $p = input('p'); $ids = input('id/a'); $jumpParam=['p' => $p,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/category_list',$jumpParam); if(empty($ids)){ $this -> error("请选择待删除数据",$jump); } if(is_array($ids)){ $where = 'id in('.implode(',',$ids).')'; }else{ $where = 'id='.$ids; } $rst=Db::name('procategory')->where($where)->delete(); if($rst!==false){ $this->success("删除成功",$jump); }else{ $this -> error("删除失败!",$jump); } } public function category_del($id=''){ $page=input('page','','intval'); $this->assign('page',$page); $p = input('p'); $ids = input('id/a'); $jumpParam=['p' => $p,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/category_list',$jumpParam); if(empty($ids)){ $this -> error("请选择待删除数据",$jump); } $rst=Db::name('procategory')->where(['id'=>$id])->delete(); if($rst!==false){ $this->success("删除成功",$jump); }else{ $this -> error("删除失败!",$jump); } } /** * 排序 */ public function category_order() { $page=input('page','','intval'); $this->assign('page',$page); $jumpParam=['page'=>$page]; $jump=url('admin/'.$this->controller_name.'/category_list',$jumpParam); if (!request()->isAjax()){ $this->error('提交方式不正确',$jump); }else{ foreach (input('post.') as $n_id => $sort){ Db::name('procategory')->update(['id'=>$n_id,'sort'=>$sort]); } $this->success('排序更新成功',$jump); } } /** * 添加显示 */ public function category_add() { $this->table_name='procategory'; $this->assign('table_name','category'); $page=input('page','','intval'); $this->assign('page',$page); $types=Db::name('proquarter')->select(); $this->assign('types',$types); if (!request()->isAjax()){ //栏目 return $this->fetch('product/category_add'); }else{ //上传图片部分 $img_one=''; $img_one2=''; $file = request()->file('pic_one'); $file2 = request()->file('pic_one2'); $jumpParam=['page'=>$page]; $jump=url('admin/'.$this->controller_name.'/category_list',$jumpParam); $ret=$this->dealFile($file,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one=$ret['img_one']; } $ret=$this->dealFile($file2,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one2=$ret['img_one']; } $sort=input('sort','','intval'); $sl_data=array( 'title'=>input('title'), 'pid'=>input('pid'), 'style'=>input('style'), 'style2'=>input('style2'), 'uid'=>session('admin_auth.aid'), 'status'=>input('status',0), 'poster'=>$img_one,//封面图片路径 'create_time'=>time(), 'sort'=>$sort, ); //图片字段处理 if(!empty($img_one)){ $sl_data['poster']=$img_one; } if(!empty($img_one2)){ $sl_data['poster2']=$img_one2; } $rst=Db::name($this->table_name)->insert($sl_data); if($rst){ if(empty($sort)){ $lastId=Db::name($this->table_name)->getLastInsID(); Db::name($this->table_name)->where(['id'=>$lastId])->update(['sort'=>$lastId]); } $this->success('添加成功',$jump); }else{ $this->error('添加失败',$jump); } } } /** * 编辑显示 */ public function category_edit() { $types=Db::name('proquarter')->select(); $this->assign('types',$types); $this->assign('table_name','category'); $this->table_name='procategory'; $page=input('page','','intval'); $this->assign('page',$page); $jumpParam=['page'=>$page]; $jump=url('admin/'.$this->controller_name.'/category_list',$jumpParam); if (!request()->isAjax()) { $n_id = input('id'); if (empty($n_id)) { $this->error('参数错误', $jump); } $product_list = Db::name($this->table_name)->find($n_id); $this->assign('list', $product_list); return $this->fetch('product/category_edit'); }else{ $img_one=''; $img_one2=''; $button=''; $file = request()->file('pic_one'); $file2 = request()->file('pic_one2'); //单图 $ret=$this->dealFile($file,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one=$ret['img_one']; } $ret=$this->dealFile($file2,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one2=$ret['img_one']; } $sl_data=array( 'id'=>input('id'), 'pid'=>input('pid'), 'style'=>input('style'), 'style2'=>input('style2'), 'title'=>input('title'), 'status'=>input('status',0), 'sort'=>input('sort',50,'intval'), ); //图片字段处理 if(!empty($img_one)){ $sl_data['poster']=$img_one; } if(!empty($img_one2)){ $sl_data['poster2']=$img_one2; } $rst=Db::name($this->table_name)->update($sl_data); if($rst!==false){ $this->success('修改成功',$jump); }else{ $this->error('修改失败',$jump); } } } /** * 添加显示 */ public function show_add() { $this->table_name='proquarter'; $this->assign('table_name','show'); $page=input('page','','intval'); $this->assign('page',$page); if (!request()->isAjax()){ //栏目 return $this->fetch('product/show_add'); }else{ //上传图片部分 $img_one=''; $img_one2=''; $button=''; $file = request()->file('pic_one'); $file2 = request()->file('pic_one2'); $file3 = request()->file('pic_one3'); $jumpParam=['page'=>$page]; $jump=url('admin/'.$this->controller_name.'/show_list',$jumpParam); $ret=$this->dealFile($file,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one=$ret['img_one']; } $ret=$this->dealFile($file2,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one2=$ret['img_one']; } $ret=$this->dealFile($file3,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$button=$ret['img_one']; } $sort=input('sort','','intval'); $sl_data=array( 'title'=>input('title'), 'address'=>input('address'), 'open_time'=>input('open_time'), 'infor'=>input('infor'), 'color'=>input('color'), 'fcolor'=>input('fcolor'), 'style'=>input('style'), 'music'=>input('music'), 'uid'=>session('admin_auth.aid'), 'status'=>input('status',0), 'poster'=>$img_one,//封面图片路径 'create_time'=>time(), 'sort'=>$sort, ); //图片字段处理 if(!empty($img_one)){ $sl_data['poster']=$img_one; } if(!empty($img_one2)){ $sl_data['poster2']=$img_one2; } if(!empty($button)){ $sl_data['button']=$button; } $rst=Db::name($this->table_name)->insert($sl_data); if($rst){ if(empty($sort)){ $lastId=Db::name($this->table_name)->getLastInsID(); Db::name($this->table_name)->where(['id'=>$lastId])->update(['sort'=>$lastId]); } $this->success('添加成功',$jump); }else{ $this->error('添加失败',$jump); } } } /** * 编辑显示 */ public function show_edit() { $this->assign('table_name','show'); $this->table_name='proquarter'; $page=input('page','','intval'); $this->assign('page',$page); $jumpParam=['page'=>$page]; $jump=url('admin/'.$this->controller_name.'/show_list',$jumpParam); if (!request()->isAjax()) { $n_id = input('id'); if (empty($n_id)) { $this->error('参数错误', $jump); } $product_list = Db::name($this->table_name)->find($n_id); $this->assign('list', $product_list); return $this->fetch('product/show_edit'); }else{ $img_one=''; $img_one2=''; $button=''; $file = request()->file('pic_one'); $file2 = request()->file('pic_one2'); $file3 = request()->file('pic_one3'); //单图 $ret=$this->dealFile($file,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one=$ret['img_one']; } $ret=$this->dealFile($file2,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one2=$ret['img_one']; } $ret=$this->dealFile($file3,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$button=$ret['img_one']; } $sl_data=array( 'id'=>input('id'), 'infor'=>input('infor'), 'address'=>input('address'), 'open_time'=>input('open_time'), 'title'=>input('title'), 'style'=>input('style'), 'color'=>input('color'), 'fcolor'=>input('fcolor'), 'music'=>input('music'), 'status'=>input('status',0), 'sort'=>input('sort',50,'intval'), ); //图片字段处理 if(!empty($img_one)){ $sl_data['poster']=$img_one; } if(!empty($img_one2)){ $sl_data['poster2']=$img_one2; } if(!empty($button)){ $sl_data['button']=$button; } $rst=Db::name($this->table_name)->update($sl_data); if($rst!==false){ $this->success('修改成功',$jump); }else{ $this->error('修改失败',$jump); } } } /** * 列表 */ public function product_list() { $search_name=input('search_name',''); $this->assign('search_name',$search_name); //栏目 $n_cid=input('cid','','intval'); $n_kind=input('kind','','intval'); $page=input('page',1,'intval'); $postPage=input('post.page','','intval'); if($postPage)$page=$postPage; $this->assign('page',$page); $this->assign('n_kind',$n_kind); $where=[]; if($n_cid)$where['cid']=$n_cid; if($n_kind)$where['kind']=$n_kind; $db=Db::name($this->table_name)->where($where); if($search_name){ $db->where('title','like','%'.$search_name.'%'); } $news=$db->order(['sort'=>'asc','create_time'=>'desc'])->paginate(10,false,['query'=>get_query(),'page'=>$page]); $show = $news->render(); $this->assign('pageshow',$show); $this->assign('news',$news); $this->assign('n_cid',$n_cid); if(request()->isAjax()){ return $this->fetch('product/ajax_product_list'); }else{ return $this->fetch('product/product_list'); } } /** * 添加显示 */ public function product_add() { $page=input('page','','intval'); $this->assign('page',$page); $n_cid=input('cid','','intval'); $n_kind=input('kind','','intval'); $this->assign('n_kind',$n_kind); $this->assign('types',$this->maps); if (!request()->isAjax()){ //栏目 $this->assign('n_cid',$n_cid); return $this->fetch('product/product_add'); }else{ //上传图片部分 $img_one=''; $img_one2=''; $picall_url=''; $file = request()->file('pic_one'); $file2 = request()->file('pic_one2'); $files = request()->file('pic_all'); $jumpParam=['cid'=>$n_cid,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/'.$this->table_name.'_list',$jumpParam); $ret=$this->dealFile($file,$files); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one=$ret['img_one']; if(isset($ret['picall_url'])&&$ret['picall_url'])$picall_url=$ret['picall_url']; } $ret=$this->dealFile($file2,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one2=$ret['img_one']; } $sort=input('sort','','intval'); $sl_data=array( 'title'=>input('title'), 'kind'=>input('kind'), 'year'=>input('year'), 'weight'=>input('weight'), 'author'=>input('author'), 'size'=>input('size'), 'price'=>input('price'), 'gprice'=>input('gprice'), 'video'=>input('video'), 'code'=>input('code'), 'style'=>input('style'), 'description'=>input('description'), 'topic'=>input('topic'), 'material'=>input('material'), 'uid'=>session('admin_auth.aid'), 'status'=>input('status',0), 'cid'=>$n_cid, 'photos'=>$picall_url,//多图路径 'poster'=>$img_one,//封面图片路径 'xcode'=>$img_one2,//封面图片路径 'link'=>input('link'), 'kind'=>input('kind'), 'create_time'=>time(), 'sort'=>$sort, ); $rst=Db::name($this->table_name)->insert($sl_data); if($rst){ if(empty($sort)){ $lastId=Db::name($this->table_name)->getLastInsID(); Db::name($this->table_name)->where(['id'=>$lastId])->update(['sort'=>$lastId]); } $this->success('添加成功',$jump); }else{ $this->error('添加失败',$jump); } } } /** * 编辑显示 */ public function product_edit() { $this->assign('types',$this->maps); $page=input('page','','intval'); $this->assign('page',$page); $n_cid=input('cid','','intval'); $n_kind=input('kind','','intval'); $this->assign('n_kind',$n_kind); $jumpParam=['cid'=>$n_cid,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/'.$this->table_name.'_list',$jumpParam); if (!request()->isAjax()) { $n_id = input('id'); if (empty($n_id)) { $this->error('参数错误', $jump); } $this->assign('n_cid', $n_cid); $product_list = Db::name($this->table_name)->find($n_id); //多图字符串转换成数组 $pic_list = array_filter(explode(",", $product_list['photos'])); $this->assign('pic_list', $pic_list); $source = Db::name('source')->select();//来源 $this->assign('source', $source); $this->assign('list', $product_list); return $this->fetch('product/product_edit'); }else{ $pic_oldlist=input('pic_oldlist');//老多图字符串 $img_one=''; $img_one2=''; $picall_url=''; $file = request()->file('pic_one'); $file2 = request()->file('pic_one2'); $files = request()->file('pic_all'); //单图 $ret=$this->dealFile($file,$files); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one=$ret['img_one']; if(isset($ret['picall_url'])&&$ret['picall_url'])$picall_url=$ret['picall_url']; } $ret=$this->dealFile($file2,''); if(isset($ret['error'])){ $this->error($ret['error'],$jump); }else{ if(isset($ret['img_one'])&&$ret['img_one'])$img_one2=$ret['img_one']; } $sl_data=array( 'id'=>input('id'), 'year'=>input('year'), 'kind'=>input('kind'), 'weight'=>input('weight'), 'title'=>input('title'), 'author'=>input('author'), 'size'=>input('size'), 'price'=>input('price'), 'gprice'=>input('gprice'), 'video'=>input('video'), 'code'=>input('code'), 'style'=>input('style'), 'description'=>input('description'), 'topic'=>input('topic'), 'material'=>input('material'), 'status'=>input('status',0), 'link'=>input('link'), 'kind'=>input('kind'), 'sort'=>input('sort',50,'intval'), ); //图片字段处理 if(!empty($img_one)){ $sl_data['poster']=$img_one; } if(!empty($img_one2)){ $sl_data['xcode']=$img_one2;//封面图片路径 } $sl_data['photos']=$pic_oldlist.$picall_url; $rst=Db::name($this->table_name)->update($sl_data); if($rst!==false){ $this->success('修改成功',$jump); }else{ $this->error('修改失败',$jump); } } } /** * 处理上传图片 * @param string $file * @param string $files * @return NULL[]|string[] */ protected function dealFile($file='',$files=''){ $validate = config('upload_validate'); $img_one=''; $picall_url=''; //单图 if (!empty($file)) { $info = $file[0]->validate($validate)->rule('uniqid')->move(ROOT_PATH . config('upload_path') . DS . date('Y-m-d')); if ($info) { $img_url = config('upload_path'). '/' . date('Y-m-d') . '/' . $info->getFilename(); $imgfile='.'.$img_url; //等比例宿放 if(file_exists($imgfile)){ $image=Image::open($imgfile); $width=$image->width(); $height=$image->height(); if($width>1000){ $nhei=(1000/$width)*$height; $image->thumb(1000,$nhei); $image->save($imgfile); } } //写入数据库 $data['uptime'] = time(); $data['filesize'] = $info->getSize(); $data['path'] = $img_url; Db::name('plug_files')->insert($data); $img_one = $img_url; } else { return ['error'=>$file[0]->getError()]; } } //多图 if (!empty($files)) { foreach ($files as $file) { $info = $file->validate($validate)->rule('uniqid')->move(ROOT_PATH . config('upload_path') . DS . date('Y-m-d')); if ($info) { $img_url = config('upload_path'). '/' . date('Y-m-d') . '/' . $info->getFilename(); $imgfile='.'.$img_url; //等比例宿放 if(file_exists($imgfile)){ $image=Image::open($imgfile); $width=$image->width(); $height=$image->height(); if($width>1000){ $nhei=(1000/$width)*$height; $image->thumb(1000,$nhei); $image->save($imgfile); } } //写入数据库 $data['uptime'] = time(); $data['filesize'] = $info->getSize(); $data['path'] = $img_url; Db::name('plug_files')->insert($data); $picall_url = $img_url . ',' . $picall_url; } else { return ['error'=>$file[0]->getError()]; } } } return ['img_one'=>$img_one,'picall_url'=>$picall_url]; } /** * 排序 */ public function product_order() { $page=input('page','','intval'); $this->assign('page',$page); $n_cid=input('cid','','intval'); $n_kind=input('kind','','intval'); $this->assign('n_kind',$n_kind); $jumpParam=['cid'=>$n_cid,'kind'=>$n_kind,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/'.$this->table_name.'_list',$jumpParam); if (!request()->isAjax()){ $this->error('提交方式不正确',$jump); }else{ foreach (input('post.') as $n_id => $sort){ Db::name($this->table_name)->update(['id'=>$n_id,'sort'=>$sort]); } $this->success('排序更新成功',$jump); } } /** * 删除(单个) */ public function product_del() { $page=input('page','','intval'); $this->assign('page',$page); $p=input('p'); $n_cid=input('cid','','intval'); $n_kind=input('kind','','intval'); $this->assign('n_kind',$n_kind); $jumpParam=['p' => $p,'cid'=>$n_cid,'kind'=>$n_kind,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/'.$this->table_name.'_list',$jumpParam); $rst=Db::name($this->table_name)->delete(input('id')); if($rst!==false){ $this->success('删除成功',$jump); }else{ $this -> error("删除失败!",$jump); } } /** * 删除(全选) */ public function product_alldel() { $page=input('page','','intval'); $this->assign('page',$page); $p = input('p'); $ids = input('id/a'); $n_cid=input('cid',0,'intval'); $n_kind=input('kind',0,'intval'); $this->assign('n_kind',$n_kind); $jumpParam=['p' => $p,'cid'=>$n_cid,'kind'=>$n_kind,'page'=>$page]; $jump=url('admin/'.$this->controller_name.'/'.$this->table_name.'_list',$jumpParam); if(empty($ids)){ $this -> error("请选择待删除数据",$jump); } if(is_array($ids)){ $where = 'id in('.implode(',',$ids).')'; }else{ $where = 'id='.$ids; } $rst=Db::name($this->table_name)->where($where)->delete(); if($rst!==false){ $this->success("删除成功",$jump); }else{ $this -> error("删除失败!",$jump); } } /** * 审核/取消审核 */ public function product_state() { $id=input('x'); $status=Db::name($this->table_name)->where(array('id'=>$id))->value('status'); if($status==1){ $statedata = array('status'=>0); Db::name($this->table_name)->where(array('id'=>$id))->setField($statedata); $this->success('未审'); }else{ $statedata = array('status'=>1); Db::name($this->table_name)->where(array('nid'=>$id))->setField($statedata); $this->success('已审'); } } }