Ejemplo n.º 1
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    if not telephone:
        return xtjson.json_params_error(message=u'手机号不能为空!')
    p2 = re.compile('^0\d{2,3}\d{7,8}$|^1[3587]\d{9}$|^147\d{8}')
    phonematch = p2.match(telephone)
    if not phonematch:
        return xtjson.json_params_error(message=u'手机号格式错误')
    tel = FrontUser.query.filter_by(telephone=telephone).first()
    if tel:
        return xtjson.json_params_error(message=u'该手机号已被注册,请勿重复注册')
    if xtcache.get(telephone):
        return xtjson.json_params_error(message=u'验证码已发送,请于1分钟后重新发送')
    app_key = constants.ALIDAYU_APP_KEY
    app_secret = constants.ALIDAYU_APP_SECRET
    req = top.setDefaultAppInfo(app_key, app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend = ""
    req.sms_type = 'normal'
    req.sms_free_sign_name = constants.ALIDAYU_SIGN_NAME
    # 给模版的参数
    captcha = Captcha.gene_text()
    req.sms_param = "{code:'%s'}" % captcha
    req.rec_num = telephone.decode('utf-8').encode('ascii')
    req.sms_template_code = constants.ALIDAYU_TEMPLATE_CODE
    try:
        resp = req.getResponse()
        xtcache.set(telephone, captcha)
        return xtjson.json_result()
    except Exception, e:
        print e
        return xtjson.json_server_error()
Ejemplo n.º 2
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    # 获取用户名,用于发送短信验证码显示用户名
    # username = flask.request.args.get('username')
    if not telephone:
        return xtjson.json_params_error(message=u'必须指定手机号码!')
    if xtcache.get(telephone):
        return xtjson.json_params_error(message=u'验证码已发送,请1分钟后重复发送!')
    # if not username:
    #     return xtjson.json_params_error(message=u'必须输入用户名!')
    # 阿里大于APP_KEY及APP_SECRET
    app_key = constants.APP_KEY
    app_secret = constants.APP_SECRET
    req = top.setDefaultAppInfo(app_key, app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend = ""
    req.sms_type = 'normal'
    # 签名名称
    req.sms_free_sign_name = constants.SIGN_NAME
    # 随即生成字符串
    captcha = Captcha.gene_text()
    # 设置短信的模板
    req.sms_param = "{code:%s}" % captcha
    # req.sms_param = "{username:%s,code:%s}" % (username, captcha)
    req.rec_num = telephone.decode('utf-8').encode('ascii')
    req.sms_template_code = constants.TEMPLATE_CODE
    try:
        resp = req.getResponse()
        xtcache.set(telephone, captcha)
        return xtjson.json_result()
    except Exception, e:
        print e
        return xtjson.json_server_error()
Ejemplo n.º 3
0
def mail_captcha():

    # 生成验证码

    email = flask.request.args.get('email')

    if xtcache.get(email):
        return xtjson.json_params_error('该邮箱已经发送验证码了!')

    captcha = Captcha.gene_text()

    # 给请求修改邮箱的用户发送邮箱验证码

    # if xtmail.send_mail(subject='DUDU论坛邮箱验证码', receivers=email, body='邮箱验证码:'+captcha):
    #     # 1.为了下次可以验证邮箱和验证码
    #     # 2.为了防止用户不断的刷这个接口
    #     xtcache.set(email,captcha)
    #     return xtjson.json_result()
    # else:
    #     return xtjson.json_server_error()

    celery_send_mail.delay(subject='DUDU论坛邮箱验证码',
                           receivers=email,
                           body='邮箱验证码:' + captcha)
    return xtjson.json_result()
Ejemplo n.º 4
0
 def clean_captcha(self):
     # 获取验证码 name="captcha"
     captcha = self.cleaned_data.get('captcha', None)
     # 如果返回True 则返回验证码
     if not Captcha.check_captcha(captcha):
         raise forms.ValidationError(u'验证码错误!')
     else:
         return captcha
Ejemplo n.º 5
0
def graph_captcha():
    text, image = Captcha.gene_code()  #调用类方法,拿到验证码
    cache.set(text.lower(), text.lower())  #将图形验证码存储在memcache,内容全部变成小写
    out = BytesIO()  #初始化二进制流对像
    image.save(out, "png")  #将验证码存储在二进制流中,因为单纯的数据是不能被计算机识别的,只有先存储在二进制流中,并以png格式
    out.seek(0)  #保存后将指针移到开始位置,方便后面读取数据
    resp = make_response(out.read())  #从二进制流中读取数据以response对象
    resp.content_type = "/image/png"  #制定response数据类型
    return resp  #返回对象
Ejemplo n.º 6
0
def mail_captcha():
    email = flask.request.args.get('email')
    if xtcache.get(email):
        return xtjson.json_params_error(u'已经给该邮箱发送验证码,请勿重复发送!')
    captcha = Captcha.gene_text()
    if xqmail.send_mail(subject=u'论坛验证码',receivers=email,body=u'您的验证码为:'+captcha+u',请您注意保密'):
        xtcache.set(email,captcha)
        return xtjson.json_result()
    else:
        return xtjson.json_server_error()
Ejemplo n.º 7
0
def graph_captcha():
    text, image = Captcha.gene_code()
    outer = StringIO()
    image.save(outer, 'png')
    content = outer.getvalue()
    response = make_response(content)
    response.content_type = 'image/png'
    Utils_Redis.set(text.lower(), text.lower(), ex=2 * 60)

    return response
Ejemplo n.º 8
0
def common_captcha(request):
    text, image = Captcha.gene_code()

    out = StringIO()
    image.save(out, 'png')
    out.seek(0)
    response = HttpResponse(content_type='image/png')
    response.write(out.read())

    BBS_Redis.set(text.lower(), text.lower(), ex=2 * 60)
    return response
Ejemplo n.º 9
0
def send():
    form = SmscaptchaForm(request.form)
    if form.validate():
        mobile = request.form.get("telephone")
        captcha = Captcha.gene_text()  #调用验证码内容
        if send_captcha.send_capt(mobile, captcha):  #调用封装好的短信发送函数
            cache.set(mobile, captcha)  #将短信验证码存储在memcache中
            return restful.success(message="发送成功")  #成功直接调用不传参数
        else:
            return restful.paramserror(message="发送失败")
    else:
        message = form.errors.popitem()[1][0]
        return restful.paramserror(message)
Ejemplo n.º 10
0
def captcha(request):
    text,image = Captcha.gene_code()
    #image.save('text.png','png')
 ##    return HttpResponse(text)
    out = StringIO()
    image.save(out,'png')
    out.seek(0)
    response = HttpResponse(content_type='image/png')
    response.write(out.read())
    # key = text.lower()
    # value = key
    # cache.set(key,value,120)
    return response
Ejemplo n.º 11
0
def graph_captcha():
    # 验证码和图片
    text, image = Captcha.gene_code()
    out = StringIO()
    image.save(out, 'png')
    # 将StringIO的指针指向开始的位置
    out.seek(0)
    # 把图片流给读出来
    response = flask.make_response(out.read())
    # 指定响应的类型
    response.content_type = 'image/png'
    xtcache.set(text.lower(), text.lower(), timeout=2 * 60)
    return response
Ejemplo n.º 12
0
def captcha(request):
    text, image = Captcha.gene_code()
    # image.save('text.png','png') # I/0
    #需要通过StringIO这个类来把图片当成流的形式返回给客户端
    out = StringIO()  #获取"管道"
    image.save(out, 'png')  #把图片保存到管道中
    out.seek(0)  #移动文件指针到\第0个位置
    response = HttpResponse(content_type='image/png')
    response.write(out.read())
    # 把验证码数据写入到缓存中,过期时间是2分钟
    # key = text.lower()
    # value = key
    # cache.set(key,value,120)
    return response
Ejemplo n.º 13
0
def graph_captcha():
    # 验证码过期时间,默认2分钟
    timeout = flask.request.args.get('timeout', 2, int)
    # print 'timeout:', timeout
    text, image = Captcha.gene_code()
    out = StringIO()  # StringIO相当于是一个管道
    image.save(out, 'png')  # 把image塞到StingIO这个管道中
    out.seek(0)  # 将StringIO的指针指向开始的位置

    # 生成一个响应对象,out.read是把图片流给读出来
    response = flask.make_response(out.read())
    # 指定响应的类型
    response.content_type = 'image/png'
    xtcache.set(text.lower(), text.lower(), timeout=timeout * 60)
    return response
Ejemplo n.º 14
0
def graph_captcha():
    text, image = Captcha.gene_code()
    # StringIO 相当于一个管道
    out = StringIO()
    # 把image塞到StringIO这个管道中
    image.save(out, 'png')
    # 将StringIO的指针指向开始的地方 0
    out.seek(0)

    # 创建一个response响应对象, out.read是把图形读出来
    response = flask.make_response(out.read())
    # 指定响应的类型
    response.content_type = 'image/png'
    xtcache.set(text.lower(), text.lower(), timeout=3 * 60)
    return response
Ejemplo n.º 15
0
def manage_send_captcha_email():
    email=request.form.get('email')
    Captcha.number=6
    code=Captcha.gene_text()
    Captcha.number=4
    db_code=Utils_Redis.get('email')
    if db_code:
        return xtjson.json_params_error(message=u'已向该邮箱发送邮件了,请稍后再试!')
    else:
        Utils_Redis.set(email,code.lower(),ex=10*60)

        dic=dict(subject=u'报关系统修改邮箱验证码',receivers=email,body=u'您的验证码为 %s 在10分钟有效' %code)
        celery_send_email.delay(**dic)

        return xtjson.json_result()
Ejemplo n.º 16
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    if not telephone:
        return xtjson.json_params_error(message='必须指定手机号码!')

    if xtcache.get(telephone):
        return xtjson.json_params_error(message='1分钟内请勿重复发送!')

    code = Captcha.gene_text_num()
    # /accoutn/sms_captcha/?telephone=12345678900
    # telephone = flask.request.form.get('telephone')
    result = aliyun.send_sms(telephone, code=code)
    print(result)
    if result:
        xtcache.set(telephone, code)
        return xtjson.json_result()
    else:
        return xtjson.json_server_error()
Ejemplo n.º 17
0
def captcha(request):
    text, image = Captcha.gene_code()
    # 将图片保存到本地
    # image.save('text.png','png')
    # 需要通过StringIO这个类来把图片当成流的形式返回给客户端
    out = StringIO()  # 获取管道
    # 将图片保存到管道
    image.save(out, 'png')
    # 移动文件指针到第0位置
    out.seek(0)
    # 指定返回的是image的png格式
    response = HttpResponse(content_type='image/png')
    # 从0位置开始读图片
    response.write(out.read())
    # 把验证码数据写入到缓存中,过期时间设置2分钟
    # key = text.lower()
    # value = key
    # cache.set(key,text,120)
    return response
Ejemplo n.º 18
0
    def validate(self):
        if not super(SMSCaptchaForm,self).validate():
            return False
        else:
            phone=self.phone.data

            db_cache=Utils_Redis.get(phone)

            Captcha.number=6
            text=Captcha.gene_text()
            Captcha.number = 4


            if db_cache:
                self.phone.errors.append(u'已经向该手机发送验证码了,请%s分钟后再试!' %SMS_TIME)
                return False

            else:
                Utils_Redis.set(phone,text.lower(),ex=SMS_TIME*60)
                celery_send_sms.delay(phone,text)
                return True
Ejemplo n.º 19
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    if not telephone:
        return xtjson.json_params_error(message=u'必须指定手机号码!')

    if xtcache.get(telephone):
        return xtjson.json_params_error(message=u'验证码已发送,请1分钟后重复发送!')

    app_key = constants.ALIDAYU_APP_KEY
    app_secret = constants.ALIDAYU_APP_SECRET
    req = AlibabaAliqinFcSmsNumSendRequest(app_key, app_secret)
    req.extend = ""
    req.sms_type = 'normal'
    req.sms_free_sign_name = constants.ALIDAYU_SIGN_NAME
    captcha = Captcha.gene_text()
    req.sms_param = "{code:%s}" % captcha
    req.rec_num = telephone
    req.sms_template_code = constants.ALIDAYU_TEMPLATE_CODE
    try:
        resp = req.getResponse()
        xtcache.set(telephone, captcha)
        return xtjson.json_result()
    except Exception as e:
        return xtjson.json_server_error()
Ejemplo n.º 20
0
 def validate_captcha(self,field):
     captcha = field.data
     if not Captcha.check_captcha(captcha):
         wtforms.ValidationError(u'验证码错误!')
Ejemplo n.º 21
0
    def clean_captcha(self):
        captcha = self.cleaned_data.get('captcha')
        if not Captcha.check_captcha(captcha):
            raise forms.ValidationError(u'验证码错误')

        return captcha
Ejemplo n.º 22
0
 def validate_graph_captcha(self,field):
     graph_captcha = field.data
     if not Captcha.check_captcha(graph_captcha):
         raise wtforms.ValidationError(u'图形验证码错误')
Ejemplo n.º 23
0
    if not telephone:
        return xtjson.json_params_error(message=u'必须指定手机号码!')

    if xtcache.get(telephone):
        return xtjson.json_params_error(message=u'验证码已经发送,请等待1分钟后再次发送!')

    if len(telephone) != 11:
        return xtjson.json_params_error(message=u'手机号码格式不正确,请重新填写!')

    try:
        int(telephone)
    except Exception, e:
        # print e
        return xtjson.json_params_error(message=u'手机号码格式不正确,请重新填写!')

    captcha = Captcha.gene_text()
    app_key = constants.ALIDAYU_APP_KEY
    app_secret = constants.ALIDAYU_APP_SECRET
    req = top.setDefaultAppInfo(app_key, app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend = ""
    req.sms_type = 'normal'
    req.sms_free_sign_name = constants.ALIDAYU_APP_SIGN_NAME
    req.sms_param = constants.ALIDAYU_APP_PARAM % ('Jay', captcha)
    req.rec_num = telephone.decode('utf-8').encode('ascii')
    req.sms_template_code = constants.ALIDAYU_APP_TEMPLATE_CODE
    try:
        # print '短信验证码是:', captcha
        # 设置缓存
        xtcache.set(telephone, captcha)
        resp = req.getResponse()