zoukankan      html  css  js  c++  java
  • 07.微博绑定用户接口

    微博绑定用户接口

    1.oauth/urls.py中添加路由

    urlpatterns = [
        path('weibo/binduser/', views.OauthWeiboBindUser.as_view()),
    ]
    

    2.oauth/views.py中添加视图函数

    class OauthWeiboBindUser(APIView):
        permission_classes = (AllowAny, )
        
        def post(self, request):
            # 绑定用户, 两种情况1.已经注册的用户,2.未注册的用户
            # 1.获取用户名,密码,weibo_uid
            username = request.data.get('username')
            password = request.data.get('password')
            weibo_uid = request.data.get('weibo_uid')
            
            if not all([username, password, weibo_uid]):
                return Response({"code": 999, "msg": "参数不齐"})
            
            # 判断是否存在该用户
            try:
                user = User.objects.get(username=username)
            except Exception as e:
                user = None
            if user:
                # 如果存在表示是已注册用户,验证密码通过验证就绑定,返回token绑定成功
                if user.check_password(password):
                    ou = OauthUser(uid=weibo_uid, user=user, oauth_type="1")
                    ou.save()
                    payload = jwt_payload_handler(payload)  # 通过user对象获取到jwt的payload信息
                    token = jwt_encode_handler(payload)  # 生成token
                    data = jwt_response_payload_handler(token, user)
                    data["type"] = "0"  #指定未登录成功
                    return Response({"code": 0, "msg": "登录成功", "data": data})
            	else:
                	# 密码错误就返回{"code": 999, "msg": "密码错误"}
                    return Response({"code": 999, "msg": "密码错误"})
    		else:
                # 如果不存在,表示为未注册用户
                # 生成新用户,设置用户名,密码,然后绑定微博返回token
                user = User(username=username)
                user.set_password(password)
                user.save()
                ou = OauthUser(uid=weibo_uid, user=user, oauth_type="1")
                ou.save()
                payload = jwt_payload_handler(user)
                token = jwt_encode_handler(payload)
                data = jwt_response_payload_handler(token, user)
                data['type'] = "0"  # 指定为登录成功
                return Response({"code": 0, "msg": "登录成功", "data": data})
                
                
    
  • 相关阅读:
    计算机操作系统 存储器管理
    数据结构 平衡二叉树avl c++
    数据结构 线索二叉树 c++
    数据结构 赫夫曼树及其应用 c++
    c++ cstring 常用函数
    数据结构 哈希表 c++
    数据结构 静态链表
    ajax返回填充的数据不显示
    使用JSON.parse()转化成json对象需要注意的地方
    参数错误导致bug
  • 原文地址:https://www.cnblogs.com/hr20-04-19/p/13794198.html
Copyright © 2011-2022 走看看