标签:bind ret exce 成功 三方 ram lang none 绑定
oauth/urls.py
中添加路由urlpatterns = [
path(‘weibo/binduser/‘, WeiboUser.as_view()), # /oauth/weibo/callback/ ]
oauth/views.py
中添加试图函数from rest_framework.views import APIView
from rest_framework.response import Response
from oauth.models import *
from django.contrib.auth.hashers import make_password
# 微博第三方绑定
class WeiboUser(APIView):
def post(self, request):
oauth_type = 1
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": 4005,
"msg": "参数不全"
})
# 判断 username 是否存在
try:
user = User.objects.get(username=username)
oauthinfo = OauthUser.objects.create(
uid=weibo_uid,
oauth_type=oauth_type,
user=user
)
data = {
"authenticated": True,
"id": user.id,
"role": None,
"name": user.nick_name,
"username": username,
"email": user.email,
"token": create_token(user),
"type": 0
}
res_data = {
"code": 1000,
"msg": "登陆成功",
"data": data
}
return Response(res_data)
except Exception as e:
password = make_password(password)
user = User.objects.create(username=username,password=password)
oauthinfo = OauthUser.objects.create(
uid=weibo_uid,
oauth_type=oauth_type,
user=user
)
data = {
"authenticated": True,
"id": user.id,
"role": None,
"name": user.nick_name,
"username": username,
"email": user.email,
"token": create_token(user),
"type": 0
}
res_data = {
"code": 1000,
"msg": "登陆成功",
"data": data
}
return Response(res_data)
标签:bind ret exce 成功 三方 ram lang none 绑定
原文地址:https://www.cnblogs.com/chao460/p/13934741.html