Esempio n. 1
0
def get_sms_code(mobile):
    image_code = request.args.get("image_code")
    image_code_id = request.args.get("image_code_id")

    if not all([image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')


    try:
        real_image_code = redis_store.get('image_code_%s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='redis异常')

    if real_image_code == None:
        return jsonify(errno=RET.NODATA, errmsg='验证码过期')

    #删除图片验证码 写前一点 防止同一个验证码验证多次(撞库)
    try:
        redis_store.delete('image_code_%s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    #都装小写对比
    if real_image_code.lower() != image_code.lower():
        return jsonify(error=RET.DATAERR, errmsg='验证码错误')

    #检查手机60s有木有发送过
    try:
        send_flag = redis_store.get('send_sms_code_%s' % mobile)
    except Exception as e:
        current_app.logger.error(e)
    else:
        if send_flag is not None:
            #60s有过记录
            return jsonify(errno=RET.REQERR, errmsg='发送短信过于频繁60s后重试')

    #手机号是否重复
    try:
        user = User.query.filter_by(phone_num=mobile).first() #怕数据库突然崩了
    except Exception as e:
        current_app.logger.error(e)
    else:
        if user is not None:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在')

    #生成短信验证码
    sms_code = '%06d' % random.randint(0, 999999) #06d 最少6位 少的前边加0

    try:
        redis_store.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRE, sms_code) #5分钟
        #保存发送给手机号的记录防止60s重复发送
        redis_store.setex('send_sms_code_%s' % mobile, constants.SEND_SMS_CODE_EXPIRE, 1) #发送间隔60秒 1随便写
    except Exception as e:
        current_app.logger.error(e)#记录异常
        return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')

    #发送短信
    tasks_send_sms.delay(mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRE)/60], 1)
    return jsonify(errno=RET.OK, errmsg='发送成功')
Esempio n. 2
0
def save_order_comment(order_id):
    """保存订单评论信息"""
    user_id = g.user_id
    # 获取参数
    req_data = request.get_json()
    comment = req_data.get("comment")  # 评价信息

    # 检查参数
    if not comment:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    try:
        # 需要确保只能评论自己下的订单,而且订单处于待评价状态才可以
        order = Order.query.filter(Order.id == order_id,
                                   Order.user_id == user_id,
                                   Order.status == "WAIT_COMMENT").first()
        house = order.house
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="无法获取订单数据")

    if not order:
        return jsonify(errno=RET.REQERR, errmsg="操作无效")

    try:
        # 将订单的状态设置为已完成
        order.status = "COMPLETE"
        # 保存订单的评价信息
        order.comment = comment
        # 将房屋的完成订单数增加1
        house.order_count += 1
        db.session.add(order)
        db.session.add(house)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="操作失败")

    # 因为房屋详情中有订单的评价信息,为了让最新的评价信息展示在房屋详情中,所以删除redis中关于本订单房屋的详情缓存
    try:
        redis_store.delete("house_info_%s" % order.house.id)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg="OK")
Esempio n. 3
0
def register():
    req_dict = request.get_json()
    mobile = req_dict.get('mobile')
    sms_code = req_dict.get('sms_code')
    password = req_dict.get('password')
    password2 = req_dict.get('password2')

    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

    if not re.match(r'1[34578]\d{9}]', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式不对')

    if password != password2:
        return jsonify(errno=RET.PARAMERR, errmsg='2个密码不对')

    try:
        real_sms_code = redis_store.get('send_sms_code_%s' % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg='读取验证码验证码异常')

    if real_sms_code is None:
        return jsonify(errno=RET.NODATA, errmsg='短信验证码失效')

    #删除短信验证码 防止多次验证(后边再删 一条短信可以多次验证)
    try:
        redis_store.delete('send_sms_code_%s' % mobile)
    except Exception as e:
        current_app.logger.error(e)

    if real_sms_code != sms_code:
        return jsonify(errno=RET.DBERR, errmsg='短信验证码错误')
    ''' 2次查询(不用)
    try:
        user = User.query.filter_by(phone_num=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')
    else:
        if user is not None:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在')
    
    user = User()
    db.session.add(user)
    db.session.commit()
    '''

    user = User(phone_num=mobile, name=mobile)
    user.password_hash = password  #方法变属性
    try:
        db.session.add(user)
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()  #回滚操作
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在')
    except Exception as e:
        db.session.rollback()  # 回滚操作
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAEXIST, errmsg='数据库存异常')

    #保存登录状态

    session['name'] = mobile
    session['mobile'] = mobile
    session['user_id'] = user.id

    return jsonify(errno=RET.OK, errmsg='注册成功')