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()
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()
def send_message(): phone = request.args.get('phone') if not phone: return xtjson.json_params_error(message=u'必须指定一个手机号码') if xtcache.get(phone): return xtjson.json_params_error(message=u'验证码已发送,60秒内有效') app_key = contants.APP_KEY app_secret = contants.APP_SECRET req = top.setDefaultAppInfo(app_key,app_secret) req = top.api.AlibabaAliqinFcSmsNumSendRequest() req.extend="" req.sms_type='normal' req.sms_free_sign_name = contants.ALIDAYU_SIGN_NAME graph_captcha = captcha.gene_text() req.sms_param = "{code:%s}"% graph_captcha req.rec_num = phone.decode('utf-8').encode('ascii') req.sms_template_code = contants.ALIDAYU_TEMPLATE_CODE try: resp = req.getResponse() xtcache.set(phone,graph_captcha) print xtcache.get(graph_captcha) return xtjson.json_result() except Exception,e: print e return xtjson.json_server_error()
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()
def cms_send_email(request): if request.method=='POST': form=cf.CMSMailForm(request.POST) form.set_request(request) if not form.is_valid(): return xtjson.json_params_error(message=form.get_error()) else: to_mail=form.cleaned_data.get('email') mail=send_mail(to_email=to_mail) if mail: return xtjson.json_result(message='邮件发送成功!') else: return xtjson.json_server_error(message='请检查邮箱后再试')
def add_board(): if flask.request.method == 'POST': form = CMSNameForm(flask.request.form) if form.validate(): try: name = form.name.data board = BoardModel(name=name, author=flask.g.cms_user) db.session.add(board) db.session.commit() return xtjson.json_result(data=board.to_dict()) except Exception, e: return xtjson.json_server_error() else: print form.get_error() return xtjson.json_params_error(message=form.get_error())
def mail_captcha(): email = flask.request.args.get('email') if xtcache.get(email): return xtjson.json_params_error(u'该邮箱已经发送验证码了!') source = list(string.letters) for x in xrange(0, 10): source.append(str(x)) captcha_list = random.sample(source, 4) captcha = ''.join(captcha_list) if xtmail.send_mail(subject=u'潭州Python学院邮件验证码', receivers=email, body=u'邮箱验证码是:' + captcha): xtcache.set(email, captcha) return xtjson.json_result() else: return xtjson.json_server_error()
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()
def mail_captcha(): # /mail_captcha/[email protected]/ # /mail_captcha/[email protected] email = flask.request.args.get('email') if xtcache.get(email): return xtjson.json_params_error(u'该邮箱已经发送验证码了!') source = list(string.ascii_letters) for x in range(0, 10): source.append(str(x)) captcha_list = random.sample(source, 4) captcha = ''.join(captcha_list) if xtmail.send_mail(subject=u'知了课堂Python学院邮件验证码', receivers=email, body=u'邮箱验证码是:' + captcha): # 1. 为了下次可以验证邮箱和验证码 # 2. 为了防止用户不断的刷这个接口 xtcache.set(email, captcha) return xtjson.json_result() else: return xtjson.json_server_error()
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()