廣告小程序後端開發(15.積分商城:獲取積分商品列表,獲取積分商品詳情)

1.獲取積分商品列表

1.在apps/ad/views.py內新建獲取積分商品列表類:

class GetPrize(APIView):
    """
    獲取獎品
    """
    renderer_classes = [JSONRenderer]  # 渲染器

    def get(self,request):
        token = request.GET.get('token')
        page = request.GET.get('page')
        sheng = request.GET.get('sheng')
        shi = request.GET.get('shi')
        xian = request.GET.get('xian')
        user = UserProfile.objects.filter(token=token).first()
        if user and page:
            page=int(page)
            if sheng:
                # 全省
                num = Area.objects.filter(name=sheng).first().num
                re_list = Prize.objects.filter(area__num__gt=num,area__num__lt=(num+10000)).order_by('-add_time')[0:(10 * page)]
                re=PrizeModelSerializer(re_list,many=True)
                return Response(re.data)
            elif shi:
                # 全市
                num = Area.objects.filter(name=shi).first().num
                # print(num)
                re_list = Prize.objects.filter(area__num__gte=num, area__num__lt=(num + 100)).order_by('-add_time')[0:(10 * page)]
                re = PrizeModelSerializer(re_list, many=True)
                return Response(re.data)
            elif xian:
                # 全縣
                num = Area.objects.filter(name=xian).first().num
                # print(num)
                re_list = Prize.objects.filter(area__num=num).order_by('-add_time')[0:(10 * page)]
                re = PrizeModelSerializer(re_list, many=True)
                return Response(re.data)
            else:
                re_list = Prize.objects.all().order_by('-add_time')[0:(10 * page)]
                re = PrizeModelSerializer(re_list, many=True)
                return Response(re.data)
        else:
            return HttpResponse('參數不足')

2.在apps/ad/urls.py內配置路由:

from django.urls import path
from .views import GetCategory,CreateAdOrShop,UpLoadImg,GetPubGoods,GetGoodDesc,GetUserGoods,DelUserGood,EditGood,DelImg,GetPrize

urlpatterns = [
    path('getcategory/',GetCategory.as_view()),
    path('createadorshop/',CreateAdOrShop.as_view()),
    path('uploadimg/',UpLoadImg.as_view()),
    path('getpubgoods/',GetPubGoods.as_view()),
    path('getgooddesc/',GetGoodDesc.as_view()),
    path('getusergoods/',GetUserGoods.as_view()),
    path('delusergood/',DelUserGood.as_view()),
    path('editgood/',EditGood.as_view()),
    path('delimg/',DelImg.as_view()),
    path('getprize/',GetPrize.as_view()),
]

2.獲取積分獎品詳情

1.在apps/ad/views.py中新建GetPrizeDesc類:

class GetPrizeDesc(APIView):
    """
    獲取獎品詳情
    """
    renderer_classes = [JSONRenderer]  # 渲染器

    def get(self, request):
        token = request.GET.get('token')
        id=request.GET.get('prizeid')
        user = UserProfile.objects.filter(token=token).first()
        if user and id:
            id=int(id)
            re_obj=Prize.objects.filter(id=id).first()
            re=PrizeModelSerializer(re_obj)
            return Response(re.data)
        else:
            return HttpResponse('參數不足')

2.在apps/ad/urls.py內配置路由

from django.urls import path
from .views import GetCategory,CreateAdOrShop,UpLoadImg,GetPubGoods,GetGoodDesc,GetUserGoods,DelUserGood,EditGood,DelImg,GetPrize,GetPrizeDesc

urlpatterns = [
    path('getcategory/',GetCategory.as_view()),
    path('createadorshop/',CreateAdOrShop.as_view()),
    path('uploadimg/',UpLoadImg.as_view()),
    path('getpubgoods/',GetPubGoods.as_view()),
    path('getgooddesc/',GetGoodDesc.as_view()),
    path('getusergoods/',GetUserGoods.as_view()),
    path('delusergood/',DelUserGood.as_view()),
    path('editgood/',EditGood.as_view()),
    path('delimg/',DelImg.as_view()),
    path('getprize/',GetPrize.as_view()),
    path('getprizedesc/',GetPrizeDesc.as_view()),
]
相關文章
相關標籤/搜索