title = '商品管理'; $query = $this->_query($this->table)->where('is_deleted',0)->where(['is_integral'=>1])->like('name'); $query->dateBetween('create_at')->order('sort desc , id desc')->page(); } /** * 数据列表处理 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ protected function _index_page_filter(&$data) { foreach ($data as $k=>&$v){ } } /** * 添加商品 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function add() { $this->title = '添加商品'; $lev_set = Db::table('member_level')->field('id,name')->select(); $lev_set = array_column($lev_set,null,'id'); $lev_data = ['0'=>'不限']; array_map(function ($v)use (&$lev_data){ $lev_data[$v['id']] = $v['name']; },$lev_set); $this->lev_set =$lev_data; // 分类 $goods_cate = Db::table('store_goods_cate') ->field('id,title') ->where(['is_deleted'=>0,'status'=>1,'pid'=>0]) ->order('sort desc ,id desc') ->select(); array_walk($goods_cate,function (&$v){ $v['children'] =Db::table('store_goods_cate') ->where(['is_deleted'=>0,'status'=>1,'pid'=>$v['id']]) ->order('sort desc ,id desc') ->select(); }); $this->goods_cate = $goods_cate; $this->all_temp = Db::table('freight_template') ->field('id,name')->where(['status'=>1,'is_deleted'=>0]) ->order('sort desc ,id desc')->select(); $this->_form($this->table, 'form'); } /** * 编辑商品 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ function edit() { $this->title = '编辑商品'; $lev_set = Db::table('member_level')->field('id,name')->select(); $lev_set = array_column($lev_set,null,'id'); $lev_data = ['0'=>'不限']; array_map(function ($v)use (&$lev_data){ $lev_data[$v['id']] = $v['name']; },$lev_set); $this->lev_set =$lev_data; // 分类 $goods_cate = Db::table('store_goods_cate') ->field('id,title') ->where(['is_deleted'=>0,'status'=>1,'pid'=>0]) ->order('sort desc ,id desc') ->select(); array_walk($goods_cate,function (&$v){ $v['children'] =Db::table('store_goods_cate') ->where(['is_deleted'=>0,'status'=>1,'pid'=>$v['id']]) ->order('sort desc ,id desc') ->select(); }); $this->goods_cate = $goods_cate; // 规格 $goods_spec = Db::table('store_goods_specifica') ->field('id,title,detail') ->where(['is_deleted'=>0]) ->order('sort desc ,id desc') ->select(); array_walk($goods_spec,function (&$val){ // $val['detail'] = json_decode($val['detail']); }); $goods_spec = array_column($goods_spec,null,'id'); $this->goods_spec = $goods_spec; $this->all_temp = Db::table('freight_template') ->field('id,name')->where(['status'=>1,'is_deleted'=>0]) ->order('sort desc ,id desc')->select(); $this->_form($this->table, 'form'); } /** * 表单数据处理 * @auth true * @menu true * @param array $data */ protected function _form_filter(&$data) { if($this->request->isGet()){ if($this->request->action() == 'edit') { if($data['spec']) $data['spec'] = json_decode($data['spec'],true); } } // 添加或编辑商品 if ($this->request->isPost()) { $spec_count = count($data['goods_no']); $goods_spec = []; $detail_list = []; if(isset($data['spec_id']) && $data['spec_id'] > 0) { $spec_info = Db::table('store_goods_specifica')->find($data['spec_id'] ); $detail_list = $spec_info['detail'] ? json_decode($spec_info['detail']) : []; } $total_num = 0; for ($i= 0;$i < $spec_count;$i++) { $goods_spec[$i]['goods_no'] = $data['goods_no'][$i]; $goods_spec[$i]['spec_img'] = $data['cover']; $goods_spec[$i]['store_num'] = $data['store_num'][$i] > 0 ?$data['store_num'][$i] :0 ; $goods_spec[$i]['price'] = $data['price'][$i] >0 ? $data['price'][$i] :0; ['price'][$i] >0 ? $data['price'][$i] :0 ; $goods_spec[$i]['integral'] = $data['integral'][$i] >0 ? $data['integral'][$i] :0; ['price'][$i] >0 ? $data['price'][$i] :0 ; $goods_spec[$i]['sell_money'] = 0; $goods_spec[$i]['seckill_money'] = 0; $goods_spec[$i]['spec_exp'] = ''; $goods_spec[$i]['spec_key'] = ''; $total_num +=$goods_spec[$i]['store_num'] ; } $data['spec'] =json_encode($goods_spec); $data['stock'] =$total_num; // 总库存 $data['floor_price'] = min( array_column($goods_spec,'sell_money') );// 最低价 $data['integral'] = $data['integral'][0]; $data['is_integral'] = 1; } } /** * @auth true * @menu true * 商品上架 */ public function up() { $this->_save($this->table, ['status' => '1']); } /** * @auth true * @menu true * 商品下架 */ public function down() { $this->_save($this->table, ['status' => '0']); } /** * @auth true * @menu true * 商品下架 */ public function del() { $this->_save($this->table, ['is_deleted' => '1']); } public function upload(){ if(!in_array($_FILES["file"]["type"],['image/png','image/jpeg','image/jpeg'])) echo '图片类型不支持'; if($_FILES["file"]["size"] > 50000000) echo '图片大小最大50M'; if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "
"; // $dir = env('root_path') . 'public/static/pic'; $dir = $_SERVER['DOCUMENT_ROOT']. '/static/pic'; /* var_dump($dir); if(!is_dir($dir)) { var_dump(mkdir($dir)); }*/ move_uploaded_file($_FILES["file"]["tmp_name"], $dir."/" . $_FILES["file"]["name"]); echo $dir."/" . $_FILES["file"]["name"]; } }