Ejemplo n.º 1
0
def get_imae_code():
    """
     1. 获取传入的验证码编号,并编号是否有值
     2. 生成图片验证码
     3. 保存编号和其对应的图片验证码内容到redis
     4. 返回验证码图片
     :return:
     """
    # current_app.logger.error("error log")
    # 1. 获取传入的验证码编号,并编号是否有值
    args = request.args
    cur = args.get("cur")
    pre = args.get("pre")
    if not cur:
        abort(403)

    # 2. 生成图片验证码
    _, text, image = captcha.generate_captcha()
    current_app.logger.debug(text)
    # 删除之前保存的数据
    # 3. 保存编号和其对应的图片验证码内容到redis
    try:
        redis_store.delete("ImageCode_" + pre)
        redis_store.set("ImageCode_" + cur, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        # 保存出现错误,返回JSON数据提示错误
        return jsonify(errno=RET.DBERR, errmsg="保存验证码失败")
    # 4. 返回验证码图片

    reponse = make_response(image)
    return reponse
Ejemplo n.º 2
0
def verify():
    # 获取参数:uuid
    # 生成验证码
    # 删除之前验证码并保存当前验证码
    # 保存'验证码'&'uuid'到redis
    # 返回验证码

    # 获取参数:uuid
    cur = request.args.get('cur')
    pre = request.args.get('pre')
    # 生成验证码
    name, text, image = captcha.generate_captcha()
    # 删除之前验证码并保存当前验证码
    # 保存'验证码'&'uuid'到redis
    # 格式:redis.set(key, value, 过期时间)
    print '验证码:', text
    try:
        if pre != '':
            redis_store.delete('image_code:%s' % pre)
        redis_store.set('image_code:%s' % cur, text, IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存验证码失败')
    # 返回验证码
    res = make_response(image)
    # 修改响应的类型
    res.headers['Content-Type'] = 'image/jpg'
    return res
Ejemplo n.º 3
0
def get_area():
    """
    1.获取城区信息
    2.组织响应,返回数据
    :return: 
    """
    try:
        areas_json_str = redis_store.get('areas')
        if areas_json_str:
            areas_dict_li = json.loads(areas_json_str)
            return jsonify(errno=RET.OK, errmsg='OK', data=areas_dict_li)
    except Exception as e:
        current_app.logger.error(e)

    # 1.获取城区信息
    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger(e)
        return jsonify(errno=RET.DBERR, errmsg='查询城区信息失败')
    # 2. 组织数据
    areas_dict_list = []
    for area in areas:
        areas_dict_list.append(area.to_dict())

    try:
        redis_store.set('areas', json.dumps(areas_dict_list),
                        constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg='OK', data=areas_dict_list)
Ejemplo n.º 4
0
def send_sms_code():
    req_data = request.data
    req_dict = json.loads(req_data)
    mobile = req_dict['mobile']
    image_code = req_dict['image_code']
    image_code_id = req_dict['image_code_id']
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='信息不完整')
    if not re.match(r'1[3456789]\d{9}',mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码格式不对')
    try:
        real_image_code = redis_store.get('imagecode:%s'% image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询图片验证码错误')
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码过期')
    if real_image_code != image_code:
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')
    # todo: 发生短信验证码
    sms_code = '%06d'% random.randint(0,999999)
    current_app.logger.info('短信验证码'+sms_code)
    try:
        redis_store.set('sms_code:%s'%mobile, sms_code,constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='保存验证码失败')

    res = CCP().send_template_sms(mobile ,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60], 1)
    if res != 1 :
        return jsonify(errno=RET.THIRDERR,errmsg='发送短信失败')
    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Ejemplo n.º 5
0
def get_image_code():
    """提供图片验证码
    1.接受请求,获取uuid
    2.生成图片验证码
    3.使用UUID存储图片验证码内容到redis
    4.返回图片验证码
    """

    # 1.接受请求,获取uuid
    uuid = request.args.get('uuid')
    last_uuid = request.args.get('last_uuid')
    if not uuid:
        abort(403)
        # return jsonify(errno=RET.PARAMERR, errmsg=u'缺少参数')

    # 2.生成验证码:text是验证码的文字信息,image验证码的图片信息
    name, text, image = captcha.generate_captcha()

    # 3.使用UUID存储图片验证码内容到redis
    try:
        if last_uuid:
            # 上次的uuid还存在,删除上次的uuid对应的记录
            redis_store.delete('ImageCode:' + last_uuid)

        # 保存本次需要记录的验证码数据
        redis_store.set('ImageCode:' + uuid, text, constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        print e
        return jsonify(errno=RET.DBERR, errmsg=u'保存验证码失败')

    # 4.返回图片验证码
    resposne = make_response(image)
    resposne.headers['Content-Type'] = 'image/jpg'
    return resposne
Ejemplo n.º 6
0
def get_areas():
    """查询地区信息"""
    ###eval:会根据字符串的数据结构,自动生成对应结果的对象

    try:
        area_dict_list = redis_store.get('Areas')
        if area_dict_list:
            return jsonify(errno=RET.OK, errmsg='ok', data=eval(area_dict_list))

    except Exception as e:
        current_app.logger.error(e)

    # 1,查询地区信息
    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询地区信息失败')

    # 2,将地区模型对象列表转成字典列表
    area_dict_list = []
    for area in areas:
        area_dict_list.append(area.to_dict())

    # 缓存城区数据
    # 缓存是可有可无的,不能return。会影响主逻辑的运行
    try:
        redis_store.set('Areas', area_dict_list, constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    # 3,响应地区信息,只认识字典或者字典列表
    return jsonify(errno=RET.OK, errmsg='读取地区信息成功', data=area_dict_list)
Ejemplo n.º 7
0
def get_areas():
    """提供城区信息
    1.直接查询所有城区信息
    2.构造城区信息响应数据
    3.响应城区信息
    """
    try:
        area_dict_list = redis_store.get("Areas")
        if area_dict_list:
          return jsonify(errno=RET.OK, errmsg='OK', data=eval(area_dict_list))
    except Exception as e:
        current_app.logger.error(e)
    # 1.直接查询所有城区信息areas == [Area,Area,Area]

    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询城区信息失败')

    # 2.构造城区信息响应数据:将areas转成字典列表
    area_dict_list = []
    for area in areas:
        area_dict_list.append(area.to_dict())

    try:
        redis_store.set("Areas", area_dict_list, constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)



    # 3.响应城区信息:只认识字典或者字典列表
    return jsonify(errno=RET.OK, errmsg='OK', data=area_dict_list)
Ejemplo n.º 8
0
def get_image_code():
    """
    生成图片验证码
    1.获取uuid
    2.生成图片验证码
    3.在redis中存储图片验证码
    4.返回图片验证码
    :return: 
    """
    # 1.获取uuid
    uuid = request.args.get('cur_id')
    if not uuid:
        abort(403)
    # 2.生成图片验证码
    name, text, data = captcha.generate_captcha()
    # 3.在redis中存储图片验证码
    try:
        redis_store.set('imagecode:%s' % uuid, text, constants.IMAGE_CODE_REDIS_EXPIRES)
        current_app.logger.info('图片验证码:'+text)
    except Exception as e:
        # 生成日志并保存
        current_app.logger.error(e)

        return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')
    # 4.返回图片验证码
    response = make_response(data)
    response.headers['Content-Type'] = 'image/jpg'
    return response
Ejemplo n.º 9
0
def get_image_code():
    '''
    1. 获取传入的验证码id
    2. 生成新的验证码
    3. 删除旧的验证码信息, 保存新的验证码信息
    4. 返回验证码图片
    :return:
    '''

    # 获取新验证码和旧验证码ID
    args = request.args
    cur = args.get('cur')
    pre = args.get('pre')
    if not cur:
        abort(403)
    # 获取验证码图片及内容
    _, text, image = captcha.generate_captcha()

    current_app.logger.debug(text)
    # 保存新验证码内容, 删除旧验证码内容
    try:
        redis_store.set('ImageCode_' + cur, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
        redis_store.delete('ImageCode_' + pre)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='验证码保存失败')

    response = make_response(image)
    response.headers['Content-Type'] = 'image/jpg'

    return response
Ejemplo n.º 10
0
def get_image_code():
    """提供图片验证码"""

    # 取得uuid号码
    uuid = request.args.get('uuid')
    global last_uuid

    # 判定uuid是否为空
    if not uuid:
        abort(403)

    # 生成图片验证码的名字,文字信息,图片
    name, text, image = captcha.generate_captcha()

    current_app.logger.debug(text)

    try:
        if redis_store.get('ImageCode:' + last_uuid):
            redis_store.delete('ImageCode:' + last_uuid)
        redis_store.set('ImageCode:' + uuid, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:

        current_app.logger.errer(e)
        return jsonify(error_no=RET.DBERR, error_msg=u'存储图片验证码失败')
    #响应图片验证码
    response = make_response(image)

    last_uuid = uuid
    # 响应头的文件类型改为图片类型
    response.headers['Content-Type'] = 'image/jpg'

    return response
Ejemplo n.º 11
0
def get_image_code():
    uuid = request.args.get('uuid')
    print(uuid)
    last_uuid = request.args.get('last_uuid')
    print(last_uuid)
    if not uuid:
        abort(403)
    name, text, image = captcha.generate_captcha()
    logging.debug('图片验证码文字信息:' + text)
    try:
        if last_uuid:

            redis_store.delete('ImageCode:' + last_uuid)

        redis_store.set('ImageCode:' + uuid, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        print e
        logging.error(e)
        # restful要求返回响应状态
        return jsonify(errno=RET.DBERR, errmsg=u'保存验证码失败')
        # 4.返回图片验证码
    resposne = make_response(image)
    resposne.headers['Content-Type'] = 'image/jpg'
    return resposne
Ejemplo n.º 12
0
def send_sms_code():
    """发送短信验证码
    1.获取参数:手机号,验证码,uuid
    2.判断是否缺少参数,并对手机号格式进行校验
    3.获取服务器存储的验证码
    4.跟客户端传入的验证码进行对比
    5.如果对比成功就生成短信验证码
    6.调用单例类发送短信
    7.如果发送短信成功,就保存短信验证码到redis数据库
    8.响应发送短信的结果
    """

    # 1.获取参数:手机号,验证码uuid
    json_str = request.data
    json_dict = json.loads(json_str)
    mobile = json_dict.get('mobile')
    imageCode_client = json_dict.get('imageCode')
    uuid = json_dict.get('uuid')

    # 2.1 验证参数是否为空
    if not all([mobile, imageCode_client, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')
    # 2.2 验证手机号码是否为合法
    if not re.match(r'^1[358]{1}\d{9}$' , mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')

    # 3.1 验证图片验证码是否正确
    try:
        imageCode_server = redis_store.get('ImageCode:%s' % uuid)
    except Exception as e:
        logging.error(e)
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='查询验证码失败')
    # 3.2 验证码不存在就报错
    if not imageCode_server:
        return jsonify(errno=RET.NODATA, errmsg='验证码不存在')

    # 4.1 客户端验证码和传过来的用户自己输入的验证码进行对比
    if imageCode_client.lower() != imageCode_server.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='输入的图片验证码错误')

    # 5.todo 如果对比成功就生成验证码短信
    sms_code = "%06d" % random.randint(0, 999999)
    print sms_code

    # 6.调用单例类发送短信
    # ret = CCP().send_sms_msg(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES/60], 1)
    # if ret != 1:
    #     return jsonify(errno=RET.THIRDERR, errmsg='第三方接口发送验证码失败')

    # todo 7.发送验证码成功就保存短信验证码到redis数据库中去
    try:
        redis_store.set('SMS:%s' % mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES )
    except Exception as e:
        logging.error(e)
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码失败')

    # 8.响应发送短信的结果
    return jsonify(errno=RET.OK, errmsg='发送验证码成功')
Ejemplo n.º 13
0
def get_areas():
    """
    获取城区信息:
    """
    # 1. 从数据库中获取所有的城区信息
    # 先尝试从缓存中获取城区的信息,如果获取到,直接返回,如果获取不到,再去查询数据库
    try:
        area_json_str = redis_store.get("ares")
        if area_json_str:
            areas_dict_li = json.loads(area_json_str)
            return jsonify(errno=RET.OK, errmsg='OK', data=areas_dict_li)
    except Exception as e:
        current_app.logger.error(e)

    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger(e)
        return jsonify(errno=RET.DBERR, errmsg='查询城区信息失败')

    # 2. 组织数据,返回应答
    areas_dict_li = []
    for area in areas:
        areas_dict_li.append(area.to_dict())

    # redis设置缓存
    try:
        redis_store.set("areas", json.dumps(areas_dict_li),
                        constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg='OK', data=areas_dict_li)
Ejemplo n.º 14
0
def get_areas():
    """
    1. 查询出所有的城区
    2. 返回
    :return:
    """

    # 先从redis中查询
    try:
        areas_dict = redis_store.get("areas")
    except Exception as e:
        current_app.logger.error(e)

    if areas_dict:
        return jsonify(errno=RET.OK, errmsg="ok",data=eval(areas_dict))

    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR,errmsg="数据库查询错误")

     # 因为不能直接返回对象数组,所以定义一个列表,去中保存每一个模型所对应的字典信息
    areas_dict=[]
    for area in areas:
        areas_dict.append(area.to_dict())
        # print area.to_dict

    # 将数据保存到redis中
    try:
        redis_store.set("areas",areas_dict,AREA_INFO_REDIS_EXPIRES)
    except Exception  as e:
        current_app.logger.error(e)
    return jsonify(errno=RET.OK,errmsg="OK",data=areas_dict)
Ejemplo n.º 15
0
def get_image_code():
    #1.接收html定义的uuid值,并校验uuid
    uuid = request.args.get('uuid')
    if not uuid:
        abort(403)
    #2.生成图片验证码
    name, text, image = captcha.generate_captcha()
    current_app.logger.debug(text)
    #debug只能在测试模式下使用
    #logging.debug(text)

    # current_app.logger.warning(text)

    #3.使用redis数据库存储图片验证码,ImageCode:uuid作为key
    try:
        if uuid:
            redis_store.delete('ImageCode:%s' % last_uuid)
        redis_store.set('ImageCode:%s' % uuid, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        print e
        #有可能使服务器崩溃的警报,线上要用error模式
        # logging.error(e)
        #两种显示日志方法都可以使用
        current_app.logger.error(e)

        return jsonify(errno=RET.DBERR, errmsg='存储验证码错误')
    #记录当前uuid,方便下一次使用时作为上一次的uuid,删除text
    global last_uuid
    last_uuid = uuid
    #4.响应图片验证码
    #修改响应头的信息,指定响应内容是image.jpg
    respone = make_response(image)
    respone.headers['Content-Type'] = 'image.jpg'
    return respone
Ejemplo n.º 16
0
def get_areas():
    """获取城区信息"""

    # 先从 Redis 中获取
    try:
        areas_array = redis_store.get('areas')
        if areas_array:
            return jsonify(errno=RET.OK,
                           errmsg='OK',
                           data={'areas': eval(areas_array)})
    except Exception as e:
        current_app.logger.error(e)

    # 1. 获取所有的城区信息
    areas = Area.query.all()

    # 2.因为 areas 是一个对象的列表,不能直接返回,需要将其转化成字典的列表
    areas_array = []
    for area in areas:
        areas_array.append(area.to_dict())

    # 3. 将数据缓存到 redis中
    try:
        redis_store.set('areas', areas_array, AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    # 数据返回
    return jsonify(errno=RET.OK, errmsg='OK', data={'areas': areas_array})
Ejemplo n.º 17
0
def get_house_detail(house_id):
    """
    1. 通过房屋的id查询出指定的房屋
    2. 将房屋的相关数据进行返回(房屋信息,当前登录用户的id)
    3. 考虑是否缓存的问题
    :param house_id:
    :return:
    """
    # 先尝试从redis中去取
    # 获取到当前登录用户的id,如果没有登录,那么就返回-1
    user_id = session.get("user_id",-1)
    try:
        house_dict = redis_store.get(("house_detail_%d" %house_id))
        if house_dict:
            # 如果取到有数据,那么直接返回
            return jsonify(errno=RET.OK, errmsg="OK", data={"user_id": user_id, "house": eval(house_dict)})
    except Exception as e:
        current_app.logger.error(e)

    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据查询错误")

    if not house:
        return  jsonify(errno=RET.NODATA, errmsg="房屋不存在")

    house_dict=house.to_full_dict()
    # 缓存当前房屋数据
    try:
        redis_store.set(("house_detail_%d"%house_id),house_dict,HOUSE_DETAIL_REDIS_EXPIRE_SECOND )
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.OK, errmsg="OK", data={"user_id": user_id, "house": house_dict})
Ejemplo n.º 18
0
def get_house_index():
    """
    1. 查询数据库 > order_by,limit
    2. 返回

    :return: 首页订单量最高的5条数据
    """
    # 先从缓存中去加载数据
    try:
        houses_dict = redis_store.get("home_house")
        if houses_dict:
            return jsonify(errno=RET.OK, errmsg="OK", data={"houses": eval(houses_dict)})
    except Exception as e:
        current_app.logger.error(e)

    try:
        houses = House.query.order_by(House.order_count.desc()).limit(HOME_PAGE_MAX_HOUSES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据失败")

    # 将对象列表转成字典列表
    houses_dict = []
    for house in houses:
        houses_dict.append(house.to_basic_dict())

    # 将数据进行缓存
    try:
        redis_store.set("home_house",houses_dict, HOME_PAGE_DATA_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg="OK", data={"houses": houses_dict})
Ejemplo n.º 19
0
def get_areas():
    """提供城区信息
    1.查询所有的城区信息
    2.构造响应数据
    3.响应结果
    """
    try:
        area_dict_list = redis_store.get('Areas')
        if area_dict_list:
            return jsonify(errno=RET.OK,
                           errmsg='OK',
                           data=eval(area_dict_list))
    except Exception as e:
        current_app.logger.error(e)

    # 1.查询所有的城区信息 areas == [Area,Area,Area,...]
    try:
        areas = Area.query.all()  # 查询类对象的所有信息
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询城区信息失败')
    # 2.构造响应数据
    area_dict_list = []
    for area in areas:
        area_dict_list.append(area.to_dict())

    # 缓存城区信息到redis : 没有缓存成功也没有影响,因为前爱你会判断和查询
    try:
        redis_store.set('Areas', area_dict_list,
                        constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg='OK', data=area_dict_list)
Ejemplo n.º 20
0
def send_sms_code():
    """发送短信验证码
    1,和获取参数,手机号,验证码,uuid
    2,判断是否缺少参数,并对手机号格式进行效验
    3,获取服务器存储的验证码
    4,跟客户端发来的验证码做对比
    5,对比成功,生成短信验证码
    6,调用单列类发送短信
    7,如果发送短信成功,就保存短信验证码到redis数据库
    8,响应结果
    """

    # 1,和获取参数,手机号,验证码,uuid
    json_str = request.data
    json_dict = json.loads(json_str)
    mobile = json_dict.get('mobile')
    imageCode_Client = json_dict.get('imageCode')
    uuid = json_dict.get('uuid')

    # 2,判断是否缺少参数,并对手机号格式进行效验
    if not all([mobile, imageCode_Client, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误')

    # 3,获取服务器存储的验证码
    try:
        ImageCode_Server = redis_store.get('ImageCode:%s' % uuid)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询验证码失败')
    if not ImageCode_Server:
        return jsonify(errno=RET.NODATA, errmsg='状态码不存在')

    # 4,跟客户端发来的验证码做对比
    if imageCode_Client.lower() != ImageCode_Server.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='验证码输入错误')

    # 5,对比成功,生成短信验证码
    sms_code = '%06d' % random.randint(0, 999999)
    print '===' * 20
    print u'短信验证码为:' + sms_code
    print '===' * 20

    # # 6,调用单列类发送短信
    # result = CCP().send_sms_code(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], '1')
    # if result != 1:
    #     return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码失败')

    # 7,如果发送短信成功,就保存短信验证码到redis数据库
    try:
        redis_store.set('SMS:%s' % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码失败')

    # 8,响应结果
    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Ejemplo n.º 21
0
def send_sms_code():
    """ 发送短信验证码
    1.接受参数:手机号,图片验证码,uuid
    2.判断参数是否缺少,并且要对手机号进行校验
    3.获取服务器存储的图片验证码,uuid作为key
    4.与客户端传入的图片验证码对比,如果对比成功
    5.发送短信给用户
    6.响应短信发送的结果
    """

    # 1.接受参数:手机号,图片验证码,uuid
    # data : 保存请求报文里面的原始的字符串,开发文档约定,客户端发送的是json字符串
    json_str = request.data
    json_dict = json.loads(json_str)

    mobile = json_dict.get('mobile')
    image_client = json_dict.get('imagecode')
    uuid = json_dict.get('uuid')

    # 2.判断参数是否缺少,并且要对手机号进行校验
    if not all([mobile, image_client, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg=u'缺少参数')
    # 校验手机号码是否合法
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg=u'号码格式错误')

    # 3.获取服务器存储的图片验证码,uuid作为key
    try:
        image_server = redis_store.get('ImageCode:' + uuid)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=u'查询服务器验证码失败')

    # 判断是否为空或者过期
    if not image_server:
        return jsonify(errno=RET.NODATA, errmsg=u'验证码已失效')

    # 4.与客户端传入的图片验证码对比,如果对比不成功
    if image_server.lower() != image_client.lower():
        return jsonify(errno=RET.DATAERR, errmsg=u'验证码有误')

    # 5.生成短信验证码
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug('短信验证码为:' + sms_code)

    # 6.使用云通讯将短信验证码发送到注册用户手中
    # result = CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], '1')
    # if result != 1:
    #     return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码失败')

    # 7.存储短信验证码到redis中:短信验证码在redis中的有效期一定要和短信验证码的提示信息一致
    try:
        redis_store.set('Mobile:' + mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码失败')

    # 8.响应短信发送的结果
    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Ejemplo n.º 22
0
def index():
    #测试redis数据哭
    from iHome import redis_store
    redis_store.set('name', 'sz007hehehe')
    # #测试session:flask自带的session模块,用于存储session
    # from flask import session
    # session['name'] = 'sz07sz07'
    return 'index'
Ejemplo n.º 23
0
def send_sms_code():
    """发送短信验证码
    1.获取参数:手机号,验证码,uuid
    2.判断是否缺少参数,并对手机号格式进行校验
    3.获取服务器存储的验证码
    4.跟客户端传入的验证码进行对比
    5.如果对比成功就生成短信验证码
    6.调用单例类发送短信
    7.如果发送短信成功,就保存短信验证码到redis数据库
    8.响应发送短信的结果
    """
    # 1.获取参数:手机号,验证码,uuid
    json_str = request.data
    json_dict = json.loads(json_str)
    mobile = json_dict.get('mobile')
    imageCode_client = json_dict.get('imageCode')
    uuid = json_dict.get('uuid')

    # 2.判断是否缺少参数,并对手机号格式进行校验
    if not all([mobile, imageCode_client, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机格式错误')

    #3.获取服务器存储的验证码
    try:
        imageCode_server = redis_store.get('ImageCode:%s' % uuid)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询验证码失败')
    if not imageCode_server:
        return jsonify(errno=RET.NODATA, errmsg='状态码不存在')

    #4.跟客户端传入的验证码进行对比
    if imageCode_client.lower() != imageCode_server.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='验证码输入有误')

    #5.如果对比成功就生成短信验证码
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug(sms_code)

    #6.调用单例类发送短信
    result = CCP().send_sms_code(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], '1')
    print result
    if result != 1:
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码失败')

    #7.如果发送短信成功,就保存短信验证码到redis数据库
    try:
        redis_store.set('SMS:%s' % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码失败')

    #8.响应发送短信的结果
    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Ejemplo n.º 24
0
def sms_code():
    # json局部刷新
    # 获取手机号, 验证码, 验证码id
    # 判断数据是否存在
    # 判断是否是手机号
    # 判断验证码是否正确
    # 从redis中查询数据, 如果查询出错返回错误信息
    # 如果没有查到数据返回错误信息
    # 判断验证码是否存在 不存在返回错误信息
    # 判断验证码是否输入正确, 不正确返回错误信息
    # 因为下面要用到第三方接口所以要try一下
    # 调用接口, 发送短信 验证码
    # 如果出错返回errno: & errmsg:
    # 返回状态信息
    # 获取手机号, 验证码

    # 获取手机号, 验证码
    req_data = request.data
    req_dic = json.loads(req_data)
    mobile = req_dic.get('mobile')
    imageCode = req_dic.get('imageCode')
    imageId = req_dic.get('imageId')
    # 判断数据是否存在
    if not all([mobile, imageCode, imageId]):
        return jsonify(errno=RET.PARAMERR, errmsg=u'数据不完整')
    # 判断是否是手机号
    if not re.match(r'1[3456789]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg=u'手机号错误')
    # 判断验证码是否正确
    # 从redis中查询数据, 如果查询出错返回错误信息
    try:
        red_data = redis_store.get('image_code:%s' % imageId)
    except Exception as e:
        current_app.logger.error(u'查不到数据')
        return jsonify(errno=RET.DBERR, errmsg=u'查询验证码失败')
    # 如果没有查到数据, 验证码不存在
    if not red_data:
        return jsonify(errno=RET.DBERR, errmsg=u'验证码过期')
    # 判断验证码是否输入正确, 不正确返回错误信息
    if red_data != imageCode:
        return jsonify(errno=RET.DBERR, errmsg=u'验证码输入错误')
    # 调用接口, 发送短信 验证码
    # 如果出错返回errno: & errmsg:
    sms_cod = '%06.f' % random.randint(0, 999999)
    print '短信验证码:%s' % sms_cod
    try:
        redis_store.set('%ssms_code:' % mobile, sms_cod)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg=u'短信验证码发送失败')
    try:
        status = CCP().sendtemplatessms(mobile, [sms_cod, '1'], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg=u'短信验证码发送失败')
    if status == 1:
        return jsonify(errno=RET.OK, errmsg=u'短信发送成功')
    return jsonify(errno=RET.THIRDERR, errmsg=u'短信验证码发送失败')
Ejemplo n.º 25
0
def send_sms():
    """1 接收参数, 判断是否有值
    2 校验手机号是否合理
    3 取出传过来的图片ID, 去Redis查询对应的数值, 是否相等
    4 校验图片验证码是否正确
    5 生成内容, 给手机发送验证码
    6 保存手机验证码的内容和手机号ID
    7 返回发送成功的响应"""
    data = request.data
    data_dict = json.loads(data)
    mobile = data_dict.get("mobile")
    image_code = data_dict.get("image_code")
    image_code_id = data_dict.get("image_code_id")
    # print mobile
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    if not re.match("^1[3578][0-9]{9}$", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不正确")

    try:
        real_image_code = redis_store.get("ImageCode_" + image_code_id)
        if real_image_code:
            redis_store.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(rerrno=RET.DBERR, errmsg="获取图片验证码失败")
    print real_image_code
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")

    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        user = None  # 如果查询时出现错误,也需要给user初始化,如果不初始化,会报未定义的异常
        current_app.logger.error(e)
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="该手机已被注册")

    # 5生成内容, 给手机发送验证码
    phone_code = random.randint(0, 999999)
    sms_code = "%06d" % phone_code
    # current_app.logger.info(sms_code)
    current_app.logger.debug("短信验证码的内容:%s" % sms_code)
    #result = CCP().send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES / 60],"1")
    # if result != 1:
    #     return jsonify(error=RET.THIRDERR,errmsg="发送短信失败")
    #     # 6. redis中保存短信验证码内容

    try:
        print mobile
        redis_store.set("SMS_" + mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")
    # 7. 返回发送成功的响应
    return jsonify(errno=RET.OK, errmsg="发送成功")
Ejemplo n.º 26
0
def send_sms_code():
    """发送验证码
    1.获取用户输入的手机号 验证码 uuid
    2.判断获取的参数不为空,并校验手机号
    3.获取数据库中存储的验证码text
    4.用用户输入的验证码与数据库中的验证码对比
    5.对比成功后生成短信验证码
    6. 调用单例发送短信验证码
    7.如果短信验证码发送成功,则把短信验证码保存到redis数据库
    8.响应发送短信的结果
    """
    #1.获取用户输入的手机号 验证码 uuid
    #因为前端传过来的值是json格式的字符串,所以用data接收
    json_str = request.data
    #将json格式的字符串转为字典格式获取手机号 验证码 uuid
    json_dict = json.loads(json_str)
    mobile = json_dict.get('mobile')
    imageCode_client = json_dict.get('imageCode')
    uuid = json_dict.get('uuid')
    #2.判断获取的参数不为空,并校验手机号
    #判断参数是否存在
    if not all([mobile, imageCode_client, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')
    #判断手机格式是否正确
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号错误')
    #3.获取数据库中存储的验证码text
    try:
        imageCode_server = redis_store.get('ImageCode:%s' % uuid)

    except Exception as e:
        return jsonify(errno=RET.DBERR, errmsg='查询验证码失败')
    if not imageCode_server:
        return jsonify(errno=RET.NODATA, errmsg='验证码不存在')
    #4.用用户输入的验证码与数据库中的验证码对比
    if imageCode_client.lower() != imageCode_server.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='输入的验证码错误')
    #5.对比成功后生成短信验证码
    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug(sms_code)

    #6. 调用单例发送短信验证码
    # result = CCP().send_sms_code(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],'1')
    # if result != 1:
    #     return jsonify(errno = RET.THIRDERR,errmsg = '短信验证码发送失败')
    #7.如果短信验证码发送成功,则把短信验证码保存到redis数据库
    try:
        redis_store.set('SMS:%s' % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)

    except Exception as e:
        current_app.logger.debug(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码失败')

    #8.响应发送短信的结果
    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Ejemplo n.º 27
0
def send_sms_code():
    """
    发送短信息验证码
    :return:
    """
    # 1.获取参数(手机号,图片验证码,图片验证码id)
    # 获取json
    req_data = request.data
    req_dict = json.loads(req_data)

    mobile = req_dict.get('mobile')
    image_code = req_dict.get('image_code')
    image_code_id = req_dict.get('image_code_id')

    # 2.判断参数的完整性并且进行参数校验
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg=' ')

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

    # 3.从redis中获取对应的图片验证码
    try:
        real_image_code = redis_store.get('imagecode:%s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询图片验证码错误')
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期')
    # 4.对比图片验证码
    image_code = image_code.encode("utf-8")
    if real_image_code != image_code:
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')
    # 5.发送短信通知
    # 生成短信验证码
    sms_code = '%06d' % random.randint(0, 999999)  #333
    # 发送短信验证码
    res = CCP().send_template_sms(
        mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1)

    if res != 1:
        return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")
    try:
        redis_store.set('sms_code:%s' % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存验证码失败')
    # 6.返回消息
    return jsonify(errno=RET.OK, errmsg='发送短信成功')
Ejemplo n.º 28
0
def get_image_code():
    cur_id = request.args.get('cur_id')
    if not cur_id:
        abort(403)

    name, text, data = captcha.generate_captcha()
    current_app.logger.info('图片验证码'+text)
    try:
        redis_store.set('imagecode:%s' %cur_id, text, constants.IMAGE_CODE_REDIS_EXPIRES )
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg='保存图片验证码失败')


    response = make_response(data)
    response.headers['Content-Type'] = 'image/jpg'
    return response
Ejemplo n.º 29
0
def send_mes_code():
    # 1. 获取参数:手机号码,图片验证码,uuid
    json_str = request.data
    json_dict = json.loads(json_str)
    mobile = json_dict.get('mobile')
    imagecode = json_dict.get('imagecode')
    uuid = json_dict.get('uuid')

    # 2. 验证参数是否为空,并且对手机号码进行验证
    if not all([mobile, imagecode, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码格式错误')
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码格式错误')
    # 3.获取服务器存储的验证码,uuid为k,进行比较,
    try:
        redis_imagecode = redis_store.get('ImageCode:' + uuid)
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询服务器验证码失败')
    # 验证码对比
    if imagecode.lower() != redis_imagecode.lower():
        return jsonify(errno=RET.DATAERR, errmsg='验证码输入有误')
    #判断是否为空或者过期
    if not redis_imagecode:
        return jsonify(errno=RET.NODATA, errmsg='验证码不存在')

    # 4.对比成功,生成短信验证码为字符串
    mobile_code = '%06d' % random.randint(0, 999999)
    logging.debug('短信验证码为:' + mobile_code)
    # 6.使用云通讯将短信验证码发送到注册用户手中
    #result = CCP().send_template_sms(mobile,[mobile_code,constants.SMS_CODE_REDIS_EXPIRES/60],'1')
    #if result != 1:
    #return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码失败')

    # 7.存储短信验证码到redis中:短信验证码在redis中的有效期一定要和短信验证码的提示信息一致
    try:
        redis_store.set('Mobile:' + mobile, mobile_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码失败')

    # 8.响应短信发送的结果
    # response = make_response(result)
    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Ejemplo n.º 30
0
def get_image_code():
    """提供图片验证码
       1.获取uuid,并校验uuid
       2.生成图片验证码
       3.使用redis数据库缓存图片验证码,uuid作为key
       4.响应图片验证码
       """
    # 1.获取uuid,并校验uuid
    uuid = request.args.get('uuid')
    if not uuid:
        abort(403)

    # 2.生成图片验证码
    name, text, image = captcha.generate_captcha()
    # print text
    # logging.debug('验证码内容为:' + text)
    current_app.logger.debug('app验证码内容为:' + text)

    try:
        # 3.使用redis数据库缓存图片验证码,uuid作为key

        # 如果有last_uuid
        if last_uuid:
            redis_store.delete('ImageCode:%s' % last_uuid)

        # 过期时间300秒
        redis_store.set('ImageCode:%s' % uuid, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:

        logging.error(e)
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存验证码失败')

    # 记录当前的uuid,方便下次使用时作为上一次的uuid,删除上次的text

    global last_uuid
    last_uuid = uuid

    # 4.响应图片验证码
    # 修改响应头信息,指定响应的内容是image/jpg
    response = make_response(image)
    response.headers['Content-Type'] = 'image/jpg'
    # 响应图片验证码
    return response