商品由商家发布,可以根据实际情况设置多种属性,多个规格。商品模块所涉及的表有:
goods、goodscommon、goodsimages。他们的关系如下图
卖家添加商品时,所用到的控制器是Sellergoodsadd,第一步add_step_one操作显示卖家绑定的分类
// 实例化商品分类模型 $goodsclass_model = model('goodsclass'); // 商品分类 $goods_class = $goodsclass_model->getGoodsclass(session('store_id'));
第二步add_step_two操作如果不是自营店铺或者没有绑定全部分类则检查所选商品分类是否在卖家绑定的分类中
// 如果不是自营店铺或者自营店铺未绑定全部商品类目,读取绑定分类 if (!check_platform_store_bindingall_goodsclass()) { //商品分类 支持批量显示分类 $storebindclass_model = model('storebindclass'); $goods_class = model('goodsclass')->getGoodsclassForCacheModel(); $where['store_id'] = session('store_id'); $class_2 = isset($goods_class[$gc_id]['gc_parent_id'])?$goods_class[$gc_id]['gc_parent_id']:0; $class_1 = isset($goods_class[$class_2]['gc_parent_id'])?$goods_class[$class_2]['gc_parent_id']:0; $where['class_1'] = ($class_1>0)?$class_1:(($class_2>0)?$class_2:$gc_id); $where['class_2'] = ($class_1>0)?$class_2:(($class_2>0)?$gc_id:0); $where['class_3'] = ($class_1>0 && $class_2>0)?$gc_id:0; $bind_info = $storebindclass_model->getStorebindclassInfo($where); if (empty($bind_info)) { $where['class_3'] = 0; $bind_info = $storebindclass_model->getStorebindclassInfo($where); if (empty($bind_info)) { $where['class_2'] = 0; $where['class_3'] = 0; $bind_info = $storebindclass_model->getStorebindclassInfo($where); if (empty($bind_info)) { $where['class_1'] = 0; $where['class_2'] = 0; $where['class_3'] = 0; $bind_info = model('storebindclass')->getStorebindclassInfo($where); if (empty($bind_info)) { $this->error(lang('store_goods_index_again_choose_category2')); } } } } }
获取所选分类下所关联的商品属性与规格
// 获取类型相关数据 $typeinfo = model('type')->getAttribute($goods_class['type_id'], session('store_id'), $gc_id); list($spec_json, $spec_list, $attr_list, $brand_list) = $typeinfo;
商品图片上传时,通过ajax异步上传图片到Sellergoodsadd/image_upload
检查是否超过可上传的图片数量
// 判断图片数量是否超限 $album_model = model('album'); $album_limit = $this->store_grade['storegrade_album_limit']; if ($album_limit > 0) { $album_count = $album_model->getCount(array('store_id' => session('store_id'))); if ($album_count >= $album_limit) { $error = lang('store_goods_album_climit'); exit(json_encode(array('error' => $error))); } }
用公共方法upload_albumpic上传图片文件,如果是本地存储则用公共方法create_albumpic_thumb同时生成240、480、1280规格的压缩图
//本地图片保存 $file_object = request()->file($file_name); $upload_path = BASE_UPLOAD_PATH . DS . $upload_path; $info = $file_object->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_path, $save_name); if ($info) { $img_path = $upload_path . '/' . $info->getFilename(); create_albumpic_thumb($upload_path,$info->getFilename()); return array('code' => '10000', 'message' => '', 'result' => $img_path); } else { $error = $file_object->getError(); $data['code'] = '10001'; $data['message'] = $error; $data['result'] = $_FILES[$file_name]['name']; return $data; }
第三步save_goods操作保存商品数据
将商品公共信息保存在goodscommon表,将商品属性serialize序列化存储在goods_attr列中,商品规格信息保存在goods表