租房掌柜微信小程序Api以及小程序前端模板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

193 lines
7.0 KiB

<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Db;
/**
* 应用入口
* Class Index
* @package app\index\controller
*/
class Index extends Controller
{
public $table = 'CloudHouse';//房源表
public $table_room = 'CloudHouseRoom';//子房间表
/**
* 首页API
*/
public function index()
{
// $_collection = new Collection();
// $_data['collection'] = $_collection->index();
$_data['room'] = $this->roomShare();
$user_id = $_SERVER['HTTP_USERID'];
if (empty($user_id)) {
$_data['expire_num'] = 0;
} else {
//房源类
$_house = new House();
//子房间到期数量
$_data['expire_num'] = $_house->expire_room(3, false);
//子房间到期数量
$_data['house_expire_num'] = $_house->expire(3, false);
}
return send_http_status(200, $_data);
}
/**
* 获取未出租共享房源
* @param int $num 公里数 默认为2
* @param int $latitude 纬度
* @param int $longitude 经度
* @return \type
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function roomShare()
{
$_search = $this->request->param('', '', 'filter_danger_str');
//判断是否含有 附近几公里的搜索条件
$num = $this->request->param('num', '', 'filter_danger_str');
$latitude = $this->request->param('latitude', '', 'filter_danger_str');
$longitude = $this->request->param('longitude', '', 'filter_danger_str');
$_db = Db::name($this->table_room);
if (!empty($num) && !empty($latitude) && !empty($longitude)) {
$distance = 1;
//查询附近几公里的一手房源数据
$sql = 'SELECT id,(
6371 * acos (
cos ( radians(' . $latitude . ') )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(' . $longitude . ') )
+ sin ( radians(' . $latitude . ') )
* sin( radians( latitude ) )
)
) AS distance
FROM cloud_house
HAVING distance < ' . $num . '
ORDER BY distance
LIMIT 0 , 500';
$_house = Db::query($sql);
if (!empty($_house)) {
$house_ids = implode(array_column($_house, 'id'), ',');
}
//判断几公里搜索条件是否成立即 house_ids是否有值
$house_ids = !empty($house_ids) ? $house_ids : 0;
$_db->where('h.id', 'in', $house_ids);
}
//判断是否为会员
$user_id = $_SERVER['HTTP_USERID'];
// $vip_level = Db::name('cloud_member')->field('vip_level')->find($user_id);
// if (empty($vip_level) || $vip_level['vip_level'] < 1) {
// $_db->where('h.user_id', $user_id);
// }
//城市条件搜索
if (!empty($_search['province']) && empty($distance)) {
$_db->where('h.province', $_search['province']);
$_db->where('h.city', $_search['city']);
$_db->where('h.area', $_search['area']);
}
if (!empty($_search['search']) && empty($distance)) {
$_db->where('h.name', 'like', '%' . $_search['search'] . '%');
}
$_db->alias('r')->leftjoin(['cloud_house' => 'h'], 'r.house_id = h.id')->leftjoin('cloud_member m', 'r.user_id = m.id');
$_db->where('r.is_del', 0);
$_db->where('h.is_del', 0);
$_db->where(function ($query) {
$query->where('r.end_time', '=', 0);
$query->whereOr('r.end_time', '<', time());
});
$count = $_db->count();
$page = $this->request->param('page', 1, 'filter_danger_str');
$pagesize = $this->request->param('pagesize', 10, 'filter_danger_str');
$pages = ceil($count / $pagesize); //总页数
$_db->page($page, $pagesize)->order('r.id', "DESC");
$field = 'r.*,h.apartment,h.id AS house_id,h.name,h.name,h.photo,h.province,h.city,h.area,h.door_number,h.floor_num,m.phone';
$_result = $_db->field($field)->select();
self::formatRoomData($_result);
$_data['list'] = $_result;
$_data['pages'] = $pages;
self::formatData($_data['list']);
return $_data;
}
/**
* 获取未出租共享房源 根据公里数
* @param int $num 公里数 默认为2
* @param int $latitude 纬度
* @param int $longitude 经度
* @return \type
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function roomShareLocation($num = 2, $latitude = '', $longitude = '')
{
}
/**
* 房间数据格式化
* @param $data
*/
static private function formatRoomData(&$data)
{
if (!$data) {
return;
}
array_walk($data, function (&$v, &$k) {
$v['days'] = time_days($v['end_time']);
//列表封面
if (isset($v['photo'])) {
$_arr = explode(',', $v['photo']);
$v['thumb'] = !empty($_arr[0]) ? $_arr[0] : config('default_list_thumb');
}
});
}
static private function formatData(&$data)
{
if (!$data) {
return;
}
array_walk($data, function (&$v, &$k) {
!empty($v['create_time']) ? $v['create_time'] = date('Y-m-d', $v['create_time']) : $v['create_time'] = '';
!empty($v['update_time']) ? $v['update_time'] = date('Y-m-d', $v['update_time']) : $v['update_time'] = '';
!empty($v['end_time']) ? $v['end_time'] = date('Y-m-d', $v['end_time']) : $v['end_time'] = '';
!empty($v['start_time']) ? $v['start_time'] = date('Y-m-d', $v['start_time']) : $v['start_time'] = '';
isset($v['sex']) ? $v['sex'] = config('sex')[$v['sex']] : '';
//列表封面
if (isset($v['photo'])) {
$_arr = explode(',', $v['photo']);
$v['thumb'] = !empty($_arr[0]) ? $_arr[0] : config('default_list_thumb');
}
});
}
}