评价订单商品
提示:
点击《我的订单》页面中的《待评价》按钮,进入到订单商品评价页面。
一、评价订单商品 1. 展示商品评价页面1.请求方式
选项方案请求方法GET请求地址/orders/comment/ # 订单商品评价
url(r'orders/comment/$', views.OrderCommentView.as_view(), name='comment'),
2.请求参数:查询参数
参数名类型是否必传说明order_idint是订单编号3.响应结果:HTML
goods_judge.html
4.后端接口定义和实现
class OrderCommentView(LoginRequiredMixin, View):
"""订单商品评价"""
def get(self, request):
"""展示商品评价页面"""
# 接收参数
order_id = request.GET.get('order_id')
# 校验参数
try:
OrderInfo.objects.get(order_id=order_id, user=request.user)
except OrderInfo.DoesNotExist:
return http.HttpResponseNotFound('订单不存在')
# 查询订单中未被评价的商品信息
try:
uncomment_goods = OrderGoods.objects.filter(order_id=order_id, is_commented=False)
except Exception:
return http.HttpResponseServerError('订单商品信息出错')
# 构造待评价商品数据
uncomment_goods_list = []
for goods in uncomment_goods:
uncomment_goods_list.append({
'order_id':goods.order.order_id,
'sku_id':goods.sku.id,
'name':goods.sku.name,
'price':str(goods.price),
'default_image_url':goods.sku.default_image.url,
'comment':goods.comment,
'score':goods.score,
'is_anonymous':str(goods.is_anonymous),
})
# 渲染模板
context = {
'uncomment_goods_list': uncomment_goods_list
}
return render(request, 'goods_judge.html', context)
2. 评价订单商品
1.请求方式
选项方案请求方法POST请求地址/orders/comment/ # 订单商品评价
url(r'orders/comment/$', views.OrderCommentView.as_view(), name='comment'),
2.请求参数:查询参数
参数名类型是否必传说明order_idint是订单编号3.响应结果:JSON
字段说明code状态码errmsg错误信息4.后端接口定义和实现
class OrderCommentView(LoginRequiredMixin, View):
"""订单商品评价"""
def get(self, request):
"""展示商品评价页面"""
......
def post(self, request):
"""评价订单商品"""
# 接收参数
json_dict = json.loads(request.body.decode())
order_id = json_dict.get('order_id')
sku_id = json_dict.get('sku_id')
score = json_dict.get('score')
comment = json_dict.get('comment')
is_anonymous = json_dict.get('is_anonymous')
# 校验参数
if not all([order_id, sku_id, score, comment]):
return http.HttpResponseForbidden('缺少必传参数')
try:
OrderInfo.objects.filter(order_id=order_id, user=request.user, status=OrderInfo.ORDER_STATUS_ENUM['UNCOMMENT'])
except OrderInfo.DoesNotExist:
return http.HttpResponseForbidden('参数order_id错误')
try:
sku = SKU.objects.get(id=sku_id)
except SKU.DoesNotExist:
return http.HttpResponseForbidden('参数sku_id错误')
if is_anonymous:
if not isinstance(is_anonymous, bool):
return http.HttpResponseForbidden('参数is_anonymous错误')
# 保存订单商品评价数据
OrderGoods.objects.filter(order_id=order_id, sku_id=sku_id, is_commented=False).update(
comment=comment,
score=score,
is_anonymous=is_anonymous,
is_commented=True
)
# 累计评论数据
sku.comments += 1
sku.save()
sku.spu.comments += 1
sku.spu.save()
# 如果所有订单商品都已评价,则修改订单状态为已完成
if OrderGoods.objects.filter(order_id=order_id, is_commented=False).count() == 0:
OrderInfo.objects.filter(order_id=order_id).update(status=OrderInfo.ORDER_STATUS_ENUM['FINISHED'])
return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '评价成功'})
二、详情页展示评价信息
1.请求方式
选项方案请求方法POST请求地址/comments/(?P\d+)/ # 订单商品评价信息
url(r'comments/(?P\d+)/$', views.GoodsCommentView.as_view()),
2.请求参数:查询参数
参数名类型是否必传说明sku_idint是商品SKU编号3.响应结果:JSON
字段说明code状态码errmsg错误信息comment_list[ ]评价列表username发表评价的用户comment评价内容score分数{
"code":"0",
"errmsg":"OK",
"comment_list":[
{
"username":"itcast",
"comment":"这是一个好手机!",
"score":4
}
]
}
4.后端接口定义和实现
class GoodsCommentView(View):
"""订单商品评价信息"""
def get(self, request, sku_id):
# 获取被评价的订单商品信息
order_goods_list = OrderGoods.objects.filter(sku_id=sku_id, is_commented=True).order_by('-create_time')[:30]
# 序列化
comment_list = []
for order_goods in order_goods_list:
username = order_goods.order.user.username
comment_list.append({
'username': username[0] + '***' + username[-1] if order_goods.is_anonymous else username,
'comment':order_goods.comment,
'score':order_goods.score,
})
return http.JsonResponse({'code':RETCODE.OK, 'errmsg':'OK', 'comment_list': comment_list})
5.渲染商品评价信息
-
[[comment.username]]
[[comment.comment]]
商品评价([[ comments.length ]])
¥{{ sku.price }}
[[ comments.length ]]人评价
提示:订单商品评价完成后,一个订单的流程就结束了,订单状态修改为已完成
。