def set_auth(): # 获取参数 request_dict = request.json real_name = request_dict.get('real_name') id_card = request_dict.get('id_card') # 验证有效性 if not all([real_name, id_card]): return jsonify(errno=RET.PARAMERR, errmsg='缺少必要参数') # 查询用户 try: user = User().query.get(g.user_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询用户信息失败') if (user.real_name is not None) & (user.id_card is not None): return jsonify(errno=RET.DATAEXIST, errmsg='该用户已实名认证') user.real_name = real_name user.id_card = id_card try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存用户信息失败') return jsonify(errno=RET.OK, errmsg='OK')
def register(): """注册""" # 获取参数 req_dict = request.get_json() mobile = req_dict.get("mobile") sms_code = req_dict.get("sms_code") password = req_dict.get("password") password2 = req_dict.get("password2") # 校验参数 # 1. 参数完整性 if not all([mobile, sms_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg=u"参数不完整") # 2. 手机号 if not re.match(r"1[34578]\d{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg=u"手机号格式错误") # 3. 密码 if password != password2: return jsonify(errno=RET.PARAMERR, errmsg=u"两次密码不一致") # 4. 短信验证码 try: real_sms_code = redis_store.get("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=u"读取短信验证码异常") if real_sms_code is None: return jsonify(errno=RET.NODATA, errmsg=u"短信验证码失效") try: # 删除redis中的短信验证码,防止重复使用校验 redis_store.delete("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) if real_sms_code != sms_code: return jsonify(errno=RET.DATAERR, errmsg=u"短信验证码错误") # 业务处理 # 1. 保存用户的注册数据到数据库中 user = User(name=mobile, mobile=mobile) user.password = password # 设置属性,调用模型类中password.setter装饰方法 try: db.session.add(user) db.session.commit() except IntegrityError as e: # 模型类中设置mobile为unique,因此手机号若存在会在保存时抛出异常 db.session.rollback() # 回滚到异常commit提交前的状态 current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg=u"手机号已存在") except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=u"查询数据库异常") # 2. 保存登录状态到session中 session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id # 返回结果 return jsonify(errno=RET.OK, errmsg=u"注册成功")
def register(): try: req_data = request.get_json() except Exception as e: return jsonify(errno=RET.PARAMERR, errmsg='请传入json数据') if not req_data: return jsonify(errno=RET.PARAMERR, errmsg='请传入json数据') mobile = req_data.get('mobile') sms_code = req_data.get('sms_code') password = req_data.get('password') password2 = req_data.get('password2') if not all([mobile, sms_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg='参数不完整') elif not re.match('1\d{10}', mobile): return jsonify(errno=RET.PARAMERR, errmsg='手机号格式错误') elif password != password2: return jsonify(errno=RET.PARAMERR, errmsg='两次输入密码不相等') try: real_sms_code = redis_store.get('sms_code_%s' % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='redis数据库错误') else: if not real_sms_code: return jsonify(errno=RET.NODATA, errmsg='短信验证码已过期') elif real_sms_code != sms_code: return jsonify(errno=RET.PARAMERR, errmsg='短信验证码错误') # 删除redis中的短信验证码,防止重复使用校验 try: redis_store.delete("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) # 保存用户的注册数据到数据库中 user = User(name=mobile, mobile=mobile) # user.generate_password_hash(password) user.password = password # 设置属性 try: db.session.add(user) db.session.commit() except IntegrityError as e: # 数据库操作错误后的回滚 db.session.rollback() # 表示手机号出现了重复值,即手机号已注册过 current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg="手机号已存在") except Exception as e: db.session.rollback() # 表示手机号出现了重复值,即手机号已注册过 current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据库异常") session['name'] = user.name session['mobile'] = user.mobile session['user_id'] = user.id return jsonify(errno=RET.OK, errmsg='注册用户成功')
def register(): """用户注册处理""" # 接收参数 # 获取json数据,返回字典 req_data = request.get_json() mobile = req_data.get('mobile') sms_code = req_data.get('sms_code') password = req_data.get('password') cpassword = req_data.get('cpassword') # 校验参数 if not all([mobile, sms_code, password, cpassword]): return jsonify(errcode=RET.PARAMERR, errmsg='数据不完整') # 校验手机号 if not re.match(r'1[34578]\d{9}', mobile): return jsonify(errcode=RET.PARAMERR, errmsg='手机号格式错误') # 两次密码是否相同 if password != cpassword: return jsonify(errcode=RET.PARAMERR, errmsg='两次密码不相同') # 短信验证码是否正确 try: redis_sms_code = redis_store.get('sms_code_%s' % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errcode=RET.DBERR, errmsg='读取验证码数据库错误') if redis_sms_code is None: return jsonify(errcode=RET.DATAERR, errmsg='短信验证码过期') if sms_code != redis_sms_code: return jsonify(errcode=RET.DATAERR, errmsg='短信验证码错误') # 手机号是否注册 user = User(name=mobile, mobile=mobile) user.password = password try: db.session.add(user) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errcode=RET.DATAERR, errmsg='手机号已被注册') except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errcode=RET.DBERR, errmsg='数据库异常') # 业务逻辑处理 session['user_id'] = user.id session['username'] = mobile session['mobile'] = mobile # 返回 return jsonify(errcode=RET.OK, errmsg='注册成功')
def register(): """用户注册""" # 从json中获取参数 user_data = request.get_json() if not user_data: return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") mobile = user_data.get("mobile") # 手机号 sms_code = user_data.get("sms_code") # 短信验证码 password = user_data.get("password") # 密码 # 检查参数 if not all([mobile, sms_code, password]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 手机号格式校验 if not re.match(r"1[34578]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不正确") # 检查短信验证码 # 从redis中读取真实的短信验证码 try: real_sms_code = redis_store.get("SMSCode_" + mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="读取验证码异常") # 判断验证码是否过期 if not real_sms_code: return jsonify(errno=RET.DATAERR, errmsg="短信验证码过期") # 判断用户输入的验证码的正确性 if real_sms_code != str(sms_code): return jsonify(errno=RET.DATAERR, errmsg="短信验证码无效") # 已经进行过短信验证码的对比校验,所以删除redis中的smscode try: redis_store.delete("SMSCode_" + mobile) except Exception as e: current_app.logger.error(e) # 保存用户信息 user = User(name=mobile, mobile=mobile) # 通过设置user模型的password属性,实际调用了设置密码的方法,对密码进行了加密 user.password = password try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DATAEXIST, errmsg="手机号已存在") # 保存用户的session数据 session["user_id"] = user.id session["name"] = mobile session["mobile"] = mobile return jsonify(errno=RET.OK, errmsg="OK", data=user.to_dict())
def register(): """ 用户注册模块 :参数:mobile, mobile_code,password,password2 :return:jsonfiy{"errno":data, “errmsg”:data} """ #获取json数据 dir_Json = request.get_json() mobile = dir_Json.get('mobile') mobile_code = dir_Json.get('mobile_code') password = dir_Json.get('password') password2 = dir_Json.get('password2') #判断数据完整性 if not all([mobile, mobile_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg="missing data") #判断手机号格式 if not re.match(r'1[345678]\d{9}', mobile): return jsonify(errno=RET.DATAERR, errmsg="mobile Incorrect data") #判断密码 if password != password2: return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致") #验证手机验证码 try: redis_mobile_code = redis_store.get("mobile_code_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="rediss error") if not mobile_code: return jsonify(errno=RET.DATAERR, errmsg="missing mobile_code") if redis_mobile_code != mobile_code: return jsonify(errno=RET.DATAERR, errmsg=" mobile_code error") #判断手机是否存在,设置密码 try: user = User(name=mobile, mobile=mobile) user.password = password db.session.add(user) db.session.commit() except IntegrityError as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg=" data exist ") except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="mysql error") #保存session数据 session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg="register ok ")
def register(): """signup """ # Gets the requested JSON data and returns the dictionary req_dict = request.get_json() mobile = req_dict.get("mobile") #sms_code = req_dict.get("sms_code") password = req_dict.get("password") password2 = req_dict.get("password2") # Calibration parameters #if not all([mobile, sms_code, password, password2]): #return jsonify(errno=RET.PARAMERR, errmsg="Parameter Incomplete") if not all([mobile, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg="Parameter Incomplete") # Determine the format of the phone number if not re.match(r"1[34578]\d{9}", mobile): # wrong format return jsonify(errno=RET.PARAMERR, errmsg="The format of mobile phone number is wrong") if password != password2: return jsonify(errno=RET.PARAMERR, errmsg="The two passwords don't match") # Save the user's registration data to the database user = User(name=mobile, mobile=mobile) # user.generate_password_hash(password) user.password = password # set attributes try: db.session.add(user) db.session.commit() except IntegrityError as e: # A rollback after a database operation error db.session.rollback() # It indicates that the phone number has a duplicate value # the phone number has been registered current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg="existent phone number") except Exception as e: db.session.rollback() # It indicates that the phone number has a duplicate value, # the phone number has been registered current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="Query Database Exception") # Save the login state to the session session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id # reture result return jsonify(errno=RET.OK, errmsg="registered successfully ")
def register(): """ :return: """ # request.json.get() 获取单个json数据 # 获取整个json数据包 json_data = request.get_json() if not json_data: return jsonify(errno=RET.PARAMERR, errmsg='参数错误') # 从json数据包中挨个提取参数 mobile = json_data.get('mobile') sms_code = json_data.get('sms_code') password = json_data.get('password') if not all([mobile, sms_code, password]): return jsonify(errno=RET.PARAMERR, errmsg='缺少必传参数') # 校验手机号 if not re.match(r'1[3-9]\d{9}$', mobile): return jsonify(errno=RET.PARAMERR, errmsg='手机格式错误') # 从redis中取出真实的短信验证码 try: server_sms_code = redis_cli.get('SMSCode_' + mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询数据失败') # 判断查询结果 if not server_sms_code: return jsonify(errno=RET.DATAERR, errmsg='数据失效') # 先比较 再删除 if server_sms_code != str(sms_code): return jsonify(errno=RET.DATAERR, errmsg='短信验证码错误') try: redis_cli.delete('SMSCode_' + mobile) except Exception as e: current_app.logger.error(e) # 实例化模型类对象, 保存用户信息 user = User() user.mobile = mobile user.name = mobile user.password = password try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='用户注册失败') # 缓存用户信息,使用session对象,存到redis中 session['user_id'] = user.id session['name'] = mobile session['mobile'] = mobile # 返回结果, # data表示用户数据,是注册业务完成后,返回注册结果相关的附属信息 return jsonify(errno=RET.OK, errmsg='注册成功', data=user.to_dict())
def user(): # 获取参数 phonecode = request.json.get("phonecode") mobile = request.json.get("mobile") password = request.json.get("password") # 校验参数 # print(sms_code,mobile,password) if not all([phonecode, mobile, password]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 手机号校验 if not re.match(r"1[345678]\d{9}$", mobile): # print("手机校验失败") return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 判断用户是否一存在 try: user = User.query.filter(User.mobile == mobile).first() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if user: return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 校验短信验证码,更具手机号取出短信验证码 # print("手机校验通过了") try: real_phonecode = sr.get("sms_code_id" + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 如获取到了验证码 if real_phonecode != phonecode: # print("验证码错误") return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 记录用户数据 user = User() user.mobile = mobile user.name = mobile # user.password_hash=password # 直接存储密码为明文密码 不安全 # 使用计算属性封装密码 user.password = password db.session.add(user) try: db.session.commit() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 使用session 记录用户登录状态,记录主键就可以查询出其他的数据 session["user_id"] = user.id # json 返回结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): req_dict = request.get_json() mobile = req_dict.get("mobile") sms_code = req_dict.get("sms_code") password = req_dict.get("password") password2 = req_dict.get("password2") if not all([mobile, sms_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 判断手机号格式 if not re.match(r"1[34578]\d{9}", mobile): # 格式不对 return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误") if password != password2: return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致") # 从redis中提取短信验证码 try: real_sms_code = redis_store.get("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="读取真实验证码异常") # 判断短信验证码是否过期 if real_sms_code is None: return jsonify(errno=RET.NODATA, errmsg="短信验证码失效") # 判断用户填写短信验证码的正确性 if real_sms_code != sms_code: return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误") # 判断用户的手机号是否注册过 # try: # user = User.query.filter_by(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="手机号已经存在!") # 保存用户的注册数据到数据库中 user = User(name=mobile, password_hash=password, mobile) try: db.session.add(user) db.session.commit() except IndentationError as e: current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg="手机号已经存在") except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库异常") # 保存登录状态到session中 # 返回登录后的结果 session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg="注册成功")
def register(): """register :return: """ req_dict = request.get_json() mobile = req_dict.get("mobile") sms_code = req_dict.get("sms_code") pwd = req_dict.get("password") pwd2 = req_dict.get("password2") # 数据是否完整 if not all([mobile, sms_code, pwd]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") # 判断手机格式 if not re.match(r"1[34578]\d{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机格式错误") if pwd != pwd2: return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致") # 判断短信验证码是否正确 try: real_sms_code = redis_store.get("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="读取短信验证码异常") if real_sms_code is None: return jsonify(errno=RET.NODATA, errmsg="短信验证码失效") try: redis_store.delete("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) # 验证码是否正确 if real_sms_code != sms_code: return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误") # 保存用户信息 user = User(name=mobile, mobile=mobile) user.password = pwd try: db.session.add(user) db.session.commit() except IntegrityError as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="手机号存在") except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库异常") # 保存登录数据 session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id # return return jsonify(errno=RET.OK, errmsg="ok")
def register(): """ 1. 获取参数和判断是否有值 2. 从redis中获取指定手机号对应的短信验证码的 3. 校验验证码 4. 初始化 user 模型,并设置数据并添加到数据库 5. 保存当前用户的状态 6. 返回注册的结果 :return: """ # 1. 获取参数和判断是否有值 data_dict = request.json mobile = data_dict.get("mobile") phonecode = data_dict.get("phonecode") password = data_dict.get("password") if not all([mobile, phonecode, password]): return jsonify(errno=RET.PARAMERR, errmsg="参数不全") # 2. 从redis中获取指定手机号对应的短信验证码的 try: sms_code = sr.get("SMS_" + mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="获取本地验证码失败") if not sms_code: return jsonify(errno=RET.NODATA, errmsg="短信验证码过期") # 3. 校验验证码 if phonecode != sms_code: return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误") # 4. 初始化 user 模型,并设置数据并添加到数据库 user = User() user.name = mobile user.mobile = mobile # 对密码进行处理 user.password = password try: db.session.add(user) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DATAERR, errmsg="数据保存错误") session["user_id"] = user.id session["name"] = user.name session["mobile"] = user.mobile return jsonify(errno=RET.OK, errmsg="注册成功")
def register(): """ 注册 请求参数 手机号 验证码 秘密 确认密码 请求方式 json :return: """ req_data = request.get_json() mobile = req_data.get("mobile") sms_code = req_data.get("sms_code") password = req_data.get("password") password2 = req_data.get("password2") if not all([mobile, sms_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") if password != password2: return jsonify(errno=RET.PARAMERR, errmsg="两次秘密不一样") if not re.match(r'1[34678]\d{9}', mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不对") try: redis_sms_code = redis_store.get("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="获取手机验证码错误") if redis_sms_code is None: return jsonify(errno=RET.NODATA, errmsg="手机验证码失效") try: redis_store.delete("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) if sms_code != redis_sms_code: return jsonify(errno=RET.DATAERR, errmsg="验证码不对") user = User(name=mobile, mobile=mobile) user.password = password try: db.session.add(user) db.session.commit() except IntegrityError as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg="手机号已经存在") except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库查询异常") session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg="注册成功")
def register(): """注册 请求的参数:手机号,短信验证码,密码 参数格式:json """ # 获取参数:获取请求的json数据,返回字典 req_dict = request.get_json() mobile = req_dict.get('mobile') sms_code = req_dict.get('sms_code') password = req_dict.get('password') password2 = req_dict.get('password2') # 检验参数的完整性 if not all([mobile, password]): return jsonify(errno=RET.PARAMERR, errmsg='请求参数不完整') # 判断手机号格式 if not re.match(r"1[34578]\d{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg='手机号格式不正确') # 判断两次密码是否一致 if password != password2: return jsonify(errno=RET.PARAMERR, errmsg='两次密码不一致') # 判断手机号是否注册过,将注册信息保存到数据库中 user = User(name=mobile, mobile=mobile) user.password = password # from sqlalchemy.exc import IntegrityError try: db.session.add(user) db.session.commit() except IntegrityError as e: # 数据库操作错误后回滚 db.session.rollback() # 表示手机号出现了重复,即手机号被注册过了 current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册') except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='数据库查询异常') # 将登陆状态保存到session中 session['name'] = mobile session['mobile'] = mobile session['user_id'] = user.id # 返回结果 return jsonify(errno=RET.OK, errmsg='注册成功')
def register(): # 获取手机号,短信验证码,密码 requ_dict = request.get_json() mobile = requ_dict.get("mobile") sms_code = requ_dict.get("sms_code") password = requ_dict.get("password") password2 = requ_dict.get("password2") # 手机号和短信验证码的校验 if not all([mobile, sms_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg="数据不完整") if not re.match(r"1[35689]\d{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机格式不正确") if password != password2: return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致") # 判断短信验证码是否正确 try: real_sms_code = redis_store.get(mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="redis查询错误") else: if real_sms_code != sms_code: 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 not None: # return jsonify(errno=RET.DATAEXIST, errmsg="用户已注册") # 把手机号保存为user类的对象 user = User(mobile=mobile, name=mobile) user.password = password try: db.session.add(user) db.session.commit() except IntegrityError as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg="用户已注册") except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库异常") # 保存用户登陆状态 session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg="注册成功")
def register(): # 获取参数 mobile = request.form.get('mobile') password = request.form.get('password') name = request.form.get('name') # 判断完整性 if not all([mobile, password]): return jsonify(errno=RET.PARAMERR, errmsg='用户名密码不完整') # 写入数据库 b = User(mobile=mobile, password_hash=password, name=name) db.session.add(b) db.session.commit() return jsonify(errno=RET.OK, errmsg='注册成功')
def get_auth(): try: user = User().query.get(g.user_id) houses = House.query.filter(House.user_id == g.user_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询用户信息失败') real_name = user.real_name id_card = user.id_card houses = [house.to_basic_dict() for house in houses] return jsonify(errno=RET.OK, errmsg='OK', data={'real_name': real_name, 'id_card': id_card, 'houses': houses})
def register(): """用户注册接口: 1.获取参数phone_num 手机号,phonecode 短信验证码,password 密码 2.校验数据 3.从Redis获取短信验证码,和传来的数据校验,如果正确 4.新增user对象, 5.跳转首页,保持登录状态 :return 返回注册信息{ 're_code':'0','msg':'注册成功'} """ # 1.获取参数phone_num 手机号,phonecode 短信验证码,password 密码 json_dict = request.json phone_num = json_dict.get('phone_num') phonecode_client = json_dict.get('phonecode') password = json_dict.get('password') # 2.校验数据 if not all([phone_num, phonecode_client, password]): return jsonify(re_code=RET.PARAMERR, msg='参数不完整') # 校验手机号是否正确 if not re.match( r'^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$', phone_num): return jsonify(re_code=RET.PARAMERR, msg='手机号不正确') # 3.从Redis获取短信验证码,和传来的数据校验,如果正确 try: phonecode_server = redis_conn.get('PhoneCode:' + phone_num) except Exception as e: current_app.logger.debug(e) return jsonify(re_code=RET.DBERR, msg='查询短信验证码失败') if phonecode_server.decode() != phonecode_client: return jsonify(re_code=RET.PARAMERR, msg='短信验证码错误') # 4.新增user对象, user = User() user.name = phone_num user.phone_num = phone_num user.password_hash = password try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.debug(e) db.session.rollback() return jsonify(re_code=RET.DBERR, msg='注册失败') # 5.跳转首页,保持登录状态 session['user_id'] = user.id session['name'] = user.name session['phone_num'] = user.phone_num # 6.响应结果 return jsonify(re_code=RET.OK, msg='注册成功')
def register(): """ 注册 :return: """ req_dict=request.get_json() mobile=req_dict.get("mobile") password=req_dict.get("password") password2=req_dict.get("password2") if not all([mobile,password,password2]): return jsonify(code=RET.PARAMERR,msg="参数不完整") if not re.match(r"1[34578]\d{9}",mobile): return jsonify(code=RET.PARAMERR,msg="手机格式错误") if password!=password2: return jsonify(code=RET.PARAMERR,msg="两次密码不一致") try: user=User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) return jsonify(code=RET.DBERR,msg="数据库异常") else: if user is not None: return jsonify(code=RET.DATAEXIST,msg="手机号已存在") user=User(name=mobile,mobile=mobile) user.password=password try: db.session.add(user) db.session.commit() except IntegrityError as e: db.session.rollback() current_app.logger.error(e) return jsonify(code=RET.DATAEXIST,msg="手机号已存在") except Exception as e: current_app.logger.error(e) return jsonify(code=RET.DATAEXIST, msg="查询数据库异常") session['name']=mobile session['mobile']=mobile session['user_id']=user.id return jsonify(code=RET.OK,msg="注册成功")
def register(): '''注册 请求的参数: 手机号、短信验证码、密码、确认密码 参数格式:json ''' req_dict = request.get_json() mobile = req_dict.get("mobile") sms_code = req_dict.get("sms_code") password = req_dict.get("password") password2 = req_dict.get("password2") if not all([mobile, sms_code, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") if not re.match(r'1[3-9]\d{9}', mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误") if password != password2: return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致") try: real_sms_code = redis_store.get("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="读取真实短信验证码异常") if real_sms_code is None: return jsonify(errno=RET.NODATA, errmsg="短信验证码失效") try: redis_store.delete("sms_code_%s" % mobile) except Exception as e: current_app.logger.error(e) if real_sms_code.decode() != sms_code: return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误,请重新获取验证码") # 盐值加密 user = User(name=mobile, mobile=mobile) # user.generate_password_hash(password) user.password = password try: db.session.add(user) db.session.commit() except IntegrityError as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg="手机号已存在") except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据库异常") session["name"] = mobile session["mobile"] = mobile session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg="注册成功")
def register(): resq = request.json if not resq: return jsonify(errno=RET.PARAMERR, errmsg='请求参数缺失') mobile = resq.get('mobile') clien_sms_code = resq.get('smscode') password = resq.get('password') if not all([mobile, clien_sms_code, password]): return jsonify(errno=RET.DATAERR, errmsg='请求参数不全') try: user = User.query.filter_by(mobile=mobile).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='校对用户表失败') if user: return jsonify(errno=RET.DATAERR, errmsg='用户已注册') try: server_sms_code = redis_store.get('mobile:' + mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询短信验证码失败') if not server_sms_code: return jsonify(errno=RET.DATAEXIST, errmsg='短信验证码失效') if server_sms_code != clien_sms_code: return jsonify(errno=RET.DATAERR, errmsg='短信验证码不正确') try: redis_store.delete('mobile:' + mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='删除短信验证码失败') # user = User(name=mobile, mobile=mobile) user = User() user.name = mobile user.mobile = mobile user.password = password try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='写入用户表错误') session['name'] = user.name session['user_id'] = user.id session['mobile'] = mobile return jsonify(errno=RET.OK, errmsg='OK')
def register(): """用户注册 请求的参数:手机号mobile、学生姓名name、密码password 确认密码password2 参数格式:json 返回:json数据 """ # 获取请求的json数据,返回字典 req_dict = request.get_json() mobile = req_dict.get("mobile") name = req_dict.get("name") password = req_dict.get("password") password2 = req_dict.get("password2") # 校验参数 if not all([mobile, name, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg="请求参数不完整") # 判断手机号格式 if not re.match(r"1[34578]\d{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg='手机号格式不正确') # 判断两次密码是否一致 if password != password2: return jsonify(errno=RET.PARAMERR, errmsg='两次密码不一致') # 判断手机号是否注册过,将注册信息保存到数据库中 user = User(name=name, mobile=mobile) user.password = password # 密码加密 # from sqlalchemy.exc import IntegrityError try: db.session.add(user) db.session.commit() except IntegrityError as e: # 数据库操作错误后回滚 db.session.rollback() # 表示手机号出现了重复,即手机号被注册过了 current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg='手机号已注册') except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='数据库查询异常') # 将登陆状态保存到session中 session['name'] = name session['mobile'] = mobile session['user_id'] = user.id # 返回结果 return jsonify(errno=RET.OK, errmsg='注册成功')
def register(): """ 手机号 密码 """ req_dict = request.get_json() mobile = req_dict.get('mobile') password = req_dict.get('password') password2 = req_dict.get('password2') print(mobile, password, password2) if not all([mobile, password, password2]): return jsonify(errno = RET.PARAMERR, errmsg = '参数不完整') if not re.match(r"1[345678]\d{9}", mobile): return jsonify(errno = RET.PARAMERR, errmsg = "手机号格式错误") if password != password2: return jsonify( errno= RET.PARAMERR, errmsg = "两次输入的密码不一致" ) user = User(name=mobile, mobile=mobile) # 对密码的加密:使用sha256算法加密 # pbkdf2:sha256:50000 $ cK0U6uNi $ 7ff2b0e361172433e8d391b3865a35d5cf252a30db0f3bf42b0f719771430225 user.password = password try: db.session.add(user) db.session.commit() except IntegrityError as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST , errmsg='手机号已经存在') except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR , errmsg='数据库查询出错') # 在session中保存用户的数据,相当于登录状态 session['name'] = mobile session['mobile'] = mobile session['user_id'] = user.id return jsonify(errno=RET.OK, errmsg= "注册成功")
def register(): """用户注册接口""" # 1.获取参数phone_num 手机号,phonecode 短信验证码,password 密码 phone_num = request.json.get('phone_num') phonecode_client = request.json.get('phonecode') password = request.json.get('password') #2.校验数据 if not all([phone_num, phonecode_client, password]): return jsonify(re_code=RET.PARAMERR, msg='参数不完整') # 校验手机号是否正确 if not re.match(r'1[3456789]\d{9}$', phone_num): return jsonify(re_code=RET.PARAMERR, msg='手机号不正确') # 3.从Redis获取短信验证码,和传来的数据校验,如果正确 try: phonecode_server = redis_conn.get('PhoneCode:' + phone_num) except Exception as e: current_app.logger.debug(e) return jsonify(re_code=RET.DBERR, msg='查询短信验证码失败') if phonecode_server != phonecode_client: return jsonify(re_code=RET.PARAMERR, msg='短信验证码错误') #4.新增user对象, user = User() user.name = phone_num user.phone_num = phone_num user.password_hash = password try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.debug(e) db.session.rollback() return jsonify(re_code=RET.DBERR, msg='注册失败') #5.跳转首页,保持登录状态 session['user_id'] = user.id session['name'] = user.name session['phone_num'] = user.phone_num #6.响应结果 return jsonify(re_code=RET.OK, msg='注册成功')
def registe(): # 获取参数判断是否空 date_dict = request.json mobile = date_dict.get("mobile") phonecode = date_dict.get("phonecode") password = date_dict.get("password") if not all([mobile,phonecode,password]): return jsonify(erron=RET.PARAMERR,errmsg="参数不全") #取到本地验证码 try: sms_code = redis_store.get("SMS_"+mobile) except Exception as e: current_app.logger.error(e) return jsonify(erron=RET.DATAERR,errmsg="本地短信验证码获取失败") if not sms_code: return jsonify(erron=RET.NODATA,errmsg="短信验证码过期") # 本地验证码和传入的短信验证码进行对比 if phonecode != sms_code: return jsonify(erron=RET.DATAERR,errmsg="短信验证码错误") user = User() user.name = mobile user.mobile = mobile user.password = password try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(erron=RET.DBERR,errmsg="保存数据失败") # 保存登陆状态 session["name"] = mobile session["moblie"] = mobile session["user_id"] = user.id return jsonify(erron=RET.OK,errmsg="注册成功")
def register(): dict_data = request.json mobile = dict_data.get("mobile") sms_code = dict_data.get("sms_code") password = dict_data.get("password") if not all([mobile, sms_code, password]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") if not re.match(r"1[3456789]\d{9}", mobile): return jsonify(errno=RET.DATAERR, errmsg="手机号码格式不对") try: redis_sms_code = redis_store.get("sms_code:%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="获取短信验证码") if not redis_sms_code: return jsonify(errno=RET.DBERR, errmsg="短信验证码已经过期") try: redis_store.delete("sms_code:%s" % mobile) except Exception as e: return jsonify(errno=RET.DATAERR, errmsg="短信验证码填写出错") user = User() user.name = mobile user.mobile = mobile user.pasword = password try: db.session.add(user) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="用户注册失败") return jsonify(errno=RET.OK, errmsg="注册成功")
def set_user_image(): """设置用户头像""" # 判断是否登录 # 获取头像参数 try: img_data = request.files.get('avatar') except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='传入参数错误') # 校验参数合法性 # 上传图片 try: key = upload_image(img_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='上传用户头像失败') # 查询用户 try: user = User().query.get(g.user_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询用户信息失败') # 给用户添加图片链接 user.avatar_url = key try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存用户头像失败') # 响应结果 avatar_url = constants.QINIU_DOMIN_PREFIX + key return jsonify(errno=RET.OK, errmsg='OK', data={'avatar_url': avatar_url})
def register(): # 1. 获取参数和判断是否有值 Post_register = request.json mobile = Post_register.get('mobile') ses_code = Post_register.get('phonecode') password = Post_register.get('password') if not all([mobile, ses_code, password]): current_app.logger.error("参数不足") return jsonify(errno=RET.PARAMERR, errsmg="参数不足") # 判断手机号码 if not re.match(r"1[2345678][0-9]{9}", mobile): return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误") # 2. 从redis中获取指定手机号对应的短信验证码的 try: real_sms_code = sr.get("SMS_CODE_%s" % mobile) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询redis中短信验证码异常") # 3. 校验验证码 if ses_code != real_sms_code: # 3.3 不相等:短信验证码填写错误 return jsonify(errno=RET.DATAERR, errmsg="短信验证码填写错误") # 4. 初始化 user 模型,并设置数据并添加到数据库 user = User() user.name = mobile user.mobile = mobile user.password = password # 5. 保存当前用户的状态 try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存对象异常") # 6. 返回注册的结果 return jsonify(errno=RET.OK, errmsg="注册成功")
def register(): # 获取参数 request_dict = request.json mobile = request_dict.get('mobile') sms_code_client = request_dict.get('sms_code') password = request_dict.get('password') password2 = request_dict.get('password2') # 判断参数有效性 if not all([mobile, sms_code_client, password, password2]): return jsonify(errno=RET.PARAMERR, errmsg='参数不完整') if not re.match(r'^1[3-8]\d{9}$', mobile): return jsonify(errom=RET.PARAMERR, errmsg='手机号码格式不合法') if password != password2: return jsonify(errno=RET.PARAMERR, errmsg='两次输入的密码不一致') try: sms_code_server = redis_store.get('sms_code:%s' % mobile) except Exception as e: return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if not sms_code_server: return jsonify(errno=RET.DATAERR, errmsg='验证码不存在,请先获取验证码') if sms_code_client != sms_code_server: return jsonify(errno=RET.PARAMERR, errmsg='手机验证码错误') user = User() user.mobile = mobile user.name = mobile user.password = password try: db.session.add(user) db.session.commit() except Exception as e: print(e) db.session.rollback() session['user_id'] = user.id session['name'] = user.name session['mobile'] = user.mobile return jsonify(errno=RET.OK, errmsg='注册成功')
def reg(): if request.method == 'GET': return render_template("reg.html") else: req_dict = request.get_json() username = req_dict.get("username") password = req_dict.get("password") mobile = req_dict.get("mobile") user = None try: user = User.query.filter_by(username=username).first() if user is not None: return jsonify(errno=RET.DATAEXIST, errmsg="用户名已存在") user = User.query.filter_by(mobile=mobile).first() if user is not None: return jsonify(errno=RET.DATAEXIST, errmsg="手机已存在") password = generate_password_hash(password) avatar_url = "/static/images/avatar.png" user = User(username=username, password=password, mobile=mobile, avatar_url=avatar_url) db.session.add(user) db.session.commit() return jsonify(errno=RET.OK, errmsg="注册成功") except Exception as e: print(e)