0.页面展示效果
1.设置用户实名认证信息–后端代码
@api.route("/users/auth", methods=["POST"])
@login_required
def set_user_auth():"""保存实名认证信息"""user_id = g.user_id# 获取参数req_data = request.get_json()if not req_data:return jsonify(errno=RET.PARAMERR, errmsg="参数错误")real_name = req_data.get("real_name") # 真实姓名id_card = req_data.get("id_card") # 身份证号# 参数校验if not all([real_name, id_card]):return jsonify(errno=RET.PARAMERR, errmsg="参数错误")# 保存用户的姓名与身份证号try:User.query.filter_by(id=user_id, real_name=None, id_card=None)\.update({"real_name": real_name, "id_card": id_card})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")
2.前端代码
<!DOCTYPE html>
<html>
<head> <meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><title>爱家-实名认证</title><link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet"><link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"><link href="/static/css/reset.css" rel="stylesheet"><link href="/static/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet"><link href="/static/css/ihome/main.css" rel="stylesheet"><link href="/static/css/ihome/profile.css" rel="stylesheet">
</head>
<body><div class="container"><div class="top-bar"><div class="nav-bar"><h3 class="page-title">实名认证</h3><a class="nav-btn fl" href="/my.html"><span><i class="fa fa-angle-left fa-2x"></i></span></a></div></div><div class="menus-con"><ul class="menus-list"><li><div class="menu-title"><h3>认证信息</h3></div><div class="menu-content"><form id="form-auth" method="post"><div class="form-group"><label for="real-name">真实姓名</label><input type="text" class="form-control" name="real_name" id="real-name" ></div><div class="form-group"><label for="id-card">身份证号码</label><input type="text" class="form-control" name="id_card" id="id-card"></div> <div class="error-msg"><i class="fa fa-exclamation-circle"></i>信息填写不完整,请补全信息</div><input type="submit" class="btn btn-success" value="保存"></form></div></li></ul></div><div class="popup_con"><div class="popup"><p>保存成功!</p></div><div class="mask"></div></div><div class="footer"><p><span><i class="fa fa-copyright"></i></span>爱家租房 享受家的温馨</p></div> </div><script src="/static/js/jquery.min.js"></script><script src="/static/js/jquery.form.min.js"></script><script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script><script src="/static/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script><script src="/static/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script><script src="/static/js/ihome/auth.js"></script>
</body>
</html>
3.js代码
function showSuccessMsg() {$('.popup_con').fadeIn('fast', function() {setTimeout(function(){$('.popup_con').fadeOut('fast',function(){}); },1000) });
}function getCookie(name) {var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");return r ? r[1] : undefined;
}$(document).ready(functifon(){// 查询用户的实名认证信息$.get("/api/v1.0/users/auth", function(resp){// 4101代表用户未登录if ("4101" == resp.errno) {location.href = "/login.html";} else if ("0" == resp.errno) {// 如果返回的数据中real_name与id_card不为null,表示用户有填写实名信息if (resp.data.real_name && resp.data.id_card) {$("#real-name").val(resp.data.real_name);$("#id-card").val(resp.data.id_card);// 给input添加disabled属性,禁止用户修改$("#real-name").prop("disabled", true);$("#id-card").prop("disabled", true);// 隐藏提交保存按钮$("#form-auth>input[type=submit]").hide();}} else {alert(resp.errmsg);}}, "json");// 管理实名信息表单的提交行为$("#form-auth").submit(function(e){e.preventDefault();// 如果用户没有填写完整,展示错误信息var realName = $("#real-name").val();var idCard = $("#id-card").val();if (realName == "" || idCard == "") {$(".error-msg").show();}// 将表单的数据转换为json字符串var data = {real_name: realName,id_card: idCard};var jsonData = JSON.stringify(data);// 向后端发送请求$.ajax({url:"/api/v1.0/users/auth",type:"post",data: jsonData,contentType: "application/json",dataType: "json",headers: {"X-CSRFTOKEN": getCookie("csrf_token")},success: function (resp) {if (0 == resp.errno) {$(".error-msg").hide();// 显示保存成功的提示信息showSuccessMsg();$("#real-name").prop("disabled", true);$("#id-card").prop("disabled", true);$("#form-auth>input[type=submit]").hide();}}});})})
4.获取用户实名认证信息–后端代码
@api.route("/users/auth", methods=["GET"])
@login_required
def get_user_auth():"""获取用户的实名认证信息"""user_id = g.user_id# 在数据库中查询信息try:user = User.query.get(user_id)except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="获取用户实名信息失败")if user is None:return jsonify(errno=RET.NODATA, errmsg="无效操作")return jsonify(errno=RET.OK, errmsg="OK", data=user.auth_to_dict())