安居客首页
控制器(Houseinfo)代码
public function banner(Request $request)
{
$bannerdata = Houseinfo::where('is_recommend', '1')->orderBy('id', 'desc')->limit(5)->get();
$bannerdata = $bannerdata->toArray();
$newdata = [];
foreach ($bannerdata as $k => $v) {
//获取房源中第一张图片
$newdata[$k]['pic'] = "http://hzf/avatars/" . explode(",", $v['house_pic'])[0];
$newdata[$k]['house_name'] = $v['house_name'];
$newdata[$k]['id'] = $v['id'];
}
return ['code' => 0, 'data' => $newdata, 'msg' => '查询成功'];
}
public function getlist(Request $request)
{
$data = Houseinfo::select("id", "house_name", "house_using_area", "house_rent", "house_shi", "house_ting", "house_pic")->paginate(10);
return ["code" => 0, "msg" => "查询成功", 'data' => $data];
}
public function getoneHouseinfo(Request $request)
{
$data = Houseinfo::where("id", $request["id"])->first();
//房东
$data["house_owner"] = Houseowner::where("id", $data["house_owner"])->first();
//朝向
$data["house_direction"] = House::where("id", $data["house_direction"])->value("name");
//配置
$data["house_config"] = House::whereIn("id", explode(",", $data["house_config"]))->get();
if ($data) {
return ["code" => 0, "msg" => "查询成功", 'data' => $data];
} else {
return ["code" => 1, "msg" => "查询失败", 'data' => ""];
}
}
public function getsearchlist(Request $request)
{
$attrfiled = $request["file"];
if ($attrfiled == "house_rent") {
$in = $request["in"];
$between = explode("-", $in);
$data = Houseinfo::select("id", "house_name", "house_using_area", "house_rent", "house_shi", "house_ting", "house_pic")->whereIn("house_rent", $between)->orderBy("id")->paginate(10);
} else {
$houseattrid = $request["id"];
$data = Houseinfo::select("id", "house_name", "house_using_area", "house_rent", "house_shi", "house_ting", "house_pic")->where([$attrfiled => $houseattrid])->orderBy('id')->paginate(10);
}
return ["code" => 0, "msg" => "查询成功", 'data' => $data];
}
public function getCity(Request $request)
{
$latitude = $request["latitude"];
$longitude = $request["longitude"];
$url = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=$longitude,$latitude&key=ba386467ddc87ee7b1a8ab3fa7b40f76&radius=1000&extensions=all";
$data = file_get_contents($url);
$data = json_decode($data, true);
return $data["regeocode"]["addressComponent"]["province"];
}
public function looked(Request $request)
{
$openid = $request["openid"];
$renting_id = Renting::where("openid", $openid)->value("id");
// dd($renting_id);
$house_id = Notice::where("renting_id", $renting_id)->pluck("houseinfo_id");
// dd($house_id);
$house = Houseinfo::whereIn("id", $house_id)->get();
return $house;
}
模型层
Houseinfo
use SoftDeletes;
protected $dates=["deleted_at"];
protected $table="houseinfos";
public $timestamps=false;
protected $appends=["house_picurl","shi_ting"];
public function getshitingAttribute()
{
return $this->house_shi."室".$this->house_ting."厅".$this->house_wei."卫";
}
public function gethousepicurlAttribute()
{
return "http://hzf/avatars/".explode(",",$this->house_pic)[0];
}
public static function index()
{
return self::get();
}
public static function getalldata()
{
//获取所有的省份
$city=City::where(["pid"=>0])->get();
// dd($city->toArray());
//获取所有的房东
$houseowner=Houseowner::index();
// dd($houseowner);
//获取所有的朝向
$house_direction=House::where(["field_name"=>"house_direction"])->value("id");
$house_direction_data=House::where("pid",$house_direction)->get();
// dd($house_direction_data);
//获取所有的租赁方式
$house_rent_class=House::where(["field_name"=>"house_rent_class"])->value("id");
$house_rent_class_data=House::where("pid",$house_rent_class)->get();
// dd($house_rent_class_data);
//获取所有的配套设施
$house_config=House::where(["field_name"=>"house_config"])->value("id");
$house_config_data=House::where("pid",$house_config)->get();
return ["city"=>$city,"houseowner"=>$houseowner,"house_direction_data"=>$house_direction_data,"house_rent_class_data"=>$house_rent_class_data,"house_config_data"=>$house_config_data];
}
public static function Add($data)
{
return self::insert($data);
}
public static function destroy($id)
{
return self::where("id",$id)->delete();
}
houseowner(模型层)
use SoftDeletes;
protected $table="houseowners";
protected $dates=["deleted_at"];
public $timestamps=false;
public static function index()
{
return self::withTrashed()->get();
}
public static function Add($data)
{
return self::insert($data);
}
public static function Del($id)
{
return self::where("id",$id)->delete();
}
public static function delif($id)
{
return self::onlyTrashed()->where("id",$id)->restore();
}
House(模型层)
use SoftDeletes;
protected $dates=["deleted_at"];
protected $table="house";
public $timestamps=false;
protected $appends=["pic"];
public function getPicAttribute()
{
return "http://hzf/avatars/".$this->attributes["icon"];
}
public static function getinfo($where=[])
{
return self::withTrashed()->where($where)->get();
}
public static function Add($data)
{
return self::insert($data);
}
public static function Del($id)
{
return self::where("id",$id)->delete();
}
public static function restores($id)
{
return self::onlyTrashed()->where("id",$id)->restore();
}
public static function edit($id)
{
return self::where("id",$id)->first();
}
public static function updateds($id,$data)
{
return self::where("id",$id)->update($data);
}
Renting(模型层)
protected $table="rentings";
Notice(模型层)
protected $table="notices";
public $timestamps=false;
use SoftDeletes;
protected $dates=["deleted_at"];
//关联房东表
public function houseowner()
{
return $this->belongsTo(Houseowner::class,"houseowner_id");
}
//执行添加
public static function add($data)
{
return self::insert($data);
}
//执行删除
public static function del($id)
{
return self::where("id",$id)->delete();
}
//还原
public static function res($id)
{
return self::onlyTrashed()->where("id",$id)->restore();
}
public static function getOne($id)
{
return self::where("id",$id)->first();
}
public static function updata($id,$data)
{
return self::where("id",$id)->update($data);
}
小程序:(1)
html:
<!-- 搜索 -->
<view class="searchBox">
<view class="searchBox-area">
<text>{{getcity}}</text>
<text class="iconfont icon-xiangxia font10"></text>
</view>
<view class="searchBox-input">
<text class="iconfont icon-sousuo1"></text>
<navigator open-type="navigate" url="">你想住在哪?</navigator>
</view>
</view>
<!-- 幻灯片 -->
<swiper class="swiper-img" autoplay="true" circular="{{true}}" indicator-dots="{{true}}">
<swiper-item wx:for="{{banner}}" wx:key="item">
<image src="{{item.pic}}" />
</swiper-item>
</swiper>
<!-- 图标导航 -->
<view class="icon-nav" >
<navigator hover-class="none" open-type="navigate" url="">
<image src="/images/zufang1.png" />
<text>整租</text>
</navigator>
<navigator hover-class="none" open-type="navigate" url="">
<image src="/images/zufang2.png" />
<text>合租</text>
</navigator>
<navigator hover-class="none" open-type="navigate" url="">
<image src="/images/zufang3.png" />
<text>直租</text>
</navigator>
<navigator hover-class="none" open-type="navigate" url="">
<image src="/images/zufang4.png" />
<text>短租</text>
</navigator>
</view>
<!-- 租房小组 -->
<view class="renting-group">
<view class="renting-group-nav">
<view class="renting-group-nav-title">租房小组</view>
<view class="renting-group-nav-more">更多</view>
</view>
<view class="renting-group-itme">
<navigator open-type="navigate" url="" wx:for="{{house_group}}" wx:key="item">
<image src="{{item.pic}}" />
</navigator>
</view>
</view>
<!-- 推荐房源 -->
<view class="recommend">
<view class="nav">
<view class="recommend-search">
<view class="searchBox-area" data-index="{{0}}" bind:tap="onShadeDiv">
<text>区域</text>
<text class="iconfont font10 {{ arrows[0] }}"></text>
</view>
<view class="searchBox-area" data-index="{{1}}" bind:tap="onShadeDiv">
<text>方式</text>
<text class="iconfont font10 {{ arrows[1] }}"></text>
</view>
<view class="searchBox-area" data-index="{{2}}" bind:tap="onShadeDiv">
<text>租金</text>
<text class="iconfont font10 {{ arrows[2] }}"></text>
</view>
<view class="searchBox-area" data-index="{{3}}" bind:tap="onShadeDiv">
<text>筛选</text>
<text class="iconfont font10 {{ arrows[3] }}"></text>
</view>
</view>
<!-- 选择器 -->
<view class="shadediv {{show[0]}}">你好世界1</view>
<view class="shadediv {{show[1]}}">你好世界2</view>
<view class="shadediv {{show[2]}}">你好世界3</view>
<view class="shadediv {{show[3]}}">你好世界4</view>
</view>
<scroll-view scroll-y="{{true}}" class="sview" bindscrolltolower="fang" bindscroll="fangcroll" scroll-top="{{top}}" scroll-with-animation ="{{true}}">
<view class="recommend" wx:for="{{house_list}}" wx:key="item">
<navigator class="recommend-list" open-type="navigate" url="/pages/fang/fang?id={{item.id}}">
<view>
<image src="{{item.house_picurl}}" />
</view>
<view class="recommend-list-item">
<view class="recommend-list-item-title">{{item.house_name}}</view>
<view class="recommend-list-item-spec">{{item.house_using_area}}平米 | {{item.shi_ting}}</view>
<view class="recommend-list-item-tag">
<text class="tag1">押一付三</text>
<text class="tag2">免押金</text>
<text class="tag3">精装</text>
</view>
<view class="recommend-list-item-price">
<text>{{item.house_rent}}</text>
元/月
</view>
</view>
</navigator>
</view>
</scroll-view>
</view>
<!-- 回到顶部 -->
<view class="gototop {{toshow}}" bindtap="gototop">
<text class="iconfont font30 icon-fanhuidingbu"></text>
</view>
js:
// pages/index/index.js
Page({
/**
* 页面的初始数据
*/
data: {
show: ['', '', '', ''],
showflag: [false, false, false, false],
arrows:['icon-xiangxia','icon-xiangxia','icon-xiangxia','icon-xiangxia'],
banner:[],
house_group:[],
house_list:[],
page:1,
last_page:1,
toshow:"",
top:0,
getcity:""
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
let _this=this;
wx.getLocation({
type: 'wgs84',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
wx.request({
url: 'http://hzf/api/getCity', //仅为示例,并非真实的接口地址
data: {
latitude:latitude,
longitude:longitude
},
success (res) {
console.log(res.data);
_this.setData({
getcity:res.data
})
}
})
}
})
// 登录请求
wx.login({
timeout:2000,
success:({code})=>{
// code有效期是5分钟
// console.log(code);
// 发起request请求到自己的服务器
wx.request({
url: 'http://hzf/api/wxlogin',
data: {code},
method: 'POST',
success:ret=>{
// console.log(ret);
}
})
}
})
wx.request({
url: 'http://hzf/api/banner', //仅为示例,并非真实的接口地址
success (res) {
// console.log(res.data)
_this.setData({
banner:res.data.data
})
}
})
wx.request({
url: 'http://hzf/api/attr', //仅为示例,并非真实的接口地址
data: {
filed:"house_group"
},
success (res) {
// console.log(res.data.data)
_this.setData({
house_group:res.data.data
})
}
})
this.fang();
},
fang:function(){
//this.data.page
if(this.data.page<=this.data.last_page){
wx.showLoading({
title: '加载中',
mask:true
})
const thit=this;
wx.request({
url: 'http://hzf/api/getlist', //仅为示例,并非真实的接口地址
data: {
page:thit.data.page
},
success (res) {
console.log(res)
// console.log(thit.data);
thit.setData({
house_list:[...thit.data.house_list,...res.data.data.data],
page:thit.data.page+1,
last_page:res.data.data.last_page,
})
console.log(thit.data.page);
wx.hideLoading({})
}
})
}else{
wx.showToast({
title: '已经没有数据了',
icon: 'none',
mask: true
})
}
},
fangcroll:function(ret){
if(ret.detail.scrollTop>=300){
this.setData({
toshow:"toshow"
})
}else{
this.setData({
toshow:""
})
}
},
gototop:function(ret){
this.setData({
top:0
})
},
// 遮罩
onShadeDiv(evt) {
let index = evt.currentTarget.dataset.index;
let show = this.data.show;
let showflag = this.data.showflag;
let arrows = this.data.arrows;
if (showflag[index]) { // 已显示,再次点击隐藏起来
show[index] = '';
showflag[index] = false;
arrows[index] = 'icon-xiangxia';
} else {
for (let key in show) {
if (key == index) {
show[key] = 'now';
showflag[key] = true;
arrows[key] = 'icon-xiangshang';
} else {
show[key] = '';
showflag[key] = false;
arrows[key] = 'icon-xiangxia';
}
}
}
this.setData({
show,
showflag,
arrows
})
}
})