Пример #1
0
    def post(self):
        params = register_parse.parse_args()

        email = params.get("email")
        pwd = params.get("pwd")
        confirm_pwd = params.get("confirm_pwd")
        User.query.paginate
        # 判断密码和确认密码是否一致
        if pwd != confirm_pwd:
            return {"code": 2, "msg": "密码和确认密码不一致"}

        res = User.creat_user(email=email, pwd=pwd)
        # 给你发个邮件 让你激活
        url = "http://" + request.host + "/active/" + create_unique_str()
        print(url)

        if res:
            # send_email.delay(email, url, res.id, mail)

            msg = Message("欢迎注册爱鲜蜂后台管理", [email], sender="*****@*****.**")
            msg.html = render_template("active.html", url=url)
            mail.send(msg)

            key = url.split("/")[-1]
            cache.set(key, res.id, timeout=60 * 60)
            return {"data": "/index"}
        else:
            return {"code": 3, "msg": "注册失败"}
Пример #2
0
def register_api():
    if request.method == "GET":
        #此处可以重定向到登录页面
        return "注册成功"
    else:
        #解析参数
        params = request.form
        u_phone = params.get("u_phone")
        u_name = params.get("u_name")
        u_password = params.get("u_password")
        email = params.get("email")
        u_icon = request.form.get("u_icon", None)
        #校验参数
        if u_name and u_password and u_phone and len(u_name) > 3 and len(
                str(u_phone)) == 11:
            #校验手机号
            enc_phone = User.query.filter(User.u_phone == u_phone).all()
            #若为True 则手机号注册过
            if len(enc_phone) == 0:
                # 给密码加密
                enc_pwd_str = enc_pwd(u_password)
                #实例化
                u = User()
                u.u_name = u_name
                u.u_password = enc_pwd_str
                #校验用户头像,若为None,则没上传
                if u_icon == None:
                    return "没上传头像,请上传头像"
                else:
                    #生成唯一头像名字
                    file_name = create_rand_str() + ".jpg"
                    #拼接文保存路径
                    file_path = os.path.join(BASE_DIR,
                                             'static/icon' + file_name)
                    #保存文件
                    file = open(file_path, "wb")
                    for i in u_icon.chunks():
                        u.u_icon = file_path
                        #保存文件路径
                #将用户对象保存到数据库
                db.session.add(u)
                db.session.commit()

                res = send_mail(email, request.host)
                # 设置缓存
                cache.set(res, u.id, 60 * 60)
                data = {"code": SUCCESS, "msg": '注册成功', "data": []}
                return jsonify(data)
            else:
                #手机号已被注册
                return jsonify({
                    "code": FAIL,
                    "msg": "手机号已被注册,请换个手机号重新注册",
                    "data": []
                })
        else:
            #参数不合适
            return jsonify({"code": NOT_LOGIN, "msg": "参数不对,请核对", "data": []})
Пример #3
0
def heheda():
    #反爬虫 首先检查有没有user_agent,再看IP如果在30秒访问10次,又就杀死
    user_agent = request.user_agent
    g.tang = "咸"
    if not user_agent:
        return jsonify({"code": 1000, "msg": "换一个网站"}), 500
    ip = request.remote_addr
    key = ip + "fangpa"
    times = cache.get("key")
    if not times:
        cache.set(key, 1, 30)
    else:
        if int(times) >= 3:
            return "搞你妈", 404
        else:
            cache.set(key, times + 1, 30)
Пример #4
0
def my_cache():
    #先获取ip,拼接key
    ip = request.remote_addr
    key = ip + "day04"
    #去缓存尝试拿数据
    data = cache.get(key)
    if data:
        print("有数据")
        return jsonify(data)
    else:
        #查数据库
        print("查数据")
        new_data = {"code": 1, "msg": "ok", "data": "呵呵哒"}
        #设置缓存
        cache.set(key, new_data, 30)
        return jsonify(new_data)
Пример #5
0
def enc_pwd_login():
    #解析参数
    u_phone = request.form.get("u_phone")
    #校验参数
    if u_phone:
        u = User.query.filter(User.u_phone == u_phone).all()
        if len(u) == 0:
            #用户不存在
            data = {"code": PERMISSION_DENY, "msg": "用户名不存在", "data": []}
            return jsonify(data)
        else:
            #随机生成一个六位数
            random_num = random.randrange(100000, 1000000)
            #发短信,此处不会写,跳过
            #设置缓存, 60*60秒过期
            cache.set(u_phone, random_num, 60 * 60)
            data = {"code": SUCCESS, "msg": "用户登录验证码,请查收", "data": u_phone}
            return jsonify(data)
    else:
        #参数错误
        data = {"code": NOT_LOGIN, "msg": "参数有误,无法操作", "data": []}
        return jsonify(data)
Пример #6
0
    def post(self):
        params = register_parse.parse_args()

        email = params.get("email")
        pwd = params.get("pwd")
        pwd_confirm = params.get("pwd_confirm")

        #判断密码和确认密码是否一致
        if pwd != pwd_confirm:
            return {"code": 2, "msg": "密码和确认密码不一致"}
        # 创建用户
        res = User.create_user(email=email, pwd=pwd)
        url = "http://" + request.host + "active/" + create_uniqu_str()
        if res:
            # send_email.delay(email, url, res.id,mail, cache)
            msg = Message("欢迎注册爱鲜蜂后台管理", [email], sender="*****@*****.**")
            msg.html = render_template("active.html", url=url)
            mail.send(msg)
            # 拼接
            key = url.split("/")[-1]
            cache.set(key, res.id, timeout=60 * 60)
            return {"data": "/index"}
        else:
            return {"code": 3, "msg": "注册失败"}