后台的login功能实现
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.cache import cache
from app01 import models
import hashlib,time
from app01.wx import wx_Login
class Login(APIView):
def post(self,request):
param = request.data
if param.get("code"):
data = wx_Login.login(param.get('code'))
if data:
val = data['openid']+"&"+data["session_key"]
key = str(int(time.time())+data['openid'])
# 加密验证签发
md5 = hashlib.md5()
md5.update(key.encode("utf-8"))
key = md5.hexdigest()
cache.set(key,val)
has_user = models.Wxuser.objects.filter(openid=data['openid']).first()
if not has_user:
models.Wxuser.objects.create(openid=data['openid'])
return Response({"code":200,"msg":"ok","data":{"login_key":key}})
else:
return Response({"code":200,"msg":"code错误"})
else:
return Response({"code":200,"msg":"缺少参数"})
前台调用认证登录
商城表的设计
# Create your models here.
class Wxuser(models.Model):
id = models.AutoField(primary_key=True)
openid=models.CharField(max_length=255)
name = models.CharField(max_length=50)
avatar = models.CharField(max_length=200)
language = models.CharField(max_length=50)
province = models.CharField(max_length=50)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
gender = models.CharField(max_length=50),
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __str__(self):
return self.openid
class Category(models.Model):
cat_id=models.AutoField(primary_key=True)
category_name=models.CharField(max_length=50)
parent=models.ForeignKey(to='Category', to_field='cat_id', related_name="Category", on_delete=models.CASCADE, db_constraint=False,blank=True,null=True)
p_order=models.IntegerField(default=0)
is_show =models.BooleanField(default=1)
image = models.OneToOneField(to='Images', to_field='image_id', on_delete=models.CASCADE, db_constraint=False,null=True)
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __str__(self):
return self.category_name
class Images(models.Model):
image_id=models.AutoField(primary_key=True)
name=models.CharField(max_length=30,default="0")
image_url=models.ImageField(upload_to="")
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Product(models.Model):
product_id=models.AutoField(primary_key=True)
name=models.CharField(max_length=200)
price=models.DecimalField(max_digits=10, decimal_places=2)
weight=models.IntegerField(default=0)
cat = models.ForeignKey(to="Category", to_field="cat_id", related_name="Product", db_constraint=False, on_delete=models.CASCADE)
intor = models.TextField(max_length=250)#详细介绍
brief = models.TextField(max_length=250)#商品简介
image=models.OneToOneField(to='Images',to_field='image_id',on_delete=models.CASCADE,db_constraint=False)
stock = models.OneToOneField(to="Stock", to_field="stock_id", db_constraint=False, on_delete=models.CASCADE)
buy_count=models.IntegerField(default=0)#购买量
disabled = models.BooleanField(default=1)#是否显示
w_order=models.IntegerField(default=0)#权重
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Order(models.Model):
order_id = models.CharField(max_length=50, unique=True, primary_key=True)
status_choices = (("active", '活动订单'), ("dead", '作废订单'), ("finish", '已完成订单'))
status = models.CharField(choices=status_choices, default="active", max_length=50)
pay_status_choices = ((0, '未付款'), (1, '已付款'))
pay_status = models.SmallIntegerField(choices=pay_status_choices, default=0)
payed = models.DecimalField(max_digits=10, decimal_places=2,default=0)
order_total = models.DecimalField(max_digits=10, decimal_places=2,default=0)
ship_status_choices = ((0, '未发货'), (1, '已发货'))
pay_app = models.CharField(max_length=100)
wxuser = models.ForeignKey(to="Wxuser", to_field="id", related_name="Order", db_constraint=False,on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
memo = models.CharField(max_length=200, default=0)
consignee_name = models.CharField(max_length=200, default=0)
consignee_area = models.CharField(max_length=200, default=0)
consignee_address = models.CharField(max_length=200, default=0)
consignee_zip = models.CharField(max_length=200, default=0)
consignee_mobile = models.CharField(max_length=200,default=0)
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __str__(self):
return self.order_id
class Order_items(models.Model):
item_id = models.AutoField(primary_key=True)
order= models.ForeignKey(to="Order", to_field="order_id", related_name="Order_items", db_constraint=False,on_delete=models.CASCADE)
product=models.ForeignKey(to="Product", to_field="product_id", related_name="Order_items", db_constraint=False,on_delete=models.CASCADE,null=True)
name = models.CharField(max_length=200)
image = models.ForeignKey(to='Images', to_field='image_id',related_name="Order_items", on_delete=models.CASCADE,db_constraint=False)
price = models.DecimalField(max_digits=10, decimal_places=2,default=0)
amount=models.DecimalField(max_digits=10, decimal_places=2,default=0)
nums=models.IntegerField()
send_nums=models.IntegerField(null=True)
brief=models.CharField(max_length=200)
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
class Stock(models.Model):
stock_id=models.AutoField(primary_key=True)
name=models.CharField(max_length=100)
quantity=models.IntegerField(default=0)#库存量
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Banner(models.Model):
product = models.OneToOneField(to="Product", to_field="product_id", db_constraint=False, on_delete=models.CASCADE)
w_order = models.IntegerField(default=0) # 权重
image = models.OneToOneField(to='Images', to_field='image_id', on_delete=models.CASCADE, db_constraint=False)
is_show =models.BooleanField(default=1)
creat_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __str__(self):
轮播图的实现
后端
from rest_framework.views import APIView
from rest_framework.response import Response
from app01 import models
from app01.my_ser import Banner_ser
class List(APIView):
def post(self,request):
data = models.Banner.objects.filter(is_show=True).order_by("-w_order")
data = Banner_ser.Banner_ser(instance=data,many=True,context={"request":request}).data
return Response({
"code":200,
"msg":"ok",
"data":data
})
序列化类
from rest_framework import serializers
from app01 import models
class Banner_ser(serializers.ModelSerializer):
image_url = serializers.ImageField(source="image.image_url")
product_id =serializers.IntegerField(source="product.product_id")
class Meta:
models = models.Banner
fields = "__all__"
热销产品的推荐
from rest_framework.views import APIView
from rest_framework.response import Response
from app01.my_ser import Goods_ser
from app01 import models
class HotGoods(APIView):
def post(self,request):
data = models.Product.objects.filter(disabled=True).order_by("-buy_count","-w_order")
data = Goods_ser.Goods_ser(instance=data,many=True,context={"request":request}).data
return Response({"code":200,"msg":"ok","data":data})
序列化
from rest_framework import serializers
from app01 import models
class Goods_ser(serializers.ModelSerializer):
image_url=serializers.ImageField(source="image.image_url")
class Meta:
model=models.Product
fields="__all__"