• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

广告小程序后端开发(16.优惠券系统:原理流程图,奖品实验数据,生成二维码) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

业务需求:在一个广告小程序内发许多其他的小程序的优惠券。

1.原理流程图

 

2.奖品(优惠券)实验数据

1.修改apps/ad/models.py中的Prize表类

class Prize(models.Model):
    """奖品"""
    title = models.CharField(max_length=15, null=True, blank=True, verbose_name=\'标题\', help_text=\'最多15字\')
    url=models.CharField(max_length=200, null=True, blank=True, verbose_name=\'小程序url\')
    area = models.ForeignKey(Area, null=True, blank=True, verbose_name="地区", on_delete=models.CASCADE)
    get_num=models.IntegerField(default=0,verbose_name=\'被领取数\')
    price = models.IntegerField(default=0, verbose_name=\'所需积分\')
    all_num=models.IntegerField(default=0,verbose_name=\'发布数\')
    is_out=models.BooleanField(default=False,verbose_name=\'是否已售罄\')
    desc = models.TextField(default=\'\', max_length=400, verbose_name=\'描述\', help_text=\'最多400字\')
    usage = models.TextField(default=\'\', max_length=200, verbose_name=\'使用方式\', help_text=\'最多200字\')
    image = models.ImageField(max_length=200, default=\'\', upload_to=\'prizeimg/\', verbose_name=\'奖品图片\')
    end_time = models.DateTimeField(default=datetime.now, verbose_name=\'过期时间\')
    add_time = models.DateTimeField(default=datetime.now, verbose_name=\'添加时间\')

    class Meta:
        verbose_name = "奖品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

    def image_url(self):
        return mark_safe(\'<img src="/media/{0}" class="field_img">\'.format(self.image))

    image_url.short_description = \'奖品图片\'

执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

2.修改apps/ad/adminx.py中的PrizeAdmin

class PrizeAdmin(object):
    list_display = ["title", "get_num","is_out","url","price","area", "all_num", "desc","usage","end_time","image_url","add_time"]
    list_filter = ["title", "get_num","is_out","url", "price", "area","all_num", "desc","usage","end_time","add_time"]
    search_fields = ["title", "get_num", "price", "all_num", "desc","usage"]

3.通过xadmin后台,直接向奖品表Prize中填入一些实验

3.生成图片二维码

1.从网上下载14张(这个数目可以自定义的),然后放到gg/static/下新建目录2下,这些图片用于生成二维码的背景图:

 

2.安装myqr

pip install myqr

3.修改apps/user_operation/models.py中的GetPrize表类:

class GetPrize(models.Model):
    """兑奖表"""
    user = models.ForeignKey(UserProfile, verbose_name="用户", on_delete=models.CASCADE)
    prize=models.ForeignKey(Prize, verbose_name="奖品", on_delete=models.CASCADE)
    title = models.CharField(max_length=15, null=True, blank=True, verbose_name=\'标题\', help_text=\'最多15字\')
    status = models.CharField(choices=(("0", "未使用"), ("1", "已使用")),default="0",max_length=6, verbose_name="使用状态")
    twoimg=models.ImageField(max_length=200, default=\'\', upload_to=\'twoimg/\', verbose_name=\'二维码\')
    end_time = models.DateTimeField(default=datetime.now, verbose_name=\'过期时间\')
    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

    def image_url(self):
        return mark_safe(\'<img src="/media/{0}" class="field_img">\'.format(self.twoimg))

    image_url.short_description = \'二维码\'

执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

4.在apps/user_operation/views.py中新建BuyUseJiFen类:

#……
from users.models import UserProfile
from .serializers import MessageModelSerializer,GetPrizeModelSerializer
from .models import Message,GetPrize
from ad.models import Prize
from MyQR import myqr
import os,random
from gg.settings import BASE_DIR

#……


class BuyUseJiFen(APIView):
    """购买积分商品,生成二维码,控制每个用户领取一次"""
    renderer_classes = [JSONRenderer]  # 渲染器

    def get(self,request):
        token = request.GET.get(\'token\')
        user = UserProfile.objects.filter(token=token).first()
        prizeid=request.GET.get(\'prizeid\')
        if user and prizeid:
            prizeid_int = int(prizeid)
            prize = Prize.objects.filter(id=prizeid_int).first()
            if prize.get_num>=prize.all_num:
                # 如果奖品的领取数大于等于发布数,则返回已售罄
                prize.is_out=True
                prize.save()
                return HttpResponse(\'403\')
            else:
                # 兑奖表,查看用户是否已经领取过了这个优惠券,如果已经领取过了则返回已经领取过了
                p=GetPrize.objects.filter(user=user,prize=prize).first()
                if p:
                    # 已经领取过了
                    return HttpResponse(\'404\')
                else:
                    # 用户未领取
                    # 扣除对应积分
                    if (user.jifen - prize.price)>=0:
                        user.jifen = user.jifen - prize.price
                        user.save()
                        # 制作二维码
                        two_name=str(user.id)+\'and\'+prizeid+\'.png\'#生成二维码文件名
                        b = os.path.join(BASE_DIR, \'static/2/\' + str(random.randint(1, 14)) + \'.jpg\')#背景图片路径
                        c_dir = os.path.join(BASE_DIR, \'media/twoimg\')#生成二维码的存放目录
                        user_openid=user.open_id
                        url=prize.url+"?pid="+prizeid+"&oid="+user_openid
                        myqr.run(words=url,version=5,level=\'H\',picture=b,colorized=True,save_dir=c_dir,save_name=two_name)
                        # 生成兑奖表记录
                        new_getprize=GetPrize()
                        new_getprize.user=user
                        new_getprize.prize=prize
                        new_getprize.title=prize.title
                        new_getprize.end_time=prize.end_time
                        new_getprize.twoimg=\'twoimg/\'+two_name
                        new_getprize.save()
                        # 奖品被领取数+1
                        prize.is_out = False
                        prize.get_num += 1
                        prize.save()
                        msg = Message()
                        msg.user = user
                        msg.content = "您成功领取了 奖品:" +prize.title
                        msg.save()
                        return HttpResponse(\'200\')
                    else:
                        # 积分不足
                        return HttpResponse(\'405\')
        else:
            return HttpResponse(\'参数不足\')

 

5.在apps/user_operation/urls.py内配置路由:

from django.urls import path
from .views import GetMessage,ReadMessage,DelMessage,BuyUseJiFen

urlpatterns = [
    path(\'getmessage/\',GetMessage.as_view()),
    path(\'readmessage/\',ReadMessage.as_view()),
    path(\'delmessage/\',DelMessage.as_view()),
    path(\'buyusejifen/\',BuyUseJiFen.as_view()),
]

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
精选的8个小程序源码发布时间:2022-07-18
下一篇:
微信小程序 关注公众号组件发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap