1.获取未兑付未过期的我的奖品
1.在apps/user_operation/views.py中新建GeMyPrize:
class GeMyPrize(APIView): """ 获取我的奖品 """ renderer_classes = [JSONRenderer] # 渲染器 def get(self,request): token = request.GET.get('token') user = UserProfile.objects.filter(token=token).first() if user: p_list=GetPrize.objects.filter(user=user,end_time__gt=datetime.now(),status='0').order_by('-add_time') p=GetPrizeModelSerializer(p_list,many=True) return Response(p.data) else: return HttpResponse('参数不足')
2.在apps/user_operation/urls.py内配置路由:
from django.urls import path from .views import GetMessage,ReadMessage,DelMessage,BuyUseJiFen,GeMyPrize urlpatterns = [ path('getmessage/',GetMessage.as_view()), path('readmessage/',ReadMessage.as_view()), path('delmessage/',DelMessage.as_view()), path('buyusejifen/',BuyUseJiFen.as_view()), path('getmyprize/',GeMyPrize.as_view()), ]
2.获取优惠券参数和兑付优惠券
1.简单原理:
扫码操作于普通小程序端,而要直接操作广告小程序的数据库必须要在广告小程序端,所以需要用两个视图类进行对接配合,其中两个视图类的get负责获取优惠券参数的功能实现,post则负责兑付优惠券的功能实现。
2.在apps/user_operation/models.py中新建属于普通小程序段的兑付表PayPrize:
class PayPrize(models.Model): """奖品兑付表小程序端""" admin_user=models.CharField(max_length=20,verbose_name='职工微信名',null=True,blank=True) user = models.CharField(max_length=20, verbose_name='微信用户名', null=True, blank=True) prize_title = models.CharField(max_length=20, verbose_name='奖券名', null=True, blank=True) add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "小程序端兑付表" verbose_name_plural = verbose_name def __str__(self): return self.prize_title
执行数据更新命令:
python manage.py makemigrations
python manage.py migrate
3.在apps/user_operation/adminx.py内注册:
class PayPrizeAdmin(object): list_display = ["admin_user","user","prize_title","add_time"] list_filter = ["admin_user","user","prize_title","add_time"] xadmin.site.register(PayPrize, PayPrizeAdmin)
4.在apps/user_operation/views.py中新建类CheckingPirze,EditGetPrize:
class CheckingPirze(APIView): """扫码访问小程序验证""" renderer_classes = [JSONRenderer] # 渲染器 def get(self,request): token = request.GET.get('token') # 扫码者 admin_user = UserProfile.objects.filter(token=token).first() openid = request.GET.get('oid') user = UserProfile.objects.filter(open_id=openid).first() prizeid=request.GET.get('pid') if user and prizeid and admin_user.is_staff: # 扫码者必须是员工才可以往下走 res = urllib.request.urlopen(PRIZEURL+"?openid="+openid+"&prizeid="+prizeid) # print('1',res) # print('查询结果',type(res.read().decode("utf-8"))) obj = json.loads(res.read().decode("utf-8")) # print('obj',obj) re_user=UserProfileModelSerializer(user) return Response(data=({'getprize':obj,'user':re_user.data})) else: return HttpResponse('参数不足') def post(self,request): token = request.POST.get('token') # 扫码者 admin_user = UserProfile.objects.filter(token=token).first() getprizeid=request.POST.get('getprizeid') if getprizeid and admin_user.is_staff: # 扫码者必须是员工才可以往下走 data_dict = {"gid":getprizeid} data_string = urllib.parse.urlencode(data_dict) last_data = bytes(data_string, encoding='utf-8') res = urllib.request.urlopen(PRIZEURL,data=last_data) st=res.read().decode("utf-8") dict_st=eval(st) # print('res:',dict_st) # if res.read().decode("utf-8")=="200": # print('code:',type(dict_st["code"])) if dict_st["code"]=='200': # print('username',dict_st["user"]) # print('title', dict_st["title"]) pay_prize=PayPrize() pay_prize.admin_user=admin_user.nickName pay_prize.user=dict_st["user"] pay_prize.prize_title=dict_st["title"] pay_prize.save() return HttpResponse(dict_st["code"]) else: return HttpResponse('参数不足') class EditGetPrize(APIView): """get查询,post修改""" renderer_classes = [JSONRenderer] # 渲染器 def get(self, request): openid = request.GET.get('openid') user = UserProfile.objects.filter(open_id=openid).first() prizeid = request.GET.get('prizeid') if user and prizeid: prizeid=int(prizeid) prize = Prize.objects.filter(id=prizeid).first() p=GetPrize.objects.filter(user=user,prize=prize).first() re_p=GetPrizeModelSerializer(p) if p: return Response(re_p.data) else: #查无领取记录 return HttpResponse('404') else: return HttpResponse('参数不足') def post(self, request): get_pid=request.POST.get('gid') if get_pid: get_pid=int(get_pid) # print('getid',get_pid) pr=GetPrize.objects.filter(id=get_pid,end_time__gt=datetime.now()).first() if pr: if pr.status=='1': # 已经兑付过的了 re={'code': '405', 'msg': '此奖品已经兑付过'} return Response(data=re) else: pr.status = '1' pr.save() user = pr.user title = pr.title msg = Message() msg.user = user msg.content = "您成功使用了积分奖券:" + title msg.save() # print('新消息',msg.content) re = {'code': '200', 'msg': '兑奖成功','title':title,'user':user.nickName} return Response(data=re) else: # 奖品已过期 re = {'code': '404', 'msg': '此奖品不存在或已过期'} return Response(data=re) else: re = {'code': '403', 'msg': '参数不足'} return Response(data=re)
5.在apps/user_operation/urls.py内配置路由:
from django.urls import path from .views import GetMessage,ReadMessage,DelMessage,BuyUseJiFen,CheckingPirze,EditGetPrize,GeMyPrize urlpatterns = [ path('getmessage/',GetMessage.as_view()), path('readmessage/',ReadMessage.as_view()), path('delmessage/',DelMessage.as_view()), path('buyusejifen/',BuyUseJiFen.as_view()), path('checkingprize/',CheckingPirze.as_view()), path('editgetprize/',EditGetPrize.as_view()), path('getmyprize/',GeMyPrize.as_view()), ]
6.在settings.py中配置:
# 广告平台兑奖url PRIZEURL='http://127.0.0.1:8000/useroperation/editgetprize/'