1.轮播图
class CarouselView(APIView):
def get(self,request):
# 1.获取轮番图:
lun = Carousel.objects.all()
# 将数据序列化:
ser = CarouselSer(lun,many=True)
# 返回数据:
return Response({'msg':'获取轮番图成功','code':200,'carousel':ser.data})
轮播图前端
this.$axios
.get("/goods/carousel/")
.then(res => {
console.log("@@carousel res:",res)
/***
*
* return Response({
* "code":"",
* "msg":"",
* "carousel":[{
* ”imgPath“:”“,
* ”describes“:”“,
* },{...},{...},{...}])
*/
this.carousel = res.data.carousel;
})
.catch(err => {
return Promise.reject(err);
});
2.获取一类数据
class OneCategory(APIView):
def get(self,request):
# 获取前端传递过来的种类:
zhong = request.query_params.get('categoryName')
# 根据种类找到对应的后端种类数据:
try:
good_name = Category.objects.get(cate_name=zhong)
except Exception as e:
print(e)
return Response({'msg':'种类不存在!','code':404})
# 根据商品种类获取商品信息:
good_data = good_name.goods_set.all()
# 序列化:
ser = GoodsSer(good_data,many=True)
# 返回数据:
return Response({
'msg':'获取种类数据成功',
'code':200,
'result':ser.data,
})
获取一类数据前端
"/goods/oneCategory/all/"
this.$axios
.get(api, {
params: {categoryName:categoryName},
})
.then(res => {
console.log("@@获取"+categoryName+"的响应", res)
//获取属性,并赋值,如'phoneList'
this[val] = res.data.result;
})
.catch(err => {
return Promise.reject(err);
});
3.热门数据
# 热门商品:
class HotProduct(APIView):
def get(self,request):
# 1.获取热门很多热门的种类:
r_zhong = request.query_params.getlist('categoryName[]')
# 利用循环找到对应的很多热门的种类放在列表中:
r_list = []
for i in r_zhong:
r_zh = Category.objects.get(cate_name=i)
r_list.append(r_zh)
# 根据一些种类找到所对应的各组商品:
good_list = []
for i in r_list:
good_data = i.goods_set.all()
good_list.append(good_data)
# 进行累计:
from functools import reduce
lei = reduce((lambda x,y:x|y),good_list)
# 进行序列化返回:
ser = GoodsSer(lei,many=True)
return Response({
'msg':'返回成功',
'code':200,
'result':ser.data
})
热门数据前端
this.getPromo(
["电视机", "空调", "洗衣机"],
"applianceList",
"/goods/getHotProduct/"
);
this.getPromo(
["保护套", "保护膜", "充电器", "充电宝"],
"accessoryList",
"/goods/getHotProduct/"
);
4.获取详情商品
# 获取详情商品
class OneGood(APIView):
def get(self,request):
# 1.获取商品详情:
good_id = request.query_params.get('productID')
# 找出后端对应id:
try:
good = Goods.objects.get(id=good_id)
except Exception as e:
print(e)
return Response({'msg':'数据不存在!','code':404})
# 进行序列化返回:
ser = GoodsSer(good)
return Response(ser.data)
获取详情商品前端
// 获取商品详细信息
getDetails(val) {//val为商品id
this.$axios
.get("/goods/onegood/", {
params:{productID: val}
})
.then(res => {
console.log("@@一个商品:", res)
this.productDetails = res.data;
// 然后模板就可以解析数据了
})
.catch(err => {
return Promise.reject(err);
});
},
5.获取详情轮播图
class OneGoodImgs(APIView):
def get(self,request):
# 获取商品id:
good_id = request.query_params.get('productID')
# 获取后端数据:
try:
good_data = Goods.objects.get(id=good_id)
except Exception as e:
print(e)
return Response({'msg':'数据不存在!','code':404})
# 根据商品id获取图品数据:
good = good_data.goodimg_set.all()
# 进行序列化:
ser = GoodImgSer(good,many=True)
# 返回数据:
return Response(ser.data)
获取详情轮播图前端
// 获取商品图片
getDetailsPicture(val) {//通过商品id获取其图片
this.$axios
.get("/goods/onegood/imgs/", {
params:{productID: val}
})
.then(res => {
console.log("@详情页轮播图:",res)
this.productPicture = res.data;
})
.catch(err => {
return Promise.reject(err);
});
},