Пример #1
0
 def post(self, request):
     '''
     后台登录接口
     '''
     try:
         serializer = self.get_serializer(data=request.data)
         if not serializer.is_valid():
             return Response({
                 "message": str(serializer.errors),
                 "errorCode": 2,
                 "data": {}
             })
         data = (serializer.data)
         username = data.get('username')
         password = data.get('password')
         user = User.objects.filter(
             Q(username=username) | Q(mobile=username)
             | Q(email=username)).first()
         if not user:
             return Response({
                 "message": "用户不存在",
                 "errorCode": 2,
                 "data": {}
             })
         if user.group.group_type in ['NormalUser']:
             return Response({
                 "message": "非法登录,不是后台用户!",
                 "errorCode": 2,
                 "data": {}
             })
         if user.is_freeze in ['1', 1]:
             return Response({
                 "message": "账号被冻结,无法登录。",
                 "errorCode": 2,
                 "data": {}
             })
         if user.password == password:
             token_data = jwt_response_payload_handler(
                 jwt_encode_handler(payload=jwt_payload_handler(user)),
                 user, request)
             user.update_time = datetime.datetime.now()
             user.save()
             return Response({
                 "message": "登录成功",
                 "errorCode": 0,
                 "data": token_data
             })
         else:
             return Response({
                 "message": "密码错误",
                 "errorCode": 2,
                 "data": {}
             })
     except Exception as e:
         print('发生错误:', e)
         return Response({
             "message": "出现了无法预料的view视图错误:%s" % e,
             "errorCode": 1,
             "data": {}
         })
Пример #2
0
 def post(self, request):
     '''
     微信APP第三方登录接口
     '''
     try:
         serializer = self.get_serializer(data=request.data)
         if not serializer.is_valid():
             return Response({"message": str(serializer.errors), "errorCode": 2, "data": {}})
         # print(serializer.data)
         code = serializer.data.get('code')
         user_info = wechat_app_login(code)
         if user_info.get('errcode') and user_info.get('errcode') != 0:
             return Response(user_info)
         # 根据 unionid 查找用户
         user = User.objects.filter(union_id=user_info.get('unionid')).first()
         is_login = 0
         if not user:
             is_login = 1
             user = User()
             user.group_id = 3
             user.union_id = user_info.get('unionid')
             user.nick_name = user_info.get('nickname')
             user.avatar_url = user_info.get('headimgurl')
             user.gender = user_info.get('sex')
             user.region = ' '.join([str(item) for item in [user_info.get('country'), user_info.get('province'), user_info.get('city')] if item != None])
             user.save()
         token_data = jwt_response_payload_handler(jwt_encode_handler(jwt_payload_handler(user)), user, request)
         token_data['is_login'] = is_login
         user.update_time = datetime.datetime.now()
         user.save()
         return Response({"message": "登录成功", "errorCode": 0, "data": token_data})
     except Exception as e:
         print('发生错误:',e)
         return Response({"message": "出现了无法预料的view视图错误:%s" % e, "errorCode": 1, "data": {}})
Пример #3
0
 def post(self, request):
     '''
     后台登录接口
     '''
     try:
         serializer = self.get_serializer(data=request.data)
         if not serializer.is_valid():
             return Response({
                 "message": str(serializer.errors),
                 "errorCode": 4,
                 "data": {}
             })
         data = (serializer.data)
         username = data.get('username')
         password = data.get('password')
         user = User.objects.filter(
             Q(username=username) | Q(phone=username)
             | Q(email=username)).first()
         if not user:
             return Response({
                 "message": "用户不存在",
                 "errorCode": 2,
                 "data": {}
             })
         if user.status == '0':
             return Response({
                 "message": "账号被冻结,无法登录。",
                 "errorCode": 2,
                 "data": {}
             })
         if user.password == password:
             payload = jwt_payload_handler(user)
             token = jwt_encode_handler(payload)
             data = jwt_response_payload_handler(token, user, request)
             user.update_time = datetime.datetime.now()
             user.save()
             return Response({
                 "message": "登录成功",
                 "errorCode": 0,
                 "data": data
             })
         else:
             return Response({
                 "message": "密码错误",
                 "errorCode": 1,
                 "data": {}
             })
     except Exception as e:
         print('发生错误:', e)
         return Response({
             "message": "出现了无法预料的view视图错误:%s" % e,
             "errorCode": 1,
             "data": {}
         })
Пример #4
0
    def post(self, request, *args, **kwargs):
        '''
        登录
        '''
        request_data = kwargs.get('data')
        username = request_data.get('username')
        password = request_data.get('password')

        obj_user = UserModel.objects.filter(Q(username=username) | Q(mobile=username) | Q(email=username)).first()
        if not obj_user:
            raise CustomerError(errno=1002, errmsg='用户不存在')

        if obj_user.password == password:
            token_data = jwt_response_payload_handler(jwt_encode_handler(payload=jwt_payload_handler(obj_user)), obj_user, request)
            obj_user.last_login = datetime.datetime.now()
            obj_user.save()
            return token_data
        else:
            raise CustomerError(errno=1002, errmsg='密码错误')
Пример #5
0
 def post(self, request):
     '''
     手机号快速登录接口
     '''
     try:
         json_data = {"message": "ok", "errorCode": 0, "data": {}}
         serializer = self.get_serializer(data=request.data)
         if not serializer.is_valid():
             return Response({"message": str(serializer.errors), "errorCode": 4, "data": {}})
         mobile = serializer.data.get('mobile')
         code = serializer.data.get('code')
         is_login = 1
         # 搜索用户
         user_obj = User.objects.filter(mobile=mobile).first()
         # 搜索缓存
         need_value = cache.get(mobile)
         if not need_value:
             return Response({"message": "验证码未找到,请重新发送后重试。", "errorCode": 2, "data": {}})
         if need_value != code:
             return Response({"message": "验证码错误。", "errorCode": 2, "data": {}})
         if not user_obj:
             is_login = 0
             # 1
             # user_obj = User()
             # user_obj.group_id = 3
             # user_obj.nick_name = mobile + '手机用户'
             # user_obj.save()
             # 2
             user_obj = User(group_id=3, nick_name=mobile + '手机用户', mobile=mobile)
             user_obj.save()
         token_data = jwt_response_payload_handler(jwt_encode_handler(jwt_payload_handler(user_obj)), user_obj, request)
         user_obj.update_time = datetime.datetime.now()
         user_obj.save()
         # 清除已经使用的验证码 防止验证码被盗用
         cache.delete(mobile)
         return Response({"message": "登录成功", "errorCode": 0, "data": {'token': token_data.get('token'), 'is_login': is_login}})
     except Exception as e:
         print('发生错误:',e)
         return Response({"message": "出现了无法预料的view视图错误:%s" % e, "errorCode": 1, "data": {}})
Пример #6
0
 def post(self, request):
     '''
     微信小程序登录接口
     '''
     try:
         serializer = self.get_serializer(data=request.data)
         if not serializer.is_valid():
             return Response({
                 "message": str(serializer.errors),
                 "errorCode": 2,
                 "data": {}
             })
         print(serializer.data)
         code = serializer.data.get('code')
         userInfo = serializer.data.get('userInfo')
         encrypted_data = serializer.data.get('encrypted_data')
         iv = serializer.data.get('iv')
         # print(userInfo.get('avatarUrl'))
         # 调用微信登录获取openid
         open_id, union_id, session_key = wechat_mini_login(code)
         if type(open_id) == dict:
             return Response(open_id)
         # 测试绕过微信登录
         # open_id = 'asdfasdf21341cdfq345sderffggfwe45'
         # 根据openid查找用户
         we_user = User.objects.filter(open_id=open_id).first()
         if not union_id:
             pc_obj = WXBizDataCrypt(settings.WECHAT_MINI_APPID,
                                     session_key)
             union_id = pc_obj.decrypt(encrypted_data, iv).get('unionId')
         app_user = User.objects.filter(union_id=union_id).first()
         is_login = 1
         print('查看user', user)
         if we_user and app_user:
             if not we_user.union_id:
                 we_user.union_id = union_id
                 we_user.save()
             user = we_user
         elif we_user and not app_user:
             if not we_user.union_id:
                 we_user.union_id = union_id
                 we_user.save()
             user = we_user
         elif app_user and not we_user:
             if not app_user.open_id:
                 app_user.open_id = open_id
                 app_user.save()
             user = app_user
         else:
             is_login = 0
             user = User()
             user.group_id = 3
             user.open_id = open_id
             user.union_id = union_id
             user.nick_name = userInfo.get('nickName')
             user.avatar_url = userInfo.get('avatarUrl')
             user.gender = userInfo.get('gender')
             user.region = ' '.join([
                 str(item) for item in [
                     userInfo.get('country'),
                     userInfo.get('province'),
                     userInfo.get('city')
                 ] if item != None
             ])
             user.save()
         token_data = jwt_response_payload_handler(
             jwt_encode_handler(payload=jwt_payload_handler(user)), user,
             request)
         token_data['is_login'] = is_login
         user.update_time = datetime.datetime.now()
         user.save()
         return Response({
             "message": "登录成功",
             "errorCode": 0,
             "data": token_data
         })
     except Exception as e:
         print('发生错误:', e)
         return Response({
             "message": "出现了无法预料的view视图错误:%s" % e,
             "errorCode": 1,
             "data": {}
         })