示例#1
0
def logout(request):
    sm = SessionManage(request.session)
    try:
        sm.logout()
        return get_res_json(
            data={
                'redirecturl':
                '/' if not IS_ON_WEBPACK_DEVELOPMENT else '/login.html',
                'msg': '登出成功'
            })
    except BaseException as e:
        print(e)
        return get_res_json(code=0, msg="登出失败")
示例#2
0
    def load_data(self):
        data = None
        # 取出数据
        if len(self.request.body) is 0:
            return {
                'is_pass': False,
                'res': get_res_json(code=0, msg='需要【邮箱】、【密码】')
            }
        try:
            data = json.loads(self.request.body)
        except BaseException as e:
            return {'is_pass': False, 'res': get_res_json(code=0, msg='数据非法')}

        return {'is_pass': True, 'res': data}
示例#3
0
 def _verify(self, data):
     uf = RegisterForm(data)
     # 验证不通过,返回错误信息
     if not uf.is_valid():
         msg = uf.get_form_error_msg()
         return {'is_pass': False, 'res': get_res_json(code=0, msg=msg)}
     return {'is_pass': True}
示例#4
0
def get_redirect_login_page_response(request):
    # 末尾是html
    if len(re.findall('html$', request.path)) > 0:
        return HttpResponseRedirect('/' if not IS_ON_WEBPACK_DEVELOPMENT else '/login.html')
    else:
        # 一般的异步请求,封装code 302
        return get_res_json(code=302, msg="请登录", data={
            'redirecturl': '/login' if not IS_ON_WEBPACK_DEVELOPMENT else '/login.html'
        })
示例#5
0
    def _save_into_mysql(self, email, md5pw, phone):
        vcode = None
        # 连接数据库
        with MySQLTool(host=mysql_config['host'],
                       user=mysql_config['user'],
                       password=mysql_config['pw'],
                       port=mysql_config['port'],
                       database=mysql_config['database']) as mtool:
            # 查看有没有同名的用户
            result = mtool.run_sql(
                [['select (email) from user_info where email = %s', [email]]])
            # 打印结果e
            print(result)
            # 判定密码是否相等
            if len(result) > 0:
                return get_res_json(code=0, msg="该邮箱已注册,请更换邮箱")

            # 获取当前时间
            nowtime = get_date_time()

            # 插入
            row_id = mtool.insert_row(
                'INSERT user_info'
                '(id, email, pw, phone, permission, status, create_time, lastlogin_time) VALUES'
                '(%s, %s,   %s,  %s,    0,          0,      %s,          %s)',
                [None, email, md5pw, phone, nowtime, nowtime])

            if row_id is False:
                return get_res_json(code=0, msg='注册失败')

            vcode = self._get_verify_code()
            self._insert_info_into_verify(mtool, email, vcode)

        # 发送激活邮件给用户
        send_result = self.send_verify_email(email, vcode)

        # 发送失败——》返回错误信息
        if send_result.code is not 200:
            return get_res_json(code=200, data={'msg': send_result.msg})

        # 此时跳转到邮件发送提示页面,提示用户点击邮箱里的链接进行验证
        return get_res_json(code=200,
                            data={'msg': '用户注册成功,已发送激活邮件,请访问邮箱打开激活邮件以激活账号'})
示例#6
0
def register(request):
    if request.method != 'POST':
        return get_res_json(code=0, msg="请通过POST请求来进行查询")

    rm = RegisterManager(request)
    data = rm.load_data()
    if data['is_pass'] is False:
        return data['res']
    result = rm.register(data['res'])
    return result
    def load_data(self, request):
        # 取出数据
        if len(request.body) is 0:
            return {
                'is_pass': False,
                'res': get_res_json(code=0, msg='需要【邮箱】、【密码】')
            }
        try:
            data = json.loads(request.body)
        except BaseException as e:
            return {
                'is_pass': False,
                'res': get_res_json(code=0, msg='登录数据非法')
            }

        verify_result = self._verify(data)

        # 如果验证通过
        if verify_result['is_pass'] is True:
            self.email = data.get('email')
            self.password = data.get('password')
            return {'is_pass': True}
        else:
            return verify_result
示例#8
0
def login(request):
    # 获取当前时间
    nowtime = get_date_time()
    print(nowtime)

    if request.method != 'POST':
        return get_res_json(code=0, msg="请通过POST请求来进行登陆")

    data = json.loads(request.body)
    uf = UserForm(data)
    # 验证不通过,返回错误信息
    if not uf.is_valid():
        msg = uf.get_form_error_msg()
        return get_res_json(code=0, msg=msg)

    username = data.get('username', '')
    password = data.get('password', '')
    print(username, password)
    tool = Md5Tool()
    md5pw = tool.get_md5(password)
    print(md5pw)
    # 连接数据库
    with MySQLTool(host=mysql_config['host'],
                   user=mysql_config['user'],
                   password=mysql_config['pw'],
                   database=mysql_config['database']) as mtool:
        # 执行sql并获得返回结果
        result = mtool.run_sql(
            [['select * from developer_info where name = %s', [username]]])
        # 打印结果e
        print(result)
        # 判定密码是否相等
        if len(result) > 0:
            if md5pw == result[0][2]:
                # 再判定该用户状态是否正常
                if result[0][4] != 0:
                    return get_res_json(code=0, msg="该用户禁止登陆")

                sm = SessionManage(request.session)
                sm.set_login(result[0])
                # 更新登陆时间
                mtool.update_row(
                    'UPDATE developer_info SET lastlogin_time = %s WHERE name = %s',
                    [nowtime, username])
                return get_res_json(
                    data={
                        'redirecturl':
                        '/home'
                        if not IS_ON_WEBPACK_DEVELOPMENT else '/home.html',
                        'msg':
                        '登陆成功'
                    })
            else:
                return get_res_json(code=0, msg="密码错误")
        else:
            return get_res_json(code=0, msg="不存在该用户")
示例#9
0
def login(request):
    if request.method != 'POST':
        return get_res_json(code=0, msg="请通过POST请求来进行查询")

    lm = LoginManager()
    # 先读取数据,读取失败返回提示信息
    load_result = lm.load_data(request)
    if load_result['is_pass'] is False:
        login_log(lm.email, -1)
        return load_result['res']

    # 然后执行登录的逻辑,查看是否登录成功
    login_result = lm.login()
    # code不是200说明失败,返回报错信息
    # code = 0 返回默认报错信息
    if login_result['code'] is 0:
        login_log(lm.email, 0)
        return get_res_json(code=0, msg=login_result['msg'])

    # code = 1 表示 邮箱未激活,提示用户去激活邮箱
    if login_result['code'] is 1:
        # todo 这里跳转的页面应该不一样
        login_log(lm.email, 1)
        return get_res_json(code=0, msg=login_result['msg'])

    # code = 200 表示正常
    if login_result['code'] is 200:
        user_info_data = login_result['data']
        # todo 这里添加token到session里
        # todo 测试时,返回默认提示成功数据
        login_log(lm.email, 200)
        return get_res_json(code=200, msg=login_result['msg'])

    # 理论上不应该执行到这里,如果执行到这里,提示错误
    login_log(lm.email, 2)
    return get_res_json(code=2, msg="服务器错误")
def register(request):
    # 获取当前时间
    nowtime = get_date_time()
    print(nowtime)

    if request.method != 'POST':
        return get_res_json(code=0, msg="请通过POST请求来进行登陆")

    data = json.loads(request.body)
    uf = UserForm(data)
    # 验证不通过,返回错误信息
    if not uf.is_valid():
        msg = uf.get_form_error_msg()
        return get_res_json(code=0, msg=msg)

    username = data.get('username')
    password = data.get('password')
    email = data.get('email', '')
    print(username, password, email)

    tool = Md5Tool()
    md5pw = tool.get_md5(password)

    print(md5pw)

    # 连接数据库
    with MySQLTool(host=mysql_config['host'],
                   user=mysql_config['user'],
                   password=mysql_config['pw'],
                   database=mysql_config['database']) as mtool:
        # 查看有没有同名的用户
        result = mtool.run_sql(
            [['select (name) from developer_info where name = %s',
              [username]]])
        # 打印结果e
        print(result)
        # 判定密码是否相等
        if len(result) > 0:
            return get_res_json(code=0, msg="该用户名已注册,请更换用户名")

        # 插入
        row_id = mtool.insert_row(
            'INSERT developer_info'
            '(id, name, pw, permission, status, create_time, lastlogin_time, email) VALUES'
            '(%s, %s,   %s, 3,          0,      %s,          %s,             %s)',
            [None, username, md5pw, nowtime, nowtime, email])

        if row_id is False:
            return get_res_json(code=0, msg='注册失败')

        sm = SessionManage(request.session)
        sm.set_login([row_id, username, '', 3, 0, nowtime, nowtime, email])

        return get_res_json(
            code=200,
            data={
                'msg':
                '用户注册成功,正在跳转中...',
                'redirecturl':
                '/home' if not IS_ON_WEBPACK_DEVELOPMENT else '/home.html',
            })