def send_sms(mobile, dates, temp_id): ccp = CCP() try: result = ccp.send_template_sms(mobile, dates, temp_id) except Exception: result = -2 return result
def send_sms(to, datas, temp_id): """发送短信的异步任务""" ccp = CCP() try: result = ccp.send_template_sms(to, datas, temp_id) except Exception as e: result = -2 return result
def send_sms(to, datas, temp_id): """发送短信的异步任务""" ccp = CCP() ccp.send_template_sms(to, datas, temp_id) # celery开启的命令 # celery -A ihome.tasks.task_sms worker -l info
def send_sms(tid, mobile, datas): """发送短信的异步任务""" ccp = CCP() try: result = ccp.send_template_sms(tid, mobile, datas) except Exception as e: result = -2 return result
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='参数不完整') # 业务逻辑处理 # 从redis中取出真实的图片验证码 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 is None: # 表示图片验证码过期或者没有 return jsonify(errno=RET.NODATA, errmsg='图片验证失败') # 与用户填写的值进行对比 if real_image_code.lower() != image_code.lower(): # 表示用户填写错误 return jsonify(errno=RET.DATAERR, errmsg='图片验证错误') # 判断手机号是否存在 try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) # 不用终止,可以继续发送短信 else: if user is None: # 表示手机号已存在 return jsonify(errno=RET.DATAEXIST, errmsg='手机号已存在') # 如果手机号不存在,则生成短信验证码 sms_code = '%06d' % random.randint(0, 999999) # 保存真实的短信验证码 try: redis_store.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 保存发送给这个手机号的记录,防止用户在60s内再次发送短信 redis_store.setex('send_sms_code_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='保存短信验证码异常') # 发送短信 ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES / 60)], 1) # 返回值 if result == 0: # 发送成功 return jsonify(errno=RET.OK, errmsg='发送成功') else: return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
def send_sms(to, datas, temp_id): """"发送短信的异步任务""" import ssl # 取消证书验证 ssl._create_default_https_context = ssl._create_unverified_context # 修改_serverIP的值 _serverIP = 'app.cloopen.com' ccp = CCP() ccp.send_template_sms(to, datas, temp_id)
def get_sms_code(mobile): """ 发送手机验证码 :param mobile: :return: """ 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: redis_image_code = redis_store.get("image_code_%s" % image_code_id) redis_store.delete("image_code_%s" % image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库异常") if redis_image_code is None: return jsonify(errno=RET.NODATA, errmsg="验证码过期") if redis_image_code.lower() != image_code.lower(): return jsonify(errno=RET.DATAERR, errmsg="验证码错误") try: # 判断在60秒内用户有没有发送过验证码,如果有表示用户请求频繁,拒绝处理 send_flg = redis_store.get("send_sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) else: if send_flg is not None: # 60秒内发送过验证码 return jsonify(errno=RET.REQERR, errmsg="请求过于平凡,请60秒后再试") # 生成手机验证码信息 sms_code = "%06d" % random.randint(0, 999999) try: redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) redis_store.setex("send_sms_code_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存验证码异常") try: ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES/60)], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="发送验证码异常") if result == 0: return jsonify(errno=RET.OK, errmsg="发送成功") else: return jsonify(errno=RET.THIRDERR, errmsg="发送失败")
def send_sms(): # 1. 接收参数并判断是否有值 receiver = request.json mobile = receiver.get('mobile') image_code = receiver.get('image_code') image_code_id = receiver.get('image_code_id') if not all([mobile, image_code, image_code_id]): current_app.logger.error("参数不足") # 错误信息 err_dict = {"errno": RET.PARAMERR, "errmsg": "参数不足"} return jsonify(err_dict) # 2. 校验手机号是正确 if not re.match(r"1[2345678][0-9]{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误") # 3. 通过传入的图片编码去redis中查询真实的图片验证码内容 try: real_image_code = sr.get("Iamge_Code_%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.PARAMERR, errmsg="图片失效") # 4. 进行验证码内容的比对 if image_code.lower() != real_image_code.lower(): return jsonify(errno=RET.PARAMERR, errmsg="图片错误") # 5. 生成发送短信的内容并发送短信 # TODO: 判断手机号码是否已经注册 【提高用户体验】 try: user = User.query.filter(User.mobile == mobile).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询用户数据异常") if user: return jsonify(errno=RET.DATAEXIST, errmsg="手机号码已经注册") real_sms_code = random.randint(0, 999999) # 6位的密码 real_sms_code = "%06d" % real_sms_code # 发送短信验证码 try: result = CCP().send_template_sms(mobile, {real_sms_code, 5}, 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常") # 发送短信验证码失败 if result == -1: return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常") # 6. redis中保存短信验证码内容 sr.setex("SMS_CODE_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, real_sms_code) # 7. 返回发送成功的响应 return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
def login_send_sms(): # 1. 接收参数并判断是否有值 mobile = request.json.get('mobile') if not mobile: current_app.logger.error('参数不足') return jsonify(errno=RET.PARAMERR, errmsg='请输入手机号码') # 2. 校验手机号是正确 if not re.match(r"1[2345678][0-9]{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误") # 3. 生成发送短信的内容并发送短信 # TODO: 判断手机号码是否已经注册 【提高用户体验】 try: user = User.query.filter(User.mobile == mobile).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询用户数据异常") if not user: # 当前手机号码未注册 return jsonify(errno=RET.USERERR, errmsg="当前手机号码未注册") # 3.4.1 生成6位的随机短信 real_sms_code = random.randint(0, 999999) # 6位,前面补零 real_sms_code = "%06d" % real_sms_code print(real_sms_code) # 4.发送短信验证码成功 try: result = CCP().send_template_sms(mobile, {real_sms_code, 5}, 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常") # 发送短信验证码失败:告知前端 if result == -1: return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常") # 6. redis中保存短信验证码内容 sr.setex("SMS_CODE_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, real_sms_code) # 7. 返回发送成功的响应 return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
def get_sms_code(mobile): # 1. 获取参数 image_code = request.args.get('image_code') image_code_id = request.args.get('image_code_id') # 2. 效验参数 if not all([image_code, image_code_id]): response_dict = {'errno': RET.PARAMERR, 'errmsg': '参数不全, 请填写完整'} return jsonify(response_dict) # 3. 逻辑处理 # 1. 和redis数据对比: 获取redis数据, 判断是否为None, 无论正确与否都要删除, 和用户传入的数据做对比 # 2. 判断用户是否已经注册过, 判断是否为空 # 3. 发送短信验证码: 自行生成验证码, 保存到redis, 调用云通信接口发送验证码 # 3.1 获取redis数据 --> try try: real_image_code = redis_store.get('image_code_' + image_code_id) except Exception as e: logging.error(e) response_dict = {'errno': RET.DBERR, 'errmsg': '访问数据库异常'} return jsonify(response_dict) # 3.2判断是否为None # 数据库, 获取空值就是None if real_image_code is None: response_dict = {'errno': RET.NODATA, 'errmsg': '验证码已过期/或已删除'} return jsonify(response_dict) # 3.3 无论正确与否都要删除 try # 图片验证码通常只能用一次 try: redis_store.delete('image_code_' + image_code_id) except Exception as e: logging.error(e) # 理论上应该返回错误信息. 但是从用户体验的角度来说. 用户没有做错事, 只是服务器删除失败. # 就算没有立即删掉, 2分钟后也会过期. 可以考虑不删除 --> 产品经理角度(将来和产品经理商量一下) # 3.4 和用户传入的数据做对比 # 142B 142b 为了忽略大小写的问题, 建议全大写或小写 if image_code.lower() != real_image_code.lower(): response_dict = {'errno': RET.DATAERR, 'errmsg': '验证码输入错误'} return jsonify(response_dict) # 3.5 判断用户是否已经注册过, 判断是否为空 try try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: logging.error(e) # 理论上, 需要返回错. 从体验角度讲, 后面的立即注册也会去检测用户是否注册. 因此可以考虑本次错误不返回 else: # 判断用户不为空, 说明已经注册过 if user is not None: response_dict = {'errno': RET.DATAEXIST, 'errmsg': '该手机号已存在'} return jsonify(response_dict) # 3.6 自行生成验证码 # 06d: 要求6位数, 不足以0补齐 sms_code = '%06d' % random.randint(0, 999999) # 3.7 保存到redis try try: # 第一个参数: key , 第二个参数: 过期时间 第三个参数: value redis_store.setex('sms_code_' + mobile, constants.SMS_CODE_REDIS_EXPIRE, sms_code) except Exception as e: logging.error(e) response_dict = {'errno': RET.DBERR, 'errmsg': '访问redis出错'} return jsonify(response_dict) # 3.8 调用云通信接口发送验证码 try ccp = CCP() try: status_code = ccp.send_template_sms( mobile, [sms_code, str(constants.SMS_CODE_REDIS_EXPIRE / 60)], 1) except Exception as e: logging.error(e) response_dict = {'errno': RET.THIRDERR, 'errmsg': '发送异常'} return jsonify(response_dict) # 4. 返回数据 if status_code == '000000': # 发送成功 response_dict = {'errno': RET.OK, 'errmsg': '发送成功'} return jsonify(response_dict) else: # 发送失败 response_dict = {'errno': RET.THIRDERR, 'errmsg': '发送失败'} return jsonify(response_dict)
def send_template_sms(to, datas, temp_id): """发送短信""" ccp = CCP() ret = ccp.send_template_sms(to, datas, temp_id) return ret
def get_sms_codes(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]): resp = {'errno': RET.PARAMERR, 'errmsg': '参数不全'} return jsonify(resp) # 三 逻辑处理 #1.从redis中获取数据对比 #2.判断用户是否注册过 #3.调用第三方SDK发短信 # 1.从redis中获取数据对比 # 1_1 获取redis数据 try: real_image_code = redis_store.get('image_code_%s' % image_code_id) except Exception as e: logging.error(e) resp = {'errno': RET.DBERR, 'errmsg': 'redis读取失败'} return jsonify(resp) # 1_2 判断数据是否为None # 数据库获取操作, 一定要判断None. 只要查询不出数据,就是返回None if real_image_code is None: resp = {'errno': RET.DBERR, 'errmsg': '验证码过期, 请重新刷新获取'} return jsonify(resp) # 1_3 无论是否对比成功, 先删除服务器的验证码 --> 图像验证码只能验证一次 try: redis_store.delete('image_code_%s' % image_code_id) except Exception as e: logging.error(e) resp = {'errno': RET.DBERR, 'errmsg': 'redis删除失败'} return jsonify(resp) # 1_4 对比验证码数据 # ABCD = abcd if real_image_code.lower() != image_code.lower(): resp = {'errno': RET.DATAERR, 'errmsg': '验证码填写错误, 请刷新后重试'} return jsonify(resp) # 2.判断用户是否注册过 # 2_1 查询数据库的操作 # 2_2 判断数据是否为None try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: logging.error(e) resp = {'errno': RET.DBERR, 'errmsg': 'mysql查询失败'} return jsonify(resp) else: # 执行成功走else if user is not None: # 用户已经注册过 resp = {'errno': RET.DATAEXIST, 'errmsg': '该用户的手机号已注册,请更换手机号'} return jsonify(resp) #3.调用第三方SDK发短信 # 3_1 创建6位短信验证码 000000 0000 # import random # random.randint(0, 999999) # %06d: 6位数字, 不足以0补齐 sms_code = '%06d' % random.randint(0, 999999) # 3_2 保存到redis中 try: redis_store.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRE, sms_code) except Exception as e: logging.error(e) resp = {'errno': RET.DBERR, 'errmsg': 'redis保存失败'} return jsonify(resp) # 3_3 发送验证码 ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, constants.IMAGE_CODE_YTX_EXPIRE], 1) # 四 返回数据 if result == '000000': resp = {'errno': RET.OK, 'errmsg': '发送短信成功'} return jsonify(resp) else: resp = {'errno': RET.THIRDERR, 'errmsg': '发送短信失败'} return jsonify(resp)
def send_sms(to, datas, temp_id): ccp = CCP() ccp.send_template_sms(to, datas, temp_id)
def send_sms(to, datas, temp_id): """发送短信的异步任务""" ccp = CCP() ccp.sendTemplateSMS(to, datas, temp_id)
def send_sms(to, data, temp_id): """发送短信的异步任务""" ccp = CCP() ccp.send_template_sms(to, data, temp_id)
def send_sms(mobile, datas, temp_id): """发送短信的异步任务""" ccp = CCP() ccp.send_template_sms(mobile, datas, temp_id)
def send_sms_code(mobile): '''发送短信验证码''' #获取参数 image_code_id = request.args.get('image_code_id') image_code = request.args.get('image_code') #校验参数 if not all([image_code_id, image_code]): resp = {'errno': RET.PARAMERR, 'errmsg': '参数不完整'} return jsonify(resp) #业务处理 #取出真实图片验证码 try: real_image_code = redis_store.get('image_code_%s' % image_code_id) except Exception as e: current_app.logger.error(e) resp = {'errno': RET.DBERR, 'errmsg': '获取图片验证码失败'} return jsonify(resp) #判断验证码的有效期 if real_image_code is None: #表示redis中没有这个数据 resp = {'errno': RET.NODATA, 'errmsg': '图片验证码过期'} return jsonify(resp) #删除redis中的图片验证码,防止用户多次尝试一个图片验证码 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(): #表示用户填写错误 resp = {'errno': RET.DATAERR, 'errmsg': '图片验证码有误'} return jsonify(resp) #判断用户手机是否注册过 try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) else: if user is not None: #用户已经注册过 resp = {'errno': RET.DATAEXIST, 'errmsg': '用户手机号已经注册过了'} return jsonify(resp) #创建短信短信验证码 sms_code = "%06d" % random.randint(0, 999999) #保存短信验证码 try: redis_store.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) except Exception as e: current_app.logger.error(e) resp = {'errno': RET.DBERR, 'errmsg': '保存短信验证码异常'} return jsonify(resp) #发送验证码短信 try: ccp = CCP() # result=ccp.send_template_sms(mobile,[sms_code,str(constants.SMS_CODE_REDIS_EXPIRES/60)],1) result = tasks.send_template_sms.delay( mobile, [sms_code, str(constants.SMS_CODE_REDIS_EXPIRES / 60)], 1) except Exception as e: current_app.logger.error(e) resp = {'errno': RET.THIRDERR, 'errmsg': '发送短信异常'} return jsonify(resp) if result == 0: #发送成功 resp = {'errno': RET.OK, 'errmsg': '发送短信成功'} return jsonify(resp) else: resp = {'errno': RET.THIRDERR, 'errmsg': '发送短信失败'} return jsonify(resp)
def send_template_sms(to, datas, temp_id): """发送短信""" ccp = CCP() ccp.send_template_sms(to, datas, temp_id)
def send_sms(to, datas, temp_id): '''发送短信的异步任务''' ccp = CCP() ccp.send_template_sms(to, datas, temp_id)
def send_sms_code(mobile): """发送短信验证码""" # 获取参数 image_code_id = request.args.get("image_code_id") image_code = request.args.get("image_code") # 校验参数 if not all([image_code_id, image_code]): resp = {"errno": RET.PARAMERR, "errmsg": "参数不完整"} return jsonify(resp) # 取出真实的图片验证码 try: real_image_code = redis_store.get("image_code_%s" % image_code_id) except Exception as e: current_app.logger.error(e) resp = {"errno": RET.DBERR, "errmsg": "获取图片验证码失败"} return jsonify(resp) # 判断验证码的有效期 if real_image_code is None: # 表示redis中没有这个数据 resp = {"errno": RET.NODATA, "errmsg": "图片验证码过期"} return jsonify(resp) # 验证完毕后,直接删除验证码,防止用户暴力测试同一个验证码 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(): # lower可以将所有字母变成小写 # 表示用户填写有误 resp = {"errno": RET.DATAERR, "errmsg": "图片验证码有误"} return jsonify(resp) # 判断用户手机号是否注册过 try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) # resp = { # "errno": RET.DBERR, # "errmsg": "" # } # return jsonify(resp) else: if user is not None: # 用户已经注册过 resp = {"errno": RET.DATAEXIST, "errmsg": "用户手机号已经注册过"} return jsonify(resp) # 创建短信验证码 sms_code = "%06d" % random.randint(0, 999999) # 保存短信验证码 try: redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) except Exception as e: current_app.logger.error(e) resp = {"errno": RET.DBERR, "errmsg": "保存短信验证码异常"} return jsonify(resp) # 发送短信验证码 try: ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, str(constants.SMS_CODE_REDIS_EXPIRES / 60)], 1) except Exception as e: current_app.logger.error(e) resp = {"errno": RET.THIRDERR, "errmsg": "发送短信验证码异常"} return jsonify(resp) # 返回值 if result == 0: # 发送成功 resp = {"errno": RET.OK, "errmsg": "发送短信验证码成功"} return jsonify(resp) else: resp = {"errno": RET.THIRDERR, "errmsg": "发送短信验证码失败"} return jsonify(resp)
def send_sms(to, datas, temp_id): ccp = CCP() return ccp.send_temp_sms(to, datas, temp_id)
def send_template_sms(to, dates, temp_id): """发送短信""" ccp = CCP() ret = ccp.send_template_sms(to, dates, temp_id) return ret
def send_sms(to, datas, temp_id): """发送短信的异步任务""" ccp = CCP() # 可以返回状态值 return ccp.send_template_sms(to, datas, temp_id)
def send_template_sms(to, datas, tem_id): """发送短信""" cpp = CCP() cpp.send_template_sms(to, datas, tem_id)
def get_sms_code(mobile): """获取短信验证码""" # 获取数据 image_codes_id = request.args.get("image_codes_id") image_code = request.args.get("image_code") # 验证数据 if not all([image_codes_id, image_code]): return jsonify(errno=RET.PARAMERR, errmsg="数据不完整") # 业务逻辑处理 # 1. 验证图片验证码正确性 # 从redis中读取真实的图片验证码 try: real_image_code = redis_store.get("image_codes_%s" % image_codes_id) # 此时读取出来的值是二进制格式的! except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="redis数据库异常") # 判断图片验证码是否过期 if real_image_code is None: # 图片验证码不存在或者过期 return jsonify(errno=RET.NODATA, errmsg="图片验证码过期") # 删除redis中的图片验证码,防止用户使用同一个验证码验证多次 try: redis_store.delete("image_codes_%s" % image_codes_id) except Exception as e: current_app.logger.error(e) # 与用户输入的进行对比 if real_image_code.decode('utf-8').lower() != image_code.lower( ): # 在比较时,必须转为字符串才能比较,否则一直报图片验证码填写错误 # 用户填写的图片验证码错误 return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误") # 2. 发送短信验证码逻辑 # 判断此手机号是否在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: # 表示发送过 return jsonify(errno=RET.REQERR, errmsg="请求过于频繁,请60秒后重试") # 判断手机号是否已经存在 # try: # user = User.query.filter_by(mobile=mobile).first() # except Exception as e: # current_app.logger.error(e) # else: # 如果发生异常 user变量不存在 # if user is not None: # # 用户已经存在 # return jsonify(errno=RET.DATAEXIST, errmsg="手机号已经存在") # 如果不存在,就可以生成短信验证码 sms_code = "%06d" % random.randint(0, 999999) # %06d 表示六位数,不满六位用0补充 # 保存真实的短信验证码到redis数据库 try: redis_store.setex("sms_codes_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 保存给这个手机号发送过短信的记录,防止用户在60s内多次点击获取短信验证码 redis_store.setex("send_sms_code_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="redis数据库异常") # 发送短信 try: ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES / 60)], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="发送短信异常") # 改用celery异步执行发送短信的任务 # 调用后立即返回,返回值是一个对象 # result_obj = send_sms.delay(mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES/60)], 1) # print("result_obj = " % result_obj) # 暂时发不出短信,报网络错误 send_sms.delay( mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES / 60)], 1) # 通过异步任务对象,获取任务结果 # get方法默认是阻塞的 # ret = result_obj.get() # print("ret = %s" % ret) # 返回 # if result == 0: # return jsonify(errno=RET.OK, errmsg="发送成功") # else: # return jsonify(errno=RET.THIRDERR, errmsg="发送失败") return jsonify(errno=RET.OK, errmsg="发送成功")
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_id, image_code]): # 表示参数不完整 return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 业务逻辑处理 # 从redis中取出真实的图片验证码 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 is None: # 表示图片验证码没有或者过期 return jsonify(errno=RET.NODATA, errmsg="图片验证码失效") # 删除redis中的图片验证码,防止用户使用同一个图片验证码验证多次 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(errno=RET.DATAERR, errmsg="图片验证码错误") # 判断对于这个手机号的操作,在60秒内有没有之前的记录,如果有,则认为用户操作频繁,不接受处理 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: # 表示在60秒内之前有过发送的记录 return jsonify(errno=RET.REQERR, errmsg="请求过于频繁,请60秒后重试") # 判断手机号是否存在 try: user = User.query.filter_by(mobile=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) # 保存真实的短信验证码 try: redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 保存发送给这个手机号的记录,防止用户在60s内再次出发发送短信的操作 redis_store.setex("send_sms_code_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存短信验证码异常") # 发送短信 try: ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES/60)], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="发送异常") # 返回值 if result == 0: # 发送成功 return jsonify(errno=RET.OK, errmsg="发送成功") else: return jsonify(errno=RET.THIRDERR, errmsg="发送失败")
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="参数不完整") # 业务逻辑处理 # 从redis中取出真实的图片验证码 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 is None: # 表示图片验证码没有或者过期 return jsonify(errno=RET.NODATA, errmsg="图片验证失败") # 删除redis中的图片验证码,防止用户使用同一个图片验证码验证多次 try: redis_store.delete("image_code_%d" % image_code_id) except Exception as e: current_app.logger.error(e) # 与用户输入的值对比 if image_code.lower() != real_image_code.lower(): return jsonify(errno=RET.PARAMERR, 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="请求重复") # 判断手机是否存在 # 从数据库中的查询手机号是否存在 try: obj = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) else: if obj is not None: # 表示手机号已存在 return jsonify(errno=RET.DATAEXIST, errmsg="号码已存在") # 如果手机号不存在,则生成短信验证码 sms_code = "%06d" % random.randint(0, 999999) # 将短信验证码保存到redis中 try: # 记录名字,有效期,记录文本 redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 保存发送给这个手机号的记录,防止用户在60s内再次发送短信 redis_store.setex("send_sms_code_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) except Exception as e: # 记录日志 current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存短信验证码异常") # 发送短信 try: ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES / 60)], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="发送异常") # 返回值 if result == 0: # 发送成功 return jsonify(errno=RET.OK, errmsg="发送成功") else: return jsonify(errno=RET.THIRDERR, errmsg="发送失败")
def send_sms(to, datas, temp_id): """celery实现异步发送消息""" ccp = CCP() ccp.send_template_sms(to, datas, temp_id)
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='参数不完整') # 业务逻辑处理 # 从redis中取出真实的图片验证码 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 is None: # 表示图片验证码没有或者过期 return jsonify(errno=RET.NODATA, errmsg='图片验证码失效') # 删除redis中的图片验证码,防止用户使用同一个验证码验证多次 try: redis_store.delete('image_code_%s' % image_code_id) except Exception as e: current_app.logger.error(e) # 与用户填写的值对比 if real_image_code.lower() != real_image_code.lower(): # 表示用户填写错误 return jsonify(errno=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(mobile=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) # 保存真实的短信验证码 try: redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 保存发送给这个手机号的记录,防止用户在60s内再次发送 redis_store.setex('send_msm_code_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='存储验证码异常') # 发送短信 try: ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, int(constants.SMS_CODE_REDIS_EXPIRES / 60)], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='发送异常') # 返回值 if result == 0: return jsonify(errno=RET.OK, errmsg='发送成功') else: return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
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_id, image_code]): # 表示参数完整 return jsonify(errno=RET.PARAMERR, errmsg='参数不完整') # 业务逻辑处理 # 从redis中取出真实的图片验证码 try: real_image_codes = redis_store.get('image_code_%s' % image_code_id) real_image_code = str(real_image_codes, encoding="utf-8") except Exception as e: return jsonify(errno=RET.DBERR, errmsg='redis数据库异常') # 判断验证码是否过期 if real_image_code is None: # 表示图片验证码没有或者过期 return jsonify(errno=RET.NODATA, errmsg='图片验证码失效') # 删除redis中的图片验证码,防止用户使用同一个图片验证码验证多次 try: redis_store.delete('image_code_%s' % image_code_id) except Exception as e: pass # 与用户填写的值进行对比 if real_image_code.lower() != image_code.lower(): # 表示用户填写错误 return jsonify(errno=RET.DATAERR, errmsg="图片验证码错误") # 判断对于手机号的操作,在60秒内是否发送过短信 try: send_flag = redis_store.get('send_sms_code_%s' % mobile) except Exception as e: pass else: if send_flag is not None: return jsonify(RET.REQERR, errmsg='请求过于频繁,60秒后再试') # 判断手机号是否存在 try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: pass else: if user is not None: return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在') # 如果不存在则生成验证码 sms_code = random.randint(100000, 999999) # 保存短信验证码 try: redis_store.setex('sms_code_%s' % mobile, SMS_CODE_ID_TIME, sms_code) # 保存发送给这个手机号的记录,防止用户在60s内再次发送短信 redis_store.setex('send_sms_code_%s' % mobile, SEND_SMS_CODE, 1) except: return jsonify(errno=RET.DBERR, errmsg='保存短信验证码失败') # 发送短信 try: ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, int(SMS_CODE_ID_TIME / 60)], 1) except Exception as e: # return jsonify(errno=RET.THIRDERR, errmsg='发送失败') pass # 返回值 if result == 0: # 发送成功 return jsonify(errno=RET.OK, errmsg="发送成功") else: return jsonify(errno=RET.THIRDERR, errmsg='短信发送失败')
def send_sms_code(mobile): """发送短信验证码""" # 提取参数 image_code_id = request.args.get("image_code_id") image_code_text = request.args.get("image_code_text") # 校验参数 if not all([image_code_id, image_code_text]): return jsonify(errcode=RET.PARAMERR, errmsg="参数不完整") #验证图片验证的正确性: # 根据编号取出图片验证码的真实值:redis try: real_image_code_text = redis_store.get("image_code_%s" % image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errcode=RET.DBERR, errmsg="数据库异常") # 判断图片验证码的值是否过期 if real_image_code_text is None: # 表示不存在或过期 return jsonify(errcode=RET.NODATA, errmsg="图片验证码已过期") # 在redis中删除图片验证码的真实值,防止用户对同一个验证码进行二次尝试 try: redis_store.delete("image_code_%s" % image_code_id) except Exception as e: current_app.logger.error(e) # 将用户填写的与真实值进行对比 if real_image_code_text.lower() != image_code_text.lower(): # 表示用户填写图片验证码错误 return jsonify(errcode=RET.DATAERR, errmsg="图片验证码错误") # 如果相同 # 查询数据库,判断手机号是否注册过 try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) else: if user is not None: # 表示手机号注册过 return jsonify(errcode=RET.DATAEXIST, errmsg="手机号已注册过") # 手机号没有注册过 # 判断是否在60秒内发送过短信,如果发送过,则提前终止 try: flag = redis_store.get("send_sms_code_flag_%s" % mobile) except Exception as e: current_app.logger.error(e) else: if flag is not None: # 表示60秒内有发送记录 return jsonify(errcode=RET.REQERR, errmsg="发送过于频繁") # 生成短信验证码 # %06d表示格式化显示,至少6位数字,不足6为前面补0 sms_code = "%06d" % random.randint(0, 999999) # 保存手机号和短信验证码 try: redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) except Exception as e: current_app.logger.error(e) return jsonify(errcode=RET.DBERR, errmsg="保存短信验证码异常") # 保存发送的记录到redis中 try: redis_store.setex("send_sms_code_flag_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) except Exception as e: current_app.logger.error(e) # 发送短信验证码 try: ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, str(constants.IMAGE_CODE_REDIS_EXPIRES // 60)], constants.SMS_CODE_TEMPLATE) except Exception as e: current_app.logger.error(e) return jsonify(errcode=RET.THIRDERR, errmsg="发送短信异常") if result == -1: return jsonify(errcode=RET.THIRDERR, errmsg="发送短信失败") else: return jsonify(errcode=RET.OK, errmsg="发送短信成功")