前端数据代码
数据表示与发送
<view class="login_from_input form-group">
<view class="login_from_name">用户名</view>
<view class="login_from_fun"><input type="text" name="username" v-model="username" placeholder="请输如用户名"></view>
</view>
<view class="login_from_input form-group">
<view class="login_from_name">密码</view>
<view class="login_from_fun"><input type="digit" v-model="password" name="password" password="true" placeholder="请输入登录密码"></view>
</view>
<script>
export default{
data(){
return {
username:"",
password:"",
}
},
methods:{
login(){
uni.request({
url:"http://127.0.0.1:5000/auth/register",
method:"POST",
//这里的data 后端flask可以根据变量名得到 request.form.get("username")
data:{
username :this.username,
password:this.password,
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success:(res) => {
console.log(res)
}
})
}
后端接收数据
1.接受文件接口
@bp.route("/register" , methods=['GET','POST'])
@cross_origin()
def register():
form = RegisterForm(request.form)
if form.validate():
username = form.username.data
password = form.password.data
user_model = UserModel(username=username,password=generate_password_hash(password),email=email)
db.session.add(user_model)
db.session.commit()
return redirect("login")
else:
print(form.errors)
return "fail"
2.数据验证 (格式,邮箱是否已存在,验证码是否正确)
class RegisterForm(wtforms.Form):
email = wtforms.StringField(validators=[Email(message="邮箱格式错误!")])
captcha = wtforms.StringField(validators=[Length(min=4, max=4, message="验证码格式错误")])
username = wtforms.StringField(validators=[Length(min=3, max=10, message="用户名格式错误")])
password = wtforms.StringField(validators=[Length(min=6, max=20, message="密码格式错误")])
#test = wtforms.StringField(validators=[Length(min=4,max=4,message="test信息错误")])
#1. 邮箱是否已经被注册
def validate_email(self,filed):
email = filed.data
#print(email)
user = UserModel.query.filter_by(email = email).first()
if user:
raise wtforms.ValidationError(message="邮箱已经被注册")
#2. 验证码是否正确
def validate_captcha(self,filed):
captcha = filed.data
#print(captcha)
email = self.email.data
captcha_model = EmailCaptchaModel.query.filter_by(email=email, captcha=captcha).first()
#captcha_model.used = True
if not captcha_model:
raise wtforms.ValidationError(message="邮箱或验证码错误")
else:
db.session.delete(captcha_model)
db.session.commit()