Exemple #1
0
def register():
    if session.get('user_info') is not None:
        return redirect(url_for('dashboard'))

    err_msg = None
    if session.get('error_message') is not None:
        err_msg = session.get('error_message')
        session['error_message'] = None

    info_msg = None
    if session.get('info_message') is not None:
        info_msg = session.get('info_message')
        session['info_message'] = None

    invitation_code = ''
    if request.values.get('inv_code') is not None and len(
            request.values.get('inv_code')) > 0:
        invitation_code = request.values.get('inv_code')
        if not r_session.sismember('invitation_codes', invitation_code) and \
                not r_session.sismember('public_invitation_codes', invitation_code):
            session['error_message'] = '无效的邀请码。'

    return render_template('register.html',
                           err_msg=err_msg,
                           info_msg=info_msg,
                           invitation_code=invitation_code)
Exemple #2
0
def generate_recharge_cards():
    name = request.values.get('name')
    points = request.values.get('points')
    num = request.values.get('num')
    try:
        if int(num) <= 0:
            session['error_message'] = '数量填写错误'
            return redirect(url_for('recharge_cards'))
        if int(points) <= 0:
            session['error_message'] = '点数填写错误'
            return redirect(url_for('recharge_cards'))
        for i in range(0, int(num)):
            _chars = "0123456789ABCDEF"
            card_code = ''.join(random.sample(_chars, 16))
            if r_session.sismember('recharge_card_codes',
                                   card_code) or r_session.sismember(
                                       'used_card_codes', card_code):
                continue
            card = {}
            card['key'] = ''.join(random.sample(_chars, 16))
            card['code'] = card_code
            card['points'] = int(points)
            card['name'] = name
            card['status'] = '待售'
            r_session.sadd('recharge_card_codes', card_code)
            r_session.set('recharge_card:%s' % card_code, json.dumps(card))
    except Exception as e:
        session['error_message'] = e
    return redirect(url_for('recharge_cards'))
Exemple #3
0
def generate_recharge_cards():
    name = request.values.get('name')
    points = request.values.get('points')
    num = request.values.get('num')
    try:
        if int(num) <= 0:
            session['error_message']='数量填写错误'
            return redirect(url_for('recharge_cards'))
        if int(points) <= 0:
            session['error_message']='点数填写错误'
            return redirect(url_for('recharge_cards'))
        for i in range(0, int(num)):
            _chars = "0123456789ABCDEF"
            card_code=''.join(random.sample(_chars, 16))
            if r_session.sismember('recharge_card_codes',card_code) or  r_session.sismember('used_card_codes',card_code):
                continue
            card={}
            card['key']=''.join(random.sample(_chars, 16))
            card['code']=card_code
            card['points']=int(points)
            card['name']=name
            card['status']='待售'
            r_session.sadd('recharge_card_codes', card_code)
            r_session.set('recharge_card:%s' % card_code, json.dumps(card))
    except Exception as e:
        session['error_message']=e
    return redirect(url_for('recharge_cards'))
Exemple #4
0
def user_register():
    invitation_code = request.values.get('invitation_code')

    if not r_session.sismember('invitation_codes', invitation_code) and \
    not r_session.sismember('public_invitation_codes', invitation_code):
        session['error_message'] = '无效的邀请码。'
        return redirect(url_for('register'))

    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')

    if username == '':
        session['error_message'] = '用户名不能为空.'
        return redirect(url_for('register'))

    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该用户名已存在.'
        return redirect(url_for('register'))

    r = r"^[a-zA-Z0-9_.+-]+$"
    if re.match(r, username) is None:
        session['error_message'] = '用户名含有非法字符.'
        return redirect(url_for('register'))

    if len(username) < 6 or len(username) > 20:
        session['error_message'] = '用户名长度6~20个字符.'
        return redirect(url_for('register'))

    if password != re_password:
        session['error_message'] = '两次输入的密码不一致.'
        return redirect(url_for('register'))

    if len(password) < 8:
        session['error_message'] = '输入的密码必须8位数以上.'
        return redirect(url_for('register'))

    r_session.srem('invitation_codes', invitation_code)
    r_session.srem('public_invitation_codes', invitation_code)

    user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()),
                active=True, is_admin=False, max_account_no=20,
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    r_session.set('%s:%s' % ('user', username), json.dumps(user))
    r_session.sadd('users', username)

    session['info_message'] = '恭喜你,注册成功.'
    return redirect(url_for('register'))
Exemple #5
0
def user_register():
    invitation_code = request.values.get('invitation_code')

    if not r_session.sismember('invitation_codes', invitation_code) and \
    not r_session.sismember('public_invitation_codes', invitation_code):
        session['error_message'] = '无效的邀请码。'
        return redirect(url_for('register'))

    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')

    if username == '':
        session['error_message'] = '用户名不能为空.'
        return redirect(url_for('register'))

    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该用户名已存在.'
        return redirect(url_for('register'))

    r = r"(^[a-zA-Z]+[a-zA-Z0-9_-]+$)"
    if re.match(r, username) is None:
        session['error_message'] = '用户名由字母开头数字和下划线组成.'
        return redirect(url_for('register'))

    if len(username) < 6 or len(username) > 20:
        session['error_message'] = '用户名长度在6~20个字符之间.'
        return redirect(url_for('register'))

    if password != re_password:
        session['error_message'] = '两次输入的密码不一致.'
        return redirect(url_for('register'))

    if len(password) < 8:
        session['error_message'] = '输入的密码必须8位数以上.'
        return redirect(url_for('register'))

    r_session.srem('invitation_codes', invitation_code)
    r_session.srem('public_invitation_codes', invitation_code)

    user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()),
                active=True, is_admin=False, max_account_no=20,
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    r_session.set('%s:%s' % ('user', username), json.dumps(user))
    r_session.sadd('users', username)

    session['info_message'] = '恭喜你,注册成功.'
    return redirect(url_for('register'))
Exemple #6
0
def register():
    if session.get('user_info') is not None:
        return redirect(url_for('dashboard'))

    err_msg = None
    if session.get('error_message') is not None:
        err_msg = session.get('error_message')
        session['error_message'] = None

    invitation_code = ''
    if request.values.get('inv_code') is not None and len(request.values.get('inv_code')) > 0 :
        invitation_code = request.values.get('inv_code')
        if not r_session.sismember('invitation_codes', invitation_code) and \
                    not r_session.sismember('public_invitation_codes', invitation_code):
            session['error_message'] = '无效的邀请码。'

    return render_template('register.html', err_msg=err_msg,invitation_code=invitation_code)
Exemple #7
0
def user_register():
    email = request.values.get('username')
    invitation_code = request.values.get('invitation_code')
    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')

    if not r_session.sismember('invitation_codes', invitation_code) and \
            not r_session.sismember('public_invitation_codes', invitation_code):
        session['error_message'] = '无效的邀请码。'
        return redirect(url_for('register'))

    if username == '':
        session['error_message'] = '账号名不能为空。'
        return redirect(url_for('register'))

    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该账号名已存在。'
        return redirect(url_for('register'))

    if password != re_password:
        session['error_message'] = '新密码输入不一致.'
        return redirect(url_for('register'))

    if len(password) < 8:
        session['error_message'] = '密码必须8位及以上.'
        return redirect(url_for('register'))

    r_session.srem('invitation_codes', invitation_code)
    r_session.srem('public_invitation_codes', invitation_code)

    user = dict(username=username,
                password=hash_password(password),
                id=str(uuid.uuid1()),
                active=True,
                is_admin=False,
                max_account_no=20,
                email=email,
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    r_session.set('%s:%s' % ('user', username), json.dumps(user))
    r_session.set('%s:%s' % ('record', username), json.dumps(dict(diary=[])))
    r_session.sadd('users', username)

    session['info_message'] = '恭喜你,注册成功.'
    return redirect(url_for('register'))
Exemple #8
0
def register():
    if session.get("user_info") is not None:
        return redirect(url_for("dashboard"))

    err_msg = None
    if session.get("error_message") is not None:
        err_msg = session.get("error_message")
        session["error_message"] = None

    invitation_code = ""
    if request.values.get("inv_code") is not None and len(request.values.get("inv_code")) > 0:
        invitation_code = request.values.get("inv_code")
        if not r_session.sismember("invitation_codes", invitation_code) and not r_session.sismember(
            "public_invitation_codes", invitation_code
        ):
            session["error_message"] = "无效的邀请码。"

    return render_template("register.html", err_msg=err_msg, invitation_code=invitation_code)
Exemple #9
0
def pay_submit():
    session['action'] = 'info'
    user = session.get('user_info')
    pay_code = request.values.get('pay_code')
    pay_key = request.values.get('pay_key')
    if not r_session.sismember('recharge_card_codes', pay_code):
        session['error_message'] = '充值失败,充值卡不存在'
        return redirect(url_for('user_profile'))
    b_card = r_session.get('recharge_card:%s' % pay_code)
    if b_card is not None:
        card = json.loads(b_card.decode('utf-8'))
        if card['key'] == pay_key:
            user_key = '%s:%s' % ('user', user.get('username'))
            user_info = json.loads(r_session.get(user_key).decode('utf-8'))
            if 'total_account_point' in user_info.keys():
                user_info['total_account_point'] += card['points']
                if not user_info[
                        'active'] and user_info['total_account_point'] >= 0:
                    user_info['active'] = True
            else:
                session['error_message'] = '充值失败,新用户请等待后台准备好您的数据后重试,至多半小时后可以继续'
                return redirect(url_for('user_profile'))
            if user_info.get('max_account_no') is not None and user_info.get(
                    'max_account_no') > 0:
                days = int(
                    user_info.get('total_account_point') /
                    user_info.get('max_account_no'))
                if days < 36500:
                    user_info['expire_date'] = (
                        datetime.now() +
                        timedelta(days=days)).strftime('%Y-%m-%d')
                else:
                    user_info['expire_date'] = (
                        datetime.now() +
                        timedelta(days=36500)).strftime('%Y-%m-%d')
            r_session.set(user_key, json.dumps(user_info))
            points = user_info['total_account_point']
            user_info = json.loads(r_session.get(user_key).decode('utf-8'))
            if user_info['total_account_point'] != points:
                session['error_message'] = '充值失败,数据更新失败,请重试'
                return redirect(url_for('user_profile'))
            r_session.sadd('used_card_codes', pay_code)
            card['user'] = user.get('username')
            r_session.set('used_card:%s' % pay_code, json.dumps(card))
            r_session.srem('recharge_card_codes', pay_code)
            r_session.delete('recharge_card:%s' % pay_code)
            account_log(
                user.get('username'), '充值点数', '充值', '卡号:%s 冲入点数:%d 剩余:%d' %
                (pay_code, card['points'], user_info['total_account_point']))
        else:
            session['error_message'] = '充值失败,密码错误'
            return redirect(url_for('user_profile'))
    else:
        session['error_message'] = '充值失败,未知错误'
        return redirect(url_for('user_profile'))
    session['error_message'] = '充值成功'
    return redirect(url_for('user_profile'))
Exemple #10
0
def user_register():
    invitation_code = request.values.get("invitation_code")
    username = request.values.get("username")
    password = request.values.get("password")
    re_password = request.values.get("re_password")

    if not r_session.sismember("invitation_codes", invitation_code) and not r_session.sismember(
        "public_invitation_codes", invitation_code
    ):
        session["error_message"] = "无效的邀请码。"
        return redirect(url_for("register"))

    if username == "":
        session["error_message"] = "账号名不能为空。"
        return redirect(url_for("register"))

    if r_session.get("%s:%s" % ("user", username)) is not None:
        session["error_message"] = "该账号名已存在。"
        return redirect(url_for("register"))

    if password != re_password:
        session["error_message"] = "新密码输入不一致."
        return redirect(url_for("register"))

    if len(password) < 8:
        session["error_message"] = "密码必须8位及以上."
        return redirect(url_for("register"))

    r_session.srem("invitation_codes", invitation_code)
    r_session.srem("public_invitation_codes", invitation_code)

    user = dict(
        username=username,
        password=hash_password(password),
        id=str(uuid.uuid1()),
        active=True,
        is_admin=False,
        max_account_no=2,
        created_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
    )
    r_session.set("%s:%s" % ("user", username), json.dumps(user))
    r_session.sadd("users", username)
    return redirect(url_for("login"))
Exemple #11
0
def user_register():
    invitation_code = request.values.get('invitation_code')
    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')

    if not r_session.sismember('invitation_codes', invitation_code) and \
            not r_session.sismember('public_invitation_codes', invitation_code):
        session['error_message'] = '无效的邀请码。'
        return redirect(url_for('register'))

    if username == '':
        session['error_message'] = '账号名不能为空。'
        return redirect(url_for('register'))

    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该账号名已存在。'
        return redirect(url_for('register'))

    if password != re_password:
        session['error_message'] = '新密码输入不一致.'
        return redirect(url_for('register'))

    if len(password) < 8:
        session['error_message'] = '密码必须8位及以上.'
        return redirect(url_for('register'))

    r_session.srem('invitation_codes', invitation_code)
    r_session.srem('public_invitation_codes', invitation_code)

    user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()),
                active=True, is_admin=False, max_account_no=20,
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    r_session.set('%s:%s' % ('user', username), json.dumps(user))
    r_session.set('%s:%s' % ('record', username), json.dumps(dict(diary=[])))
    r_session.sadd('users', username)

    session['info_message'] = '恭喜你,注册成功.'
    return redirect(url_for('register'))
Exemple #12
0
def register_user():
    email = request.values.get('username')
    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')
    r = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
    if re.match(r, email) is None:
        session['error_message'] = '邮箱地址格式不正确.'
        return redirect(url_for('add_user'))
    if username == '':
        session['error_message'] = '账号名不能为空。'
        return redirect(url_for('add_user'))
    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该账号名已存在。'
        return redirect(url_for('add_user'))
    if password != re_password:
        session['error_message'] = '密码输入不一致.'
        return redirect(url_for('add_user'))
    if len(password) < 8:
        session['error_message'] = '密码必须8位及以上.'
        return redirect(url_for('add_user'))
    if r_session.sismember('email', email):
        session['error_message'] = '该邮件地址已被注册.'
        return redirect(url_for('add_user'))
    config_key = '%s:%s' % ('user', 'system')
    config_info = json.loads(r_session.get(config_key).decode('utf-8'))
    if 'trial_period' not in config_info.keys():
        config_info['trial_period'] = 14
    user = dict(username=username,
                password=hash_password(password),
                id=str(uuid.uuid1()),
                active=True,
                is_admin=False,
                max_account_no=1,
                email=email,
                total_account_point=config_info['trial_period'],
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    days = int(user.get('total_account_point') / user.get('max_account_no'))
    if days < 36500:
        user['expire_date'] = (datetime.now() +
                               timedelta(days=days)).strftime('%Y-%m-%d')
    else:
        user['expire_date'] = (datetime.now() +
                               timedelta(days=36500)).strftime('%Y-%m-%d')
    r_session.set('%s:%s' % ('user', user.get('username')), json.dumps(user))
    r_session.set('%s:%s' % ('record', user.get('username')),
                  json.dumps(dict(diary=[])))
    r_session.sadd('users', user.get('username'))
    r_session.sadd('email', user.get('email'))
    session['info_message'] = '注册成功'
    return redirect(url_for('admin_user'))
Exemple #13
0
def pay_submit():
    session['action'] = 'info'
    user = session.get('user_info')
    pay_code = request.values.get('pay_code')
    pay_key = request.values.get('pay_key')
    if not r_session.sismember('recharge_card_codes',pay_code):
        session['error_message'] = '充值失败,充值卡不存在'
        return redirect(url_for('user_profile'))
    b_card = r_session.get('recharge_card:%s' % pay_code)
    if b_card is not None:
        card=json.loads(b_card.decode('utf-8'))
        if card['key'] == pay_key:
            user_key = '%s:%s' % ('user', user.get('username'))
            user_info = json.loads(r_session.get(user_key).decode('utf-8'))
            if 'total_account_point' in user_info.keys():
                user_info['total_account_point'] += card['points']
                if not user_info['active'] and user_info['total_account_point'] >= 0:
                    user_info['active'] = True
            else:
                session['error_message'] = '充值失败,新用户请等待后台准备好您的数据后重试,至多半小时后可以继续'
                return redirect(url_for('user_profile'))
            if user_info.get('max_account_no') is not None and user_info.get('max_account_no') > 0:
                days=int(user_info.get('total_account_point')/user_info.get('max_account_no'))
                if days<36500:
                    user_info['expire_date'] = (datetime.now() + timedelta(days=days)).strftime('%Y-%m-%d')
                else:
                    user_info['expire_date'] = (datetime.now() + timedelta(days=36500)).strftime('%Y-%m-%d')
            r_session.set(user_key,json.dumps(user_info))
            points=user_info['total_account_point']
            user_info = json.loads(r_session.get(user_key).decode('utf-8'))
            if user_info['total_account_point'] != points:
                session['error_message'] = '充值失败,数据更新失败,请重试'
                return redirect(url_for('user_profile'))
            r_session.sadd('used_card_codes', pay_code)
            card['user']=user.get('username')
            r_session.set('used_card:%s' % pay_code, json.dumps(card))
            r_session.srem('recharge_card_codes',pay_code)
            r_session.delete('recharge_card:%s' % pay_code)
            account_log(user.get('username'),'充值点数','充值','卡号:%s 冲入点数:%d 剩余:%d' % (pay_code,card['points'],user_info['total_account_point']))
        else:
            session['error_message'] = '充值失败,密码错误'
            return redirect(url_for('user_profile'))
    else:
        session['error_message'] = '充值失败,未知错误'
        return redirect(url_for('user_profile'))
    session['error_message'] = '充值成功'
    return redirect(url_for('user_profile'))
Exemple #14
0
def register_user():
    email = request.values.get('username')
    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')
    r = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
    if re.match(r, email) is None:
        session['error_message'] = '邮箱地址格式不正确.'
        return redirect(url_for('add_user'))
    if username == '':
        session['error_message'] = '账号名不能为空。'
        return redirect(url_for('add_user'))
    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该账号名已存在。'
        return redirect(url_for('add_user'))
    if password != re_password:
        session['error_message'] = '密码输入不一致.'
        return redirect(url_for('add_user'))
    if len(password) < 8:
        session['error_message'] = '密码必须8位及以上.'
        return redirect(url_for('add_user'))
    if r_session.sismember('email', email):
        session['error_message'] = '该邮件地址已被注册.'
        return redirect(url_for('add_user'))
    config_key = '%s:%s' % ('user', 'system')
    config_info = json.loads(r_session.get(config_key).decode('utf-8'))
    if 'trial_period' not in config_info.keys():
        config_info['trial_period'] = 14
    user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()),
                active=True, is_admin=False, max_account_no=1, email=email,total_account_point=config_info['trial_period'],
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    days=int(user.get('total_account_point')/user.get('max_account_no'))
    if days<36500:
        user['expire_date'] = (datetime.now() + timedelta(days=days)).strftime('%Y-%m-%d')
    else:
        user['expire_date'] = (datetime.now() + timedelta(days=36500)).strftime('%Y-%m-%d')
    r_session.set('%s:%s' % ('user', user.get('username')), json.dumps(user))
    r_session.set('%s:%s' % ('record', user.get('username')), json.dumps(dict(diary=[])))
    r_session.sadd('users', user.get('username'))
    r_session.sadd('email', user.get('email'))
    session['info_message'] = '注册成功'
    return redirect(url_for('admin_user'))
Exemple #15
0
def user_register():
    email = request.values.get('username')
    invitation_code = request.values.get('invitation_code')
    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')

    r = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
    if re.match(r, email) is None:
        session['error_message'] = '邮箱地址格式不正确.'
        return redirect(url_for('register'))

    if username == '':
        session['error_message'] = '账号名不能为空。'
        return redirect(url_for('register'))

    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该账号名已存在。'
        return redirect(url_for('register'))

    if password != re_password:
        session['error_message'] = '新密码输入不一致.'
        return redirect(url_for('register'))

    if len(password) < 8:
        session['error_message'] = '密码必须8位及以上.'
        return redirect(url_for('register'))

    if r_session.sismember('email', email):
        session['error_message'] = '该邮件地址已被注册.'
        return redirect(url_for('register'))

    if not r_session.sismember('invitation_codes', invitation_code) and \
            not r_session.sismember('public_invitation_codes', invitation_code):
        session['error_message'] = '无效的邀请码。'
        return redirect(url_for('register'))

    email_code = r_session.get('emailcode:%s' % email)
    if email_code is not None:
        code_time = json.loads(email_code.decode('utf-8'))
        if datetime.strptime(code_time, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=5) > datetime.now():
            session['error_message'] = '发送邮件过于频繁 请稍候再试.'
            return redirect(url_for('register'))

    _chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    key = ''.join(random.sample(_chars, 36))
    user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()),
                active=True, is_admin=False, max_account_no=20, email=email,
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

    r_session.setex('emailcode:%s' % email, json.dumps(user.get('created_time')), 60*5)
    r_session.setex('activecode:%s' % key, json.dumps(user), 60*30)

    bytesString = key.encode('utf-8')
    encodestr = base64.b64encode(bytesString)

    if user_email(email, encodestr.decode('utf-8')) != True:
        session['error_message'] = '激活帐户邮件发送失败 邮箱不存在.'
        return redirect(url_for('register'))

    r_session.srem('invitation_codes', invitation_code)
    r_session.srem('public_invitation_codes', invitation_code)

    session['info_message'] = '激活帐户邮件已发送到您的邮箱.'
    return redirect(url_for('register'))
Exemple #16
0
def user_register():
    email = request.values.get('username')
    invitation_code = request.values.get('invitation_code')
    username = request.values.get('username')
    password = request.values.get('password')
    re_password = request.values.get('re_password')

    r = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
    if re.match(r, email) is None:
        session['error_message'] = '邮箱地址格式不正确.'
        return redirect(url_for('register'))

    if username == '':
        session['error_message'] = '账号名不能为空。'
        return redirect(url_for('register'))

    if r_session.get('%s:%s' % ('user', username)) is not None:
        session['error_message'] = '该账号名已存在。'
        return redirect(url_for('register'))

    if password != re_password:
        session['error_message'] = '密码输入不一致.'
        return redirect(url_for('register'))

    if len(password) < 8:
        session['error_message'] = '密码必须8位及以上.'
        return redirect(url_for('register'))

    if r_session.sismember('email', email):
        session['error_message'] = '该邮件地址已被注册.'
        return redirect(url_for('register'))

    if not r_session.sismember('invitation_codes', invitation_code) and \
            not r_session.sismember('public_invitation_codes', invitation_code):
        session['error_message'] = '无效的邀请码。'
        return redirect(url_for('register'))

    email_code = r_session.get('emailcode:%s' % email)
    if email_code is not None:
        code_time = json.loads(email_code.decode('utf-8'))
        if datetime.strptime(code_time, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=5) > datetime.now():
            session['error_message'] = '发送邮件过于频繁 请稍候再试.'
            return redirect(url_for('register'))

    _chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    key = ''.join(random.sample(_chars, 36))
    config_key = '%s:%s' % ('user', 'system')
    config_info = json.loads(r_session.get(config_key).decode('utf-8'))
    if 'trial_period' not in config_info.keys():
        config_info['trial_period'] = 14
    user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()),
                active=True, is_admin=False, max_account_no=1, email=email,total_account_point=config_info['trial_period'],
                created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    days=int(user.get('total_account_point')/user.get('max_account_no'))
    if days<36500:
        user['expire_date'] = (datetime.now() + timedelta(days=days)).strftime('%Y-%m-%d')
    else:
        user['expire_date'] = (datetime.now() + timedelta(days=36500)).strftime('%Y-%m-%d')

    r_session.setex('emailcode:%s' % email, json.dumps(user.get('created_time')), 60*5)
    r_session.setex('activecode:%s' % key, json.dumps(user), 60*30)

    bytesString = key.encode('utf-8')
    encodestr = base64.b64encode(bytesString)

    if user_email(email, encodestr.decode('utf-8')) != True:
        session['error_message'] = '激活帐户邮件发送失败 邮箱不存在.'
        return redirect(url_for('register'))

    r_session.srem('invitation_codes', invitation_code)
    r_session.srem('public_invitation_codes', invitation_code)

    session['info_message'] = '激活帐户邮件已发送到您的邮箱.'
    return redirect(url_for('register'))