文章目录
- 0.效果展示
- 1.思路总结
- 2.后端接口
- 3.前端js
- 4.前端html
0.效果展示
1.思路总结
1)房屋详情页面开始加载时,detail.js首先通过定义的函数(重点:document.location.search),截取需要向后端取得详情页面的house_id;
2)后端经过逻辑处理,首先向redis中获取相应房屋的具体信息,若redis中不存在,则向数据库查询,返回给前端detail.js;
接下来,detail.js分两部分对前端页面进行渲染;
效果如下:
3)同时判断当前用户是否为房东,如果不为房东,则想用户展示“即刻预订”按钮。
2.后端接口
houses.py相关接口:
@api.route("/houses/<int:house_id>", methods=["GET"])
def get_house_detail(house_id):"""获取房屋详情"""# 前端在房屋详情页面展示时,如果浏览页面的用户不是该房屋的房东,则展示预定按钮,否则不展示,# 所以需要后端返回登录用户的user_id# 尝试获取用户登录的信息,若登录,则返回给前端登录用户的user_id,否则返回user_id=-1user_id = session.get("user_id", "-1")# 校验参数if not house_id:return jsonify(errno=RET.PARAMERR, errmsg="参数缺失")# 先从redis缓存中获取信息try:ret = redis_store.get("house_info_%s" % house_id)except Exception as e:current_app.logger.error(e)ret = Noneif ret:current_app.logger.info("hit house info redis")return '{"errno":"0", "errmsg":"OK", "data":{"user_id":%s, "house":%s}}' % (user_id, ret), \200, {"Content-Type": "application/json"}# 查询数据库try:house = House.query.get(house_id)except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="查询数据失败")if not house:return jsonify(errno=RET.NODATA, errmsg="房屋不存在")# 将房屋对象数据转换为字典try:house_data = house.to_full_dict()except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DATAERR, errmsg="数据出错")# 存入到redis中json_house = json.dumps(house_data)try:redis_store.setex("house_info_%s" % house_id, constants.HOUSE_DETAIL_REDIS_EXPIRE_SECOND, json_house)except Exception as e:current_app.logger.error(e)resp = '{"errno":"0", "errmsg":"OK", "data":{"user_id":%s, "house":%s}}' % (user_id, json_house), \200, {"Content-Type": "application/json"}return resp
3.前端js
detail.js
function hrefBack() {history.go(-1);
}// 解析提取url中的查询字符串参数
function decodeQuery(){var search = decodeURI(document.location.search);return search.replace(/(^\?)/, '').split('&').reduce(function(result, item){values = item.split('=');result[values[0]] = values[1];return result;}, {});
}$(document).ready(function(){// 获取详情页面要展示的房屋编号var queryData = decodeQuery();var houseId = queryData["id"];// 获取该房屋的详细信息$.get("/api/v1.0/houses/" + houseId, function(resp){if ("0" == resp.errno) {$(".swiper-container").html(template("house-image-tmpl", {img_urls:resp.data.house.img_urls, price:resp.data.house.price}));$(".detail-con").html(template("house-detail-tmpl", {house:resp.data.house}));// resp.user_id为访问页面用户,resp.data.user_id为房东if (resp.data.user_id != resp.data.house.user_id) {$(".book-house").attr("href", "/booking.html?hid="+resp.data.house.hid);$(".book-house").show();}var mySwiper = new Swiper ('.swiper-container', {loop: true,autoplay: 2000,autoplayDisableOnInteraction: false,pagination: '.swiper-pagination',paginationType: 'fraction'});}})
})
4.前端html
detail.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/swiper/css/swiper.min.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/detail.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="#" onclick="hrefBack();"><span><i class="fa fa-angle-left fa-2x"></i></span></a></div></div><div class="swiper-container"></div><script id="house-image-tmpl" type="text/html"><ul class="swiper-wrapper">{{each img_urls as img_url}}<li class="swiper-slide"><img src="{{img_url}}"></li>{{/each}}</ul><div class="swiper-pagination"></div><div class="house-price">¥<span>{{(price/100.0).toFixed(0)}}</span>/晚</div></script><div class="detail-con"></div><script id="house-detail-tmpl" type="text/html"><div class="detail-header layout-style"><h2 class="house-title">{{house.title}}</h2><div class="landlord-pic"><img src="{{house.user_avatar}}"></div><h2 class="landlord-name">房东: <span>{{house.user_name}}</span></h2></div><div class="house-info layout-style"><h3>房屋地址</h3><ul class="house-info-list text-center"><li>{{house.address}}</li></ul></div><ul class="house-type layout-style"><li><span class="icon-house"></span><div class="icon-text"><h3>出租{{house.room_count}}间</h3><p>房屋面积:{{house.acreage}}平米</p><p>房屋户型:{{house.unit}}</p></div></li><li><span class="icon-user"></span><div class="icon-text"><h3>宜住{{house.capacity}}人</h3></div></li><li><span class="icon-bed"></span><div class="icon-text"><h3>卧床配置</h3><p>{{house.beds}}</p></div></li></ul><div class="house-info layout-style"><h3>房间详情</h3><ul class="house-info-list"><li>收取押金<span>{{(house.deposit/100.0).toFixed(0)}}</span></li><li>最少入住天数<span>{{house.min_days}}</span></li><li>最多入住天数<span>{{if house.max_days==0}}无限制{{else}}{{house.max_days}}{{/if}}</span></li></ul></div><div class="house-facility layout-style"><h3>配套设施</h3><ul class="house-facility-list clearfix"><li><span class="{{if house.facilities.indexOf(1)>=0}}wirelessnetwork-ico{{else}}jinzhi-ico{{/if}}"></span>无线网络</li><li><span class="{{if house.facilities.indexOf(2)>=0}}shower-ico{{else}}jinzhi-ico{{/if}}"></span>热水淋浴</li><li><span class="{{if house.facilities.indexOf(3)>=0}}aircondition-ico{{else}}jinzhi-ico{{/if}}"></span>空调</li><li><span class="{{if house.facilities.indexOf(4)>=0}}heater-ico{{else}}jinzhi-ico{{/if}}"></span>暖气</li><li><span class="{{if house.facilities.indexOf(5)>=0}}smoke-ico{{else}}jinzhi-ico{{/if}}"></span>允许吸烟</li><li><span class="{{if house.facilities.indexOf(6)>=0}}drinking-ico{{else}}jinzhi-ico{{/if}}"></span>饮水设备</li><li><span class="{{if house.facilities.indexOf(7)>=0}}brush-ico{{else}}jinzhi-ico{{/if}}"></span>牙具</li><li><span class="{{if house.facilities.indexOf(8)>=0}}soap-ico{{else}}jinzhi-ico{{/if}}"></span>香皂</li><li><span class="{{if house.facilities.indexOf(9)>=0}}slippers-ico{{else}}jinzhi-ico{{/if}}"></span>拖鞋</li><li><span class="{{if house.facilities.indexOf(10)>=0}}toiletpaper-ico{{else}}jinzhi-ico{{/if}}"></span>手纸</li><li><span class="{{if house.facilities.indexOf(11)>=0}}towel-ico{{else}}jinzhi-ico{{/if}}"></span>毛巾</li><li><span class="{{if house.facilities.indexOf(12)>=0}}toiletries-ico{{else}}jinzhi-ico{{/if}}"></span>沐浴露、洗发露</li><li><span class="{{if house.facilities.indexOf(13)>=0}}icebox-ico{{else}}jinzhi-ico{{/if}}"></span>冰箱</li><li><span class="{{if house.facilities.indexOf(14)>=0}}washer-ico{{else}}jinzhi-ico{{/if}}"></span>洗衣机</li><li><span class="{{if house.facilities.indexOf(15)>=0}}elevator-ico{{else}}jinzhi-ico{{/if}}"></span>电梯</li><li><span class="{{if house.facilities.indexOf(16)>=0}}iscook-ico{{else}}jinzhi-ico{{/if}}"></span>允许做饭</li><li><span class="{{if house.facilities.indexOf(17)>=0}}pet-ico{{else}}jinzhi-ico{{/if}}"></span>允许带宠物</li><li><span class="{{if house.facilities.indexOf(18)>=0}}meet-ico{{else}}jinzhi-ico{{/if}}"></span>允许聚会</li><li><span class="{{if house.facilities.indexOf(19)>=0}}accesssys-ico{{else}}jinzhi-ico{{/if}}"></span>门禁系统</li><li><span class="{{if house.facilities.indexOf(20)>=0}}parkingspace-ico{{else}}jinzhi-ico{{/if}}"></span>停车位</li><li><span class="{{if house.facilities.indexOf(21)>=0}}wirednetwork-ico{{else}}jinzhi-ico{{/if}}"></span>有线网络</li><li><span class="{{if house.facilities.indexOf(22)>=0}}tv-ico{{else}}jinzhi-ico{{/if}}"></span>电视</li><li><span class="{{if house.facilities.indexOf(23)>=0}}hotbathtub-ico{{else}}jinzhi-ico{{/if}}"></span>浴缸</li></ul></div>{{if house.comments }}<div class="house-info layout-style"><h3>评价信息</h3><ul class="house-comment-list">{{ each house.comments as comment}}<li><p>{{comment.user_name}}<span class="fr">{{comment.ctime}}</span></p><p>{{comment.comment}}</p></li>{{/each}}</ul></div>{{/if}}</script><a class="book-house" href="">即刻预定</a><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/swiper/js/swiper.jquery.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/detail.js"></script>
</body>
</html>