2023-01-19 02:36:01 +00:00

403 lines
15 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_detail','get_json','get_village','thumbs_up'] ]
];
/**
* 获取村庄
*
* //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)
->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 = JWT_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 = JWT_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;
$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($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_information_useraddress')->group('village_id')->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_information_useraddress')->where('area_id',$code)->group('village_id')->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_information_useraddress')->where('street_id',$code)->group('village_id')->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'=>'村庄数'],
['num'=>$num2,'text'=>'村民数'],
['num'=>$num3,'text'=>'户数'],
];
$this->apiSuccess('获取成功', $list);
} else {
$this->apiError('获取失败');
}
}
// 搜索村庄
public function get_village($keyword=''){
if (empty($keyword)) {
$this->apiError('缺少参数');
}
$where[] = ['address', 'like', '%'.$keyword.'%'];
$list = Db::table('fa_szxc_village')
->where($where)
->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 = JWT_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 = JWT_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) {
// 判断redis
$user_id = JWT_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;
$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['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['is_dz'] = 0;
$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 thumbs_up($id=0){
$user_id = JWT_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('点赞成功');
}
}
}