2023-03-27 14:38:02 +08:00

747 lines
29 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\controller;
use app\api\BaseController;
use app\api\middleware\Auth;
use think\facade\Db;
use think\facade\Cache;
/**
* 村庄接口.
*/
class Village extends BaseController
{
/**
* 控制器中间件 [不需要鉴权]
* @var array
*/
protected $middleware = [
Auth::class => ['except' => ['get_list','get_ranking_list','get_detail','get_json','get_village','get_ranking_town','get_town_list'] ]
];
/**
* 获取村庄
*
* //php think crud -t slide_info -c work/slideinfo
* php think menu -c work/slideinfo
*/
public function get_list($area_id=0,$street_id=0,$type=1, $page = 1)
{
if ($area_id){
$where['area_id'] = $area_id;
}
if ($street_id){
$where['street_id'] = $street_id;
}
if($type==1){
$where['is_hot'] = 1;
}
if($type==2){
$where['is_tourism'] = 1;
}
$where['status'] = 1;
$list = Db::table('fa_szxc_village')
->where($where)
->where('village_id','>',0)
->page($page,10)
->field('id,title,address,people_num,images,views,likes,createtime,area_id,street_id,village_id')
->order('likes desc')
->withAttr('images', function ($value, $data) {
if ($data['images'] != '') {
return explode(',',$data['images']);
}
})
->withAttr('is_dz', function ($value, $data) {
// 判断redis
$user_id = $this->request->uid;
if($user_id){
$name = $user_id . $data['id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
return 1;
}else{
return 0;
}
}else{
return 0;
}
})
->select();
if($list){
$this->apiSuccess('获取成功',$list);
}else{
$this->apiError('暂无数据',[]);
}
}
/**
* 获取村庄
*
* //php think crud -t slide_info -c work/slideinfo
* php think menu -c work/slideinfo
*/
public function get_ranking_list($page = 1,$limit=10)
{
$where['status'] = 1;
$list = Db::table('fa_szxc_village')
->where($where)
->where('village_id','>',0)
->page($page,$limit)
->field('id,title,address,people_num,images,views,likes,createtime,area_id,street_id,village_id')
->order('likes desc')
->withAttr('images', function ($value, $data) {
if ($data['images'] != '') {
return explode(',',$data['images']);
}
})
->withAttr('is_dz', function ($value, $data) {
// 判断redis
$user_id = $this->request->uid;
if($user_id){
$name = $user_id . $data['id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
return 1;
}else{
return 0;
}
}else{
return 0;
}
})
->select();
if($list){
$this->apiSuccess('获取成功',$list);
}else{
$this->apiError('暂无数据',[]);
}
}
// 获取村庄详情
public function get_detail($id){
if (empty($id)) {
$this->apiError('缺少参数');
}
$where[] = ['id', '=', $id];
$news = Db::table('fa_szxc_village')->where($where)->field('id,title,address,people_num,images,views,likes,createtime,introduce,info,area_id,street_id,village_id')->find();
if ($news) {
// 判断redis
$user_id = $this->request->uid;
if($user_id){
$name = $user_id . $news['id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
$news['is_dz'] = 1;
}else{
$news['is_dz'] = 0;
}
}else{
$news['is_dz'] = 0;
}
// 获取天气
// 101271007 纳溪 510503
// 101271002 江阳区 510502
// 101271008 龙马潭区 510504
// 101271003 泸县 510521
// 101271004 合江 510522
// 101271006 古蔺 510525
// 101271005 叙永 510524
// 判断区域
if($news['area_id'] == '510503'){
$name = '101271007';
}
if($news['area_id'] == '510502'){
$name = '101271002';
}
if($news['area_id'] == '510504'){
$name = '101271008';
}
if($news['area_id'] == '510521'){
$name = '101271003';
}
if($news['area_id'] == '510522'){
$name = '101271004';
}
if($news['area_id'] == '510525'){
$name = '101271006';
}
if($news['area_id'] == '510524'){
$name = '101271005';
}
$url = "https://devapi.qweather.com/v7/weather/now?key=b3e94fa75aae4551b6a5db150b409261&location=".$name;
$url1 = "https://devapi.qweather.com/v7/indices/1d?key=b3e94fa75aae4551b6a5db150b409261&type=0&location=".$name;
$is_cun = Cache::store('redis')->get($name);
$is_cun1 = Cache::store('redis')->get($name.'-1');
if($is_cun){
$data = json_decode($is_cun,1);
$data1 = json_decode($is_cun1,1);
$news['weather'] = $data;
$news['weather_indices'] = $data1;
}else{
$data = file_get_contents("compress.zlib://".$url);
$data1 = file_get_contents("compress.zlib://".$url1);
Cache::store('redis')->set($name,$data,1800);
Cache::store('redis')->set($name.'-1',$data1,1800);
$data = json_decode($data,1);
$data1 = json_decode($data1,1);
$news['weather'] = $data;
$news['weather_indices'] = $data1;
}
// halt($data);die;
$news['people_num'] = Db::table('fa_szxc_information_useraddress')->where('village_id',$news['village_id'])->where('status',1)->count();
// 增加阅读数
Db::table('fa_szxc_village')->where($where)->inc('views', '1')->update();
if ($news['images']) {
$news['images'] = explode(',',$news['images']);
}
$this->apiSuccess('获取成功', $news);
} else {
$this->apiError('获取失败');
}
}
// 获取区域json
public function get_json($code){
if (empty($code)) {
$this->apiError('缺少参数');
}
$where[] = ['code', '=', $code];
$list = Db::table('fa_szxc_json')->where($where)->find();
if ($list) {
//级别1市级2区县3乡镇
if($list['level'] == 1){
$num1 = Db::table('fa_szxc_village')->where('village_id','>',0)->where('status',1)->count();
$num2 = Db::table('fa_szxc_information_useraddress')->count();
$num3 = Db::table('fa_szxc_information_usermsg')->where('is_hz',1)->count();
}elseif($list['level'] == 2){
$num1 = Db::table('fa_szxc_village')->where('area_id',$code)->where('village_id','>',0)->where('status',1)->count();
$num2 = Db::table('fa_szxc_information_useraddress')->where('area_id',$code)->count();
$num3 = Db::table('fa_szxc_information_usermsg')->where('is_hz',1)->where('area_id',$code)->count();
}
elseif($list['level'] == 3){
$num1 = Db::table('fa_szxc_village')->where('street_id',$code)->where('village_id','>',0)->where('status',1)->count();
$num2 = Db::table('fa_szxc_information_useraddress')->where('street_id',$code)->count();
$num3 = Db::table('fa_szxc_information_usermsg')->where('is_hz',1)->where('street_id',$code)->count();
}else{
$num1 = 0;
$num2 =0;
$num3 =0;
}
$list['data'] = [
['num'=>$num1,'text'=>'村庄数','img'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/cunzhaung.png'],
['num'=>$num2,'text'=>'村民数','img'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/cunming.png'],
['num'=>$num3,'text'=>'户数','img'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/hushu.png'],
];
$this->apiSuccess('获取成功', $list);
} else {
$num1 = Db::table('fa_szxc_village')->where('street_id',$code)->where('village_id','>',0)->where('status',1)->count();
$num2 = Db::table('fa_szxc_information_useraddress')->where('street_id',$code)->count();
$num3 = Db::table('fa_szxc_information_usermsg')->where('is_hz',1)->where('street_id',$code)->count();
$list['data'] = [
['num'=>$num1,'text'=>'村庄数','img'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/cunzhaung.png'],
['num'=>$num2,'text'=>'村民数','img'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/cunming.png'],
['num'=>$num3,'text'=>'户数','img'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/hushu.png'],
];
$this->apiSuccess('获取成功', $list);
}
}
// 搜索村庄
public function get_village($keyword=''){
if (empty($keyword)) {
$this->apiError('缺少参数');
}
$where[] = ['address', 'like', '%'.$keyword.'%'];
$list = Db::table('fa_szxc_village')
->where($where)
->where('village_id','>',0)
->field('id,title,address,people_num,images,views,likes,createtime')
->order('likes desc')
->withAttr('images', function ($value, $data) {
if ($data['images'] != '') {
return explode(',',$data['images']);
}
})
->withAttr('is_dz', function ($value, $data) {
// 判断redis
$user_id = $this->request->uid;
if($user_id){
$name = $user_id . $data['id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
return 1;
}else{
return 0;
}
}else{
return 0;
}
})
->select();
if ($list) {
$this->apiSuccess('获取成功', $list);
} else {
$this->apiError('暂无数据','暂无数据');
}
}
// 我的村庄
public function get_my_village(){
$user_id = $this->request->uid;
if (empty($user_id)) {
$this->apiError('请先登录');
}
$where[] = ['user_id', '=', $user_id];
// 获取用户地区
$address = Db::table('fa_szxc_information_useraddress')->where($where)->find();
// 组装地址名
if ($address['area_id'] != '') {
$area_name = Db::table('fa_geo_area')->where('area_code', $address['area_id'])->value('area_name');
}
if ($address['street_id'] != '') {
$street_name = Db::table('fa_geo_street')->where('street_code', $address['street_id'])->value('street_name');
}
if ($address['village_id'] != '') {
$village = Db::table('fa_geo_village')->where('village_code', $address['village_id'])->value('village_name');
}
if ($address['brigade_id'] != '') {
$brigade_name = Db::table('fa_geo_brigade')->where('id', $address['brigade_id'])->value('brigade_name');
}
$address_name = '泸州市'.$area_name . $street_name . $village . $brigade_name;
$www['area_id'] = $address['area_id'];
$www['street_id'] = $address['street_id'];
$www['village_id'] = $address['village_id'];
$news = Db::table('fa_szxc_village')->where($www)->field('id,title,address,people_num,images,views,likes,createtime,introduce,info,area_id,street_id,village_id')->find();
if ($news) {
$news['area_name'] = $area_name;
$news['street_name'] = $street_name;
$news['village'] = $village;
$news['brigade_name'] = $brigade_name;
// 判断redis
$user_id = $this->request->uid;
if($user_id){
$name = $user_id . $news['id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
$news['is_dz'] = 1;
}else{
$news['is_dz'] = 0;
}
}else{
$news['is_dz'] = 0;
}
// 获取天气
// 101271007 纳溪 510503
// 101271002 江阳区 510502
// 101271008 龙马潭区 510504
// 101271003 泸县 510521
// 101271004 合江 510522
// 101271006 古蔺 510525
// 101271005 叙永 510524
// 判断区域
if($news['area_id'] == '510503'){
$name = '101271007';
}
if($news['area_id'] == '510502'){
$name = '101271002';
}
if($news['area_id'] == '510504'){
$name = '101271008';
}
if($news['area_id'] == '510521'){
$name = '101271003';
}
if($news['area_id'] == '510522'){
$name = '101271004';
}
if($news['area_id'] == '510525'){
$name = '101271006';
}
if($news['area_id'] == '510524'){
$name = '101271005';
}
$url = "https://devapi.qweather.com/v7/weather/now?key=b3e94fa75aae4551b6a5db150b409261&location=".$name;
$url1 = "https://devapi.qweather.com/v7/indices/1d?key=b3e94fa75aae4551b6a5db150b409261&type=0&location=".$name;
$is_cun = Cache::store('redis')->get($name);
$is_cun1 = Cache::store('redis')->get($name.'-1');
if($is_cun){
$data = json_decode($is_cun,1);
$data1 = json_decode($is_cun1,1);
$news['weather'] = $data;
$news['weather_indices'] = $data1;
}else{
$data = file_get_contents("compress.zlib://".$url);
$data1 = file_get_contents("compress.zlib://".$url1);
Cache::store('redis')->set($name,$data,1800);
Cache::store('redis')->set($name.'-1',$data1,1800);
$data = json_decode($data,1);
$data1 = json_decode($data1,1);
$news['weather'] = $data;
$news['weather_indices'] = $data1;
}
// halt($data);die;
$news['people_num'] = Db::table('fa_szxc_information_useraddress')->where('village_id',$news['village_id'])->where('status',1)->count();
// 增加阅读数
Db::table('fa_szxc_village')->where('id',$news['id'])->inc('views', '1')->update();
if ($news['images']) {
$news['images'] = explode(',',$news['images']);
}
$this->apiSuccess('获取成功', $news);
} else {
// 判断redis
if($this->request->uid){
$name = $this->request->uid . $address['village_id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
$news['is_dz'] = 1;
}else{
$news['is_dz'] = 0;
}
}else{
$news['is_dz'] = 0;
}
$news['id'] = $address['village_id'];
$news['title'] = $address_name;
$news['address'] = $address_name;
$news['images'] = ["https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230115/4dc84e69408fef859e8553a5c7091197.jpg"];
$news['info'] = $address_name;
$news['area_id'] = $address['area_id'];
$news['street_id'] = $address['street_id'];
$news['village_id'] = $address['village_id'];
// 获取天气
// 101271007 纳溪 510503
// 101271002 江阳区 510502
// 101271008 龙马潭区 510504
// 101271003 泸县 510521
// 101271004 合江 510522
// 101271006 古蔺 510525
// 101271005 叙永 510524
// 判断区域
if($news['area_id'] == '510503'){
$name = '101271007';
}
if($news['area_id'] == '510502'){
$name = '101271002';
}
if($news['area_id'] == '510504'){
$name = '101271008';
}
if($news['area_id'] == '510521'){
$name = '101271003';
}
if($news['area_id'] == '510522'){
$name = '101271004';
}
if($news['area_id'] == '510525'){
$name = '101271006';
}
if($news['area_id'] == '510524'){
$name = '101271005';
}
$url = "https://devapi.qweather.com/v7/weather/now?key=b3e94fa75aae4551b6a5db150b409261&location=".$name;
$url1 = "https://devapi.qweather.com/v7/indices/1d?key=b3e94fa75aae4551b6a5db150b409261&type=0&location=".$name;
$is_cun = Cache::store('redis')->get($name);
$is_cun1 = Cache::store('redis')->get($name.'-1');
if($is_cun){
$data = json_decode($is_cun,1);
$data1 = json_decode($is_cun1,1);
$news['weather'] = $data;
$news['weather_indices'] = $data1;
}else{
$data = file_get_contents("compress.zlib://".$url);
$data1 = file_get_contents("compress.zlib://".$url1);
Cache::store('redis')->set($name,$data,1800);
Cache::store('redis')->set($name.'-1',$data1,1800);
$data = json_decode($data,1);
$data1 = json_decode($data1,1);
$news['weather'] = $data;
$news['weather_indices'] = $data1;
}
// halt($data);die;
$news['people_num'] = Db::table('fa_szxc_information_useraddress')->where('village_id',$news['village_id'])->where('status',1)->count();
$this->apiSuccess('获取成功', $news);
}
}
// 点赞村庄
public function thumbs_up($id=0){
$user_id = $this->request->uid;
if (empty($user_id)) {
$this->apiError('请先登录');
}
if(empty($id)){
$this->apiError('请选择村庄');
}
$name = $user_id . $id;
// 判断redis
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
$this->apiError('今天已经点赞');
}else{
Cache::store('redis')->set($name,$name,84600);
//增加点赞数
$where['id'] = $id;
Db::table('fa_szxc_village')->where($where)->inc('likes', '1')->update();
$this->apiSuccess('点赞成功');
}
}
// 我的镇
public function get_my_town(){
$user_id = $this->request->uid;
if (empty($user_id)) {
$this->apiError('请先登录');
}
$where[] = ['user_id', '=', $user_id];
// 获取用户地区
$address = Db::table('fa_szxc_information_useraddress')->where($where)->find();
// 组装地址名
if ($address['area_id'] != '') {
$area_name = Db::table('fa_geo_area')->where('area_code', $address['area_id'])->value('area_name');
}
if ($address['street_id'] != '') {
$street_name = Db::table('fa_geo_street')->where('street_code', $address['street_id'])->value('street_name');
}
// if ($address['village_id'] != '') {
// $village = Db::table('fa_geo_village')->where('village_code', $address['village_id'])->value('village_name');
// }
// if ($address['brigade_id'] != '') {
// $brigade_name = Db::table('fa_geo_brigade')->where('id', $address['brigade_id'])->value('brigade_name');
// }
$address_name = '泸州市'.$area_name . $street_name ;
$www['area_id'] = $address['area_id'];
$www['street_id'] = $address['street_id'];
$www['village_id'] = '0';
$news = Db::table('fa_szxc_village')->where($www)->field('id,title,address,people_num,images,views,likes,createtime,introduce,info,area_id,street_id,village_id')->find();
if ($news) {
$news['dz_num'] = Db::table('fa_szxc_village')->where('street_id',$address['street_id'])->sum('likes');
// 获取天气
// 101271007 纳溪 510503
// 101271002 江阳区 510502
// 101271008 龙马潭区 510504
// 101271003 泸县 510521
// 101271004 合江 510522
// 101271006 古蔺 510525
// 101271005 叙永 510524
// 判断区域
if($news['area_id'] == '510503'){
$name = '101271007';
}
if($news['area_id'] == '510502'){
$name = '101271002';
}
if($news['area_id'] == '510504'){
$name = '101271008';
}
if($news['area_id'] == '510521'){
$name = '101271003';
}
if($news['area_id'] == '510522'){
$name = '101271004';
}
if($news['area_id'] == '510525'){
$name = '101271006';
}
if($news['area_id'] == '510524'){
$name = '101271005';
}
$url = "https://devapi.qweather.com/v7/weather/now?key=b3e94fa75aae4551b6a5db150b409261&location=".$name;
$is_cun = Cache::store('redis')->get($name);
if($is_cun){
$data = json_decode($is_cun,1);
$news['weather'] = $data;
}else{
$data = file_get_contents("compress.zlib://".$url);
Cache::store('redis')->set($name,$data,1800);
$data = json_decode($data,1);
$news['weather'] = $data;
}
// halt($data);die;
$news['people_num'] = Db::table('fa_szxc_information_useraddress')->where('village_id',$news['village_id'])->where('status',1)->count();
// 增加阅读数
Db::table('fa_szxc_village')->where('id',$news['id'])->inc('views', '1')->update();
if ($news['images']) {
$news['images'] = explode(',',$news['images']);
}
$this->apiSuccess('获取成功', $news);
}else{
$news['dz_num'] = Db::table('fa_szxc_village')->where('street_id',$address['street_id'])->sum('likes');
$news['id'] = $address['street_id'];
$news['title'] = $address_name;
$news['address'] = $address_name;
$news['images'] = ["https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230115/4dc84e69408fef859e8553a5c7091197.jpg"];
$news['info'] = $address_name;
$news['area_id'] = $address['area_id'];
$news['street_id'] = $address['street_id'];
$news['village_id'] = $address['village_id'];
// 获取天气
// 101271007 纳溪 510503
// 101271002 江阳区 510502
// 101271008 龙马潭区 510504
// 101271003 泸县 510521
// 101271004 合江 510522
// 101271006 古蔺 510525
// 101271005 叙永 510524
// 判断区域
if($news['area_id'] == '510503'){
$name = '101271007';
}
if($news['area_id'] == '510502'){
$name = '101271002';
}
if($news['area_id'] == '510504'){
$name = '101271008';
}
if($news['area_id'] == '510521'){
$name = '101271003';
}
if($news['area_id'] == '510522'){
$name = '101271004';
}
if($news['area_id'] == '510525'){
$name = '101271006';
}
if($news['area_id'] == '510524'){
$name = '101271005';
}
$url = "https://devapi.qweather.com/v7/weather/now?key=b3e94fa75aae4551b6a5db150b409261&location=".$name;
$is_cun = Cache::store('redis')->get($name);
if($is_cun){
$data = json_decode($is_cun,1);
$news['weather'] = $data;
}else{
$data = file_get_contents("compress.zlib://".$url);
Cache::store('redis')->set($name,$data,1800);
$data = json_decode($data,1);
$news['weather'] = $data;
}
// halt($data);die;
$news['people_num'] = Db::table('fa_szxc_information_useraddress')->where('village_id',$news['village_id'])->where('status',1)->count();
}
$this->apiSuccess('获取成功', $news);
}
/**
* 获取镇排名
*
*/
public function get_ranking_town($page = 1,$limit=10)
{
$www[] = ['status','=',1];
$www[] = ['village_id','=',0];
$arr = Db::table('fa_szxc_village')->where($www)->select()->toArray();
foreach ($arr as $k=>$v){
$www2['status'] = 1;
$www2['street_id'] = $v['street_id'];
$www2['area_id'] = $v['area_id'];
$sum = Db::table('fa_szxc_village')->where($www2)->where('village_id','>',0)->sum('likes');
if($v['likes'] < $sum){
$ddd['likes'] = $sum;
$mmm['id'] = $v['id'];
Db::table('fa_szxc_village')->where($mmm)->update($ddd);
}
}
$where['status'] = 1;
$list = Db::table('fa_szxc_village')
->where($where)
->where('village_id','=',0)
->page($page,$limit)
->field('id,title,address,people_num,images,views,likes,createtime,area_id,street_id,village_id')
->order('likes desc')
->withAttr('images', function ($value, $data) {
if ($data['images'] != '') {
return explode(',',$data['images']);
}else{
return '';
}
})
->withAttr('is_dz', function ($value, $data) {
// 判断redis
$user_id = $this->request->uid;
if($user_id){
$name = $user_id . $data['id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
return 1;
}else{
return 0;
}
}else{
return 0;
}
})
->select();
if($list){
$this->apiSuccess('获取成功',$list);
}else{
$this->apiError('暂无数据',[]);
}
}
/**
* 获取镇
*
*/
public function get_town_list($area_id=0,$street_id=0,$type=1, $page = 1)
{
if ($area_id){
$where['area_id'] = $area_id;
}
// if ($street_id){
// $where['street_id'] = $street_id;
// }
if($type==1){
$where['is_hot'] = 1;
}
if($type==2){
$where['is_tourism'] = 1;
}
$where['status'] = 1;
$list = Db::table('fa_szxc_village')
->where($where)
->where('village_id','=',0)
->page($page,10)
->field('id,title,address,people_num,images,views,likes,createtime,area_id,street_id,village_id')
->order('likes desc')
->withAttr('images', function ($value, $data) {
if ($data['images'] != '') {
return explode(',',$data['images']);
}
})
->withAttr('is_dz', function ($value, $data) {
// 判断redis
$user_id = $this->request->uid;
if($user_id){
$name = $user_id . $data['id'];
$is_zan = Cache::store('redis')->get($name);
if($is_zan){
return 1;
}else{
return 0;
}
}else{
return 0;
}
})
->select();
if($list){
$this->apiSuccess('获取成功',$list);
}else{
$this->apiError('暂无数据',[]);
}
}
}