def sms_code(): # 获取参数 dict_data = request.json mobile = dict_data.get("mobile") image_code = dict_data.get("image_code") image_code_id = dict_data.get("image_code_id") # 校验参数是否为空 if not all([mobile, image_code, image_code_id]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 校验手机号格式是否正确 if not re.match("1[35789]\d{9}", mobile): return jsonify(errno=RET.DATAERR, errmsg="手机号格式错误") # 通过验证码编号取出,redis中的图片验证码A try: redis_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="获取图片验证码异常") # 判断验证码A是否过期 if not redis_image_code: return jsonify(errno=RET.NODATA, errmsg="图片验证码已过期") try: # 未过期,就删除redis中的图片验证码 redis_store.delete("image_code:%s" % image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="图片验证码操作异常") # 判断验证码A和传入进来的验证码B是否相等 if image_code.lower() != redis_image_code.lower(): return jsonify(errno=RET.DATAERR, errnsg="图片验证码填写错误") # 生成验证码 sms_code = "%06d" % random.randint(0, 999999) current_app.logger.debug("短信验证码:%s" % sms_code) # 发送短信验证码,调用ccp ccp = CCP() try: result = ccp.send_template_sms( mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="云通讯发送异常") if result == -1: return jsonify(errno=RET.DATAERR, errmsg="短信发送失败") # 储存短信验证码到redis中 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="保存验证码失败") # 返回发送状态 return jsonify(errno=RET.OK, errmsg="发送成功")
def sms_code(): # 1.获取参数 req_data = request.data json_data = json.loads(req_data, encoding="utf-8") mobile = json_data["mobile"] image_code = json_data["image_code"] image_code_id = json_data["image_code_id"] # 2.参数的为空校验 if not all([mobile, image_code, image_code_id]): err_rsp(RET.PARAMERR, "参数错误!") # 3.校验手机的格式 if not re.match('1[3-9]\d{9}', mobile): err_rsp(RET.DATAERR, "手机号格式错误!") # 4.通过图片验证码编号获取,图片验证码 try: pic_code = redis_store.get("image_code:%s" % image_code_id) except Exception as e: current_app.logger.error(e) err_rsp(RET.DBERR, "操作redis失败") # 5.判断图片验证码是否过期 if not pic_code: err_rsp(RET.NODATA, "验证码过期") # 6.判断图片验证码是否正确 if pic_code.upper() != image_code.upper(): err_rsp(RET.DATAERR, "图片验证码不正确") # 7.核验成功,删除redis中的图片验证码,短信验证码的层级更高,就不需要图片验证码对用户注册进行验证 try: redis_store.delete("image_code:%s" % image_code_id) except Exception as e: current_app.logger.error(e) err_rsp(RET.DBERR, "操作redis失败") # 8.生成一个随机的短信验证码,调用cCp发送短信,判断是否成功 rd_sms_code = "%06d" % random.randint(0, 999999) ccp = CCP() statue_code = ccp.send_template_sms('18332672733', [rd_sms_code, 5], 1) if statue_code == -1: err_rsp(RET.DBERR, "验证码发送失败") # 9.将短信保存到redis中 try: redis_store.set("rd_sms_code:%s" % mobile, rd_sms_code, constants.SMS_CODE_REDIS_EXPIRES) except Exception as e: err_rsp(RET.DBERR, "操作redis失败") # 10.返回 return err_rsp(RET.OK, "短信发送成功!")
def get_sms_code(): """ 短信验证功能 在点击获取验证码时 通过main.js的sendSMSmsg方法中的ajax请求 """ msg_dict = request.json mobile = msg_dict.get('mobile') image_code = msg_dict.get('image_code') image_code_id = msg_dict.get('image_code_id') try: session_img_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=error_map[RET.DBERR]) if session_img_code: if image_code.lower() == session_img_code.lower(): redis_store.delete(image_code_id) ccp = CCP() msg_info = '%06d' % random.randint(0, 999999) print(msg_info) try: result = ccp.send_template_sms( mobile, [msg_info, SMS_CODE_REDIS_EXPIRES / 60], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, erromsg=error_map[RET.THIRDERR]) if result == -1: return jsonify(errno=RET.DATAERR, errmsg='短信发送失败') else: try: redis_store.set('sms_code: %s' % mobile, msg_info, SMS_CODE_REDIS_EXPIRES) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK]) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DATAERR, errmsg=error_map[RET.DATAERR]) else: return jsonify(errno=RET.DATAERR, errmsg='验证码输入错误') else: return jsonify(errno=RET.DATAEXIST, errmsg='验证码已失效')
def send_sms_code(): mobile=request.json.get('mobile') image_code=request.json.get('image_code') image_code_id=request.json.get('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_'+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='数据已过期') try: redis_store.delete('ImageCode_'+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='图片验证码错误') sms_code='%06d' % random.randint(0,999999) try: redis_store.setex('SMSCode_'+mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code) 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,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(): """ 思路分析: 1.获取参数 2.校验参数,为空检验,格式校验 3.取出redis中的图片验证码 4.判断是否过期 5.删除redis中图片验证码 6.正确性判断 7.生成短信验证码 8.发送短信 9.判断是否发送成功 10.保存短信验证码到redis 11.返回响应 """ # 1.获取参数 json_data = request.data dict_data = json.loads(json_data) mobile = dict_data.get('mobile') image_code = dict_data.get('image_code') image_code_id = dict_data.get('image_code_id') # 2.校验参数,为空检验,格式校验 if not all([mobile,image_code,image_code_id]): return jsonify(errno=RET.PARAMERR,errmsg="参数不全") if not re.match('1[35789]\d{9}',mobile): return jsonify(errno=RET.DATAERR,errmsg="手机号格式不正确") # 3.取出redis中的图片验证码 try: redis_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="数据获取失败") # 4.判断是否过期 if not redis_image_code: return jsonify(errno=RET.NODATA,errmsg="图片验证码过期") # 5.删除redis中图片验证码 try: redis_store.delete('image_code:%s'%image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR,errmsg="获取失败") # 6.正确性判断 if image_code.upper() != redis_image_code.upper(): return jsonify(errno=RET.DATAERR,errmsg="图片验证码错误") # # 7.生成短信验证码 sms_code = '%06d'%random.randint(0,999999) current_app.logger.debug('短信验证码 = %s'%sms_code ) # 8.发送短信 try: ccp = CCP() result = ccp.send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR,errmsg="云通讯发送失败") # 9.判断是否发送成功 if result == -1: return jsonify(errno=RET.DATAERR,errmsg="发送短信失败") # 10.保存短信验证码到redis 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="短信保存失败") # 11.返回响应 return jsonify(errno=RET.OK,errmsg="发送成功")
def sms_code(): # 获取参数 mobile = request.json.get('mobile') image_code = request.json.get('image_code') image_code_id = request.json.get('image_code_id') # 检查参数 if not all([image_code, mobile]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') if not re.match(r'1[3456789]\d{9}$', mobile): return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误') try: text = redis_store.get('Imagecode_' + image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='验证码已过期') try: redis_store.delete('Imagecode_' + image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='获取数据失败') # 判断获取结果 if not text: return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期') # 判断图片验证码输入是否正确而 if image_code.lower() != text.lower(): return jsonify(errno=RET.PARAMERR, errmsg='图片验证码错误') # 删除图片验证码 try: redis_store.delete('Imagecode_' + image_code_id) except Exception as e: current_app.logger.error(e) # 判断手机是否注册 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='手机号已注册') # 调用第三方工具发送短信 sms_data = "%06d" % random.randint(1, 999999) print(sms_data) # 存储验证码信息 try: redis_store.setex('smscode_' + mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_data) 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_data, 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_code(): """ 发送短信 接受参数--检查参数--业务处理--返回结果 1.获取参数,手机号mobile,验证码内容image_code,验证码编号image_code_id 2.检查参数,参数必须全部存在 3.检查手机号的格式,使用正则 4.检查图片验证码是否正确 5.从redis中获取真实的图片验证码 (可能超时不存在了) 6.判断获取结果是否存在 7.如果存在,先删除redis数据库中的图片验证码,因为图片验证码只能比较一次,比较一次的本质,是只能读取redis数据库一次 8.比较图片验证码是否正确 9.生成短信验证码6位数 10.把短信验证码存储在redis数据库中 11.调用云通讯发送短信 12.保存发送结果(000000),用来判断发送是否成功 13.返回结果 :return: """ # 获取post请求的参数 mobile = request.json.get('mobile') image_code = request.json.get('image_code') image_code_id = request.json.get('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='手机号格式错误') # 根据uuid从redis中获取图片验证码 try: real_image_code = redis_store.get('ImageCode_' + 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='图片验证码已经过期') # 如果获取到了图片验证码,需要把redis中的图片验证码删除,因为只能get一次 try: redis_store.delete('ImageCode_' + 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='图片验证码错误 ') # 根据手机号来查询用户未注册,使用模型类User try: user = User.query.filter(User.mobile == mobile).first() except Exception as e: return jsonify(errno=RET.DBERR, errmsg='查询用户数据失败') else: if user is not None: return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册') # 生成短信随机码 sms_code = '%06d' % random.randint(0, 999999) # 存入redis中 try: redis_store.setex('SMSCode_' + mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='保存数据失败') # 调用云通讯发送短信 try: # 构造发送短信对象 ccp = CCP() # 第一个参数是手机号,第二个参数是短信内容,第三个参数是模板id result = ccp.send_template_sms( mobile, [sms_code, 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 set_sms_code(): """ # 1获取参数 # 2校验参数 # 3根据图片验证码编号获取redis中的图片验证码 # 4判断是否过期 # 5判断图片验证码是否相等 # 6生成短信验证码 # 7发送短信,调用ccp.send_template_sms方法 # 8保存短信验证码到redis中 #9.返回前段浏览器 :return: """ """ 获取Post 请求提数据,解析成字典格式的三种方法 1.json_data=request.data dict_data = json.loads(json_data) 2.dict_data=request.get_json() 3.dict_data=request.json """ # 1获取参数 json_data = request.data dict_data = json.loads(json_data) mobile = dict_data.get('mobile') image_code = dict_data.get('image_code') image_code_id = dict_data.get('image_code_id') # 2校验参数 if not all([mobile, image_code, image_code_id]): return jsonify(errno=RET.PARAMERR, errmsg='参数不能为空') # 3根据图片验证码编号获取redis中的图片验证码 try: redis_image_code = redis_store.get('image_code:%s' % image_code_id) except Exception as e: current_app.logger.error(e) # 4判断是否过期 if not redis_image_code: return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期') # 5判断图片验证码是否相等 if image_code.lower() != redis_image_code.lower(): return jsonify(errno=RET.DATAERR, errmsg='图片验证码填写错误') # 可以删除图片验证码 try: redis_store.delete('image_code:%s' % image_code_id) except Exception as e: current_app.logger.error(e) # 6生成短信验证码 sms_code = "%06d" % random.randint(0, 999999) # 7发送短信,调用ccp.send_template_sms方法 ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, 5], 1) if result == -1: return jsonify(errno=RET.THIRDERR, errmsg='短信发送失败') # 8保存短信验证码到redis中 try: redis_store.set('sms_code:%s' % mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES) except Exception as e: current_app.logger.error(e) # 9.返回前段浏览器 return jsonify(errno=RET.OK, errmsg='发送成功')
def sendSMSCode(): """ 发送短信 1、获取参数,mobile,image_code,image_code_id 2、判断参数的完整性 3、验证手机号格式,正则 4、校验图片验证码,从redis读取真实的图片验证码 real_image_code = redis_store.get(key) 5、判断获取结果是否存在,如果不存在,表示已过期 6、如果图片验证码存在,需要删除图片验证码,本质是任意一个图片验证码,只能读取一次; 7、比较图片验证码内容是否一致; 8、查询mysql数据库,确认手机号是否已经注册; 9、生成短信随机数;六位 10、把短信随机数保存到redis数据库中,设置有效期 11、调用云通讯扩展,来发送短信,保存发送结果 12、判断发送结果是否成功。 :return: """ mobile = request.json.get("mobile") userCode = request.json.get("userCode") uuid = request.json.get("uuid") if not all([mobile, userCode, uuid]): return jsonify(error= RET.DATAERR, errmsg = "获取用户输入的验证码失败") # # 手机号码长度验证, # if len(mobile) != 11: # return jsonify(error=RET.DATAERR, errmsg="手机有误") # 验证手机号,re.match()只匹配符合相关,并没有长度匹配校验 # 例如:这个匹配如果写了大于11位,他会匹配符合11的内容 result = re.match(r"1[3456789]\d{9}$", mobile) if not result: return jsonify(error=RET.PARAMERR, errmsg="手机号不规范") # 获取真是验证码 redis_key = "image" + uuid try: real_code = redis.get(redis_key) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.DBERR, errmsg="获取用户输入的验证码失败") if not real_code: return jsonify(error=RET.DATAERR, errmsg="获取redis的图片验证码失败") try: # 删除验证码 redis.delete(redis_key) except Exception as e: current_app.logger.error(e) if real_code.lower() != userCode.lower(): return jsonify(error=RET.DATAERR, errmsg="验证码输入有误") # 验证用户是否已注册 try: result2 = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) return jsonify(error=RET.DBERR, errmsg="查询mysql数据库失败") else: if result2 is not None: return jsonify(error=RET.DATAEXIST, errmsg="用户已注册") # 发送验证码 # 构造六位数的短信随机数 sms_code = '%06d' % random.randint(0, 999999) try: redis.setex("Mobile" + mobile, constants.IMAGE_CODE_REDIS_EXPIRES, sms_code) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.DATAERR, errmsg="存入短信验证码失败") try: ccp = CCP() # 注意: 测试的短信模板编号为1 result3 = ccp.send_template_sms(mobile, [sms_code, constants.IMAGE_CODE_REDIS_EXPIRES /60], 1) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.THIRDERR, errmsg="第三方错误") # 将验证码存入redis if result3 == 0: # 发送成功 return jsonify(error=RET.OK, errmsg='发送成功') else: # 发送失败 return jsonify(error=RET.THIRDERR, errmsg='发送失败')
def send_sms_code(): # 接收参数 检查参数 业务处理 返回结果 # 1 获取参数 mobile image_code image_code_id # 2 检查参数 参数必须全部都存在 # 3 检查手机号的格式,使用正则表达式 # 4 检查图片验证码是否正确 # 5 从redis中获取真实的图片验证码 # 6 判断图片获取结果是否存在 # 7 如果存在先删除redis数据库中的图片验证码,因为图片验证码只能比较一次,只能读取redis数据库一次 # 8 比较图片验证码是否正确 # 9 生成短信验证码,6位数 # 10 把短信验证码存储在redis数据库中 # 11 调用云通讯发送短信 # 12 保存发送结果,用来判断发送是否成功 # 13 返回结果 # 获取post请求的三个参数 mobile = request.json.get('mobile') image_code = request.json.get('image_code') image_code_id = request.json.get('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_' + 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='图片验证码已过期') # 如果获取到验证码需要把redis中的图片验证码删除,因为只能get一次 try: redis_store.delete('ImageCode_' + 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='图片验证码错误') # 根据手机号来查询用户未注册 try: user = User.query.filter(User.mobile == 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='手机号已注册') # 生成短信随机码 sms_code = '%06d' % random.randint(0, 9999999) try: redis_store.setex('SMSCode_' + mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) 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, constants.SMS_CODE_REDIS_EXPIRES / 60], 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='发送失败') else: return jsonify(errno=RET.OK, errmsg='发送成功')
def sms_code(): """请求路径: /passport/sms_code 请求方式: POST 请求参数: mobile, image_code,image_code_id 返回值: errno, errmsg 1.获取参数 2.效验参数,为空效验 3.验证手机号格式是否正常 4.根据图片验证码编号,取出redis图片验证码 5.判断redis中的图片验证码是否过期 6.取出图片验证码并删除redis图片的验证码 7.正确性效验,传入的图片验证码和redis是否一致 8.正常生成短信验证码,调用CCP对象来发送短信 9.判断短信是否发送成功 10.保存短信验证码到redis 11.返回发送状态 :return: """ # json_data = request.data # request_dict = json.loads(json_data) # request_dict = request.json request_dict = request.get_json() # print (request_dict) mobile = request_dict.get("mobile") image_code = request_dict.get("image_code") image_code_id = request_dict.get("image_code_id") if not all ([mobile,image_code,image_code_id]): return jsonify(errno=RET.PARAMERR,errmsg="参数不能为空") if not re.match(r"1[3-9]\d{9}",mobile): return jsonify(errno=RET.DATAERR,errmsg="手机号输入有误,请重新输入!") try: redis_image_code = redis_store.get("image_code:%s"%image_code_id) # print (redis_image_code) if not redis_image_code: return jsonify(errno=RET.NODATA,errmsg="验证码已过期,请重新输入!") except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR,errmsg="提取验证码错误!") try: if image_code.lower() != redis_image_code.lower(): redis_store.delete(image_code_id) return jsonify(errno=RET.DATAERR,errmsg="验证码输入有误!") except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR,errmsg="删除图片验证码失败!") if image_code.lower() == redis_image_code.lower(): # return jsonify(errno=RET.OK, errmsg="验证码输入正确!") sms_num = "%06d"%random.randint(0,999999) ccp = CCP() result = ccp.send_template_sms(mobile, [sms_num, constants.SMS_CODE_REDIS_EXPIRES/60], 1) # result = 0 try: if result == 0: redis_store.set("sms_code:%s"%mobile,sms_num,constants.SMS_CODE_REDIS_EXPIRES) return jsonify(errno=RET.OK,errmsg="验证码已发送") elif result == -1: return jsonify(errno=RET.THIRDERR,errmsg="发送短信验证码失败!") except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR,errmsg="数据库插入失败!")
def send_sms_code(): # 发送短信验证码流程 """ 发送短信 获取参数---检查参数---业务处理---返回结果 1、获取参数,mobile(用户的手机号),image_code(用户输入的图片验证码),image_code_id(UUID) 2、检查参数的完整性 3、检查手机号格式,正则 4、尝试从redis中获取真实的图片验证码 5、判断获取结果,如果不存在图片验证码已经过期 6、需要删除redis中存储的图片验证码,图片验证码只能比较一次,本质是只能对redis数据库get一次。 7、比较图片验证码是否正确 8、生成短信验证码,六位数 9、存储在redis数据库中 10、调用云通讯,发送短信 11、保存发送结果,判断发送是否成功 12、返回结果 :return: """ # 1、获取参数,mobile(用户的手机号),image_code(用户输入的图片验证码), image_code_id(UUID) mobile = request.json.get("mobile") image_code = request.json.get('image_code') image_code_id = request.json.get("image_code_id") # 2、检查参数的完整性 # 方法1:if not mobile and image_code and image_code_id: #判断方法2: if not all([mobile, image_code, image_code_id]): return jsonify(erron=RET.PARAMERR, errmsg='参数不完整') # 3、检查手机号格式,正则 if not re.match(r'1[3456789]\d{9}$', mobile): return jsonify(erron=RET.PARAMERR, errmsg='手机号格式错误') #尝试从redis中查询图片验证码 try: real_image_code = redis_store.get('Image_Code_' + image_code_id) except Exception as e: #利用应用上下文对象,来记录错误日志信息 current_app.logger.error(e) return jsonify(erron=RET.DBERR, errmsg='获取数据失败') #判断验证码是否为空 if not real_image_code: return jsonify(errno=RET.NODATA, errmsg='图片验证码已过期') # 删除redis中存储的图片验证码,因为图片验证码只能get一次,只能比较一次,留着也没用 #用户只能输入一次验证码,如果输入错误,从新生成验证码输入 try: redis_store.delete('ImageCode_' + image_code_id) except Exception as e: current_app.logger.error(e) #比较图片验证码的数据是否一直,lower()函数可以把大写字母转化成小写 if real_image_code.lower() != image_code.lower(): return jsonify(erron=RET.DATAERR, errmsg='图片验证码不一致') #判断手机号是否注册(不允许一个手机号注册多次): #通过查询mysql数据库查询手机号是否存在 try: #查询数据库语句 user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) return jsonify(erron=RET.DBERR, errmsg='查询用户数据失败') #判断数据是否真实存在 if user is not None: return jsonify(erron=RET.DATAEXIST, errmsg='手机号码已被注册') #生成短信验证码,'%06d' % 因为从0开始,有可能会出现5位,'%06d' % 这个可以在前多加一位 sms_code = '%06d' % random.randint(0, 999999) # 存储在redis中,key可以拼接手机号来实现key唯一 try: redis_store.setex('SMSCode_' + mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经注册') #使用云通讯发送短信验证码,send_template_sms(接收人手机号,[内容(下面代码有验证码/过期时间)],1) try: ccp = CCP() result = ccp.send_template_sms( mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常') #判断短信是否发送成功,云通讯在发送时会接收一个响应信息0为发送成功-1为失败,后端需要根据响应来判断是 #否发送成功 if result == 0: return jsonify(errno=RET.OK, errmsg='发送成功') else: return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
def sms_code(): """ 1.获取参数 2.校验参数.为空校验 3.校验手机号格式是否正确 4.通过验证码编号取出redis中的图片验证码A 5.判断验证码A是否过期 6.删除,redis中的图片验证码 7.判断验证码A和传入进来的图片验证码是否相等 8.生成短信验证码 9.发送短信验证码,调用ccp方法 10.存储短信验证码到redis中 11.返回发送状态 :return: """ # 1.获取参数 # # post请求 request.data # json_data = request.data # # 将json转成字典 # dict_data = json.loads(json_data) dict_data = request.json mobile = dict_data.get("mobile") image_code = dict_data.get("image_code") image_code_id = dict_data.get("image_code_id") # 2.校验参数.为空校验 if not all([mobile, image_code, image_code_id]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 3.校验手机号格式是否正确 if not re.match("1[35789]\d{9}", mobile): return jsonify(errno=RET.DATAERR, errmsg="手机号格式错误") # 4.通过验证码编号取出redis中的图片验证码A try: redis_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="获取图片验证码异常") # 5.判断验证码A是否过期 if not redis_image_code: return jsonify(errno=RET.NODATA, errmsg="验证码已过期") # 6.删除,redis中的图片验证码 try: redis_store.delete("image_code:%s" % image_code_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="图片验证码操作异常") # 7.判断验证码A和传入进来的图片验证码是否相等 if redis_image_code.lower() != image_code.lower(): return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误") # 8.生成短信验证码 sms_code = "%06d" % random.randint(0, 999999) # current_app.logger.debug("*********短信验证码 = %s*********" % sms_code) # 9.发送短信验证码,调用ccp方法 ccp = CCP() try: result = ccp.send_template_sms( mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES / 60], 1) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="云通讯发送异常") if result == -1: return jsonify(errno=RET.DATAERR, errmsg="短信发送失败") # 10.存储短信验证码到redis中 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="保存图片验证码失败") # 11.返回发送状态 return jsonify(errno=RET.OK, errmsg="发送成功")
def send_sms_code(): """ 发送短信 接收参数-----检查参数------业务逻辑处理------返回结果 1. 获取参数,mobile, image, code, image_code_id 2. 检查参数,参数必须存在 3. 检查手机号的格式,使用正则 4. 检查图片验证码是否正确 5. 从redis中获取真实的图片验证码 6. 判断获取结果是否存在 7. 如果存在,先删除redis数据库中的图片验证码, 因为图片验证码只能比较一次,本质是只能读取一次redis数据库一次 8. 比较图片验证码是否正确 9. 生成短信验证码, 六位数 10. 存储在redis 数据库中 11. 调用云通讯发送短信 12. 保存发送结果,用来判断发送是否成功 13. 返回结果 :return: """ # 获取post请求的三个参数 mobile = request.json.get('mobile') image_code = request.json.get('image_code') image_code_id = request.json.get('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="手机格式错误") # 根据uuid从redis中获取图片验证码 try: real_image_code = redis_store.get('ImageCode_' + 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='图片验证过期') # 如果获取到了图片验证码,需要把redis中的图片验证码删除,因为只能get一次 try: redis_store.delete('ImageCode_' + image_code_id) except Exception as e: current_app.logger.error(e) # 比较图片验证是否正确, 忽略大小写 if real_image_code.lower() != image_code: return jsonify(errno=RET.DATAERR, errmsg="图片验证码错误") # 根据手机号来查询用户未注册,使用模型类User try: user = User.query.filter(User.mobile == 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="手机号已注册") # 生成短信随机数 sms_code = '%06d' % random.randint(0, 999999) # 存入redis中 try: redis_store.setex('SMSCode_' + mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存数据失败") # 调用云通讯发送短信 try: # 构造发送短信对象 ccp = CCP() # 第一个参数手机号,第二个参数短信内容, 第三个参数模板id result = ccp.send_template_sms( mobile, [sms_code, 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="发送失败")