• pickle结合redis,decode('utf-8','ignore')解码各种bytes ,支付宝沙盒测试,微信测试


    #视图函数层
    class Login(APIView):
    authentication_classes = []
    permission_classes = []
    parser_classes = [JSONParser]
    def post(self,request):
    response={'status':100,'msg':None}
    name=request.data.get('name')
    pwd=request.data.get('pwd')
    #去数据库校验该用户是否存在
    user=User.objects.filter(name=name,pwd=pwd).first()

    if user:

    #正常用户登录成功
    #返回一个唯一的随机字符串
    token=pickle.dumps(user.name)
    print(token,type(token))

    conn = get_redis_connection()
    conn.set('token', token)
    conn.expire('token', 40)
    # ensure_ascii = False
    #把生成的随机字符串存到数据库中
    # 这样不行,因为每次登录都会新插入一条
    # models.UserToken.objects.create(user=user,token=token)
    # 先去数据库中查询,如果当前用户存在记录,更新token,如果不存在,新增一条
    # 根据user取查询,如果查到数据,更新defaults中的数据,如果查不到,新增一条数据
    request.user=user
    #返回前台
    # ret=UserToken.objects.update_or_create(user=user,defaults={'token':token})
    response['msg']='登录成功'
    response['token']='zzf'
    else:
    #用户名或密码错误
    response['status'] = 101
    response['msg'] = '用户名或密码错误'
    return Response(response)

    class Books(ModelViewSet,):
    authentication_classes = [Myauthentication]
    queryset = Book.objects.all()
    serializer_class = MySerializer.BookSerializer

    class Authors(ViewSetMixin,APIView):
    authentication_classes = [Myauthentication]
    permission_classes=[]

    queryset = Book.objects.all()
    serializer_class = MySerializer.BookSerializer

    def aa(self,request):
    print(request.user.name)
    # print(request.META.get('token',None))
    author = Author.objects.filter().all()
    # page_pagination=PageNumberPagination()
    # page_pagination.page_query_param='pages'
    # page_pagination.page_size_query_param='max'
    # page_pagination.max_page_size=7

    # page_pagination = LimitOffsetPagination()
    # page_pagination.default_limit = 4
    # page_pagination.max_limit = 6
    # page_pagination.page_size=4

    page_pagination=CursorPagination()
    page_pagination.ordering='id'
    page_pagination.page_size=4
    # page_pagination.page_size_query_param=#不需要动
    response = {'statue': 1, 'msg': '获取成功'}
    ret=page_pagination.paginate_queryset(author,request,self)



    response['data']=MySerializer.AuthorSerializer(ret,many=True).data
    return page_pagination.get_paginated_response(response['data'])
    自定义认证,权限频率组件
    from app.models import *
    from rest_framework import exceptions
    from rest_framework.permissions import BasePermission
    from rest_framework.authentication import BaseAuthentication
    from rest_framework.throttling import BaseThrottle
    from django_redis import get_redis_connection
    import pickle
    class Myauthentication(BaseAuthentication):
    def authenticate(self,request):
    # print(request.user)
    token=request.GET.get('token')

    # print(token.encode('utf-8','ignore'))
    conn=get_redis_connection()
    ret=pickle.loads(conn.get('token',None))
    print(ret)
    print(conn.get('token'))
    user=User.objects.filter(name=ret).first()
    if ret==token:
    return user,token#源码将其返回后台,视图函数可以调用
    else:
    raise exceptions.AuthenticationFailed('未通过验证')

    # def authenticate_header(self, xx):
    # print(xx.GET)
    # pass

    class Mypermission(BasePermission):
    def has_permission(self, request, view):
    print(request.user)
    # user=User.objects.filter(name=request.POST.get('name')).first()
    if request.user.user_type!=0:
    return 'success'
    else:
    return False
    import time
    class Mythrottle(BaseThrottle):
    VISIT_RECORD = {}
    def __init__(self):
    self.history=None
    def allow_request(self, request, view):
    create_time=time.time()
    ip = request.META.get('REMOTE_ADDR')
    if ip not in self.VISIT_RECORD:
    self.VISIT_RECORD[ip]=[create_time,]
    return True
    self.history=self.VISIT_RECORD.get(ip)
    while create_time-self.history[-1]>60:
    self.history.pop()
    if len(self.history)<4:
    self.history.insert(0,create_time)
    return True
    else:
    return False
    def wait(self):
    create_time=time.time()
    return 60-create_time+self.history[0]


    支付宝沙盒测试

    1,配置前端页面可以向支付宝测试服务器发起请求,并提供对应的信息(根据蚂蚁金服开放平台配置 app_id等,注意支付宝公钥是应用钥匙配完后发出请求根据用户公钥得到)
    # 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info
    app_id = "2016092500594669"
    # 支付宝收到用户的支付,会向商户发两个请求,一个get请求,一个post请求
    # POST请求,用于最后的检测
    notify_url = "http://42.56.89.12:80/page2/"
    # GET请求,用于页面的跳转展示
    return_url = "http://42.56.89.12:80/page2/"
    # 用户私钥
    merchant_private_key_path = "keys/app_private_2048.txt"
    # 支付宝公钥
    alipay_public_key_path = "keys/alipay_public_2048.txt"
    # 生成一个AliPay的对象
    alipay = AliPay(
    appid=app_id,
    app_notify_url=notify_url,
    return_url=return_url,
    app_private_key_path=merchant_private_key_path,
    alipay_public_key_path=alipay_public_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥
    debug=True, # 默认False,
    )
    return alipay

    2,

    -2 调用对象的direct_pay()方法,传入商品描述,商品订单号,商品价格,返回一堆字符串,拼接到支付宝网关的后面
    -3 向支付宝网关发送get请求,看到支付页面,用户付款
    -4 付款成功,支付宝会有一个get回调,一个post回调(需要对回调内容进行验签,alipay.verify)
    -一般get回调用于显示页面
    -post回调用于修改订单状态,验签等























    -如何接入支付宝支付
    -商户号:需要跟支付宝申请,需要有公司的营业执照
    -开发人员在开发之前就拿到了
    -appid,开发用的,商户唯一的id(沙箱环境的appid:2016092000554611)
    -开发人员需要去支付宝官方文档按照步骤一步一步做(有人帮你封装好了)
    -沙箱环境(测试环境)
    -三种密钥(非对称加密)
    -应用私钥(商户私钥):不能外泄
    -应用公钥:可以给别人
    上面这两个是:用https://alipay.open.taobao.com/docs/doc.htm?treeId=291&articleId=105971&docType=1生成

    -支付宝公钥:在支付宝开发平台配置上应用公钥,自动生成的
    -在项目中配置两个密钥:
    -应用私钥
    -支付宝公钥(不是应用公钥)
    -支付宝支付流程****************************:
    -1 生成一个AliPay对象,传入,appid,get回调地址,post回调地址,商户私钥,支付宝公钥
    -商户公钥和私钥是支付宝提供的软件生成的
    -支付宝的公钥:在网站上通过商户公钥生成的
    -2 调用对象的direct_pay()方法,传入商品描述,商品订单号,商品价格,返回一堆字符串,拼接到支付宝网关的后面
    -3 向支付宝网关发送get请求,看到支付页面,用户付款
    -4 付款成功,支付宝会有一个get回调,一个post回调(需要对回调内容进行验签,alipay.verify)
    -一般get回调用于显示页面
    -post回调用于修改订单状态



    微信推送
    -推送
    -邮件推送
    -短信推送(花钱买短信接口)
    -微信推送
    -微信推送:
    -公众号(不能主动跟粉丝聊天)
    -未认证公众号
    -一天只能推送一条文章
    -已认证公众号
    -一天能推送多条
    -服务号(推送,前提是关注服务号)
    -注册服务号需要企业资质(营业执照)
    -主动推送消息
    -沙箱环境
    -企业号
    -企业里来用
    -企业中沟通的


    -所有的二维码其实都是一个url地址:
    -url地址可以转成二维码
    -二维码也能解析成url地址

    -微信推送的流程:
    -1 需要用户关注我的服务号(扫描一下二维码)
    -2 想让用户把微信唯一id添加到咱们的数据库中
    -2.1 用url(微信的)地址生成了一个二维码,让用户去扫描(包含回调地址,包含用户唯一id)
    -2.2 当用户扫描二维码,向微信服务器发送get请求,用户点确认,微信会回调到咱们的/callback/
    -2.3 回调该地址,只携带用户的唯一id回来,并没有用户微信id
    -2.4 向微信一个地址发送请求,去请求用户的微信id,请求回来
    -2.5 根据用户唯一id,去修改用户的微信id,完成后,咱们数据库就会用用户的微信id
    -3 发送消息
    -用用户微信id推送消息(普通消息和模板消息)
    -3.1 需要获取token,
    -3.2 调用发送消息接口,按照数据格式发送过去,就能给用户推送消息了


    WECHAT_CONFIG = {
    'app_id': 'wx20d174effe1375c3',
    'appsecret': 'e679b86b37688423556027f6c0aa4e88',
    'redirect_uri': 'http://42.56.89.12/callback/',
    }
    需要修改app_id 和 appsecret 

    'redirect_uri' 配置上线域名





  • 相关阅读:
    mysql mysqldump 本地数据库导入本地数据库的命令
    window mysql5.7 zip 安装
    MySQL存储过程详解 mysql 存储过程
    spring batch 读取多个文件数据导入数据库
    spring batch 以游标的方式 数据库读取数据 然后写入目标数据库
    不同浏览器上中文文件名的下载乱码问题
    spring mvc 文件下载 get请求解决中文乱码问题
    SpringMVC上传文件的三种方式
    NSPort
    iOS NSRunloop
  • 原文地址:https://www.cnblogs.com/wrqysrt/p/10656788.html
Copyright © 2020-2023  润新知