def post(self, request):
        login_data = request.data
        username = login_data['username']
        password = login_data['password']

        if username and password:
            user = auth.authenticate(request,
                                     username=username,
                                     password=password)
            if user:
                # auth.login(request, user)
                # token = AEScrypt(str(user)).aes_encrypt()
                # print(request.user.id)
                # authToken.objects.filter(user_id=request.user.id).get_or_create(key=token, user_id=request.user.id)
                jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
                jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

                payload = jwt_payload_handler(user)
                token = jwt_encode_handler(payload)

                userinfo = UserInfo.objects.filter(username=user)
                data = model_to_dict(userinfo[0],
                                     exclude=(
                                         "password",
                                         'groups',
                                     ))
                data['token'] = token
                return response.APIResponseSuccess(msg="Login Success",
                                                   data=data)
            return response.APIResponseErrorParams(msg="user is not exist",
                                                   code=301)
        return response.APIResponseErrorParams(
            msg="username or password is None", code=300)
def Register(request):
    register_data = request.data
    username = register_data['username']
    zh_name = register_data['zh_name']
    password = register_data['password']
    phone = register_data['phone']
    email = register_data['email']

    # 判断是否为空:
    if (username or zh_name or password or phone or email) is None:
        return response.APIResponseErrorParams(msg="user info cannot be empty",
                                               code=305)

    user = UserInfo.objects.filter(
        Q(username=username) | Q(email=email) | Q(phone=phone)
        | Q(zh_name=zh_name)).first()
    if user:
        return response.APIResponseErrorParams(
            msg="user info has been already, please changing it", code=306)
    else:
        userinfo = UserInfo.objects.create_user(username=username,
                                                password=password,
                                                phone=phone,
                                                email=email,
                                                zh_name=zh_name,
                                                detail="cms")
        userinfo.save()
    return response.APIResponseSuccess(msg="user create success")
def Logout(request):
    token = request.META.get('HTTP_TOKEN')
    user = authToken.objects.filter(key=token)
    if user:
        user.delete()
        return response.APIResponseSuccess(msg="Logout success")
    return response.APIResponseErrorParams(msg="{} is not exist".format(user),
                                           code=309)
def updatePhone(request):
    data = request.data
    newphone = data['phonenumber']
    if newphone is not None and len(newphone) < 11:
        return response.APIResponseErrorParams(msg="Incorrectly phone number",
                                               code=311)
    else:
        user = request.user
        if user:
            user.phone = newphone
            user.save()
            return response.APIResponseSuccess(msg='success')
def AddPhone(request):
    data = request.data
    device_name = data['dname']
    os_version = data['osVersion']
    type = data['type']
    iemi = data['iemi']
    is_home = data['is_home']
    remarks = data['remarks']
    if (device_name or os_version or type or iemi or is_home) is None:
        return response.APIResponseErrorParams(msg='params cannot be empty',
                                               code=101)

    obj, created = PhoneManage.objects.filter(iemi=iemi).get_or_create(
        dname=device_name,
        os_version=os_version,
        type=type,
        iemi=iemi,
        is_home=is_home,
        remarks=remarks)
    return response.APIResponseSuccess(msg="Add phone success")
def RestPsw(request):
    data = request.data
    oldpsw = data['oldpassword']
    newpsw1 = data['newpassword1']
    newpsw2 = data['newpassword2']
    if (oldpsw or newpsw1 or newpsw2) is None:
        return response.APIResponseErrorParams(msg="password cannot empty",
                                               code=307)
    if newpsw1 != newpsw2:
        return response.APIResponseErrorParams(
            msg="The two passwords do not match", code=308)

    user = request.user
    if user.check_password(oldpsw):
        user.set_password(newpsw1)
        user.save()
        return response.APIResponseSuccess(
            msg="Password modified successfully")
    else:
        return response.APIResponseErrorParams(msg="Incorrectly old password",
                                               code=310)