生成二维码
class QRlogin(ViewSet):
@action(methods=['GET'], detail=False)
def scan(self, request, *args, **kwargs):
img = qrcode.make("http://127.0.0.1:8000/api/v1/user/qrlogin/login/?user_id=1")
img = img.get_image()
b = BytesIO()
img.save(b, format='JPEG')
res = base64.b64encode(b.getvalue())
# return APIResponse(url='data:image/jpg;base64,'+res)
return APIResponse(url=res, user_id=1)
扫码登录,将缓存存入redis里面
@action(methods=['GET'], detail=False)
def login(self, request, *args, **kwargs):
user_id = request.GET.get('user_id')
user = User.objects.get(pk=user_id)
# 签发token,放到redis
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
user_info = {'username': user.username, 'token': token}
import time
time.sleep(5)
cache.set('user_id_%s' % user_id, user_info)
return APIResponse()
检查登录
@action(methods=['GET'], detail=False)
def check_login(self, request, *args, **kwargs):
user_id = request.GET.get('user_id')
user_info = cache.get('user_id_%s' % user_id)
if user_info:
cache.set('user_id_%s' % user_id, None)
return APIResponse(token=user_info.get('token'), username=user_info.get('username'))
else:
return APIResponse(code=101, msg='手机端尚未确认登录')
标签:info,return,get,token,生成,二维码,user,id
From: https://www.cnblogs.com/whxx/p/17571795.html