微信小程序商城

一.目录机构

二 .表设计

from django.db import models

# Create your models here.
'''
#banner
id  
image_url
product_id
is_show
w_order
create_time
update_time


#销量最高的排前面,销量一样,权重大的在前面
#product_id
id 
name
price
intor
buy_count
w_order (越大越前)
detail
image_url
scort #库存
is_show
catory_id
create_time
update_time

#scort库存
id
name
num
create_time
update_time

#category_id
id
cate_name
image_url
parent_id
create_time
update_time

#imgae
id
imgae_url
name
create_time
update_time

#订单
order_id
商品总数量
总价格
收货地址
用户id
订单状态(是否取消)
收货人电话
收人姓名
支付
create_time
update_time


order_item
id
order_id
商品id
商品价格
num
image_url
create_time
update_time

'''



# 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):
        return self.product.name
数据库表

三 .登录接口

1.后端

 user.py

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": "缺少参数"})
user.py

wx文件夹

1.settings.py

AppId=".."

AppSecret="..."

code2Session="https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code"
pay_mchid ='..'
pay_apikey = '..'

2.wx_login.py
import requests
from app01.wx import settings

def login(code):
    code_url = settings.code2Session.format(settings.AppId, settings.AppSecret, code)
    response = requests.get(code_url)
    json_response = response.json()
    if json_response.get('session_key'):
        return json_response
    else:
        return False
wx文件夹

2.前端

 三.轮播图接口

1.前端

2.后端

bananer_ser.py

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:
        model=models.Banner
        fields="__all__"
序列化

banner.py

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
        })
接口

四 热销商品接口

1.前端

2.后端

1.接口
from rest_framework.views import APIView
from rest_framework.response import  Response
from app01.my_ser import Goods_ser
from  app01 import models
from app01.my_ser import Category_ser,Goods_ser
from app01.comment import func
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})

2.序列化
from  rest_framework import serializers
from app01 import  models
class Goods_ser(serializers.ModelSerializer):
    image_url=serializers.ImageField(source="image.image_url")
    stock=serializers.IntegerField(source="stock.quantity")
    class Meta:
        model=models.Product
        fields="__all__"
后端代码

 

posted @ 2020-03-29 22:29  心慌得初夏  阅读(590)  评论(0编辑  收藏  举报
levels of contents