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

时间:2019-06-09 09:57:31   收藏:0   阅读:158

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

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=二维码)
    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:
                    # 用户未领取
                    # 制作二维码
                    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.twoimg=twoimg/+two_name
                    new_getprize.save()
                    # 扣除对应积分
                    user.jifen=user.jifen-prize.price
                    user.save()
                    # 奖品被领取数+1
                    prize.is_out = False
                    prize.get_num += 1
                    prize.save()
                    return HttpResponse(200)
        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
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!