Beispiel #1
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="不存在该用户")
Beispiel #2
0
 def _insert_info_into_verify(self, mtool, email, vcode):
     # 1、先检查该邮箱是否已有一条验证数据,有则使之失效
     # 2、插入一条该邮箱的验证信息
     # 1、使之失效
     u_result = mtool.update_row(
         'UPDATE verify_email SET is_invalid=1 WHERE email=%s', [email])
     # 获取当前时间
     nowtime = get_date_time()
     # 2、插入一条验证信息
     i_result = mtool.insert_row(
         'INSERT verify_email '
         '(email, verify_key, ctime) VALUES'
         '(%s,    %s,         %s)', [email, vcode, nowtime])
    def login(self):
        # 1、从user_info表里拉取数据
        # 1.1、失败,返回并提示报错信息
        # 1.2、没有拉取到符合条件的用户、,返回并提示报错信息
        # 1.3、拉取正常,进入2
        # 2、查看该账号是否处于激活状态
        # 2.1、未激活,返回错误提示信息,并提示用户激活(走激活url)
        # 2.2、已激活,进入3
        # 3、此时说明账号密码正确,生成token,将用户信息存到token里,返回token给用户
        email = self.email
        # 密码加密
        tool = Md5Tool()
        pw = tool.get_md5(self.password)
        with MySQLTool(host=mysql_config['host'],
                       user=mysql_config['user'],
                       password=mysql_config['pw'],
                       port=mysql_config['port'],
                       database=mysql_config['database']) as mtool:
            select_result = mtool.run_sql([[
                'SELECT * FROM user_info WHERE email = %s and pw = %s',
                [email, pw]
            ]])

            if select_result is False:
                return get_res(code=0, msg='服务器错误')

            if len(select_result) is 0:
                return get_res(code=0, msg='用户名/密码错误或用户不存在')

            user_info = {
                'id': select_result[0][0],
                'email': select_result[0][1],
                'permission': select_result[0][4],
                'status': select_result[0][5],
            }

            if user_info['permission'] is 0:
                return get_res(code=1, msg='邮箱未激活')

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

            # 更新最后登录时间
            mtool.update_row(
                'UPDATE user_info SET lastlogin_time = %s WHERE id = %',
                [nowtime, user_info['id']])

            # 生成token,返回给用户
            user_info['token'] = self.make_token()
            return get_res(code=200, data=user_info)
Beispiel #4
0
    def verify_email(self):
        email = self.email
        vcode = self.vcode
        # 然后去数据库找符合的数据
        # 连接数据库
        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 * FROM verify_email WHERE email = %s and verify_key = %s and is_pass = 0',
                [email, vcode]
            ]])

            # 如果查找失败,或者查不到符合的信息
            if result is False:
                return get_res(
                    code=0,
                    msg='激活因未知原因失败,请重试或者联系管理员。QQ:20004604,微信:qq20004604')
            if result is False or len(result) <= 0:
                return get_res(
                    code=0, msg='验证信息不存在,请重试或者联系管理员。QQ:20004604,微信:qq20004604')

            # 查到符合的信息,则更新邮箱验证表,设置该行通过
            # 获取当前时间
            nowtime = get_date_time()
            affect_verify_email_rows = mtool.update_row(
                'UPDATE verify_email SET is_pass = 1, is_invalid = 1, last_vtime = %s WHERE email = %s and verify_key = %s',
                [nowtime, email, vcode])
            if affect_verify_email_rows is False or affect_verify_email_rows is 0:
                mtool.set_uncommit()
                return get_res(
                    code=0, msg='激活失败(0),请重试或者联系管理员。QQ:20004604,微信:qq20004604')

            # 再修改用户表,设置账号状态为启用
            affect_user_info_rows = mtool.update_row(
                'UPDATE user_info SET permission = 1 WHERE email = %s',
                [email])
            if affect_user_info_rows is False or affect_user_info_rows is 0:
                mtool.set_uncommit()
                return get_res(
                    code=0, msg='激活失败(1),请重试或者联系管理员。QQ:20004604,微信:qq20004604')

            return get_res(code=200, msg='激活成功')
Beispiel #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': '用户注册成功,已发送激活邮件,请访问邮箱打开激活邮件以激活账号'})
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',
            })