文章目录
- 0.效果展示
- 1.重点总结
- 2.后端代码
- 3.前端html
- 4.前端js
0.效果展示
1.重点总结
1)用户点击“我的房源”,页面开始加载,此时myhouse.js限定只有完成实名认证的房东才可以查询已有的房源信息,因此myhouse.js首先调用用户认证的接口,根据相应的返回值,判断接下来应该返回登录页location.href = “/login.html"还是显示报警信息 $(”.auth-warn").show()。
2)如果调用用户认证的接口后,发现用户已经进行了实名注册,此时调用后端返回房屋列表信息的接口get_user_houses();
经过逻辑处理,将查询到的房屋信息转换为字典存放到列表中,并将状态码errno、返回信息errmsg、以及数据data一并通过jsonfiy()函数处理,将json格式的数据返回给前端myhouse.js文件。
3)接下来,myhouse.js文件将前端相应id的部分#houses-list进行渲染,并显示给用户。
2.后端代码
houses.py部分接口:
@api.route("/user/houses", methods=["GET"])
@login_required
def get_user_houses():"""获取房东发布的房源信息条目"""user_id = g.user_idtry:# House.query.filter_by(user_id=user_id)user = User.query.get(user_id)houses = user.housesexcept Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="获取数据失败")# 将查询到的房屋信息转换为字典存放到列表中houses_list = []if houses:for house in houses:houses_list.append(house.to_basic_dict())return jsonify(errno=RET.OK, errmsg="OK", data={"houses": houses_list})
3.前端html
myhouse.html
<!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/myhouse.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="houses-con"><ul class="houses-list auth-warn"><li><div class="house-title"><h3>尚未进行实名认证,无法发布房屋信息!</h3></div><div class="house-content"><a href="/auth.html" class="btn btn-success">去实名认证</a></div></li></ul><ul id="houses-list" class="houses-list"></ul><script id="houses-list-tmpl" type="text/html"><li><div class="new-house"><a href="/newhouse.html">发布新房源</a></div></li>{{each houses as house}}<li><a href="/detail.html?id={{house.house_id}}"><div class="house-title"><h3>房屋ID:{{house.house_id}} —— {{house.title}}</h3></div><div class="house-content"><img src="{{house.img_url}}"><div class="house-text"><ul><li>位于:{{house.area_name}}</li><li>价格:¥{{(house.price/100.0).toFixed(0)}}/晚</li><li>发布时间:{{house.ctime}}</li></ul></div></div></a></li>{{/each}}</script></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/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/template.js"></script><script src="/static/js/ihome/myhouse.js"></script>
</body>
</html>
4.前端js
myhouse.js:
$(document).ready(function(){// 对于发布房源,只有认证后的用户才可以,所以先判断用户的实名认证状态$.get("/api/v1.0/users/auth", function(resp){if ("4101" == resp.errno) {// 用户未登录location.href = "/login.html";} else if ("0" == resp.errno) {// 未认证的用户,在页面中展示 "去认证"的按钮if (!(resp.data.real_name && resp.data.id_card)) {$(".auth-warn").show();return;}// 已认证的用户,请求其之前发布的房源信息$.get("/api/v1.0/user/houses", function(resp){if ("0" == resp.errno) {$("#houses-list").html(template("houses-list-tmpl", {houses:resp.data.houses}));} else {$("#houses-list").html(template("houses-list-tmpl", {houses:[]}));}});}});
})