微博绑定用户接口(我们的约定还算数嘛。)
再就是微博绑定用,也就是三方登录账号绑定本地账号
1.微博绑定用户接口
1.1oauth/urls.py
中添加路由
class OauthWeiboBindUser(APIView):
permission_classes = (AllowAny,)
def post(self,request):
# 绑定用户,1.已注册用户,2.未注册用户
#1.1获取用户名,密码,weibo_uid
username = request.data.get('username')
password = request.data.get('password')
weibo_uid = request.data.get('weibo_uid')
if noet all([username,password,weibo_uid]):
return Response({'code':999,'msg':'参数不全'})
#判断用户是否存在此用户
try:
user = User.objects.get(username = username)
except Exception as e :
user = None
#1.已注册用户
if user:
# 1.2 如果存在就验证密码,验证通过,就绑定,返回token,登录成功
if user.check_password(password):
ou = OauthUser(uid =weibo_uid,user=user,oauth_type = '1')
ou.save()
payload = jwt_payload_handler(user) # 通过user对象获取到jwt的payload信息
token = jwt_encode_handler(payload) # 生成token
data = jwt_response_payload_handler(token,user)
data['type'] = '0' # 指定为登录成功
return Respone({'code':0,'msg':'登陆成功','data':data})
else:
return Response({'code':999,'msg':'密码错误'})
else:
# 2.未注册用户
#2.1 生成新用户,设置用户名密码,保存,然后绑定,返回token,登陆成功
user = User(username=username)
user.set_password(password)
user.save()
ou = OauthUser(uid=weibo_uid,user=user,oauth_type='1')
ou.save()
ou = OauthUser(uid=weibo_uid,user=user,oauth_type='1')
ou.save()
payload =jwt_payload_handler(user)
token = jwt_payload_handler(payload)
data = jwt_response_payload_handler(token,user)
data['type'] = '0' #指定为登陆成功
return Response({'code':0,'msg':'登录成功','data':data})