From 51231c9b8dbdfabbcd4070e06c510a27004c33bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E6=A1=83?= <1098598843@qq.com> Date: Wed, 18 Jan 2023 17:10:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/BaseController.php | 64 +- app/api/controller/Article.php | 800 ++++++++ app/api/controller/ArticleComment.php | 219 ++ app/api/controller/Ceshi.php | 309 +++ app/api/controller/Common.php | 352 ++++ app/api/controller/Ems.php | 106 + app/api/controller/Geo.php | 96 + app/api/controller/Index.php | 58 +- app/api/controller/Maintainentry.php | 1794 +++++++++++++++++ app/api/controller/MyArticle.php | 181 ++ app/api/controller/Pay.php | 50 + app/api/controller/PayNotify.php | 58 + app/api/controller/Personal.php | 518 +++++ app/api/controller/Slide.php | 55 + app/api/controller/Sms.php | 110 + app/api/controller/Statistics.php | 471 +++++ app/api/controller/Token.php | 40 + app/api/controller/User.php | 425 ++++ app/api/controller/Userinfo.php | 351 ++++ app/api/controller/Validate.php | 138 ++ app/api/controller/Village.php | 402 ++++ app/api/controller/party/Article.php | 126 ++ app/api/controller/party/Branch.php | 206 ++ app/api/controller/party/BranchPayList.php | 116 ++ app/api/controller/party/BranchPayUser.php | 127 ++ app/api/controller/party/Info.php | 104 + app/api/controller/party/Vote.php | 155 ++ app/api/controller/party/VoteComment.php | 126 ++ app/api/middleware/Auth.php | 19 +- app/api/route/party.php | 111 + app/api/validate/Maintainentry.php | 95 + app/common.php | 2 +- app/common/controller/Api.php | 345 ++++ app/common/controller/Backend.php | 567 ++++++ app/common/controller/BaseController.php | 122 ++ app/common/controller/EasyWeChat.php | 1 + app/common/controller/Frontend.php | 184 ++ app/common/controller/WxTui.php | 104 + app/common/lang/zh-cn/addon.php | 97 + app/common/library/Auth.php | 672 ++++++ app/common/library/Email.php | 215 ++ app/common/library/Ems.php | 160 ++ app/common/library/Menu.php | 238 +++ app/common/library/Sms.php | 159 ++ app/common/library/Token.php | 168 ++ app/common/library/token/Driver.php | 106 + app/common/library/token/driver/Mysql.php | 135 ++ app/common/library/token/driver/Redis.php | 184 ++ app/common/library/traits/Jump.php | 152 ++ app/common/middleware/Addon.php | 47 + .../middleware/AllowOriginMiddleware.php | 70 + app/common/middleware/FastInit.php | 105 + app/common/model/Area.php | 94 + app/common/model/Attachment.php | 91 + app/common/model/BaseModel.php | 90 + app/common/model/BreedInformation.php | 20 + app/common/model/Brigade.php | 20 + app/common/model/Category.php | 89 + app/common/model/Config.php | 236 +++ app/common/model/EliminateEvil.php | 20 + app/common/model/EliminateEvilReply.php | 20 + app/common/model/Ems.php | 18 + app/common/model/FamilyRelations.php | 20 + app/common/model/Feedback.php | 20 + app/common/model/FeedbackReply.php | 20 + app/common/model/Help.php | 20 + app/common/model/HelpReply.php | 20 + app/common/model/Heresy.php | 20 + app/common/model/HeresyReply.php | 20 + app/common/model/InformationHealthy.php | 20 + app/common/model/InformationInsurance.php | 20 + app/common/model/InformationPlanting.php | 20 + app/common/model/InformationUsermsg.php | 20 + app/common/model/Mediation.php | 20 + app/common/model/MediationReply.php | 20 + app/common/model/MoneyLog.php | 20 + app/common/model/Notice.php | 20 + app/common/model/PersonalComment.php | 20 + app/common/model/PersonalNews.php | 20 + app/common/model/ScoreLog.php | 20 + app/common/model/SlideInfo.php | 20 + app/common/model/Sms.php | 18 + app/common/model/SzscCategory.php | 20 + app/common/model/User.php | 174 ++ app/common/model/UserAddress.php | 20 + app/common/model/UserGroup.php | 17 + app/common/model/UserRule.php | 17 + app/common/model/Version.php | 48 + app/common/service/AddonService.php | 122 ++ app/common/service/AuthService.php | 27 + app/common/view/tpl/dispatch_jump.tpl | 66 + app/common/view/tpl/think_exception.tpl | 503 +++++ app/middleware.php | 2 + config/cache.php | 5 + 94 files changed, 13710 insertions(+), 22 deletions(-) create mode 100644 app/api/controller/Article.php create mode 100644 app/api/controller/ArticleComment.php create mode 100644 app/api/controller/Ceshi.php create mode 100644 app/api/controller/Common.php create mode 100644 app/api/controller/Ems.php create mode 100644 app/api/controller/Geo.php create mode 100644 app/api/controller/Maintainentry.php create mode 100644 app/api/controller/MyArticle.php create mode 100644 app/api/controller/Pay.php create mode 100644 app/api/controller/PayNotify.php create mode 100644 app/api/controller/Personal.php create mode 100644 app/api/controller/Slide.php create mode 100644 app/api/controller/Sms.php create mode 100644 app/api/controller/Statistics.php create mode 100644 app/api/controller/Token.php create mode 100644 app/api/controller/User.php create mode 100644 app/api/controller/Userinfo.php create mode 100644 app/api/controller/Validate.php create mode 100644 app/api/controller/Village.php create mode 100644 app/api/controller/party/Article.php create mode 100644 app/api/controller/party/Branch.php create mode 100644 app/api/controller/party/BranchPayList.php create mode 100644 app/api/controller/party/BranchPayUser.php create mode 100644 app/api/controller/party/Info.php create mode 100644 app/api/controller/party/Vote.php create mode 100644 app/api/controller/party/VoteComment.php create mode 100644 app/api/route/party.php create mode 100644 app/api/validate/Maintainentry.php create mode 100644 app/common/controller/Api.php create mode 100644 app/common/controller/Backend.php create mode 100644 app/common/controller/BaseController.php create mode 100644 app/common/controller/EasyWeChat.php create mode 100644 app/common/controller/Frontend.php create mode 100644 app/common/controller/WxTui.php create mode 100644 app/common/lang/zh-cn/addon.php create mode 100644 app/common/library/Auth.php create mode 100644 app/common/library/Email.php create mode 100644 app/common/library/Ems.php create mode 100644 app/common/library/Menu.php create mode 100644 app/common/library/Sms.php create mode 100644 app/common/library/Token.php create mode 100644 app/common/library/token/Driver.php create mode 100644 app/common/library/token/driver/Mysql.php create mode 100644 app/common/library/token/driver/Redis.php create mode 100644 app/common/library/traits/Jump.php create mode 100644 app/common/middleware/Addon.php create mode 100644 app/common/middleware/AllowOriginMiddleware.php create mode 100644 app/common/middleware/FastInit.php create mode 100644 app/common/model/Area.php create mode 100644 app/common/model/Attachment.php create mode 100644 app/common/model/BaseModel.php create mode 100644 app/common/model/BreedInformation.php create mode 100644 app/common/model/Brigade.php create mode 100644 app/common/model/Category.php create mode 100644 app/common/model/Config.php create mode 100644 app/common/model/EliminateEvil.php create mode 100644 app/common/model/EliminateEvilReply.php create mode 100644 app/common/model/Ems.php create mode 100644 app/common/model/FamilyRelations.php create mode 100644 app/common/model/Feedback.php create mode 100644 app/common/model/FeedbackReply.php create mode 100644 app/common/model/Help.php create mode 100644 app/common/model/HelpReply.php create mode 100644 app/common/model/Heresy.php create mode 100644 app/common/model/HeresyReply.php create mode 100644 app/common/model/InformationHealthy.php create mode 100644 app/common/model/InformationInsurance.php create mode 100644 app/common/model/InformationPlanting.php create mode 100644 app/common/model/InformationUsermsg.php create mode 100644 app/common/model/Mediation.php create mode 100644 app/common/model/MediationReply.php create mode 100644 app/common/model/MoneyLog.php create mode 100644 app/common/model/Notice.php create mode 100644 app/common/model/PersonalComment.php create mode 100644 app/common/model/PersonalNews.php create mode 100644 app/common/model/ScoreLog.php create mode 100644 app/common/model/SlideInfo.php create mode 100644 app/common/model/Sms.php create mode 100644 app/common/model/SzscCategory.php create mode 100644 app/common/model/User.php create mode 100644 app/common/model/UserAddress.php create mode 100644 app/common/model/UserGroup.php create mode 100644 app/common/model/UserRule.php create mode 100644 app/common/model/Version.php create mode 100644 app/common/service/AddonService.php create mode 100644 app/common/service/AuthService.php create mode 100644 app/common/view/tpl/dispatch_jump.tpl create mode 100644 app/common/view/tpl/think_exception.tpl diff --git a/app/api/BaseController.php b/app/api/BaseController.php index f67061b..640f924 100644 --- a/app/api/BaseController.php +++ b/app/api/BaseController.php @@ -9,8 +9,11 @@ declare (strict_types = 1); namespace app\api; +use Firebase\JWT\JWT; +use Firebase\JWT\Key; use think\App; use think\exception\HttpResponseException; +use think\facade\Db; use think\facade\Request; use think\Response; @@ -54,10 +57,10 @@ abstract class BaseController * @var string */ protected $jwt_conf = [ - 'secrect' => 'gougucms', - 'iss' => 'www.gougucms.com', //签发者 可选 - 'aud' => 'gougucms', //接收该JWT的一方,可选 - 'exptime' => 7200, //过期时间,这里设置2个小时 + 'secrect' => 'ae47e94a7dcd1fdfacb499b60e361a8d', + 'iss' => '', //签发者 可选 + 'aud' => '', //接收该JWT的一方,可选 + 'exptime' => '', //过期时间,这里设置2个小时 ]; /** * 构造方法 @@ -76,8 +79,57 @@ abstract class BaseController // 初始化 protected function initialize() { + + //每页显示数据量 $this->pageSize = Request::param('page_size', \think\facade\Config::get('app.page_size')); + $token = Request::header('x-Token'); + if ($token) { + if (strpos($token, 'Bearer') === 0){ + $token = trim(substr($token, 6)); + } + if (count(explode('.', $token)) != 3) { + return json(['code'=>404,'msg'=>'非法请求']); + } + $config = get_system_config('token'); + + //var_dump($config);exit; + try { + JWT::$leeway = 60;//当前时间减去60,把时间留点余地 + $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 + + //return (array)$decoded; +// $decoded_array = json_decode(json_encode($decoded),TRUE); +// $jwt_data = $decoded_array['data']; + + $user=Db::connect('shop')->name('nk_user')->where('user_id',$decoded->jti[0])->find(); + if (!$user){ + return json(['code'=>403,'msg'=>'签名错误']); + return false; + } + $user = Db::table('fa_user')->where('id',$user['n_user_id'])->find(); + + //$request->uid = $jwt_data['userid']; + define('JWT_UID', $user['id']); +// $response = $next($request); +// return $response; + //return $next($request); + } catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确 + return json(['code'=>403,'msg'=>'签名错误']); + }catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用 + return json(['code'=>401,'msg'=>'token失效']); + }catch(\Firebase\JWT\ExpiredException $e) { // token过期 + return json(['code'=>401,'msg'=>'token已过期']); + }catch(Exception $e) { //其他错误 + return json(['code'=>404,'msg'=>'非法请求']); + }catch(\UnexpectedValueException $e) { //其他错误 + return json(['code'=>404,'msg'=>'非法请求']); + } catch(\DomainException $e) { //其他错误 + return json(['code'=>404,'msg'=>'非法请求']); + } + }else{ + define('JWT_UID', ''); + } } /** @@ -90,7 +142,7 @@ abstract class BaseController */ protected function apiSuccess($msg = 'success',$data=[]) { - return $this->apiReturn($data, 0, $msg); + return $this->apiReturn($data, 1, $msg); } /** @@ -102,7 +154,7 @@ abstract class BaseController * @return mixed * @throws ReturnException */ - protected function apiError($msg = 'fail',$data=[], $code = 1) + protected function apiError($msg = 'fail',$data=[], $code = 0) { return $this->apiReturn($data, $code, $msg); } diff --git a/app/api/controller/Article.php b/app/api/controller/Article.php new file mode 100644 index 0000000..ee38eae --- /dev/null +++ b/app/api/controller/Article.php @@ -0,0 +1,800 @@ + ['except' => ['index','hot_list','getArticleList','details','indexs'] ] + ]; + + public function index($search = '', $category_id = 1, $page = 1, $is_time = 0, $category_type = 0) + { + $param = get_params(); + + $screen = $param['screen']??1; + $solve = $param['solve']??1; + $order = $param['order']??1; + $model = Db::table('fa_article'); + $where = [ + ['status', '=', 1], + ['category_id', '=', $category_id] + ]; + //查询升降序 + if($order==1){ + $orders='desc'; + }else{ + $orders='asc'; + } + if (!$category_id) { + unset($where[1]); + } + //根据个人村id进行查询 + if (JWT_UID) { + $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find(); + if ($find) { + if ($find['auth_range']==1){ + $where[] = ['village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $where[] = ['township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $where[] = ['county', '=', $find['area_id']]; + } + } + } + //总条数 + $count = $model->where($where)->count(); + //当月条数 + $month_count = $model->where($where)->whereMonth('view_time')->count(); + if ($search != '') { + $where[] = ['title', 'like', '%' . $search . '%']; + } + if ($category_type != 0) { + $where[] = ['category_type', '=', $category_type]; + } + //判断筛选查询 + if ($screen==2){ + foreach ($where as $key =>$value){ + $where[$key][0] = 'a.'.$value[0]; + } + //查询 + $select = Db::table('fa_article')->where($where)->page($page)->limit(20) + ->alias('a') + ->join('article_vote_side_tables avst',"a.id = avst.article_id and avst.end_time >=".date('Y-m-d')) + ->field('a.id,a.title,a.describe,a.user_id,a.view,a.view_time,a.image,a.end_time,a.is_solve,a.is_vote,a.is_nickname,a.video,a.category_type,avst.end_time as die_time') + ->order('a.id DESC') + ->select()->toArray(); + }else if ($screen==3){ + foreach ($where as $key =>$value){ + $where[$key][0] = 'a.'.$value[0]; + } + //查询 + $select = Db::table('fa_article')->where($where)->page($page)->limit(20) + ->alias('a') + ->join('article_vote_side_tables avst',"a.id = avst.article_id and avst.end_time <=".date('Y-m-d')) + ->field('a.id,a.title,a.describe,a.user_id,a.view,a.view_time,a.image,a.end_time,a.is_solve,a.is_vote,a.is_nickname,a.video,a.category_type,avst.end_time as die_time') + ->order('a.id DESC')->select()->toArray(); + }else{ + //查询是否解决 + if($solve==2){ + $where[] = ['is_solve', '=', 0]; + }else if($solve==3){ + $where[] = ['is_solve', '=', 1]; + } + $select = $model->with('user')->where($where)->page($page)->limit(20) + ->field('id,title,user_id,view,view_time,image,end_time,is_solve,is_vote,is_nickname,video,category_type,describe') + ->order('id',$orders)->select()->toArray(); + } + + foreach ($select as $key => $value) { + if ($value['is_nickname'] == 1) { + $select[$key]['nickname'] = "匿名人员"; + $select[$key]['avatar'] = ""; + $select[$key]['user_id'] = 0; + } else { + $select[$key]['nickname'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->value('name'); + } + //投票处理 + if ($value['is_vote'] == 1) { + $article_vote = Db::table('fa_article_vote_side_tables')->where('article_id', $value['id'])->find(); + if ($article_vote) { + $data = $article_vote; + $data['agree_percentage'] = 0; + $data['opposition_percentage'] = 0; + $count_vote = $data['agree'] + $data['opposition'] + $data['other']; + if ($count_vote != 0) { + if ($data['agree'] != 0) { + $data['agree_percentage'] = round(($data['agree'] / $count_vote) * 100); + } + if ($data['opposition'] != 0) { + $data['opposition_percentage'] = round(($data['opposition'] / $count_vote) * 100); + } + } + $select[$key]['extend']['vote'] = $data; + } + } else { + $select[$key]['extend'] = []; + } + if ($is_time == 1) { + if ($value['end_time'] < date('Y-m-d H:i:s')) { + if ($value['end_time'] == '0000-00-00 00:00:00') { + $select[$key]['overdue_time'] = '显示错误'; + } else { + if($value['is_solve']==1){ + $select[$key]['overdue_time'] = 0; + }else{ + $cle = time() - strtotime($value['end_time']); + $select[$key]['overdue_time'] = ceil($cle / 3600 / 24); + } + } + } else { + $select[$key]['overdue_time'] = 0; + } + + } + if ($category_id == 165) { + if ($value['category_type'] != 0) { + $find = Db::table('fa_category')->where('id', $value['category_type'])->find(); + $select[$key]['extend']['category_type_name'] = $find ? $find['name'] . ':' : ''; + } else { + $select[$key]['extend']['category_type_name'] = ''; + } + } + + + } + $this->apiSuccess('ok', ['list' => $select, 'count' => ['count' => $count, 'month_count' => $month_count]]); + } + + /**首页推荐 + * @param $category_id + * @param $page + */ + public function hot_list($category_id = 0, $page = 1, $county = 0, $township = 0, $village = 0) + { + $where = [ + ['status', '=', 1], + ]; + if (!$category_id) { + unset($where[1]); + } +// 如果登录 + if (JWT_UID) { + $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find(); + if ($find) { + if ($find['auth_range']==1){ + $where[] = ['village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $where[] = ['township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $where[] = ['county', '=', $find['area_id']]; + } + } + } +// if ($county != 0) { +// $where[] = ['county', '=', $county]; +// } +// if ($township != 0) { +// $where[] = ['township', '=', $township]; +// } +// if ($village != 0) { +// $where[] = ['village', '=', $village]; +// } + if ($category_id == 0) { + $where[] = ['category_id', 'in', [176, 162, 161, 160, 152]]; + $sos = Db::table('fa_article')->where('category_id', 150)->where('status', 1) + ->whereTime('view_time','between', [date("H:i:s",strtotime("-5 minute")), date("H:i:s",strtotime("+5 minute"))]) + ->page($page)->limit(3) + ->withAttr('user_info', function ($value, $data) { + $user = Db::table('fa_user')->where('id', $data['user_id'])->field('nickname,avatar')->find(); + $user['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $data['user_id'])->value('name'); + return $user; + }) + ->withAttr('article_comment', function ($value, $data) { + return Db::table('fa_article_comment')->where([['vote_id','=',$data['id']],['status','=',1]])->count(); + }) + ->withAttr('article_type', function ($value, $data) { + return '一键求救'; + }) + ->field('id,title,user_id,view,view_time,image,end_time,is_solve,is_vote,is_nickname,video,category_id,source')->order('id DESC') + ->withAttr('image', function ($value, $data) { + if ($data['image'] != '') { + return explode(',', $data['image']); + } + }) + ->select()->toArray(); + } else { + $where[] = ['category_id', '=', $category_id]; + $sos = []; + } + //查询 + $mapo[] = [['category_id','=','1'],['status','=','1']]; + $select = Db::table('fa_article')->where($where)->whereOr($mapo)->page($page)->limit(20) + ->withAttr('user_info', function ($value, $data) { + $user = Db::table('fa_user')->where('id', $data['user_id'])->field('nickname,avatar')->find(); + $user['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $data['user_id'])->value('name'); + return $user; + }) + ->field('id,title,describe,user_id,view,view_time,image,end_time,is_solve,is_vote,is_nickname,video,category_id')->order('id DESC') + ->withAttr('image', function ($value, $data) { + if ($data['image'] != '') { + return explode(',', $data['image']); + } + }) + ->withAttr('article_comment', function ($value, $data) { + return Db::table('fa_article_comment')->where([['vote_id','=',$data['id']],['status','=',1]])->count(); + }) + ->withAttr('article_type', function ($value, $data) { + if($data['category_id'] == 1){ + return '实时信息'; + }elseif($data['category_id'] == 176){ + return '党务公开'; + }elseif($data['category_id'] == 162){ + return '议事大厅'; + }elseif($data['category_id'] == 161){ + return '财务公开'; + }elseif($data['category_id'] == 160){ + return '村务公开'; + }elseif($data['category_id'] == 152){ + return '好人好事'; + }else{ + return ''; + } + }) + ->select()->toArray(); + $personal=Db::table('fa_szxc_personal_news') + ->withAttr('user_info', function ($value, $data) { + $user = Db::table('fa_user')->where('id', $data['user_id'])->field('nickname,avatar')->find(); + $user['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $data['user_id'])->value('name'); + return $user; + }) + ->withAttr('image', function ($value, $data) { + if ($data['image'] != '') { + return json_decode($data['image'],true); + } + }) + ->withAttr('article_comment', function ($value, $data) { + return Db::table('fa_szxc_personal_news_comment')->where([['personal_news_id','=',$data['id']],['status','=',1]])->count(); + }) + ->withAttr('article_type', function ($value, $data) { + return Db::table('fa_category')->where('id', $data['category_id'])->value('name'); + }) + ->withAttr('view_time', function ($value, $data) { + return date('Y-m-d H:i:s', $value); + }) + ->field('id,id as personal_news_id,content as title,user_id,view,createtime as view_time,images as image,video,category_id') + ->limit(5)->order('id desc')->page($page)->select()->toArray(); + $i=0; + $personal_count=count($personal); + if ($select && $personal){ + foreach($select as $key=>$value){ + if ($key!=0 &&$key%2==0 &&$i<$personal_count){ + array_splice($select,$key+$i,0,[$personal[$i]]); + ++$i; + } + } + } + $this->apiSuccess('ok', ['list' => array_merge($sos, $select)]); + } + + /**首页分类列表 + * @param $category_id + * @param $page + */ + public function getArticleList($category_id = 0, $page = 1, $type = 1) + { + $model = Db::table('fa_article'); + $where = [ + ['status', '=', 1], + ]; + //根据个人村id进行查询 + if (JWT_UID) { + $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find(); + if ($find) { + if ($find['auth_range']==1){ + $where[] = ['village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $where[] = ['township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $where[] = ['county', '=', $find['area_id']]; + } + } + } + if ($type == 1) { + $where[] = ['category_id', 'in', [162, 161, 160, 152]]; + + } else { + $where[] = ['category_id', '=', $category_id]; + } + //查询 + $select = Db::table('fa_article')->where($where)->page($page)->limit(20) + ->field('id,title,user_id,view,view_time,image,end_time,is_solve,is_vote,is_nickname,video,category_id')->order('id DESC') + ->withAttr('image', function ($value, $data) { + if ($data['image'] != '') { + return explode(',', $data['image']); + } + }) + ->withAttr('user_info', function ($value, $data) { + $user = Db::table('fa_user')->where('id', $data['user_id'])->field('nickname,avatar')->find(); + $user['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $data['user_id'])->value('name'); + return $user; + }) + ->withAttr('article_comment', function ($value, $data) { + return Db::table('fa_article_comment')->where([['vote_id','=',$data['id']],['status','=',1]])->count(); + }) + ->withAttr('article_type', function ($value, $data) { + if($data['category_id'] == 176){ + return '党务公开'; + }elseif($data['category_id'] == 162){ + return '议事大厅'; + }elseif($data['category_id'] == 161){ + return '财务公开'; + }elseif($data['category_id'] == 160){ + return '村务公开'; + }elseif($data['category_id'] == 152){ + return '好人好事'; + }else{ + return ''; + } + }) + ->select()->toArray(); + return $this->apiSuccess('ok', ['list' => $select]); + } + + /**详情 + * @param $id + * @return null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function details($id) + { +// 增加阅读数 +// $ip = 'article-details-'.$this->request->ip().'-'.$id; +// $ip_cache = Cache::get($ip); +// if(empty($ip_cache)){ +// Cache::set($ip,$id,3600*24); + $map[] =['id','=', $id]; + Db::table('fa_article')->where($map)->inc('view','1')->update(); +// } + + $where = [ + ['status', '=', 1], + ['id', '=', $id] + ]; + $find = Db::table('fa_article')->where($where) + ->withAttr('user_info', function ($data, $value) { + $find['nickname'] = "匿名人员"; + $find['mobile'] = "匿名状态无法显示"; + $find['gender'] = "匿名状态无法显示"; + $find['avatar'] = ""; + $find['user_id'] = 0; + $find['count'] = 0; + $find['end_count'] = 0; + $find['overdue_count'] = 0; + if ($value['is_nickname'] == 0) { + $user = Db::table('fa_user')->where('id', $value['user_id'])->field('nickname,avatar,mobile')->find(); + if ($user) { + $find = $user; + } + $find['nickname'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->value('name'); + } + $config_find = Db::table('fa_config')->where('id', 20)->find(); + //是否查询提案数 + if (in_array($value['category_id'], explode(',', $config_find['value']))) { + //提案总数 + $where = [ + ['category_id', '=', $value['category_id']], + ['user_id', '=', $value['user_id']] + ]; + //处理总数 + $whereTwo = [ + ['category_id', '=', $value['category_id']], + ['user_id', '=', $value['user_id']], + ['is_solve', '=', 1] + ]; + $find['count'] = Db::table('fa_article')->where($where)->count(); + $find['end_count'] = Db::table('fa_article')->where($whereTwo)->count(); + //逾期总数 + $where[] = ['is_solve', '=', 0]; + $find['overdue_count'] = Db::table('fa_article')->where($where)->whereTime('end_time', '<=', date('Y-m-d H:i:s')) + ->count(); + $usermsg = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->field('age,address_name,gender,name')->find(); + if ($value['is_nickname']==1){ + $find['age'] ='匿名状态无法显示'; + $find['address'] = '匿名状态无法显示'; + $find['gender'] = '匿名状态无法显示'; + }else{ + $find['age'] = $usermsg?$usermsg['age']:''; + $find['address'] = $usermsg?$usermsg['address_name']:''; + if ($usermsg){ + $find['gender'] =$usermsg['gender']==1?'男':'女'; + } + $find['nickname'] = $usermsg['name']; + } + $insurance = Db::table('fa_szxc_information_insurance')->where('user_id', $value['user_id'])->field('insurance_type')->find(); + if ($insurance) { + $find['insurance_type'] = $insurance['insurance_type']; + } else { + $find['insurance_type'] = ""; + } + } + return $find; + })->withAttr('category_type_title', function ($data, $value) { + if ($value['category_type'] != 0) { + $find = Db::table('fa_category')->where('id', $value['category_type'])->find(); + return $find['name']; + } + }) + ->find(); + //投票处理 + if ($find['is_vote'] == 1) { + $article_vote = Db::table('fa_article_vote_side_tables')->where('article_id', $find['id'])->find(); + if ($article_vote) { + $data = $article_vote; + $data['agree_percentage'] = 0; + $data['opposition_percentage'] = 0; + $count_vote = $data['agree'] + $data['opposition'] + $data['other']; + if ($count_vote != 0) { + if ($data['agree'] != 0) { + $data['agree_percentage'] = round(($data['agree'] / $count_vote) * 100); + } + if ($data['opposition'] != 0) { + $data['opposition_percentage'] = round(($data['opposition'] / $count_vote) * 100); + } + } + $find['extend']['vote'] = $data; + } + + } else { + $find['extend'] = []; + + } + return $this->apiSuccess('ok', $find); + } + + public function add() + { + + } + + /**提交内容 + * @return null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function post() + { + $input = get_params(); +// $valdate = new ArticleValdate(); +// $res = $valdate->check($input); +// if (!$res) { +// return $this->apiError($valdate->getError()); +// } + $useraddress = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->where('status', 1)->find(); + $data = []; + + if ($useraddress) { + $data['county'] = $useraddress['area_id']; + $data['township'] = $useraddress['street_id']; + $data['village'] = $useraddress['village_id']; + } + $data['end_time'] = date('Y-m-d H:i:s', strtotime('+1 day')); + $data['view_time'] = date('Y-m-d H:i:s'); + $data['user_id'] = JWT_UID;//添加用户的id + $data['category_id'] = $input['category_id'];//大分类id + $data['category_type'] = $input['category_type'];//小分类id + $data['title'] = $input['title'];//标题 + $data['is_nickname'] =empty($input['is_nickname'])?0:$input['is_nickname'];//是否匿名 + + $data['content'] = $input['content'];//内容 + $data['describe'] = empty($input['describe'])?'':$input['describe'];//简介 + $data['image'] = $input['image'];//封面 + $data['is_vote'] =empty($input['is_vote'])?0:$input['is_vote'];//是否投票 + if ($input['category_id']==150){ + $name=Db::table('fa_szxc_information_usermsg')->where('user_id',JWT_UID)->value('name'); + $data['title'] = '来自于'.$name.'的一键求救'; + } + $res = Db::table('fa_article')->insertGetId($data); + if ($input['is_vote'] == 1) { + $vote_data = [ + 'article_id' => $res, + 'start_time' => $input['start_time'], + 'end_time' => $input['end_time'], + ]; + Db::table('fa_article_vote_side_tables')->insert($vote_data); + } + if ($res) { + return $this->apiSuccess('添加成功'); + } else { + return $this->apiError('添加失败'); + + } + + + } + + /**获取要编辑的内容 + * @param $id + * @return null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function edit($id) + { + $model =Db::table('fa_article'); + $find = $model->where('id', $id)->find()->toArray(); + if ($find && $find['is_vote'] == 1) { + $data = Db::table('fa_article_vote_side_tables')->where('article_id', $find['id'])->find(); + $find['extend']['vote'] = $data; + } + return $this->apiSuccess('ok', $find); + + } + + /**提交要编辑的内容 + * @param $id + * @return null + */ + public function put($id) + { + $input = get_params(); + $data = []; + $data['end_time'] = date('Y-m-d H:i:s'); + $data['view_time'] = $input['view_time']; + $data['category_type'] = $input['category_type'];//小分类id + $data['title'] = $input['title'];//标题 + $data['content'] = $input['content'];//内容 + $data['describe'] = empty($input['describe'])?'':$input['describe'];//简介 + $data['image'] = $input['image'];//封面 + $data['is_vote'] = $input['is_vote'];//是否投票 + $input['update_user_id'] = JWT_UID;//更新用户的id + + $res = Db::table('fa_article')->where('id', $id)->update($data); + if ($input['is_vote'] == 1) { + $vote_data = [ + 'start_time' => $input['start_time'], + 'end_time' => $input['end_time'], + ]; + Db::table('fa_article_vote_side_tables')->where('article_id', $id)->update($vote_data); + } + if ($res) { + return $this->apiSuccess('修改成功'); + } else { + return $this->apiError('修改失败'); + } + } + + public function getWorkArticleCount() + { + $model = Db::table('fa_article'); + //根据个人村id进行查询 + if (JWT_UID) { + $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find(); + if ($find) { + if ($find['auth_range']==1){ + $where[] = ['village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $where[] = ['township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $where[] = ['county', '=', $find['area_id']]; + } + } + } + $is_solve = $model->where('is_solve', 1)->where($where)->count(); + $shuqiu = $model->where('is_solve', 0)->where($where)->where('category_id', 'in',[165,150,149,148,147])->whereTime('end_time','>', date('Y-m-d h:i:s'))->count(); + $time = $model->where('is_solve', 0)->where($where)->where('category_id', 'in',[165,150,149,148,147])->whereTime('end_time','<=', date('Y-m-d h:i:s'))->count(); + $maodun = $model->where('is_solve', 0)->where($where)->where('category_id',147)->count(); + $xiejiao = $model->where('is_solve', 0)->where($where)->where('category_id', 148)->count(); + $saohei = $model->where('is_solve', 0)->where($where)->where('category_id', 149)->count(); + $sos = $model->where('is_solve', 0)->where($where)->where('category_id', 150)->count(); + $one_shuqiu = $model->where('is_solve', 0)->where($where)->where('category_id', 165)->count(); + $select = [ + 'solve_count' => $is_solve, + 'shuqiu' => $shuqiu, + 'time' =>$time, + 'one'=>[ + 'maodun' => $maodun, + 'xiejiao' => $xiejiao, + 'saohei' => $saohei, + 'sos' => $sos, + 'shuqiu' => $one_shuqiu, + ] + ]; + return $this->apiSuccess('ok', $select); + } + + public function delete($id) + { + $model = Db::table('fa_article'); + $res = $model->where('id', $id)->update(['status' => 0]); + if ($res) { + return $this->apiSuccess('删除成功'); + } else { + return $this->apiError('删除失败'); + } + } + +// 服务大厅小红点 + public function getMyArticleCount() + { + $model = Db::table('fa_article'); + //根据个人id进行查询 + $where[] = ['a.user_id','=',JWT_UID]; + $where[] = ['b.is_read','=','0']; + //根据个人村id进行查询 + if (JWT_UID) { + $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find(); + if ($find) { + if ($find['auth_range']==1){ + $www[] = ['a.village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $www[] = ['a.township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $www[] = ['a.county', '=', $find['area_id']]; + } + } + } + $yishi = $model->alias('a')->join('fa_article_vote_side_tables b','a.id=b.article_id')->where($www)->where('a.category_id',162)->whereTime('b.end_time', '>', date('Y-m-d H:i:s'))->count(); + $maodun = $model->alias('a')->join('article_comment b','a.id=b.vote_id')->where('a.is_solve', 1)->where($where)->where('a.category_id',147)->count(); + $xiejiao = $model->alias('a')->join('article_comment b','a.id=b.vote_id')->where('a.is_solve', 1)->where($where)->where('a.category_id', 148)->count(); + $saohei = $model->alias('a')->join('article_comment b','a.id=b.vote_id')->where('a.is_solve', 1)->where($where)->where('a.category_id', 149)->count(); + $sos = $model->alias('a')->join('article_comment b','a.id=b.vote_id')->where('a.is_solve', 1)->where($where)->where('a.category_id', 150)->count(); + $one_shuqiu = $model->alias('a')->join('article_comment b','a.id=b.vote_id')->where('a.is_solve', 1)->where($where)->where('a.category_id', 165)->count(); + $select = [ + 'yishi' => $yishi, + 'maodun' => $maodun, + 'xiejiao' => $xiejiao, + 'saohei' => $saohei, + 'sos' => $sos, + 'shuqiu' => $one_shuqiu, + ]; + return $this->apiSuccess('ok', $select); + } + + public function indexs($search = '', $category_id = 1, $page = 1, $is_time = 0, $category_type = 0) + { + $param = get_params(); + $screen = $param['screen']??1; + $solve = $param['solve']??1; + $order = $param['order']??1; + $model = Db::table('fa_article'); + $where = [ + ['status', '=', 1], + ['category_id', '=', $category_id] + ]; + //查询升降序 + if($order==1){ + $orders='desc'; + }else{ + $orders='asc'; + } + if (!$category_id) { + unset($where[1]); + } + $village_id = get_params('village_id'); + + if ($village_id) { + $where[] = ['village', '=', $village_id]; + } + + //总条数 + $count = $model->where($where)->count(); + //当月条数 + $month_count = $model->where($where)->whereMonth('view_time')->count(); + if ($search != '') { + $where[] = ['title', 'like', '%' . $search . '%']; + } + if ($category_type != 0) { + $where[] = ['category_type', '=', $category_type]; + } + //判断筛选查询 + if ($screen==2){ + foreach ($where as $key =>$value){ + $where[$key][0] = 'a.'.$value[0]; + } + //查询 + $select = Db::table('fa_article')->where($where)->page($page)->limit(20) + ->alias('a') + ->join('article_vote_side_tables avst',"a.id = avst.article_id and avst.end_time >=".date('Y-m-d')) + ->field('a.id,a.title,a.describe,a.user_id,a.view,a.view_time,a.image,a.end_time,a.is_solve,a.is_vote,a.is_nickname,a.video,a.category_type,avst.end_time as die_time') + ->order('a.id DESC') + ->select()->toArray(); + }else if ($screen==3){ + foreach ($where as $key =>$value){ + $where[$key][0] = 'a.'.$value[0]; + } + //查询 + $select = Db::table('fa_article')->where($where)->page($page)->limit(20) + ->alias('a') + ->join('article_vote_side_tables avst',"a.id = avst.article_id and avst.end_time <=".date('Y-m-d')) + ->field('a.id,a.title,a.describe,a.user_id,a.view,a.view_time,a.image,a.end_time,a.is_solve,a.is_vote,a.is_nickname,a.video,a.category_type,avst.end_time as die_time') + ->order('a.id DESC')->select()->toArray(); + }else{ + //查询是否解决 + if($solve==2){ + $where[] = ['is_solve', '=', 0]; + }else if($solve==3){ + $where[] = ['is_solve', '=', 1]; + } + $select = $model->with('user')->where($where)->page($page)->limit(20) + ->field('id,title,user_id,view,view_time,image,end_time,is_solve,is_vote,is_nickname,video,category_type,describe') + ->order('id',$orders)->select()->toArray(); + } + + foreach ($select as $key => $value) { + if ($value['is_nickname'] == 1) { + $select[$key]['nickname'] = "匿名人员"; + $select[$key]['avatar'] = ""; + $select[$key]['user_id'] = 0; + } else { + $select[$key]['nickname'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->value('name'); + } + //投票处理 + if ($value['is_vote'] == 1) { + $article_vote = Db::table('fa_article_vote_side_tables')->where('article_id', $value['id'])->find(); + if ($article_vote) { + $data = $article_vote; + $data['agree_percentage'] = 0; + $data['opposition_percentage'] = 0; + $count_vote = $data['agree'] + $data['opposition'] + $data['other']; + if ($count_vote != 0) { + if ($data['agree'] != 0) { + $data['agree_percentage'] = round(($data['agree'] / $count_vote) * 100); + } + if ($data['opposition'] != 0) { + $data['opposition_percentage'] = round(($data['opposition'] / $count_vote) * 100); + } + } + $select[$key]['extend']['vote'] = $data; + } + } else { + $select[$key]['extend'] = []; + } + if ($is_time == 1) { + if ($value['end_time'] < date('Y-m-d H:i:s')) { + if ($value['end_time'] == '0000-00-00 00:00:00') { + $select[$key]['overdue_time'] = '显示错误'; + } else { + if($value['is_solve']==1){ + $select[$key]['overdue_time'] = 0; + }else{ + $cle = time() - strtotime($value['end_time']); + $select[$key]['overdue_time'] = ceil($cle / 3600 / 24); + } + } + } else { + $select[$key]['overdue_time'] = 0; + } + + } + if ($category_id == 165) { + if ($value['category_type'] != 0) { + $find = Db::table('fa_category')->where('id', $value['category_type'])->find(); + $select[$key]['extend']['category_type_name'] = $find ? $find['name'] . ':' : ''; + } else { + $select[$key]['extend']['category_type_name'] = ''; + } + } + + + } + $this->apiSuccess('ok', ['list' => $select, 'count' => ['count' => $count, 'month_count' => $month_count]]); + } + + +} diff --git a/app/api/controller/ArticleComment.php b/app/api/controller/ArticleComment.php new file mode 100644 index 0000000..5ed2a74 --- /dev/null +++ b/app/api/controller/ArticleComment.php @@ -0,0 +1,219 @@ + ['except' => ['index','OneList','isread'] ] + ]; + public function __construct(App $app) + { + parent::__construct($app); + + } + public function index($search='',$vote_id=1,$page=1) { + $select=Db::table('fa_article_comment') + ->withAttr('user_info',function ($data,$value){ + $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find(); + $data = $find; + $data['user_type_info']='群众'; + $data['user_name']='暂为填写姓名'; + if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){ + $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name'); + $data['user_name']=$msg['name']; + } + return $data; + }) + ->withAttr('reply',function ($data,$value){ + if ($value['reply']==null){ + $find='无'; + }else{ + $find=$value['reply']; + } + return $find; + }) + ->where('vote_id', $vote_id)->where('status', 1)->where('type',1)->page($page)->limit(20)->select(); + $select_type2=Db::table('fa_article_comment') + ->withAttr('user_info',function ($data,$value){ + $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find(); + $data = $find; + $data['user_type_info']='群众'; + $data['user_name']='暂为填写姓名'; + if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){ + $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name'); + $data['user_name']=$msg['name']; + } + return $data; + }) + ->withAttr('reply',function ($data,$value){ + if ($value['reply']==null){ + $find='无'; + }else{ + $find=$value['reply']; + } + return $find; + }) + ->where('vote_id', $vote_id)->where('status', 1)->where('type',2)->page($page)->limit(20)->select(); + $select_type3=Db::table('fa_article_comment') + ->withAttr('user_info',function ($data,$value){ + $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find(); + $data = $find; + $data['user_type_info']='群众'; + $data['user_name']='暂为填写姓名'; + if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){ + $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name'); + $data['user_name']=$msg['name']; + } + return $data; + }) + ->withAttr('reply',function ($data,$value){ + if ($value['reply']==null){ + $find='无'; + }else{ + $find=$value['reply']; + } + return $find; + }) + ->where('vote_id', $vote_id)->where('status', 1)->where('type',3)->page($page)->limit(20)->select(); + return $this->apiSuccess('ok',['list1'=>$select,'list2'=>$select_type2,'list3'=>$select_type3]); + } + public function OneList($vote_id=1,$page=1) { + $select=Db::table('fa_article_comment') + ->withAttr('user_info',function ($data,$value){ + $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find(); + $data = $find; + $data['user_type_info']='群众'; + if($msg['name']){ + $data['user_name']=$msg['name']; + }else{ + $data['user_name']='暂为填写姓名'; + } + if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){ + $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name'); + } + return $data; + }) + ->withAttr('reply',function ($data,$value){ + if ($value['reply']==null){ + $find='无'; + }else{ + $find=$value['reply']; + } + return $find; + })->where('status', 1)->where('vote_id',$vote_id)->page($page)->limit(20)->select(); + + return $this->apiSuccess('ok',['list'=>$select]); + } + public function add(){ + + } + public function post(){ + $input = get_params(); +// $res=$this->validate->check($input); +// if (!$res){ +// return $this->apiError($this->validate->getError()); +// } +// 判断是否过期 + $vote_end_time = Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->value('end_time'); + if($vote_end_time && strtotime($vote_end_time) < time()){ + $this->apiError('已结束议事不能投票'); + } + if ($input['type']!=0){ + $find=Db::table('fa_article_comment')->where('vote_id', $input['vote_id'])->where('user_id', JWT_UID)->find(); + if ($find){ + return $this->apiError('只能投票一次'); + } + } + if(empty($input['content'])){ + $this->apiError('请输入投票内容'); + } + $useraddress = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->where('status', 1)->find(); + + if ($useraddress) { + $input['county'] = $useraddress['area_id']; + $input['township'] = $useraddress['street_id']; + $input['village'] = $useraddress['village_id']; + } + + $input['user_id']=JWT_UID; + $input['add_time']=date('Y-m-d H:i:s'); + $res=Db::table('fa_article_comment')->save($input); + if ($res){ + if($input['type']==1){ + Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->inc('agree')->update(); + }elseif($input['type']==2){ + Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->inc('opposition')->update(); + }elseif($input['type']==3){ + Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->inc('other')->update(); + }else{ + $find=Db::table('fa_article')->where('id',$input['vote_id'])->find(); + if ($find){ + $category=[150,149,148,147,165]; + if (in_array($find['category_id'],$category)){ + Db::table('fa_article')->where('id',$input['vote_id'])->update(['is_solve'=>1]); + } + } + } + return $this->apiSuccess('评论成功'); + }else{ + return $this->apiError('添加失败'); + } + } + public function edit($id){ + $find=Db::table('fa_article_comment')->with('user')->where('id',$id)->find(); + return $this->apiSuccess('ok',$find); + + } + public function put($id){ + $input = get_params(); +// $res=$this->validate->check($input); +// if (!$res){ +// return $this->apiError($this->validate->getError()); +// } + $input['user_id'] = JWT_UID; + $res=Db::table('fa_article_comment')->where('id',$id)->update($input); + if ($res){ + return $this->apiSuccess('修改成功'); + }else{ + return $this->apiError('修改失败'); + } + } + public function delete($id){ + $res=Db::table('fa_article_comment')->where('id',$id)->update(['status'=>0]); + if ($res){ + return $this->apiSuccess('删除成功'); + }else{ + return $this->apiError('删除失败'); + } + } + +// 服务大厅清除小红点 + public function isread($vote_id=0) { + $select=Db::table('fa_article_comment')->where('status', 1)->where('vote_id',$vote_id)->select(); + + if($select->toArray()){ + $selects = $select->toArray(); + foreach ($selects as $k=>$v){ + $data['is_read'] = 1; + Db::table('fa_article_comment')->where('id',$v['id'])->update($data); + } + } + $this->apiSuccess('ok'); + } + +} diff --git a/app/api/controller/Ceshi.php b/app/api/controller/Ceshi.php new file mode 100644 index 0000000..cd6e08c --- /dev/null +++ b/app/api/controller/Ceshi.php @@ -0,0 +1,309 @@ + ['except' => ['index', 'ceshi2'] ] + ]; + + + public function ceshi2() + { + $config = [ + 'app_id' => 'wx0b3defb62f0f910b', + 'secret' => 'c02aa7ad9e4a5c423862e068b6cb4ad4', + 'token' => 'e5511202e4104e0aa9963e244b3e3785', + 'aes_key' => '', // 明文模式请勿填写 EncodingAESKey + + /** + * OAuth 配置 + * + * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login + * callback:OAuth授权完成后的回调页地址 + */ + 'oauth' => [ + 'scopes' => ['snsapi_userinfo'], + 'callback' => '/examples/oauth_callback.php', + ], + + /** + * 接口请求相关配置,超时时间等,具体可用参数请参考: + * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php + */ + 'http' => [ + 'timeout' => 5.0, + // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri + + 'retry' => true, // 使用默认重试配置 + // 'retry' => [ + // // 仅以下状态码重试 + // 'http_codes' => [429, 500] + // // 最大重试次数 + // 'max_retries' => 3, + // // 请求间隔 (毫秒) + // 'delay' => 1000, + // // 如果设置,每次重试的等待时间都会增加这个系数 + // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) + // 'multiplier' => 3 + // ], + ], + ]; + + $app = new wxApplication($config); + $utils = $app->getUtils(); + $config = $utils->buildJsSdkConfig( + 'https://h5.lihaink.cn', + ['updateAppMessageShareData', 'updateTimelineShareData'], + [], + false + ); + return $this->apiSuccess('ok', $config); + } + + public function index($code) + { + $config = [ + 'app_id' => 'wxfb4695444ab8d0d0', + 'secret' => '9d73b21c39586ccb55c716546a0b999e', + 'token' => 'easywechat', + + /** + * 接口请求相关配置,超时时间等,具体可用参数请参考: + * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php + */ + 'http' => [ + 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 + 'timeout' => 5.0, + // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri + + 'retry' => true, // 使用默认重试配置 + // 'retry' => [ + // // 仅以下状态码重试 + // 'http_codes' => [429, 500] + // // 最大重试次数 + // 'max_retries' => 3, + // // 请求间隔 (毫秒) + // 'delay' => 1000, + // // 如果设置,每次重试的等待时间都会增加这个系数 + // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) + // 'multiplier' => 3 + // ], + ], + ]; + + $app = new Application($config); + $utils = $app->getUtils(); + $response = $utils->codeToSession($code); + if ($response['openid']) { + $user = Db::table('fa_user')->where('openid', $response['openid'])->field('id,nickname,group_id,avatar,group_id')->find(); + if ($user) { + $find = Db::table('fa_szxc_information_usermsg')->where('user_id', $user['id'])->find(); + if ($find) { + $user['no_update'] = 0; + } else { + $user['no_update'] = 1; + } + //创建新Token + $token = self::getToken($user['id']); + $user['token'] = $token; + $user['expires_in'] = 2222222; + return $this->apiSuccess('ok', ['user_info' => $user]); + } else { + $time = time(); + $data = [ + 'openid' => $response['openid'], + 'group_id' => 1, + 'username' => 'wx' . $time, + 'nickname' => '微信用户' . $time, + 'avatar' => 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230111/58c84995456bc63b2660d9526f7062fc.png', + 'level' => 1, + 'createtime' => $time, + 'updatetime' => $time, + 'status' => 'normal' + ]; + $id = Db::table('fa_user')->insertGetId($data); + // 写入商城关系表 + $nk_user['n_user_id'] = $id; + $is_nk_user_id = Db::connect('shop')->name('nk_user')->where($nk_user)->find(); + if (!$is_nk_user_id) { + Db::connect('shop')->name('nk_user')->insert($nk_user); + } + + //创建新Token + $token = self::getToken($user['id']); + $userinfo = ['user_id' => $id, 'no_update' => 1, 'token' => $token, 'expires_in' => 2222222, 'nickname' => $data['nickname'], 'avatar' => $data['avatar'], 'group_id' => 1]; + return $this->apiSuccess('ok', ['user_info' => $userinfo]); + + } + } + halt($response); + + } + + /** + * @param $user_id + * @return string + */ + public function getToken($user_id){ + $time = time(); //当前时间 + $conf = $this->jwt_conf; + $token = [ + 'iss' => $conf['iss'], //签发者 可选 + 'aud' => $conf['aud'], //接收该JWT的一方,可选 + 'iat' => $time, //签发时间 + 'nbf' => $time-1 , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用 + 'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时 + 'data' => [ + //自定义信息,不要定义敏感信息 + 'userid' =>$user_id, + ] + ]; + return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token 默认'HS256' + } + + public function getopenid() + { + $code = get_params('code'); + if (empty($code)) { + $this->apiError('缺少参数'); + } + $config = [ + 'app_id' => 'wx6e14cb98394e36bc', + 'secret' => 'd8b7c06bbdb29309fed11fe62fe6b022', + 'token' => 'easywechat', + /** + * 接口请求相关配置,超时时间等,具体可用参数请参考: + * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php + */ + 'http' => [ + 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 + 'timeout' => 5.0, + // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri + + 'retry' => true, // 使用默认重试配置 + // 'retry' => [ + // // 仅以下状态码重试 + // 'http_codes' => [429, 500] + // // 最大重试次数 + // 'max_retries' => 3, + // // 请求间隔 (毫秒) + // 'delay' => 1000, + // // 如果设置,每次重试的等待时间都会增加这个系数 + // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) + // 'multiplier' => 3 + // ], + ], + ]; + + $app = new Application($config); + $utils = $app->getUtils(); + $response = $utils->codeToSession($code); + if ($response['openid']) { + $where['user_id'] = JWT_UID; + $user = Db::table('fa_user_openid')->where($where)->find(); + if ($user) { + if (!empty($user['openid']) && $user['openid'] == $response['openid']) { + $this->apiError('您已授权'); + } else { + $data['openid'] = $response['openid']; + Db::table('fa_user_openid')->where($where)->update($data); + } + } else { + $time = time(); + $data = [ + 'openid' => $response['openid'], + 'user_id' => JWT_UID, + 'createtime' => $time, + ]; + $res = Db::table('fa_user_openid')->insert($data); + if ($res) { + $this->apiSuccess('授权成功'); + } else { + $this->apiError('授权失败'); + } + } + } else { + $this->apiError('操作失败'); + } + } + + public function getoffopenid() + { + $code = get_params('code'); + if (empty($code)) { + $this->apiError('缺少参数'); + } + $config = [ + 'app_id' => 'wx0b3defb62f0f910b', + 'secret' => 'c02aa7ad9e4a5c423862e068b6cb4ad4', + 'token' => 'easywechat', + /** + * 接口请求相关配置,超时时间等,具体可用参数请参考: + * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php + */ + 'http' => [ + 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 + 'timeout' => 5.0, + // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri + + 'retry' => true, // 使用默认重试配置 + // 'retry' => [ + // // 仅以下状态码重试 + // 'http_codes' => [429, 500] + // // 最大重试次数 + // 'max_retries' => 3, + // // 请求间隔 (毫秒) + // 'delay' => 1000, + // // 如果设置,每次重试的等待时间都会增加这个系数 + // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) + // 'multiplier' => 3 + // ], + ], + ]; + + $app = new Application($config); + $utils = $app->getUtils(); + $response = $utils->codeToSession($code); + if ($response['openid']) { + $where['user_id'] = JWT_UID; + $user = Db::table('fa_user_openid')->where($where)->find(); + if ($user) { + if (!empty($user['official_account_openid']) && $user['official_account_openid'] == $response['openid']) { + $this->apiError('您已授权'); + } else { + $data['official_account_openid'] = $response['openid']; + Db::table('fa_user_openid')->where($where)->update($data); + } + } else { + $time = time(); + $data = [ + 'official_account_openid' => $response['openid'], + 'user_id' => JWT_UID, + 'createtime' => $time, + ]; + $res = Db::table('fa_user_openid')->insert($data); + if ($res) { + $this->apiSuccess('授权成功'); + } else { + $this->apiError('授权失败'); + } + } + } else { + $this->apiError('操作失败'); + } + } + + +} \ No newline at end of file diff --git a/app/api/controller/Common.php b/app/api/controller/Common.php new file mode 100644 index 0000000..b6175c3 --- /dev/null +++ b/app/api/controller/Common.php @@ -0,0 +1,352 @@ + ['except' => ['init','category','categorys','upload','get_area','get_street','get_village','idcard','get_all_category','get_all_category_health','get_brigade'] ] + ]; + /** + * 加载初始化. + * + * @param string $version 版本号 + * @param string $lng 经度 + * @param string $lat 纬度 + */ + public function init() + { + if ($version = get_params('version')) { + $lng = get_params('lng'); + $lat = get_params('lat'); + $content = [ + 'citydata' => Area::getCityFromLngLat($lng, $lat), + 'versiondata' => Version::check($version), + 'uploaddata' => Config::get('upload'), + 'coverdata' => Config::get('cover'), + ]; + $this->apiSuccess('', $content); + } else { + $this->apiError('参数不正确'); + } + } + + /** + * 上传文件. + * @ApiMethod (POST) + * + * @param File $file 文件流 + */ + public function upload() + { + $file = $this->request->file('file'); + if (empty($file)) { + $this->apiError('未上传文件或超出服务器上传限制'); + } + + //判断是否已经存在附件 + $sha1 = $file->hash(); + + $upload = Config::get('upload'); + + preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches); + $type = strtolower($matches[2]); + $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3]; + $size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0); + $fileInfo['name'] = $file->getOriginalName(); //上传文件名 + $fileInfo['type'] = $file->getOriginalMime(); //上传文件类型信息 + $fileInfo['tmp_name'] = $file->getPathname(); + $fileInfo['size'] = $file->getSize(); + $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); + $suffix = $suffix && preg_match('/^[a-zA-Z0-9]+$/', $suffix) ? $suffix : 'file'; + + $mimetypeArr = explode(',', strtolower($upload['mimetype'])); + $typeArr = explode('/', $fileInfo['type']); + + //禁止上传PHP和HTML文件 + if (in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm', 'phar', 'phtml']) || preg_match("/^php(.*)/i", $suffix)) { + $this->apiError('上传文件格式受限制'); + } + + //Mimetype值不正确 + if (stripos($fileInfo['type'], '/') === false) { + $this->apiError('上传文件格式受限制'); + } + + //验证文件后缀 + if ($upload['mimetype'] !== '*' && + ( + !in_array($suffix, $mimetypeArr) + || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr))) + ) + ) { + $this->apiError('上传文件格式受限制'); + } + + //验证是否为图片文件 + $imagewidth = $imageheight = 0; + if (in_array($fileInfo['type'], + ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix, + ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) { + $imgInfo = getimagesize($fileInfo['tmp_name']); + if (! $imgInfo || ! isset($imgInfo[0]) || ! isset($imgInfo[1])) { + $this->apiError('上传文件不是有效的图片文件'); + } + $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth; + $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight; + } + + $_validate[] = 'filesize:'.$size; + if ($upload['mimetype']) { + $_validate[] = 'fileExt:'.$upload['mimetype']; + } + $validate = implode('|', $_validate); + + $event_config = Event::trigger('upload_init', $upload,true); + if($event_config){ + $upload = array_merge($upload, $event_config); + } + try { + $savename = upload_file($file, $upload['driver'], 'uploads', $validate, $upload['cdnurl']); + } catch (\Exception $e) { + $savename = false; + $this->apiError($e->getMessage()); + } + if (! $savename) { + $this->apiError('上传失败'); + } + $category = request()->post('category'); + $category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : ''; +// $urls= Env::get('APP.URL_MY')? Env::get('APP.URL_MY'):'https://ceshi.excellentkk.cn'; + $params = [ + 'admin_id' => 0, + 'user_id' => (int) JWT_UID, + 'category' => $category, + 'filename' => mb_substr(htmlspecialchars(strip_tags($fileInfo['name'])), 0, 100), + 'filesize' => $fileInfo['size'], + 'imagewidth' => $imagewidth, + 'imageheight' => $imageheight, + 'imagetype' => $suffix, + 'imageframes' => 0, + 'mimetype' => $fileInfo['type'], + 'url' => $savename, + 'uploadtime' => time(), + 'storage' => $upload['driver'], + 'sha1' => $sha1, + ]; + $attachment = new Attachment(); + $attachment->data(array_filter($params)); + $attachment->save(); + \think\facade\Event::trigger('upload_after', $attachment); + $this->apiSuccess('上传成功', [ + 'url' =>$savename, + ]); + } + public function category($id=0,$type='',$pid=0,$is_category=false){ + + if ($is_category){ + if ($pid==162){ + $data=[['id'=>1,'name'=>'全部'],['id'=>2,'name'=>'未结束'],['id'=>3,'name'=>'已结束']]; + return $this->apiSuccess('ok',$data); + } + if ($pid==1){ + $data=[['id'=>1,'name'=>'全部'],['id'=>2,'name'=>'户主']]; + return $this->apiSuccess('ok',$data); + } + $solve=[165,147,148,149]; + if (in_array($pid,$solve)){ + $data=[['id'=>1,'name'=>'全部'],['id'=>2,'name'=>'未处理'],['id'=>3,'name'=>'已处理']]; + return $this->apiSuccess('ok',$data); + } + $order=[164,163,152,160,161]; + if (in_array($pid,$order)){ + $data=[['id'=>1,'name'=>'降序'],['id'=>2,'name'=>'升序']]; + return $this->apiSuccess('ok',$data); + } + } + if ($type){ + $where[]=['type','=',$type]; + } + if ($id){ + $where[]=['id','=',$id]; + } + if ($id==0 && $type==''){ + return $this->apiError('参数不能为空,请填写参数'); + } + $where[]=['pid','=',$pid]; + $where[]=['status','=','normal']; + $select=Db::table('fa_category')->where($where)->select(); + return $this->apiSuccess('ok',$select); + } + + public function categorys($id=0,$type='',$pid=0,$is_category=false){ + + if ($is_category){ + if ($pid==162){ + $data=[['id'=>1,'name'=>'全部'],['id'=>2,'name'=>'未结束'],['id'=>3,'name'=>'已结束']]; + return $this->apiSuccess('ok',$data); + } + if ($pid==1){ + $data=[['id'=>1,'name'=>'全部'],['id'=>2,'name'=>'户主']]; + return $this->apiSuccess('ok',$data); + } + $solve=[165,147,148,149]; + if (in_array($pid,$solve)){ + $data=[['id'=>1,'name'=>'全部'],['id'=>2,'name'=>'未处理'],['id'=>3,'name'=>'已处理']]; + return $this->apiSuccess('ok',$data); + } + $order=[164,163,152,160,161]; + if (in_array($pid,$order)){ + $data=[['id'=>1,'name'=>'降序'],['id'=>2,'name'=>'升序']]; + return $this->apiSuccess('ok',$data); + } + } + if ($type){ + $where[]=['type','=',$type]; + } + if ($id){ + $where[]=['id','=',$id]; + } + if ($id==0 && $type==''){ + return $this->apiError('参数不能为空,请填写参数'); + } + $where[]=['pid','=',$pid]; + $where[]=['status','=','normal']; + $select=Db::table('fa_category')->where($where)->field('id as category_id,name as topic_name,image as pic,pid')->select()->toArray(); + foreach ($select as $k=>$v){ + if($v['pid'] == 0){ + $select[$k]['children'] = Db::table('fa_category')->where('pid',$v['category_id'])->field('id as category_id,name as topic_name,image as pic')->select(); + } + } + return $this->apiSuccess('ok',$select); + } + public function idcard($code){ + $user_id = $this->request->get('user_id'); + if($user_id){ + $www[] = [['user_id','=',$user_id],['idcard','=',$code]]; + }else{ + $www = []; + } + $where[] = ['idcard','=',$code]; + $where[] = ['is_hz','=',1]; + $find=Db::table('fa_szxc_information_usermsg')->where($where)->whereOr($www)->field('user_id,name,age')->find(); +// dump(Db::table('fa_szxc_information_usermsg')->getLastSql());die; + return $this->apiSuccess('ok',$find); + + } + //区县数据 + public function get_area($city_code){ + $select=Db::table('fa_geo_area')->where('city_code',$city_code)->field('area_id id,area_code code,area_name name')->select(); + return $this->apiSuccess('ok',$select); + } + //街道 乡镇数据 + public function get_street($area_code){ + $select=Db::table('fa_geo_street')->where('area_code',$area_code)->field('street_id id,street_code code,street_name name')->select(); + return $this->apiSuccess('ok',$select); + } + //村数据 + public function get_village($street_code){ + $select=Db::table('fa_geo_village')->where('street_code',$street_code)->field('village_id id,village_code code,village_name name')->select(); + return $this->apiSuccess('ok',$select); + } + + //大队数据 + public function get_brigade(){ + $select=Db::table('fa_geo_brigade')->field('id,brigade_name as name')->select(); + $this->apiSuccess('ok',$select); + } + + public function get_all_category(){ + $nation=Db::table('fa_category')->where('type','=','nation')->select(); + $Zzmm=Db::table('fa_category')->where('type','=','Zzmm')->select(); + $Education=Db::table('fa_category')->where('type','=','Education')->select(); + $Occupation=Db::table('fa_category')->where('type','=','Occupation')->select(); + $Car=Db::table('fa_category')->where('type','=','Car')->select(); + $House=Db::table('fa_category')->where('type','=','House')->select(); + $Family=Db::table('fa_category')->where('type','=','Family')->select(); + $Marriage=Db::table('fa_category')->where('type','=','Marriage')->select(); + $select=[ + 'nation' =>$nation, + 'occupation' =>$Occupation, + 'car' =>$Car, + 'house' =>$House, + 'zzmm' =>$Zzmm, + 'education' =>$Education, + 'family' =>$Family, + 'marriage' =>$Marriage + ]; + return $this->apiSuccess('ok',$select); + } + public function get_all_category_insurance(){ + $Grade=Db::table('fa_category')->where('type','=','Grade')->select();//等级 + $Insurance_one=Db::table('fa_category')->where('pid','=','194')->select();//商业保险 + $Insurance_two=Db::table('fa_category')->where('pid','=','193')->select();//农业保险 + $Whether_disabled=Db::table('fa_category')->where('type','=','Whether_disabled')->select();//残疾状况 + $Medical_insurance_type=Db::table('fa_category')->where('type','=','Medical_insurance_type')->select();//残疾状况 + $select=[ + 'grade' =>$Grade, + 'Insurance_one' =>$Insurance_one, + 'Insurance_two' =>$Insurance_two, + 'Whether_disabled' =>$Whether_disabled, + 'Medical_insurance_type' =>$Medical_insurance_type, + ]; + return $this->apiSuccess('ok',$select); + } + public function get_all_category_health(){ + $Blood_type=Db::table('fa_category')->where('type','=','Blood_type')->select(); + $Insurance_type=Db::table('fa_category')->where('type','=','Insurance_type')->select(); + $Drug_allergy=Db::table('fa_category')->where('type','=','Drug_allergy')->select(); + $Exposure_history=Db::table('fa_category')->where('type','=','Exposure_history')->select(); + $History_of_disease=Db::table('fa_category')->where('type','=','History_of_disease')->select(); + $whether_disabled=Db::table('fa_category')->where('type','=','whether_disabled')->select(); + $cfpfss=Db::table('fa_category')->where('type','=','cfpfss')->select(); + $fuel_type=Db::table('fa_category')->where('type','=','fuel_type')->select(); + $Drinking_water=Db::table('fa_category')->where('type','=','Drinking_water')->select(); + $Toilet_msg=Db::table('fa_category')->where('type','=','Toilet_msg')->select(); + $Poultry_corral=Db::table('fa_category')->where('type','=','Poultry_corral')->select(); + $FamilyHistory=Db::table('fa_category')->where('type','=','FamilyHistory')->select(); + $select=[ + 'Blood_type' =>$Blood_type, + 'Insurance_type' =>$Insurance_type, + 'Drug_allergy' =>$Drug_allergy, + 'Exposure_history' =>$Exposure_history, + 'History_of_disease' =>$History_of_disease, + 'whether_disabled' =>$whether_disabled, + 'cfpfss' =>$cfpfss, + 'fuel_type' =>$fuel_type, + 'Drinking_water' =>$Drinking_water, + 'Toilet_msg' =>$Toilet_msg, + 'Poultry_corral' =>$Poultry_corral, + 'FamilyHistory' =>$FamilyHistory, + ]; + return $this->apiSuccess('ok',$select); + } +} diff --git a/app/api/controller/Ems.php b/app/api/controller/Ems.php new file mode 100644 index 0000000..a743e0b --- /dev/null +++ b/app/api/controller/Ems.php @@ -0,0 +1,106 @@ +to($params->email) + ->subject('验证码') + ->message('你的验证码是:'.$params->code) + ->send(); + + return $result; + }); + } + + /** + * 发送验证码 + * + * @param string $email 邮箱 + * @param string $event 事件名称 + */ + public function send() + { + $email = $this->request->request('email'); + $event = $this->request->request('event'); + $event = $event ? $event : 'register'; + + $last = Emslib::get($email, $event); + if ($last && time() - $last['createtime'] < 60) { + $this->error(__('发送频繁')); + } + if ($event) { + $userinfo = User::where('email',$email)->find(); + if ($event == 'register' && $userinfo) { + //已被注册 + $this->error(__('已被注册')); + } elseif (in_array($event, ['changeemail']) && $userinfo) { + //被占用 + $this->error(__('已被占用')); + } elseif (in_array($event, ['changepwd', 'resetpwd']) && ! $userinfo) { + //未注册 + $this->error(__('未注册')); + } + } + $ret = Emslib::send($email, null, $event); + if ($ret) { + $this->success(__('发送成功')); + } else { + $this->error(__('发送失败')); + } + } + + /** + * 检测验证码 + * + * @param string $email 邮箱 + * @param string $event 事件名称 + * @param string $captcha 验证码 + */ + public function check() + { + $email = $this->request->request('email'); + $event = $this->request->request('event'); + $event = $event ? $event : 'register'; + $captcha = $this->request->request('captcha'); + + if ($event) { + $userinfo = User::where('email',$email)->find(); + if ($event == 'register' && $userinfo) { + //已被注册 + $this->error(__('已被注册')); + } elseif (in_array($event, ['changeemail']) && $userinfo) { + //被占用 + $this->error(__('已被占用')); + } elseif (in_array($event, ['changepwd', 'resetpwd']) && ! $userinfo) { + //未注册 + $this->error(__('未注册')); + } + } + $ret = Emslib::check($email, $captcha, $event); + if ($ret) { + $this->success(__('成功')); + } else { + $this->error(__('验证码不正确')); + } + } +} diff --git a/app/api/controller/Geo.php b/app/api/controller/Geo.php new file mode 100644 index 0000000..55e48b1 --- /dev/null +++ b/app/api/controller/Geo.php @@ -0,0 +1,96 @@ + ['except' => ['province','city','area','street','village','brigade'] ] + ]; + + /** + * 省 + */ + public function province(){ + $res = Db::table('fa_geo_province')->where(['switch'=>1]) + ->field('province_id id,province_code code,province_name name') + ->select(); + $this->apiSuccess('OK',$res); + } + + /** + * 市 + */ + public function city(){ + $pcode = get_params('pcode'); + // $pcode = '130000'; + if(!$pcode) $this->apiError('请先选择省份'); + $res = Db::table('fa_geo_city')->where(['switch'=>1,'province_code'=>$pcode]) + ->field('city_id id,city_code code,city_name name') + ->select(); + $this->apiSuccess('OK',$res); + } + + /** + * 区 + */ + public function area(){ + $pcode = get_params('pcode'); + // $pcode = '140100'; + if(!$pcode) $this->apiError('请先选择城市'); + $res = Db::table('fa_geo_area')->where(['switch'=>1,'city_code'=>$pcode]) + ->field('area_id id,area_code code,area_name name') + ->select(); + $this->apiSuccess('OK',$res); + } + + /** + * 街道 + */ + public function street(){ + $pcode = get_params('pcode'); + // $pcode = '410102'; + if(!$pcode) $this->apiError('请先选择区/县'); + $res = Db::table('fa_geo_street')->where(['switch'=>1,'area_code'=>$pcode]) + ->field('street_id id,street_code code,street_name name') + ->select(); + $this->apiSuccess('OK',$res); + } + + /** + * 村 + */ + public function village(){ + $pcode = get_params('pcode'); + // $pcode = '410102'; + if(!$pcode) $this->apiError('请先选择镇/街道'); + $res = Db::table('fa_geo_village')->where(['switch'=>1,'street_code'=>$pcode]) + ->field('village_id id,village_code code,village_name name') + ->select(); + $this->apiSuccess('OK',$res); + } + + /** + * 大队 + */ + public function brigade(){ + + $res = Db::table('fa_geo_brigade') + ->select(); + $this->apiSuccess('OK',$res); + } + + +} diff --git a/app/api/controller/Index.php b/app/api/controller/Index.php index 8556920..6229f57 100644 --- a/app/api/controller/Index.php +++ b/app/api/controller/Index.php @@ -75,7 +75,7 @@ class Index extends BaseController */ public function index() { - $list = Db::name('Article')->select(); + $list = Db::table('fa_Article')->select(); $seo = get_system_config('web'); add_user_log('api', '首页'); $this->apiSuccess('请求成功',['list' => $list,'seo' => $seo]); @@ -102,7 +102,7 @@ class Index extends BaseController $this->apiError('参数错误'); } // 校验用户名密码 - $user = Db::name('User')->where(['username' => $param['username']])->find(); + $user = Db::table('fa_user')->where(['mobile' => $param['username']])->find(); if (empty($user)) { $this->apiError('帐号或密码错误'); } @@ -114,11 +114,10 @@ class Index extends BaseController $this->apiError('该用户禁止登录,请于平台联系'); } $data = [ - 'last_login_time' => time(), - 'last_login_ip' => request()->ip(), - 'login_num' => $user['login_num'] + 1, + 'prevtime' => time(), + 'loginip' => request()->ip(), ]; - $res = Db::name('user')->where(['id' => $user['id']])->update($data); + $res = Db::table('fa_user')->where(['id' => $user['id']])->update($data); if ($res) { $token = self::getToken($user['id']); add_user_log('api', '登录'); @@ -144,7 +143,7 @@ class Index extends BaseController if(empty($param['username']) || empty($param['pwd'])){ $this->apiError('参数错误'); } - $user = Db::name('user')->where(['username' => $param['username']])->find(); + $user = Db::table('fa_user')->where(['username' => $param['username']])->find(); if (!empty($user)) { $this->apiError('该账户已经存在'); } @@ -154,7 +153,7 @@ class Index extends BaseController $param['headimgurl'] = '/static/admin/images/icon.png'; $param['register_ip'] = request()->ip(); $char = mb_substr($param['username'], 0, 1, 'utf-8'); - $uid = Db::name('User')->strict(false)->field(true)->insertGetId($param); + $uid = Db::table('fa_user')->strict(false)->field(true)->insertGetId($param); if($uid){ add_user_log('api', '注册'); $this->apiSuccess('注册成功,请登录'); @@ -175,7 +174,48 @@ class Index extends BaseController public function demo() { $uid = JWT_UID; - $userInfo = Db::name('User')->where(['id' => $uid])->find(); + $userInfo = Db::table('fa_user')->where(['id' => $uid])->find(); $this->apiSuccess('请求成功', ['user' => $userInfo]); } + + /** + * 文章投诉接口. + */ + public function complaint(){ + $param = get_params(); + $id = $param['id']; //文章id + $content = $param['content']; //投诉内容 + $type = $param['type']??1; //标识(1文章/2朋友圈) + $user_id = JWT_UID;//用户的id + $where['article_id'] = $id; + $where['user_id'] = $user_id; + $where['type'] = $type; + $is_complaint = Db::table('fa_article_complaint')->where($where)->find(); + if($is_complaint){ + $this->apiError('您已投诉!'); + }else{ + + $useraddress = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->where('status', 1)->find(); + $data = []; + if ($useraddress) { + $data['county'] = $useraddress['area_id']; + $data['township'] = $useraddress['street_id']; + $data['village'] = $useraddress['village_id']; + } + + $data['article_id'] = $id; + $data['user_id'] = $user_id; + $data['type'] = $type; + $data['content'] = $content; + $data['createtime'] = time(); + $res = Db::table('fa_article_complaint')->strict(false)->insert($data); + if($res){ + $this->apiSuccess('投诉成功'); + }else{ + $this->apiError('投诉失败'); + } + } + } + + } diff --git a/app/api/controller/Maintainentry.php b/app/api/controller/Maintainentry.php new file mode 100644 index 0000000..1942841 --- /dev/null +++ b/app/api/controller/Maintainentry.php @@ -0,0 +1,1794 @@ + ['except' => ['getcategory'] ] + ]; + + /** + * 获取已录入信息 + * + * @ApiTitle (获取已录入信息) + * @ApiSummary (获取已录入信息) + * @ApiMethod (GET) + * @ApiRoute (/api/Maintainentry/pickUpInfo) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="page", type="integer", required=true, description="页数") + * @ApiParams (name="name", type="string", required=false, description="用户名") + * @ApiParams (name="limit", type="string", required=false, description="每页条数") + * @ApiParams (name="idcard", type="string", required=false, description="身份证号") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "获取成功", + * "time": "1668664754", + * "data": { + * "total_num": 555, + * "now_num": 1, + * "data": [ + * { + * "id": 1, + * "name": "张三", + * "age": 42, + * "gender": "男", + * "family_num": null, + * "political_outlook": null, + * "householder_id": 1 + * } + * ] + * } + * } + */ + public function pickUpInfo() + { + //村id + $village_id = Db::table('fa_szxc_information_useraddress')->where('user_id',JWT_UID)->value('village_id'); + //获取当前村总人数 + $map['village_id'] = $village_id; + $total = Db::table('fa_szxc_information_useraddress')->where($map)->count(); + //获取已录入人数 + $www[] = ['village_id','=',$village_id]; + $www[] = ['status','=','1']; + $www[] = ['enter_id','=',JWT_UID]; + $have = Db::table('fa_szxc_information_usermsg')->where($www)->count(); + $page = get_params('page')??1; + $limit = get_params('limit')??10; +// $name = get_params('name', ''); + $idcard = get_params('keyword'); +// if ($name) { +// $where['name'] = ['like', '%' . $name . '%']; //搜索姓名 +// } + if ($idcard) { + $where[] = ['name','like', '%' . $idcard . '%']; // 搜索身份证 + } + $where[] = ['village_id','=',$village_id]; + $where[] = ['status','=',1]; + $type = get_params('type'); + if ($type==2){ + $where[] = ['is_hz','=',1]; + } +// 获取已录入的人员信息 + $res = Db::table('fa_szxc_information_usermsg')->where($where) + ->order('id desc') + ->field("id,user_id,name,age,gender,family_num,political_outlook,householder_id,is_hz,address_name") + ->page($page, $limit) + ->withAttr('householder_name',function ($data,$value){ + if($value['is_hz']==1){ + return '本人'; + }else{ + $msg= Db::table('fa_szxc_information_usermsg')->where('user_id',$value['householder_id'])->value('name'); + return $msg??'暂无户主'; + } + }) + ->withAttr('family_num',function ($data,$value){ + if($value['householder_id']){ + $is_family = Db::table('fa_szxc_information_usermsg')->where('householder_id',$value['householder_id'])->count(); + }else{ + $is_family = 1; + } + return $is_family; + }) + ->withAttr('is_family',function ($data,$value){ + if($value['householder_id']){ + return 1; + }else{ + return 0; + } + }) + ->withAttr('is_planting',function ($data,$value){ + $is_planting = Db::table('fa_szxc_information_planting')->where('user_id',$value['user_id'])->find(); + if($is_planting){ + return 1; + }else{ + return 0; + } + }) + ->withAttr('political_outlook',function ($data,$value){ + $political_outlook=Db::table('fa_category')->where('id',$value['political_outlook'])->find(); + return $political_outlook?$political_outlook['name']:'无'; + }) + ->withAttr('gender',function ($data,$value){ + if ($value['gender']==1){ + return '男'; + }else{ + return '女'; + } + }) + ->select(); + $return['total_num'] = $total; + $return['now_num'] = $have; + $return['data'] = $res; + $this->apiSuccess('获取成功', $return, 1); + } + + /** + * 获取家庭成员列表 + * + * @ApiTitle (获取家庭成员列表) + * @ApiSummary (获取家庭成员列表) + * @ApiMethod (GET) + * @ApiRoute (/api/Maintainentry/getFamily) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "获取成功", + * "time": "1668742364", + * "data": { + * "yz_num": 0, + * "crops_num": 2, + * "land_area": "5", + * "address_name": "龙马潭区石洞镇顺江村1组", + * "data": [ + * { + * "id": 18, + * "user_id": 22, + * "name": "王七", + * "age": 55, + * "gender": "男", + * "family_num": null, + * "political_outlook": "群众", + * "householder_id": 1, + * "is_hz": 0, + * "address_name": "龙马潭区石洞镇顺江村1组" + * }, + * { + * "id": 17, + * "user_id": 21, + * "name": "王七", + * "age": 55, + * "gender": "男", + * "family_num": null, + * "political_outlook": "群众", + * "householder_id": 1, + * "is_hz": 0, + * "address_name": "龙马潭区石洞镇顺江村1组" + * } + * ] + * } + * } + */ + public function getFamily() + { + //用户id + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiSuccess('缺少参数', null, '0'); + } +// 获取户主id + $where['user_id'] = $user_id; + $where['status'] = 1; + $householder_id = Db::table('fa_szxc_information_usermsg')->where($where)->value('householder_id'); + $name = Db::table('fa_szxc_information_usermsg')->where($where)->value('name'); + if ($householder_id) { + $map['householder_id'] = $householder_id; + $map['status'] = 1; + } else { + $map['user_id'] = $user_id; + $map['status'] = 1; + } +// 获取家庭的人员信息 + $res = Db::table('fa_szxc_information_usermsg')->where($map) + ->order('id desc') + ->field("id,user_id,name,age,gender,family_num,political_outlook,householder_id,is_hz,address_name,family_relation") + ->select()->toArray(); + if ($res){ + if($householder_id){ + $is_family = Db::table('fa_szxc_information_usermsg')->where('householder_id',$householder_id)->count(); + }else{ + $is_family = 1; + } + foreach ($res as $k=>$v){ + if($v['is_hz']==1){ + $res[$k]['householder_name'] = '本人'; + }else{ + $msg= Db::table('fa_szxc_information_usermsg')->where('user_id',$v['householder_id'])->value('name'); + $res[$k]['householder_name'] = $msg?:'暂无户主'; + } + if($v['householder_id']){ + $res[$k]['is_family'] = 1; + }else{ + $res[$k]['is_family'] = 0; + } + $is_planting = Db::table('fa_szxc_information_planting')->where('user_id',$v['user_id'])->field('id')->find(); + if($is_planting){ + $res[$k]['is_planting'] = 1; + }else{ + $res[$k]['is_planting'] = 0; + } + if($v['gender'] == 1){ + $res[$k]['gender'] = '男'; + }else{ + $res[$k]['gender'] = '女'; + } + $family_relationship = $v['family_relation']; + $family_relationship_name=Db::table('fa_category')->where('id',$family_relationship)->field('name')->find(); + $res[$k]['family_relationship']=$family_relationship_name?$family_relationship_name['name'] :''; + $political_outlook=Db::table('fa_category')->where('id',$v['political_outlook'])->field('name')->find(); + $res[$k]['political_outlook'] = $political_outlook?$political_outlook['name']:'无'; + } + } +// 获取种植信息 +// $www['user_id'] = $user_id; +// $planting = Db::table('fa_szxc_information_planting')->where($www)->field('id,crops_msg,land_area')->find(); +// if($planting && $planting['crops_msg']){ +// $crops_msg = json_decode($planting['crops_msg'], 1); +// $crops_msg = array_filter(array_column($crops_msg,'name')); +// }else{ +// $crops_msg = []; +// } + +// 获取养殖信息 + $www['user_id'] = $user_id; + $breed = Db::table('fa_szxc_breed')->where($www)->count(); + $crops_msg = Db::table('fa_szxc_planting')->where($www)->count(); + $return['yz_num'] = $breed; //养殖种类 + $return['crops_num'] = $crops_msg;//种植种类 + $return['land_area'] = $planting['land_area'] ??0;//土地面积 + $return['address_name'] = $res[0]['address_name'] ?? ''; + $return['name'] = $name; + $return['family_num'] = $is_family; + $return['data'] = $res; + $this->apiSuccess('获取成功', $return, 1); + } + + /** + * 获取个人信息 + * + * @ApiTitle (获取个人信息) + * @ApiSummary (获取个人信息) + * @ApiMethod (GET) + * @ApiRoute (/api/Maintainentry/getUserMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "获取成功", + * "time": "1668750751", + * "data": { + * "id": 17, + * "user_id": 21, + * "name": "王七", + * "age": 55, + * "gender": "男", + * "phone": "13666666666", + * "idcard": "510504185612345678", + * "nation": "汉族", + * "education": null, + * "occupation": null, + * "family_num": null, + * "political_outlook": "群众", + * "householder_id": 1, + * "is_hz": 0, + * "address_name": "龙马潭区石洞镇顺江村1组", + * "house_msg": null, + * "car_msg": null + * } + * } + */ + public function getUserMsg() + { + //用户id + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiSuccess('缺少参数', null, '0'); + } +//Db::table('fa_szxc_information_usermsg')->where($map) +// $map['user_id'] = $user_id; +// $map['status'] = 1; +// 获取人员信息 + $res = Db::table('fa_szxc_information_usermsg')->where('status',1)->where('user_id',$user_id) + ->field("id,user_id,name,age,gender,gender as gender_id,phone,idcard,nation,nation as nation_id, + education,education as education_id,occupation,occupation as occupation_id,family_num, + political_outlook,political_outlook as political_outlook_id,householder_id,is_hz,address_name,house_msg,house_msg as house_id, + car_msg,car_msg as car_id,family_relation,family_relation as family_relation_id,marital_status,marital_status as marriage_id") + ->withAttr('address_info',function ($data,$value){ + $find=Db::table('fa_szxc_information_useraddress')->where('user_id',$value['user_id'])->find(); + return $find; + }) + ->withAttr('gender',function ($data,$value){ + if ($value['gender']==1){ + return '男'; + }else{ + return '女'; + } + }) + ->withAttr('nation',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['nation'])->find(); + return $find?$find['name']:''; + }) + ->withAttr('family_relation',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['family_relation'])->find(); + return $find?$find['name']:''; + }) + ->withAttr('political_outlook',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['political_outlook'])->find(); + return $find?$find['name']:''; + }) + ->withAttr('education',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['education'])->find(); + return $find?$find['name']:''; + }) + ->withAttr('occupation',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['occupation'])->find(); + return $find?$find['name']:''; + }) + ->withAttr('house_msg',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['house_msg'])->find(); + return $find?$find['name']:''; + }) + ->withAttr('car_msg',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['car_msg'])->find(); + return $find?$find['name']:''; + }) + ->withAttr('marital_status',function ($data,$value){ + $find=Db::table('fa_category')->where('id',$value['marital_status'])->find(); + return $find?$find['name']:''; + }) + ->find(); + if ($res) { + $this->apiSuccess('获取成功', $res, 1); + } else { + $this->apiSuccess('暂无数据', null, '0'); + } + } + + /** + * 修改个人信息 + * + * @ApiTitle (修改个人信息) + * @ApiSummary (修改个人信息) + * @ApiMethod (POST) + * @ApiRoute (/api/Maintainentry/changeUserMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiParams (name="name", type="integer", required=true, description="姓名") + * @ApiParams (name="nation", type="integer", required=true, description="民族") + * @ApiParams (name="political_outlook", type="integer", required=true, description="政治面貌") + * @ApiParams (name="phone", type="integer", required=true, description="联系电话") + * @ApiParams (name="idcard", type="integer", required=true, description="身份证号") + * @ApiParams (name="gender", type="integer", required=true, description="性别") + * @ApiParams (name="age", type="integer", required=true, description="年龄") + * @ApiParams (name="address_name", type="integer", required=true, description="村组名称") + * @ApiParams (name="house_msg", type="integer", required=true, description="房屋情况") + * @ApiParams (name="car_msg", type="integer", required=true, description="车辆情况") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "修改成功", + * "time": "1668757016", + * "data": [] + * } + */ + public function changeUserMsg() + { + //用户id + $user_id = get_params('user_id'); + $post = get_params(); + if (empty($user_id)) { + $this->apiError('缺少参数'); + } + try { + validate('Maintainentry.edit')->check($post); + } catch (\Exception $e) { + $this->apiError($e->getMessage()); + } + $map['user_id'] = $user_id; + if ($post['householder_id']==$user_id) { + $data['is_hz']=1; + } +// 修改人员信息 + $data['name'] = $post['name']; + $data['nation'] = $post['nation_id']; + $data['political_outlook'] = $post['political_outlook_id']; + $data['phone'] = $post['phone']; + $data['idcard'] = $post['idcard']; + $data['gender'] = $post['gender_id']; + $data['age'] = $post['age']; +// $data['address_name'] = $post['address_name']; + $data['house_msg'] = $post['house_id']; + $data['car_msg'] = $post['car_id']; + $data['occupation'] = $post['occupation_id']; + $data['education'] = $post['education_id']; + $data['family_relation'] = $post['family_relation_id']; + $data['householder_id'] = empty($post['householder_id'])?0:$post['householder_id']; + $data['updatetime'] = time(); + $res = Db::table('fa_szxc_information_usermsg')->where($map)->update($data); + if ($res) { + $this->apiSuccess('修改成功', null, 1); + } else { + $this->apiSuccess('修改失败', null, '0'); + } + } + + /** + * 获取家庭信息 + * + * @ApiTitle (获取家庭信息) + * @ApiSummary (获取家庭信息) + * @ApiMethod (GET) + * @ApiRoute (/api/Maintainentry/getFamilyMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "获取成功", + * "time": "1668758641", + * "data": { + * "id": 1, + * "user_id": 1, + * "name": "张三", + * "age": 42, + * "gender": "男", + * "phone": null, + * "idcard": "1351351351", + * "householder_id": 1, + * "is_hz": 1, + * "address_name": "龙马潭区石洞镇顺江村1组", + * "family_relation": "父子" + * } + * } + */ + public function getFamilyMsg() + { + //用户id + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiSuccess('缺少参数', null, '0'); + } +// 获取户主id + $map['user_id'] = $user_id; + $map['status'] = 1; + $householder_id = Db::table('fa_szxc_information_usermsg')->where($map)->field('householder_id,family_relation')->find(); + if (empty($householder_id['householder_id'])) { + $this->apiSuccess('未绑定户主', null, 1); + } +// 获取户主信息 + $where['user_id'] = $householder_id['householder_id']; + $where['status'] = 1; + $res = Db::table('fa_szxc_information_usermsg')->where($where)->field("id,user_id,name,age,gender,phone,idcard,householder_id,is_hz,address_name")->find(); + if ($res) { + $res['family_relation'] = $householder_id['family_relation']; + $this->apiSuccess('获取成功', $res, 1); + } else { + $this->apiSuccess('暂无数据', null, '0'); + } + } + + /** + * 修改家庭信息 + * + * @ApiTitle (修改家庭信息) + * @ApiSummary (修改家庭信息) + * @ApiMethod (POST) + * @ApiRoute (/api/Maintainentry/changeFamilyMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiParams (name="householder_id", type="integer", required=true, description="户主id") + * @ApiParams (name="family_relation", type="integer", required=true, description="与户主关系") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "修改成功", + * "time": "1668757016", + * "data": [] + * } + */ + public function changeFamilyMsg() + { + $post = get_params(); + //用户id + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiSuccess('缺少参数', null, '0'); + } + $map['user_id'] = $user_id; +// 修改人员信息 + $data['householder_id'] = $post['householder_id']; + $data['family_relation'] = $post['family_relation']; + $data['updatetime'] = time(); + $res = Db::table('fa_szxc_information_usermsg')->where($map)->update($data); + if ($res) { + $this->apiSuccess('修改成功', null, 1); + } else { + $this->apiSuccess('修改失败', null, '0'); + } + } + + /** + * 获取种植信息 + * + * @ApiTitle (获取种植信息) + * @ApiSummary (获取种植信息) + * @ApiMethod (GET) + * @ApiRoute (/api/Maintainentry/getPlantingMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "获取成功", + * "time": "1668993420", + * "data": { + * "id": 7, + * "user_id": 21, + * "tools_msg": [ + * { + * "name": "水稻", + * "num": 20, + * "mianji": 2 + * }, + * { + * "name": "玉米", + * "num": 30, + * "mianji": 3 + * } + * ], + * "crops_msg": [ + * { + * "name": "水稻", + * "num": 20, + * "mianji": 2 + * }, + * { + * "name": "玉米", + * "num": 30, + * "mianji": 3 + * } + * ], + * "createtime": 1668736746, + * "land_area": "5", + * "is_wz": null, + * "nature_of_land": "国有", + * "work_name": "张三", + * "work_gender": "男", + * "work_phone": null, + * "work_relation": "本人" + * } + * } + */ + public function getPlantingMsg() + { + //用户id + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiError('缺少参数'); + } + +// 获取种植信息 + $where['user_id'] = $user_id; + $res = Db::table('fa_szxc_information_planting')->where($where)->field("id,user_id,tools_msg,crops_msg,createtime,land_area,is_wz,nature_of_land,work_name,work_gender,work_phone,work_relation,work_id")->find(); + if ($res) { + $find=Db::table('fa_category')->where('id',$res['nature_of_land'])->find(); + $res['nature_of_land_id']=$res['nature_of_land']; + $res['nature_of_land']=$find['name']; + $res['tools_msg'] = $res['tools_msg'] ? json_decode($res['tools_msg'], 1):[]; + $res['crops_msg'] = $res['crops_msg'] ? json_decode($res['crops_msg'], 1):[]; + $this->apiSuccess('获取成功', $res, 1); + } else { + $this->apiError('暂无数据'); + } + } + + /** + * 修改种植信息 + * + * @ApiTitle (修改种植信息) + * @ApiSummary (修改种植信息) + * @ApiMethod (POST) + * @ApiRoute (/api/Maintainentry/changePlantingMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiParams (name="tools_msg", type="integer", required=true, description="农机农具信息") + * @ApiParams (name="crops_msg", type="integer", required=true, description="农作物信息") + * @ApiParams (name="land_area", type="integer", required=true, description="土地面积") + * @ApiParams (name="nature_of_land", type="integer", required=true, description="土地性质") + * @ApiParams (name="is_wz", type="integer", required=true, description="土地外租") + * @ApiParams (name="work_name", type="integer", required=true, description="劳动人姓名") + * @ApiParams (name="work_gender", type="integer", required=true, description="劳动人口性别") + * @ApiParams (name="work_phone", type="integer", required=false, description="劳动人联系方式") + * @ApiParams (name="work_relation", type="integer", required=true, description="劳动人与户主关系") + * @ApiParams (name="work_id", type="integer", required=true, description="劳动人id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "修改成功", + * "time": "1668757016", + * "data": [] + * } + */ + public function changePlantingMsg() + { + $post = get_params(); + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiError('缺少参数'); + } + $time = time(); + $map[] = ['user_id','=',$user_id]; + $find=Db::table('fa_szxc_information_planting')->where($map)->find(); + // 新增写入种植表 + Db::table('fa_szxc_planting')->where('user_id',$user_id)->delete(); + $mianji = 0; + if($post['crops_msg']){ + $crops_msg_arr = json_decode($post['crops_msg'],1); + foreach ($crops_msg_arr as $k=>$v){ + // 根据名字查询分类 + if($v['name']){ + $type = Db::table('fa_szxc_planting_type')->where('name',$v['name'])->find(); + if($type){ + $planting_data['type_id'] = $type['id']; + }else{ + $type_data['name'] = $v['name']; + $type_data['createtime'] = $time; + $type_id = Db::table('fa_szxc_planting_type')->strict(false)->insertGetId($type_data); + $planting_data['type_id'] = $type_id; + } + $planting_data['user_id'] = $user_id; + $planting_data['num'] = $v['num']??0; + $planting_data['measure'] = $v['mianji']??0; + $planting_data['createtime'] = $time; + Db::table('fa_szxc_planting')->strict(false)->insertGetId($planting_data); + } + // 计算面积 + if($v['mianji']){ + $mianji .= $v['mianji']; + } + } + } + + // 新增写入养殖表 + Db::table('fa_szxc_breed')->where('user_id',$user_id)->delete(); +// $mianji = 0; + if(isset($post['breed_msg']) && !empty($post['breed_msg'])){ + $breed_msg_arr = json_decode($post['breed_msg'],1); + foreach ($breed_msg_arr as $k=>$v){ +// 根据名字查询分类 + if($v['name']){ + $type = Db::table('fa_szxc_breed_type')->where('name',$v['name'])->find(); + if($type){ + $breed_data['type_id'] = $type['id']; + }else{ + $type_data['name'] = $v['name']; + $type_data['createtime'] = $time; + $type_id = Db::table('fa_szxc_breed_type')->strict(false)->insertGetId($type_data); + $breed_data['type_id'] = $type_id; + } + $breed_data['user_id'] = $user_id; + $breed_data['num'] = $v['num']??0; + $breed_data['measure'] = $v['mianji']??0; + $breed_data['createtime'] = $time; + Db::table('fa_szxc_breed')->strict(false)->insertGetId($breed_data); + } +//// 计算面积 +// if($v['mianji']){ +// $mianji .= $v['mianji']; +// } + } + } + + $waste_land_area = $post['land_area'] - $mianji; + if($waste_land_area < 0){ + $waste_land_area = 0; + } + + if ($find) { + // 修改人员信息 + $data['tools_msg'] = $post['tools_msg']; + $data['breed_msg'] = $post['breed_msg']??''; + $data['breed_area'] = $post['breed_area']??''; + $data['crops_msg'] = $post['crops_msg']; + $data['land_area'] = $post['land_area']; + $data['nature_of_land'] = $post['nature_of_land_id']; + $data['is_wz'] = $post['is_wz']; +// $data['work_name'] = $post['work_name']; +// $data['work_gender'] = $post['work_gender']; +// $data['work_phone'] = $post['work_phone'] ?? ''; +// $data['work_relation'] = $post['work_relation']; +// $data['work_id'] = $post['work_id']; + $data['waste_land_area'] = $waste_land_area; + $data['user_id'] = $user_id; + $data['updatetime'] = time(); + $res = Db::table('fa_szxc_information_planting')->where($map)->update($data); + if ($res) { + $this->apiSuccess('修改成功', null, 1); + } else { + $this->apiError('修改失败'); + } + }else{ + // 修改人员信息 + $data['tools_msg'] = $post['tools_msg']; + $data['breed_msg'] = $post['breed_msg']??''; + $data['breed_area'] = $post['breed_area']??''; + $data['crops_msg'] = $post['crops_msg']; + $data['land_area'] = $post['land_area']; + $data['nature_of_land'] = $post['nature_of_land_id']; + $data['is_wz'] = $post['is_wz']; +// $data['work_name'] = $post['work_name']; +// $data['work_gender'] = $post['work_gender']; +// $data['work_phone'] = $post['work_phone'] ?? ''; +// $data['work_relation'] = $post['work_relation']; +// $data['work_id'] = $post['work_id']; + $data['waste_land_area'] = $waste_land_area; + $data['user_id'] = $user_id; + + $data['createtime'] = time(); + $res = Db::table('fa_szxc_information_planting')->create($data); + if ($res) { + $this->apiSuccess('保存成功', null, 1); + } else { + $this->apiError('保存失败'); + } + } + + } + + /** + * 获取保险信息 + * + * @ApiTitle (获取保险信息) + * @ApiSummary (获取保险信息) + * @ApiMethod (GET) + * @ApiRoute (/api/Maintainentry/getInsuranceMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "获取成功", + * "time": 1669019298, + * "data": { + * "id": 1, + * "user_id": 3, + * "endowment_insurance_status": "已买", + * "is_subsidy": "是", + * "insurance_reason": "", + * "medical_insurance_type": "农村医保", + * "whether_disabled": "否", + * "disability_num": "", + * "disability_type": "", + * "disability_level": "", + * "insurance_type": "农村社保", + * "ny_insurance": "是", + * "sy_insurance": "是" + * } + * } + */ + public function getInsuranceMsg() + { + //用户id + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiError('缺少参数'); + } +// 获取种植信息 + $where['user_id'] = $user_id; + $res = Db::table('fa_szxc_information_insurance')->where($where) + ->withAttr('endowment_insurance_status',function($value,$data){ + if($value==1){ + $find='已交'; + }elseif($value==2){ + $find='未交'; + }else{ + $find='代缴'; + } + return $find; + }) + ->withAttr('insurance_type',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('whether_disabled',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('disability_type',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('disability_level',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('ny_insurance',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('sy_insurance',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->field("id,user_id,endowment_insurance_status,endowment_insurance_status as endowment_insurance_status_id,is_subsidy,insurance_reason,insurance_type,insurance_type as insurance_type_id, + whether_disabled,whether_disabled as whether_disabled_id,disability_num,disability_type,disability_type as disability_type_id,disability_level,disability_level as disability_level_id,ny_insurance,ny_insurance as ny_insurance_id, + sy_insurance,sy_insurance as sy_insurance_id") + ->find(); + if ($res) { + $this->apiSuccess('获取成功', $res, 1); + } else { + $this->apiError('暂无数据'); + } + } + + /** + * 修改保险信息 + * + * @ApiTitle (修改保险信息) + * @ApiSummary (修改保险信息) + * @ApiMethod (POST) + * @ApiRoute (/api/Maintainentry/changeInsuranceMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiParams (name="endowment_insurance_status", type="integer", required=true, description="养老保险状态") + * @ApiParams (name="is_subsidy", type="integer", required=true, description="是否领取社保补贴") + * @ApiParams (name="insurance_reason", type="integer", required=false, description="参保原因") + * @ApiParams (name="medical_insurance_type", type="integer", required=true, description="医保类型") + * @ApiParams (name="whether_disabled", type="integer", required=true, description="是否残疾") + * @ApiParams (name="disability_num", type="integer", required=fasle, description="残疾证号") + * @ApiParams (name="disability_type", type="integer", required=fasle, description="残疾类别") + * @ApiParams (name="disability_level", type="integer", required=fasle, description="残疾等级") + * @ApiParams (name="insurance_type", type="integer", required=true, description="参保类型") + * @ApiParams (name="ny_insurance", type="integer", required=true, description="农业保险") + * @ApiParams (name="sy_insurance", type="integer", required=true, description="商业保险") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "修改成功", + * "time": "1668757016", + * "data": [] + * } + */ + public function changeInsuranceMsg() + { + $post = get_params(); + $user_id = get_params('user_id'); + +// try { +// validate('Maintainentry.insurancemsg')->check($post); +// } catch (\Exception $e) { +// $this->apiError($e->getMessage()); +// } + + if (empty($user_id)) { + $this->apiError('缺少参数'); + } + $map['user_id'] = $user_id; + $is_have = Db::table('fa_szxc_information_insurance')->where($map)->find(); + if ($is_have) { + // 修改信息 + $data['endowment_insurance_status'] = $post['endowment_insurance_status_id']?? ''; + $data['is_subsidy'] = 0; + $data['insurance_reason'] = $post['insurance_reason'] ?? ''; +// $data['insurance_type'] = $post['insurance_type_id']; +// $data['whether_disabled'] = $post['whether_disabled']; + $data['disability_num'] = $post['disability_num'] ?? ''; + $data['disability_type'] = $post['disability_type_id']?? ''; + $data['disability_level'] = $post['disability_level_id']?? ''; + $data['insurance_type'] = $post['insurance_type_id']?? ''; + $data['ny_insurance'] = $post['ny_insurance_id']?? ''; + $data['sy_insurance'] = $post['sy_insurance_id']?? ''; + $data['updatetime'] = time(); + $res = Db::table('fa_szxc_information_insurance')->where($map)->update($data); + if ($res) { + $this->apiSuccess('修改成功', null, 1); + } else { + $this->apiError('修改失败'); + } + } else { //新增 + $data['endowment_insurance_status'] = $post['endowment_insurance_status_id']?? ''; + $data['is_subsidy'] = 0; + $data['insurance_reason'] = $post['insurance_reason'] ?? ''; +// $data['whether_disabled'] = $post['whether_disabled']; + $data['disability_num'] = $post['disability_num'] ?? ''; + $data['disability_type'] = $post['disability_type_id']?? ''; + $data['disability_level'] = $post['disability_level_id']?? ''; + $data['insurance_type'] = $post['insurance_type_id']?? ''; + $data['ny_insurance'] = $post['ny_insurance_id']?? ''; + $data['sy_insurance'] = $post['sy_insurance_id']?? ''; + $data['createtime'] = time(); + $data['user_id'] = $post['user_id']; + $res = Db::table('fa_szxc_information_insurance')->strict(false)->insert($data); + if ($res) { + $this->apiSuccess('修改成功', null, 1); + } else { + $this->apiError('修改失败'); + } + } + } + + + /** + * 获取健康信息 + * + * @ApiTitle (获取健康信息) + * @ApiSummary (获取健康信息) + * @ApiMethod (GET) + * @ApiRoute (/api/Maintainentry/getHealthyMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "获取成功", + * "time": 1669019298, + * "data": { + * "id": 1, + * "user_id": 3, + * "endowment_insurance_status": "已买", + * "is_subsidy": "是", + * "insurance_reason": "", + * "medical_insurance_type": "农村医保", + * "whether_disabled": "否", + * "disability_num": "", + * "disability_type": "", + * "disability_level": "", + * "insurance_type": "农村社保", + * "ny_insurance": "是", + * "sy_insurance": "是" + * } + * } + */ + public function getHealthyMsg() + { + //用户id + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiError('缺少参数'); + } +// 获取种植信息 + $where['user_id'] = $user_id; + $res = Db::table('fa_szxc_information_healthy')->where($where) + ->withAttr('blood_type',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('drug_allergy',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('exposure_history',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('history_of_disease',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('genetic_name',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('cfpfss',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('poultry_corral',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('toilet_msg',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('family_history',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('fuel_type',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('drinking_water',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('familial_diseases',function($value,$data){ + $find=Db::table('fa_category')->where('id',$value)->value('name'); + return $find; + }) + ->withAttr('history_of_surgery',function($value,$data){ + if($value==1){ + $find='有'; + }else{ + $find='无'; + } + return $find; + }) + ->field("id,user_id,medical_insurance_type,insurance_type,blood_type,blood_type as blood_type_id,drug_allergy,drug_allergy as drug_allergy_id,exposure_history,exposure_history as exposure_history_id,history_of_disease,history_of_disease as history_of_disease_id, + history_of_surgery,history_of_surgery as history_of_surgery_id,family_history,family_history as family_history_id,familial_diseases,familial_diseases as familial_diseases_id,genetic_history,genetic_name,genetic_name as genetic_name_id,whether_disabled,cfpfss,cfpfss as cfpfss_id,fuel_type,fuel_type as fuel_type_id,drinking_water,drinking_water as drinking_water_id, + toilet_msg,toilet_msg as toilet_msg_id,poultry_corral,poultry_corral as poultry_corral_id") + ->find(); + if ($res) { + $this->apiSuccess('获取成功', $res, 1); + } else { + $this->apiError('暂无数据'); + } + } + + + /** + * 修改健康信息 + * + * @ApiTitle (修改健康信息) + * @ApiSummary (修改健康信息) + * @ApiMethod (POST) + * @ApiRoute (/api/Maintainentry/changeHealthyMsg) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="user_id", type="integer", required=true, description="用户id") + * @ApiParams (name="endowment_insurance_status", type="integer", required=true, description="养老保险状态") + * @ApiParams (name="is_subsidy", type="integer", required=true, description="是否领取社保补贴") + * @ApiParams (name="insurance_reason", type="integer", required=false, description="参保原因") + * @ApiParams (name="medical_insurance_type", type="integer", required=true, description="医保类型") + * @ApiParams (name="whether_disabled", type="integer", required=true, description="是否残疾") + * @ApiParams (name="disability_num", type="integer", required=fasle, description="残疾证号") + * @ApiParams (name="disability_type", type="integer", required=fasle, description="残疾类别") + * @ApiParams (name="disability_level", type="integer", required=fasle, description="残疾等级") + * @ApiParams (name="insurance_type", type="integer", required=true, description="参保类型") + * @ApiParams (name="ny_insurance", type="integer", required=true, description="农业保险") + * @ApiParams (name="sy_insurance", type="integer", required=true, description="商业保险") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn { + * "code": 1, + * "msg": "修改成功", + * "time": "1668757016", + * "data": [] + * } + */ + public function changeHealthyMsg() + { + $post = get_params(); + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiError('缺少参数'); + } + + $map['user_id'] = $user_id; + $is_have = Db::table('fa_szxc_information_healthy')->where($map)->find(); + if ($is_have) { + // 修改信息 +// $data['medical_insurance_type'] = $post['medical_insurance_type']; +// $data['insurance_type'] = $post['insurance_type']; + $data['blood_type'] = $post['blood_type_id']?? ''; + $data['drug_allergy'] = $post['drug_allergy_id']?? ''; + $data['exposure_history'] = $post['exposure_history_id']?? ''; + $data['history_of_disease'] = $post['history_of_disease_id']?? ''; + $data['history_of_surgery'] = $post['history_of_surgery_id']?? ''; + $data['family_history'] = $post['family_history_id']?? ''; + $data['familial_diseases'] = $post['familial_diseases_id']?? ''; + $data['genetic_history'] = 0; + $data['genetic_name'] = $post['genetic_name_id']?? ''; +// $data['whether_disabled'] = $post['whether_disabled']; + $data['cfpfss'] = $post['cfpfss_id']?? ''; + $data['fuel_type'] = $post['fuel_type_id']?? ''; + $data['drinking_water'] = $post['drinking_water_id']?? ''; + $data['toilet_msg'] = $post['toilet_msg_id']?? ''; + $data['poultry_corral'] = $post['poultry_corral_id']?? ''; + $data['updatetime'] = time(); + $res = Db::table('fa_szxc_information_healthy')->where($map)->update($data); + if ($res) { + $this->apiSuccess('修改成功', null, 1); + } else { + $this->apiError('修改失败'); + } + } else { //新增 + $data['blood_type'] = $post['blood_type_id']?? ''; + $data['drug_allergy'] = $post['drug_allergy_id']?? ''; + $data['exposure_history'] = $post['exposure_history_id']?? ''; + $data['history_of_disease'] = $post['history_of_disease_id']?? ''; + $data['history_of_surgery'] = $post['history_of_surgery_id']?? ''; + $data['family_history'] = $post['family_history_id']?? ''; + $data['familial_diseases'] = $post['familial_diseases_id']?? ''; + $data['genetic_history'] = 0; + $data['genetic_name'] = $post['genetic_name_id']?? ''; +// $data['whether_disabled'] = $post['whether_disabled']; + $data['cfpfss'] = $post['cfpfss_id']?? ''; + $data['fuel_type'] = $post['fuel_type_id']?? ''; + $data['drinking_water'] = $post['drinking_water_id']?? ''; + $data['toilet_msg'] = $post['toilet_msg_id']?? ''; + $data['poultry_corral'] = $post['poultry_corral_id']?? ''; + $data['createtime'] = time(); + $data['user_id'] = $post['user_id']; + $res = Db::table('fa_szxc_information_healthy')->strict(false)->insert($data); + if ($res) { + $this->apiSuccess('修改成功',null, 1); + } else { + $this->apiError('修改失败'); + } + } + } + + + /** + * 新增录入接口 + * + * @ApiTitle (新增录入接口) + * @ApiSummary (新增录入接口) + * @ApiMethod (POST) + * @ApiRoute (/api/Maintainentry/newEntry) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="page", type="integer", required=true, description="页数") + * @ApiParams (name="name", type="string", required=false, description="用户名") + * @ApiParams (name="limit", type="string", required=false, description="每页条数") + * @ApiParams (name="idcard", type="string", required=false, description="身份证号") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + 'code':'1', + 'msg':'返回成功' + }) + */ + public function newEntry() + { + $post = get_params(); + try { + validate('Maintainentry.add')->check($post); + } catch (\Exception $e) { + $this->apiError($e->getMessage()); + } + + if ($post) { + Db::startTrans(); + // 获取录入人地址 + $add = Db::table('fa_szxc_information_useraddress')->where('user_id',JWT_UID)->find(); + if(empty($add)){ + $this->apiError('录入出错,请联系系统管理员'); + } + // 判断手机号是否已经注册 + $is_mobile = User::getByMobile($post['phone']); + if ($is_mobile) { + $this->apiError('手机号已被使用'); + } + + //判断身份证号是否已被使用 + $InformationUsermsg = Db::table('fa_szxc_information_usermsg')->where('idcard',$post['idcard'])->field('idcard')->find(); + if ($InformationUsermsg) { + $this->apiError('身份证号已被使用'); + } + + // 组装地址名 + if ($add['area_id'] != '') { + $area_name = Db::table('fa_geo_area')->where('area_code', $add['area_id'])->value('area_name'); + } + if ($add['street_id'] != '') { + $street_name = Db::table('fa_geo_street')->where('street_code', $add['street_id'])->value('street_name'); + } + if ($add['village_id'] != '') { + $village = Db::table('fa_geo_village')->where('village_code', $add['village_code'])->value('village_name'); + } + if ($add['brigade_id'] != '') { + $brigade_name = Db::table('fa_geo_brigade')->where('id', $add['brigade_id'])->value('brigade_name'); + } + $post['address_name'] = $area_name.$street_name.$village.$brigade_name; + $village_code = $add['village_code']; + + try { + $mobile = $post['phone']; + $ip = request()->ip(); + $time = time(); + $salt = Random::alnum(); //随机字符串 +// 注册用户 + $params = [ + 'username' => $mobile, + 'nickname' => $post['name'], + 'group_id' => 1, + 'password' => $this->getEncryptPassword('', $salt), + 'email' => '', + 'mobile' => $mobile, + 'level' => 1, + 'score' => 0, + 'avatar' => 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230111/58c84995456bc63b2660d9526f7062fc.png', + 'salt' => $salt, + 'jointime' => $time, + 'joinip' => $ip, + 'logintime' => $time, + 'loginip' => $ip, + 'prevtime' => $time, + 'status' => 'normal', + 'createtime' => $time, + 'updatetime' => $time + ]; + //写入用户表 + $userid = User::strict(false)->insertGetId($params); + + // 写入商城关系表 + $nk_user['n_user_id'] = $userid; + $is_user_phone = Db::connect('shop')->name('user')->where('phone',$mobile)->find(); + if(!$is_user_phone){ + Db::connect('shop')->name('nk_user')->insert($nk_user); + }else{ + $is_user_id = Db::connect('shop')->name('nk_user')->where('user_id',$is_user_phone['uid'])->find(); + if($is_user_id){ + Db::connect('shop')->name('nk_user')->where('user_id',$is_user_phone['uid'])->update($nk_user); + }else{ + $nk_user['user_id'] = $is_user_phone['uid']; + Db::connect('shop')->name('nk_user')->insert($nk_user); + } + } + + //写入用户信息表 + $post['user_id'] = $userid; + $post['createtime'] = $time; + $post['enter_id'] = JWT_UID; + $post['gender']= $post['gender_id']; + $post['nation']=$post['nation_id']; + $post['political_outlook']=$post['zzmm_id']; + $post['marital_status']=$post['marriage_id']; + $post['area_id'] = $add['area_id']; + $post['street_id'] = $add['street_id']; + $post['village_id'] = $add['village_id']; + $post['brigade_id'] = $add['brigade_id']; + if($post['is_hz']==1){ + $post['householder_id']=$userid; + } + Db::table('fa_szxc_information_usermsg')->strict(false)->insert($post); + + //写入用户地区表 + $address_data['user_id'] = $userid; + $address_data['area_id'] = $add['area_id']; + $address_data['street_id'] = $add['street_id']; + $address_data['village_id'] = $add['village_id']; + $address_data['brigade_id'] = $add['brigade_id']; + $address_data['village_code'] =$village_code; + $address_data['createtime'] = date('Y-m-d H:i:s'); + Db::table('fa_szxc_information_useraddress')->strict(false)->insert($address_data); + + // if($post['is_hz'] == 0){ + //写入家庭关系表 + $family_relations['user_id'] = $userid; //用户id + $family_relations['householder_id'] = $post['householder_id']; //户主id + $family_relations['family_name'] = $post['name']; //家庭成员姓名 + $family_relations['family_relationship'] = $post['family_relation'];; //家庭成员与户主关系 + $family_relations['family_gender'] = $post['gender_id']; //家庭成员性别 + $family_relations['family_phone'] = $mobile; //家庭成员联系方式 + $family_relations['family_age'] = $post['age']; //家庭成员年龄 + $family_relations['createtime'] = $time;//创建时间 + Db::table('fa_szxc_family_relations')->strict(false)->insert($family_relations); + // } + + // 写入家庭成员表 + $my_user_id = $post['householder_id']; + $f_where[] = ['user_ids','find in set',$my_user_id]; + $family_arr = Db::table('fa_szxc_family')->where($f_where)->find(); + if($family_arr){ + $u_data['user_ids'] = \think\facade\Db::raw("concat(user_ids ,"."',".$userid."')"); + $u_data['updatetime'] = $time; + Db::table('fa_szxc_family')->where('id',$family_arr['id'])->update($u_data); + }else{ + $szxc_family['user_ids'] = $my_user_id.','.$userid; + $szxc_family['createtime'] = $time; + Db::table('fa_szxc_family')->strict(false)->insert($szxc_family); + } + + //判断土地是否外租,0没有外租 + if ($post['is_waizu'] == 0) { + + // 新增写入种植表 + Db::table('fa_szxc_planting')->where('user_id',$userid)->delete(); + $mianji = 0; + if($post['crops_msg']){ + $crops_msg_arr = json_decode($post['crops_msg'],1); + foreach ($crops_msg_arr as $k=>$v){ +// 根据名字查询分类 + if($v['name']){ + $type = Db::table('fa_szxc_planting_type')->where('name',$v['name'])->find(); + if($type){ + $planting_data['type_id'] = $type['id']; + }else{ + $type_data['name'] = $v['name']; + $type_data['createtime'] = $time; + $type_id = Db::table('fa_szxc_planting_type')->strict(false)->insertGetId($type_data); + $planting_data['type_id'] = $type_id; + } + $planting_data['user_id'] = $userid; + $planting_data['num'] = $v['num']??0; + $planting_data['measure'] = $v['mianji']??0; + $planting_data['createtime'] = $time; + Db::table('fa_szxc_planting')->strict(false)->insertGetId($planting_data); + } +// 计算面积 + if($v['mianji']){ + $mianji .= $v['mianji']; + } + } + } + + // 新增写入养殖表 + Db::table('fa_szxc_breed')->where('user_id',$userid)->delete(); + if(isset($post['breed_msg']) && !empty($post['breed_msg'])){ + $breed_msg_arr = json_decode($post['breed_msg'],1); + foreach ($breed_msg_arr as $k=>$v){ +// 根据名字查询分类 + if($v['name']){ + $type = Db::table('fa_szxc_breed_type')->where('name',$v['name'])->find(); + if($type){ + $breed_data['type_id'] = $type['id']; + }else{ + $type_data['name'] = $v['name']; + $type_data['createtime'] = $time; + $type_id = Db::table('fa_szxc_breed_type')->strict(false)->insertGetId($type_data); + $breed_data['type_id'] = $type_id; + } + $breed_data['user_id'] = $userid; + $breed_data['num'] = $v['num']??0; + $breed_data['measure'] = $v['mianji']??0; + $breed_data['createtime'] = $time; + Db::table('fa_szxc_breed')->strict(false)->insertGetId($breed_data); + } +//// 计算面积 +// if($v['mianji']){ +// $mianji .= $v['mianji']; +// } + } + } + + $waste_land_area = $post['land_area'] - $mianji; + if($waste_land_area < 0){ + $waste_land_area = 0; + } + // 写入种植信息表 + $cropsarr['user_id'] = $userid; //用户id + $cropsarr['tools_msg'] = $post['tools_msg'];//农机农具信息 + $cropsarr['crops_msg'] = $post['crops_msg'];//农作物信息 + $cropsarr['land_area'] = $post['land_area'];//土地面积 + $cropsarr['nature_of_land'] = $post['nature_of_land'];//土地性质 +// $cropsarr['work_name'] = $post['work_name'];//劳动人姓名 +// $cropsarr['work_gender'] = $post['work_gender'];//劳动人口性别 +// $cropsarr['work_relation'] = $post['work_relation'];//劳动人与户主关系 +// $cropsarr['work_id'] = $post['work_id'];//劳动人id + $cropsarr['waste_land_area'] = $waste_land_area;//荒地面积 + $cropsarr['breed_msg'] = $post['breed_msg']??'';//养殖详情信息 + $cropsarr['breed_area'] = $post['breed_area']??'';//养殖面积 + $cropsarr['createtime'] = $time;//创建时间 + Db::table('fa_szxc_information_planting')->strict(false)->insert($cropsarr); + + } + Db::commit(); + $this->apiSuccess('录入成功',null, 1); + } catch (ValidateException | PDOException | Exception $e) { + Db::rollback(); + $this->apiError($e->getMessage()); + } + } else { + $this->apiError('缺少参数'); + } + } + + /** + * 获取密码加密后的字符串 + * @param string $password 密码 + * @param string $salt 密码盐 + * @return string + */ + public function getEncryptPassword($password, $salt = '') + { + return md5(md5($password) . $salt); + } + + /** + * 获取分类 + * + */ + public function getcategory() + { +// 分类标识 + $type = get_params('type'); + if (empty($type)) { + $this->apiError('缺少参数'); + } else { + $where['type'] = $type; + $where['status'] = 'normal'; + $result = Db::table('fa_category')->where($where)->field('id,name,weigh')->order('weigh desc')->select(); + if ($result) { + $this->apiSuccess('获取成功', $result, 1); + } else { + $this->apiError('暂无数据'); + } + } + } + + + /** + * 用户新增录入接口 + * + * @ApiTitle (用户新增录入接口) + * @ApiSummary (用户新增录入接口) + * @ApiMethod (POST) + * @ApiRoute (/api/Maintainentry/userEntry) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="page", type="integer", required=true, description="页数") + * @ApiParams (name="name", type="string", required=false, description="用户名") + * @ApiParams (name="limit", type="string", required=false, description="每页条数") + * @ApiParams (name="idcard", type="string", required=false, description="身份证号") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + 'code':'1', + 'msg':'返回成功' + }) + */ + public function userEntry() + { + $post = get_params(); + try { + validate('Maintainentry.myadd')->check($post); + } catch (\Exception $e) { + $this->apiError($e->getMessage()); + } + + if ($post) { +//// 验证验证码 +// if (! Sms::check($post['phone'], $post['captcha'], 'mobilelogin')) { +// $this->apiError(__('Captcha is incorrect')); +// } + + Db::startTrans(); + // 获取录入人地址 + $add = Db::table('fa_szxc_information_useraddress')->where('user_id',JWT_UID)->find(); + if(empty($add)){ + $this->apiError('录入出错,请联系系统管理员'); + } + // 判断手机号是否已经注册 + $is_mobile = User::getByMobile($post['phone']); + if ($is_mobile) { + $this->apiError('手机号已被使用'); + } + + //判断身份证号是否已被使用 + $InformationUsermsg = Db::table('fa_szxc_information_usermsg')->where('idcard',$post['idcard'])->field('idcard')->find(); + if ($InformationUsermsg) { + $this->apiError('身份证号已被使用'); + } + + // 组装地址名 + if ($add['area_id'] != '') { + $area_name = Db::table('fa_geo_area')->where('area_code', $add['area_id'])->value('area_name'); + } + if ($add['street_id'] != '') { + $street_name = Db::table('fa_geo_street')->where('street_code', $add['street_id'])->value('street_name'); + } + if ($add['village_id'] != '') { + $village = Db::table('fa_geo_village')->where('village_code', $add['village_code'])->value('village_name'); + } + if ($add['brigade_id'] != '') { + $brigade_name = Db::table('fa_geo_brigade')->where('id', $add['brigade_id'])->value('brigade_name'); + } + $post['address_name'] = $area_name.$street_name.$village.$brigade_name; + $village_code = $add['village_code']; + + try { + $mobile = $post['phone']; + $ip = request()->ip(); + $time = time(); + $salt = Random::alnum(); //随机字符串 +// 注册用户 + $params = [ + 'username' => $mobile, + 'nickname' => $post['name'], + 'group_id' => 1, + 'password' => $this->getEncryptPassword('', $salt), + 'email' => '', + 'mobile' => $mobile, + 'level' => 1, + 'score' => 0, + 'avatar' => 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230111/58c84995456bc63b2660d9526f7062fc.png', + 'salt' => $salt, + 'jointime' => $time, + 'joinip' => $ip, + 'logintime' => $time, + 'loginip' => $ip, + 'prevtime' => $time, + 'status' => 'normal', + 'createtime' => $time, + 'updatetime' => $time + ]; + //写入用户表 + $userid = User::strict(false)->insertGetId($params); + + // 写入商城关系表 + $nk_user['n_user_id'] = $userid; + $is_user_phone = Db::connect('shop')->name('user')->where('phone',$mobile)->find(); + if(!$is_user_phone){ + Db::connect('shop')->name('nk_user')->insert($nk_user); + }else{ + $is_user_id = Db::connect('shop')->name('nk_user')->where('user_id',$is_user_phone['uid'])->find(); + if($is_user_id){ + Db::connect('shop')->name('nk_user')->where('user_id',$is_user_phone['uid'])->update($nk_user); + }else{ + $nk_user['user_id'] = $is_user_phone['uid']; + Db::connect('shop')->name('nk_user')->insert($nk_user); + } + } + + //写入用户信息表 + $post['user_id'] = $userid; + $post['createtime'] = $time; + $post['enter_id'] = JWT_UID; + $post['gender']= $post['gender_id']; + $post['nation']=$post['nation_id']; + $post['political_outlook']=$post['zzmm_id']; + $post['marital_status']=$post['marriage_id']; + $post['area_id'] = $add['area_id']; + $post['street_id'] = $add['street_id']; + $post['village_id'] = $add['village_id']; + $post['brigade_id'] = $add['brigade_id']; + Db::table('fa_szxc_information_usermsg')->strict(false)->insert($post); + + //写入用户地区表 + $address_data['user_id'] = $userid; + $address_data['area_id'] = $add['area_id']; + $address_data['street_id'] = $add['street_id']; + $address_data['village_id'] = $add['village_id']; + $address_data['brigade_id'] = $add['brigade_id']; + $address_data['village_code'] =$village_code; + $address_data['createtime'] = date('Y-m-d H:i:s'); + Db::table('fa_szxc_information_useraddress')->strict(false)->insert($address_data); + +// 写入家庭成员表 + $my_user_id = JWT_UID; + $f_where[] = ['user_ids','find in set',$my_user_id]; + $family_arr = Db::table('fa_szxc_family')->where($f_where)->find(); + if($family_arr){ + $u_data['user_ids'] = \think\facade\Db::raw("concat(user_ids ,"."',".$userid."')"); + $u_data['updatetime'] = $time; + Db::table('fa_szxc_family')->where('id',$family_arr['id'])->update($u_data); +// 如果已经设置户主,更新当前人员户主ID + if($family_arr['householder_id']){ + $h_data['householder_id'] = $family_arr['householder_id']; + Db::table('fa_szxc_information_usermsg')->where('user_id',$userid)->update($h_data); + } + }else{ + $szxc_family['user_ids'] = $my_user_id.','.$userid; + $szxc_family['createtime'] = $time; + Db::table('fa_szxc_family')->strict(false)->insert($szxc_family); + } + + Db::commit(); + $this->apiSuccess('录入成功',null, 1); + } catch (ValidateException | PDOException | Exception $e) { + Db::rollback(); + $this->apiError($e->getMessage()); + } + } else { + $this->apiError('缺少参数'); + } + } + +// 获取我的家庭成员 + public function getmyFamily(){ + //用户id + $user_id = JWT_UID; + +// 获取户主id + $f_where[] = ['user_ids','find in set',$user_id]; + $family_arr = Db::table('fa_szxc_family')->where($f_where)->find(); + if ($family_arr) { + $map[] = ['user_id','in',$family_arr['user_ids']]; + } else { + $where['user_id'] = $user_id; + $where['status'] = 1; + $householder_id = Db::table('fa_szxc_information_usermsg')->where($where)->value('householder_id'); + if ($householder_id) { + $map[] = ['householder_id','=',$householder_id]; + $map[] = ['status','=',1]; + } else { + $map[] = ['user_id','=',$user_id]; + $map[] = ['status','=',1]; + } + } +// 获取家庭的人员信息 + $res = Db::table('fa_szxc_information_usermsg')->where($map) + ->order('id desc') + ->field("id,user_id,name,age,gender,family_num,political_outlook,householder_id,is_hz,address_name,family_relation") + ->select()->toArray(); + if ($res){ + // 获取户主id + $where['user_id'] = $user_id; + $where['status'] = 1; + $name = Db::table('fa_szxc_information_usermsg')->where($where)->value('name'); + $is_set_hz = 0; + foreach ($res as $k=>$v){ + if($v['is_hz']==1){ + $is_set_hz = 1; + $res[$k]['householder_name'] = '本人'; + }else{ + $msg= Db::table('fa_szxc_information_usermsg')->where('user_id',$v['householder_id'])->value('name'); + $res[$k]['householder_name'] = $msg?:'暂无户主'; + } + if($v['householder_id']){ + $res[$k]['is_family'] = 1; + }else{ + $res[$k]['is_family'] = 0; + } + $is_planting = Db::table('fa_szxc_information_planting')->where('user_id',$v['user_id'])->field('id')->find(); + if($is_planting){ + $res[$k]['is_planting'] = 1; + }else{ + $res[$k]['is_planting'] = 0; + } + if($v['gender'] == 1){ + $res[$k]['gender'] = '男'; + }else{ + $res[$k]['gender'] = '女'; + } + $family_relationship = $v['family_relation']; + $family_relationship_name=Db::table('fa_category')->where('id',$family_relationship)->field('name')->find(); + $res[$k]['family_relationship']=$family_relationship_name?$family_relationship_name['name'] :''; + $political_outlook=Db::table('fa_category')->where('id',$v['political_outlook'])->field('name')->find(); + $res[$k]['political_outlook'] = $political_outlook?$political_outlook['name']:'无'; + } + } +// 获取种植信息 +// $www['user_id'] = $user_id; +// $planting = Db::table('fa_szxc_information_planting')->where($www)->field('id,crops_msg,land_area')->find(); +// if($planting && $planting['crops_msg']){ +// $crops_msg = json_decode($planting['crops_msg'], 1); +// $crops_msg = array_filter(array_column($crops_msg,'name')); +// }else{ +// $crops_msg = []; +// } + +// 获取养殖信息 + $www['user_id'] = $user_id; + $breed = Db::table('fa_szxc_breed')->where($www)->count(); + $crops_msg = Db::table('fa_szxc_planting')->where($www)->count(); + $return['yz_num'] = $breed; //养殖种类 + $return['crops_num'] = $crops_msg;//种植种类 + $return['land_area'] = $planting['land_area'] ??0;//土地面积 + $return['address_name'] = $res[0]['address_name'] ?? ''; + $return['name'] = $name; + $return['family_num'] = Db::table('fa_szxc_information_usermsg')->where($map)->count(); + $return['is_set_hz'] = $is_set_hz; + $return['data'] = $res; + $this->apiSuccess('获取成功', $return, 1); + } + +// 设置为户主 + public function setFamily(){ + $user_id = get_params('user_id'); + if (empty($user_id)) { + $this->apiError('缺少参数'); + } + $f_where[] = ['user_ids','find in set',$user_id]; + $family_arr = Db::table('fa_szxc_family')->where($f_where)->find(); + if($family_arr){ + if($family_arr['householder_id']){ + $this->apiError('已经设置户主'); + }else{ + Db::startTrans(); + try { + $map[] = ['user_id','in',$family_arr['user_ids']]; +// 更新家庭成员表 + Db::table('fa_szxc_family')->where('id',$family_arr['id'])->update(['householder_id'=>$user_id]); +// 更新用户信息表 + $hz_data['householder_id'] = $user_id; + $hz_data['is_hz'] = 0; + Db::table('fa_szxc_information_usermsg')->where($map)->update($hz_data); + $hz_data['is_hz'] = 1; + Db::table('fa_szxc_information_usermsg')->where('user_id',$user_id)->update($hz_data); + + Db::commit(); + $this->apiSuccess('设置成功',null, 1); + } catch (ValidateException | \PDOException | Exception $e) { + Db::rollback(); + $this->apiError($e->getMessage()); + } + } + }else{ + Db::startTrans(); + try { + $map[] = ['user_id','=',$user_id]; +// 更新用户信息表 + $hz_data['householder_id'] = $user_id; + $hz_data['is_hz'] = 1; + Db::table('fa_szxc_information_usermsg')->where($map)->update($hz_data); + + Db::commit(); + $this->apiSuccess('设置成功',null, 1); + } catch (ValidateException | \PDOException | Exception $e) { + Db::rollback(); + $this->apiError($e->getMessage()); + } + } + } + +// 验证手机号、身份证是否使用 + public function is_have($phone=0,$idcard=0,$captcha=0){ + + // 验证验证码 +// if (! Sms::check($phone, $captcha, 'mobilelogin')) { +// $this->apiError(__('验证码不正确')); +// } + + // 判断手机号是否已经注册 + $is_mobile = User::getByMobile($phone); + if ($is_mobile) { + $this->apiError('手机号已被使用'); + } + + //判断是否已被使用 + $is_phone = Db::table('fa_szxc_information_usermsg')->where('phone',$phone)->field('phone')->find(); + if ($is_phone) { + $this->apiError('手机号已被使用'); + } + + //判断身份证号是否已被使用 + $InformationUsermsg = Db::table('fa_szxc_information_usermsg')->where('idcard',$idcard)->field('idcard')->find(); + if ($InformationUsermsg) { + $this->apiError('身份证号已被使用'); + } + $this->apiSuccess('验证通过','验证通过'); + } + + + + + +} diff --git a/app/api/controller/MyArticle.php b/app/api/controller/MyArticle.php new file mode 100644 index 0000000..7cbedf7 --- /dev/null +++ b/app/api/controller/MyArticle.php @@ -0,0 +1,181 @@ + ['except' => ['index','details'] ] + ]; + + public function index($search = '', $category_id = 1, $page = 1, $is_time=0,) + { + $model = Db::table('fa_article'); + $where = [ + ['status', '=', 1], + ['category_id', '=', $category_id], + ['user_id', '=', JWT_UID] + ]; + if (!$category_id) { + unset($where[1]); + } + //总条数 + $count = $model->where($where)->count(); + //当月条数 + $month_count = $model->where($where)->whereMonth('view_time')->count(); + if ($search != '') { + $where[] = ['title', 'like', '%' . $search . '%']; + } + //查询 + $select = $model->with('user')->where($where)->page($page)->limit(20) + ->field('id,title,user_id,view,view_time,image,end_time,is_solve,is_vote,is_nickname,describe')->order('id DESC')->select()->toArray(); + foreach ($select as $key => $value) { + if ($value['is_nickname'] == 1) { + $select[$key]['nickname'] = "匿名人员"; + $select[$key]['avatar'] = ""; + $select[$key]['user_id'] = 0; + } + $select[$key]['nickname'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->value('name'); + //投票处理 + if ($value['is_vote'] == 1) { + $article_vote = Db::table('fa_article_vote_side_tables')->where('article_id', $value['id'])->find(); + if ($article_vote) { + $data = $article_vote; + $data['agree_percentage'] = 0; + $data['opposition_percentage'] = 0; + $count_vote = $data['agree'] + $data['opposition'] + $data['other']; + if ($count_vote != 0) { + if ($data['agree'] != 0) { + $data['agree_percentage'] = round(($data['agree'] / $count_vote) * 100); + } + if ($data['opposition'] != 0) { + $data['opposition_percentage'] = round(($data['opposition'] / $count_vote) * 100); + } + } + $select[$key]['extend']['vote'] = $data; + } + } else { + $select[$key]['extend'] = []; + } + if ($is_time==1){ + if ($value['end_time']where('vote_id',$value['id'])->value('is_read'); + } + return $this->apiSuccess('ok', ['list' => $select, 'count' => ['count' => $count, 'month_count' => $month_count]]); + } + + /**详情 + * @param $id + * @return null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function details($id) + { + $where = [ + ['status', '=', 1], + ['id', '=', $id], + ['user_id', '=', JWT_UID] + + ]; + $find = Db::table('fa_article')->where($where)->withAttr('user_info', function ($data, $value) { + $find['nickname'] = "匿名人员"; + $find['mobile'] = ""; + $find['avatar'] = ""; + $find['user_id'] = 0; + $find['count'] = 0; + $find['end_count'] = 0; + $find['overdue_count'] = 0; + if ($value['is_nickname'] == 0) { + $user = Db::table('fa_user')->where('id', $value['user_id'])->field('nickname,avatar,mobile')->find(); + if ($user) { + $find = $user; + } + } + $config_find = Db::table('fa_config')->where('id', 20)->find(); + //是否查询提案数 + if (in_array($value['category_id'], explode(',', $config_find['value']))) { + //提案总数 + $where = [ + ['category_id', '=', $value['category_id']], + ['user_id', '=', $value['user_id']] + ]; + //处理总数 + $whereTwo = [ + ['category_id', '=', $value['category_id']], + ['user_id', '=', $value['user_id']], + ['is_solve', '=', 1] + ]; + $find['count'] = Db::table('fa_article')->where($where)->count(); + $find['end_count'] = Db::table('fa_article')->where($whereTwo)->count(); + //逾期总数 + $where = [ + ['is_solve', '=', 0] + ]; + $find['overdue_count'] = Db::table('fa_article')->where($where)->whereTime('end_time', '<=', date('Y-m-d H:i:s')) + ->count(); + $usermsg = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->field('age,address_name')->find(); + $find['age'] = $usermsg['age']; + $find['address'] = $usermsg['address_name']; + $insurance=Db::table('fa_szxc_information_insurance')->where('user_id', $value['user_id'])->field('insurance_type')->find(); + if ($insurance){ + $find['insurance_type']=$insurance['insurance_type']; + }else{ + $find['insurance_type']=""; + } + } + return $find; + })->withAttr('category_type_title', function ($data, $value) { + if ($value['category_type'] != 0) { + $find = Db::table('fa_category')->where('id', $value['category_type'])->find(); + return $find['name']; + } + }) + ->find(); + //投票处理 + if ($find['is_vote'] == 1) { + $article_vote = Db::table('fa_article_vote_side_tables')->where('article_id', $find['id'])->find(); + if ($article_vote) { + $data = $article_vote; + $data['agree_percentage'] = 0; + $data['opposition_percentage'] = 0; + $count_vote = $data['agree'] + $data['opposition'] + $data['other']; + if ($count_vote != 0) { + if ($data['agree'] != 0) { + $data['agree_percentage'] = round(($data['agree'] / $count_vote) * 100); + } + if ($data['opposition'] != 0) { + $data['opposition_percentage'] = round(($data['opposition'] / $count_vote) * 100); + } + } + $find['extend']['vote'] = $data; + } + + } else { + $find['extend'] = []; + + } + return $this->apiSuccess('ok', $find); + } + +} diff --git a/app/api/controller/Pay.php b/app/api/controller/Pay.php new file mode 100644 index 0000000..91cfd33 --- /dev/null +++ b/app/api/controller/Pay.php @@ -0,0 +1,50 @@ + '1635725673', + 'app_id' => 'wx6e14cb98394e36bc', + + // 商户证书 + 'private_key' =>config_path() . 'certs/apiclient_key.pem', + 'certificate' =>config_path() . 'certs/apiclient_cert.pem', + + /** + * 接口请求相关配置,超时时间等,具体可用参数请参考: + * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php + */ + 'http' => [ + 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 + 'timeout' => 5.0, + // 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri + ], + ]; + $app = new Application($config); + $response = $app->getClient()->postJson('v3/pay/transactions/jsapi', [ + 'appid' => $app->getConfig()['app_id'], //注意在配置文件中加上app_id + 'mchid' => $app->getConfig()['mch_id'], //商户号 + 'out_trade_no' => md5(123456),//订单号 + 'notify_url' => 'https://ceshi.excellentkk.cn/api/pay_notify', + "description" => "Image形象店-深圳腾大-QQ公仔", + "amount" => [ + "total" => 1, + "currency" => "CNY" + ], + "payer" => [ + "openid" => "oLO1l5FlKiF7A5eEIXKKLDPRjjOI" // <---- 请修改为服务号下单用户的 openid + ] + ]); + + return $this->success('ok',$response->toArray()); + } +} \ No newline at end of file diff --git a/app/api/controller/PayNotify.php b/app/api/controller/PayNotify.php new file mode 100644 index 0000000..bb317dd --- /dev/null +++ b/app/api/controller/PayNotify.php @@ -0,0 +1,58 @@ + 1635725673, + 'app_id' => 'wx6e14cb98394e36bc', + + // 商户证书 + 'private_key' => config_path() .'certs/apiclient_key.pem', + 'certificate' => config_path() .'certs/apiclient_cert.pem', + + // v3 API 秘钥 +// 'secret_key' => '43A03299A3C3FED3D8CE7B820Fxxxxx', + + // v2 API 秘钥 + 'v2_secret_key' => '95d195Dcf6ec66156dfeeb4E7435faef', + + // 平台证书:微信支付 APIv3 平台证书,需要使用工具下载 + // 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader +// 'platform_certs' => [ +// // '/path/to/wechatpay/cert.pem', +// ], + + /** + * 接口请求相关配置,超时时间等,具体可用参数请参考: + * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php + */ + 'http' => [ + 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 + 'timeout' => 5.0, + // 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri + ], + ]; + + $app= new Application($config); + $server = $app->getServer(); + $server->handlePaid(function (Message $message, \Closure $next) { + Log::error($message); + // $message->out_trade_no 获取商户订单号 + // $message->payer['openid'] 获取支付者 openid + // 🚨🚨🚨 注意:推送信息不一定靠谱哈,请务必验证 + // 建议是拿订单号调用微信支付查询接口,以查询到的订单状态为准 + return $next($message); + }); + +// 默认返回 ['code' => 'SUCCESS', 'message' => '成功'] + return $server->serve(); + + } + +} \ No newline at end of file diff --git a/app/api/controller/Personal.php b/app/api/controller/Personal.php new file mode 100644 index 0000000..b6e8428 --- /dev/null +++ b/app/api/controller/Personal.php @@ -0,0 +1,518 @@ + ['except' => ['getdetails','getpinglun','getalllist'] ] + ]; + + /** + * 获取评论列表 + * + * @ApiTitle (获取评论列表) + * @ApiSummary (获取评论列表) + * @ApiMethod (GET) + * @ApiRoute (/api/Personal/getCommentList) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="title", type="integer", required=fasle, description="标题") + * @ApiParams (name="page", type="string", required=true, description="页数") + * @ApiParams (name="limit", type="string", required=true, description="每页条数") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + * 'code':'1', + * 'msg':'返回成功' + * }) + */ + public function getCommentList() + { + $user_id = JWT_UID; + + //根据个人村id进行查询 + if (JWT_UID) { + $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find(); + if ($find) { + if ($find['auth_range'] == 1) { + $where[] = $map[] = $map2[] = $whe2[] = ['village', '=', $find['village_id']]; + $whe[] = ['a.village', '=', $find['village_id']]; + } elseif ($find['auth_range'] == 2) { + $where[] = $map[] = $map2[] = $whe2[] = ['township', '=', $find['street_id']]; + $whe[] = ['a.township', '=', $find['street_id']]; + } elseif ($find['auth_range'] == 3) { + $where[] = $map[] = $map2[] = $whe2[] = ['county', '=', $find['area_id']]; + $whe[] = ['a.county', '=', $find['area_id']]; + } + } + } + +// $www['village_id'] = $village_id; +// $user_id_arr = UserAddress::where($www)->column('user_id'); +// $where[] = ['user_id', 'in', $user_id_arr]; + + $where[] = ['status', 'in', '0,1']; +// 获取本人发布所有评论总数 + $total1 = Db::table('fa_szxc_personal_news_comment')->where($where)->count(); + $total2 = Db::table('fa_article_comment')->where($where)->count(); + $total = $total1 + $total2; +// 获取当月发布总数 + $monthday = get_month_begin_end(); + $map[] = $map2[] = ['status', 'in', '0,1']; + $map[] = ['createtime', 'between', [$monthday['beginThismonth'], $monthday['endThismonth']]]; + $map2[] = ['add_time', 'between', [$monthday['beginThismonth'], $monthday['endThismonth']]]; + $month1 = Db::table('fa_szxc_personal_news_comment')->where($map)->count(); + $month2 = Db::table('fa_article_comment')->where($map2)->count(); + $month = $month1 + $month2; + + // 获取评论列表 + $title = $this->request->get('keyword'); + if ($title) { + $whe[] = ['a.content', 'like', '%' . $title . '%']; + $whe2[] = ['content', 'like', '%' . $title . '%']; + } + $whe[] = ['a.status', '=', 0]; + $page = $this->request->get('page', 1); + $limit = $this->request->get('limit', 10); + $list = Db::table('fa_szxc_personal_news_comment')->alias('a')->join('fa_szxc_personal_news b', 'a.personal_news_id=b.id')->where($whe)->field('a.id,a.content,a.createtime,a.user_id,b.user_id as uid')->select()->toArray(); + if ($list) { + foreach ($list as $key => $value) { + $list[$key]['createtime'] = date('Y-m-d', $value['createtime']); + $list[$key]['news_author'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['uid'])->value('name'); + $list[$key]['username'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->value('name'); + $list[$key]['avatar'] = Db::table('fa_user')->where('id', $value['user_id'])->value('avatar'); + $list[$key]['type'] = 'friends'; + } + } + + $whe2[] = ['status', '=', 0]; + $list2 = Db::table('fa_article_comment')->where($whe2)->field('id,content,add_time,vote_id')->select()->toArray(); + if ($list2) { + foreach ($list2 as $key => $value) { + $uid = Db::table('fa_article')->where('id', $value['vote_id'])->value('user_id'); + $list2[$key]['createtime'] = substr($value['add_time'], 0, 10); + $list2[$key]['news_author'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $uid)->value('name'); + $list2[$key]['username'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $uid)->value('name'); + $list2[$key]['avatar'] = Db::table('fa_user')->where('id', $uid)->value('avatar'); + $list2[$key]['type'] = 'article'; + } + } + $list = array_merge($list, $list2); +// dump($list);die; + $return['total_num'] = $total; + $return['now_num'] = $month; + $return['data'] = $list; + $this->apiSuccess('获取成功', $return, 1); + } + + /** + * 一键审核 + * + * @ApiTitle (一键审核) + * @ApiSummary (一键审核) + * @ApiMethod (GET) + * @ApiRoute (/api/Personal/oneCAlickAudit) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + * 'code':'1', + * 'msg':'返回成功' + * }) + */ + public function oneCAlickAudit() + { + $user_id = JWT_UID; + + //根据个人村id进行查询 + if (JWT_UID) { + $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find(); + if ($find) { + if ($find['auth_range'] == 1) { + $where[] = ['village', '=', $find['village_id']]; + } elseif ($find['auth_range'] == 2) { + $where[] = ['township', '=', $find['street_id']]; + } elseif ($find['auth_range'] == 3) { + $where[] = ['county', '=', $find['area_id']]; + } + } + } + $where[] = ['status', '=', 0]; + + $data['status'] = $data2['status'] = 1; + $data['updatetime'] = time(); +// Db::startTrans(); +// try { + Db::table('fa_szxc_personal_news_comment')->where($where)->update($data); + Db::table('fa_article_comment')->where($where)->update($data2); + Db::commit(); + $this->apiSuccess('一键审核成功'); +// } catch (\Exception $e) { +// $this->apiSuccess('一键审核成功'); +// } + } + + /** + * 删除评论 + * + * @ApiTitle (删除评论) + * @ApiSummary (删除评论) + * @ApiMethod (GET) + * @ApiRoute (/api/Personal/delComment) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + * 'code':'1', + * 'msg':'返回成功' + * }) + */ + public function delComment() + { + $id = $this->request->get('id'); + $type = $this->request->get('type'); + if (empty($id) || empty($type)) { + $this->apiError('缺少参数'); + } + if ($type == 'friends') { //朋友圈 + $where['id'] = $id; + $where['status'] = 0; + $res = Db::table('fa_szxc_personal_news_comment')->where($where)->delete(); + if ($res) { + $this->apiSuccess('删除成功', $res, 1); + } else { + $this->apiError('删除失败'); + } + } + if ($type == 'article') { //文章 + $where['id'] = $id; + $where['status'] = 0; + $res = Db::table('fa_article_comment')->where($where)->delete(); + if ($res) { + $this->apiSuccess('删除成功', $res, 1); + } else { + $this->apiError('删除失败'); + } + } + } + + + /** + * 添加编辑个人新鲜事 + * + * @ApiTitle (添加编辑个人新鲜事) + * @ApiSummary (添加编辑个人新鲜事) + * @ApiMethod (POST) + * @ApiRoute (/api/Personal/addOrEditNews) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="id", type="integer", required=false, description="公告ID") + * @ApiParams (name="title", type="integer", required=false, description="标题") + * @ApiParams (name="content", type="integer", required=false, description="内容") + * @ApiParams (name="release_time", type="integer", required=false, description="发布时间") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + * 'code':'1', + * 'msg':'返回成功' + * }) + */ + public function addOrEditNews() + { + $post = $this->request->post(); + $time = time(); + $useraddress = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->where('status', 1)->find(); + $data = []; + + if ($useraddress) { + $data['county'] = $post['county'] = $useraddress['area_id']; + $data['township'] = $post['township'] = $useraddress['street_id']; + $data['village'] = $post['village'] = $useraddress['village_id']; + } + if(isset($post['topic_id']) && !empty($post['topic_id'])){ + $post['category_id'] = $post['topic_id']; + } + if(empty($post['category_id'])){ + $this->apiError('请选择分类'); + } + // 公告ID + $id = $this->request->post('id'); + if ($id) { //编辑 + $map['id'] = $id; + $is_have = Db::table('fa_szxc_personal_news')->where($map)->find(); + if ($is_have) { + $data['title'] = $post['title']; + $data['content'] = $post['content']; + $data['images'] = $post['images']; + $data['video'] = $post['video']; + $data['updatetime'] = $time; + $result = Db::table('fa_szxc_personal_news')->where($map)->update($data); + if ($result) { + $this->apiSuccess('发布成功', null, 1); + } else { + $this->apiError('发布失败'); + } + } else { + $this->apiError('未找到该说说'); + } + } else {//添加 + $post['createtime'] = $time; + $post['user_id'] = JWT_UID; + $res = Db::table('fa_szxc_personal_news')->strict(false)->insert($post); + if ($res) { + $this->apiSuccess('发布成功', $res, 1); + } else { + $this->apiError('发布失败'); + } + } + } + + /** + * 评论个人新鲜事 + * + * @ApiTitle (评论个人新鲜事) + * @ApiSummary (评论个人新鲜事) + * @ApiMethod (POST) + * @ApiRoute (/api/Personal/newsComment) + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") + * @ApiParams (name="personal_news_id", type="integer", required=true, description="新鲜事ID") + * @ApiParams (name="content", type="integer", required=true, description="评论内容") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + * 'code':'1', + * 'msg':'返回成功' + * }) + */ + public function newsComment() + { + $post = $this->request->post(); + if (!$post['personal_news_id']) { + $this->apiError('缺少参数'); + } + $where['id'] = $post['personal_news_id']; + $news = Db::table('fa_szxc_personal_news')->where($where)->find(); + if (!$news) { + $this->apiError('参数错误'); + } + if(empty($post['content'])){ + $this->apiError('请输入评论内容'); + } + + $useraddress = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->where('status', 1)->find(); + $data = []; + if ($useraddress) { + $post['county'] = $useraddress['area_id']; + $post['township'] = $useraddress['street_id']; + $post['village'] = $useraddress['village_id']; + } + + $time = time(); + $post['createtime'] = $time; + $post['user_id'] = JWT_UID; + $res = Db::table('fa_szxc_personal_news_comment')->strict(false)->insert($post); + if ($res) { + $this->apiSuccess('操作成功', $res, 1); + } else { + $this->apiError('发布失败'); + } + } + +// 获取说说详情 + public function getdetails($id) + { + if (empty($id)) { + $this->apiError('缺少参数'); + } + $where[] = ['id', '=', $id]; + $news = Db::table('fa_szxc_personal_news')->where($where)->find(); + if ($news) { + // 增加阅读数 +// $ip = 'personal_news-details-' . $this->request->ip() . '-' . $id; +// $ip_cache = Cache::get($ip); +// if (empty($ip_cache)) { +// Cache::set($ip, $ip, 3600 * 24); + $map[] = ['id', '=', $id]; + Db::table('fa_szxc_personal_news')->where($map)->inc('view', '1')->update(); +// } + if ($news['images']) { + $news['images'] = json_decode($news['images']); + } + $news['view_time'] = date("Y-m-d", $news['createtime']); + $news['nickname'] = Db::table('fa_szxc_information_usermsg')->where('user_id',$news['user_id'])->value('name'); + $news['avatar'] = Db::table('fa_user')->where('id',$news['user_id'])->value('avatar'); + $this->apiSuccess('获取成功', $news); + } else { + $this->apiError('获取失败'); + } + } + + // 说说详情获取评论 + public function getpinglun() + { + $id = $this->request->get('id'); + if (empty($id)) { + $this->apiError('缺少参数'); + } + $where[] = ['personal_news_id', '=', $id]; + $where[] = ['status', '=', 1]; + $news = Db::table('fa_szxc_personal_news_comment') + ->withAttr('user_type_info',function($value,$data){ + $political_outlook = Db::table('fa_szxc_information_usermsg')->where('user_id',$data['user_id'])->value('political_outlook'); + if($political_outlook){ + return Db::table('fa_category')->where('id',$political_outlook)->value('name'); + }else{ + return '群众'; + } + }) + ->withAttr('nickname',function($value,$data){ + return Db::table('fa_szxc_information_usermsg')->where('user_id',$data['user_id'])->value('name'); + }) + ->withAttr('avatar',function($value,$data){ + return Db::table('fa_user')->where('id',$data['user_id'])->value('avatar'); + }) + ->withAttr('createtime',function($value,$data){ + return date("Y-m-d", $data['createtime']); + }) + ->where($where)->field('id,user_id,content,createtime')->select()->toArray(); + + $this->apiSuccess('获取成功', $news); + } + + +// 根据分类获取朋友圈列表 + public function getlist($page = 1, $county = 0, $township = 0, $village = 0){ + $category_id = $this->request->get('category_id'); + if(empty($category_id)){ + $this->apiError('缺少参数'); + } + $address = Db::table('fa_szxc_information_useraddress')->where('user_id',JWT_UID)->find(); + if($address){ + $where[] = ['county', '=', $address['area_id']]; + $where[] = ['township', '=', $address['street_id']]; + $where[] = ['village', '=', $address['village_id']]; + } +// if ($county != 0) { +// $where[] = ['county', '=', $county]; +// } +// if ($township != 0) { +// $where[] = ['township', '=', $township]; +// } +// if ($village != 0) { +// +// } +// 获取对应下级分类 + $category_id_arr = Db::table('fa_category')->where('pid',$category_id)->whereOr('id',$category_id)->column('id'); + $where[] = ['category_id', 'in', $category_id_arr]; + $list = Db::table('fa_szxc_personal_news') + ->withAttr('user_info', function ($value, $data) { + $user = Db::table('fa_user')->where('id', $data['user_id'])->field('nickname,avatar')->find(); + $user['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $data['user_id'])->value('name'); + return $user; + }) + ->withAttr('image', function ($value, $data) { + if ($data['image'] != '') { + return json_decode($data['image'],true); + } + }) + ->withAttr('article_comment', function ($value, $data) { + return Db::table('fa_szxc_personal_news_comment')->where([['personal_news_id','=',$data['id']],['status','=',1]])->count(); + }) + ->withAttr('article_type', function ($value, $data) { + return Db::table('fa_category')->where('id', $data['category_id'])->value('name'); + }) + ->withAttr('view_time', function ($value, $data) { + return date('Y-m-d H:i:s', $value); + }) + ->where($where) + ->field('id,id as personal_news_id,content as title,user_id,view,createtime as view_time,images as image,video,category_id') + ->limit(10)->order('id desc')->page($page)->select()->toArray(); + $this->apiSuccess('ok', ['list' => $list]); + } + + //获取当前区域朋友圈列表 + public function getlists($page = 1){ + $address = Db::table('fa_szxc_information_useraddress')->where('user_id',JWT_UID)->find(); + if($address){ + $where[] = ['county', '=', $address['area_id']]; + $where[] = ['township', '=', $address['street_id']]; + $where[] = ['village', '=', $address['village_id']]; + } + + $list = Db::table('fa_szxc_personal_news') + ->withAttr('user_info', function ($value, $data) { + $user = Db::table('fa_user')->where('id', $data['user_id'])->field('nickname,avatar')->find(); + $user['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $data['user_id'])->value('name'); + return $user; + }) + ->withAttr('image', function ($value, $data) { + if ($data['image'] != '') { + return json_decode($data['image'],true); + } + }) + ->withAttr('article_comment', function ($value, $data) { + return Db::table('fa_szxc_personal_news_comment')->where([['personal_news_id','=',$data['id']],['status','=',1]])->count(); + }) + ->withAttr('article_type', function ($value, $data) { + return Db::table('fa_category')->where('id', $data['category_id'])->value('name'); + }) + ->withAttr('view_time', function ($value, $data) { + return date('Y-m-d H:i:s', $value); + }) + ->where($where) + ->field('id,id as personal_news_id,content as title,user_id,view,createtime as view_time,images as image,video,category_id') + ->limit(10)->order('id desc')->page($page)->select()->toArray(); + $this->apiSuccess('ok', ['list' => $list]); + } + + //获取所有朋友圈列表 + public function getalllist($page = 1){ +// $address = Db::table('fa_szxc_information_useraddress')->where('user_id',JWT_UID)->find(); +// if($address){ +// $where[] = ['county', '=', $address['area_id']]; +// $where[] = ['township', '=', $address['street_id']]; +// $where[] = ['village', '=', $address['village_id']]; +// } + + $list = Db::table('fa_szxc_personal_news') + ->withAttr('user_info', function ($value, $data) { + $user = Db::table('fa_user')->where('id', $data['user_id'])->field('nickname,avatar')->find(); + $user['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $data['user_id'])->value('name'); + return $user; + }) + ->withAttr('image', function ($value, $data) { + if ($data['image'] != '') { + return json_decode($data['image'],true); + } + }) + ->withAttr('article_comment', function ($value, $data) { + return Db::table('fa_szxc_personal_news_comment')->where([['personal_news_id','=',$data['id']],['status','=',1]])->count(); + }) + ->withAttr('article_type', function ($value, $data) { + return Db::table('fa_category')->where('id', $data['category_id'])->value('name'); + }) + ->withAttr('view_time', function ($value, $data) { + return date('Y-m-d H:i:s', $value); + }) + ->field('id,id as personal_news_id,content as title,user_id,view,createtime as view_time,images as image,video,category_id') + ->limit(10)->order('id desc')->page($page)->select()->toArray(); + $this->apiSuccess('ok', ['list' => $list]); + } + + +} diff --git a/app/api/controller/Slide.php b/app/api/controller/Slide.php new file mode 100644 index 0000000..5204a59 --- /dev/null +++ b/app/api/controller/Slide.php @@ -0,0 +1,55 @@ + ['except' => ['get_slide'] ] + ]; + /** + * 获取轮播 + * + * //php think crud -t slide_info -c work/slideinfo + * php think menu -c work/slideinfo + */ + public function get_slide() + { + $township = get_params('township'); // 乡镇 + if($township){ + $where['township'] = $township; + }else{ + $where['township'] = 0; + } + $slide_id = get_params('slide_id'); // 位置 + if($slide_id == 'index'){ + $where['slide_id'] = 3; + }else{ + $where['slide_id'] = 2; + } + $where['status'] = 1; + $list = Db::table('fa_slide_info')->where($where) + ->order('sort desc') + ->select(); + $is_arr = $list->toarray(); + if($is_arr){ + $this->apiSuccess('获取成功',$list); + }else{ + $this->apiError('暂无数据',[]); + } + } + +} diff --git a/app/api/controller/Sms.php b/app/api/controller/Sms.php new file mode 100644 index 0000000..1a61d9c --- /dev/null +++ b/app/api/controller/Sms.php @@ -0,0 +1,110 @@ + ['except' => ['send','check'] ] + ]; + /** + * 发送验证码 + * + * @param string $mobile 手机号 + * @param string $event 事件名称 + */ + public function send() + { + $mobile = get_params('mobile'); + $event = get_params('event'); + $event = $event ? $event : 'register'; + + if (! $mobile || ! Validate::regex($mobile, "^1\d{10}$")) { + $this->apiError('手机号不正确'); + } + $last = Smslib::get($mobile, $event); + if ($last && time() - $last['createtime'] < 60) { + $this->apiError('发送频繁'); + } + $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count(); + if ($ipSendTotal >= 5) { + $this->apiError('发送频繁'); + } + if ($event) { + $userinfo = Db::table('fa_user')->where('mobile',$mobile)->find(); + if ($event == 'register' && $userinfo) { + //已被注册 + $this->apiError('已被注册'); + } elseif (in_array($event, ['changemobile']) && $userinfo) { + //被占用 + $this->apiError('已被占用'); + } elseif (in_array($event, ['changepwd', 'resetpwd']) && ! $userinfo) { + //未注册 + $this->apiError('未注册'); + } + } + if (!Event::hasListener('sms_send')) { + $this->apiError('请在后台插件管理安装短信验证插件'); + } + $ret = Smslib::send($mobile, null, $event); + if ($ret) { + $this->apiSuccess('发送成功'); + } else { + $this->apiError('发送失败,请检查短信配置是否正确'); + } + } + + /** + * 检测验证码 + * + * @param string $mobile 手机号 + * @param string $event 事件名称 + * @param string $captcha 验证码 + */ + public function check() + { + $mobile = get_params('mobile'); + $event = get_params('event'); + $event = $event ? $event : 'register'; + $captcha = get_params('captcha'); + + if (! $mobile || ! Validate::regex($mobile, "^1\d{10}$")) { + $this->apiError('手机号不正确'); + } + if ($event) { + $userinfo = Db::table('fa_user')->where('mobile',$mobile)->find(); + if ($event == 'register' && $userinfo) { + //已被注册 + $this->apiError('已被注册'); + } elseif (in_array($event, ['changemobile']) && $userinfo) { + //被占用 + $this->apiError('已被占用'); + } elseif (in_array($event, ['changepwd', 'resetpwd']) && ! $userinfo) { + //未注册 + $this->apiError('未注册'); + } + } + $ret = Smslib::check($mobile, $captcha, $event); + if ($ret) { + $this->apiSuccess('成功'); + } else { + $this->apiError('验证码不正确'); + } + } + + + +} diff --git a/app/api/controller/Statistics.php b/app/api/controller/Statistics.php new file mode 100644 index 0000000..110bbf4 --- /dev/null +++ b/app/api/controller/Statistics.php @@ -0,0 +1,471 @@ +whereAge('>', 80)->count(); + //61-80岁 + $num2 = InformationUsermsg::where($where)->whereAge('<=', 80)->whereAge('>', 60)->count(); + //36-60岁 + $num3 = InformationUsermsg::where($where)->whereAge('<=', 60)->whereAge('>=', 36)->count(); + //18-35岁 + $num4 = InformationUsermsg::where($where)->whereAge('<', 36)->whereAge('>', 17)->count(); + //0-17岁 + $num5 = InformationUsermsg::where($where)->whereAge('<', 18)->count(); + $return = [ + ['value'=>$num1,'name'=>'80岁以上'], + ['value'=>$num2,'name'=>'61-80岁'], + ['value'=>$num3,'name'=>'36-60岁'], + ['value'=>$num4,'name'=>'18-35岁'], + ['value'=>$num5,'name'=>'0-17岁'] + ]; + $this->success('获取成功',$return); + } + + //人群关爱(特困人员、高龄老人、低保人员、空巢老人、残疾人员、留守儿童) + public function get_people2($area_id=0,$street_id=0,$village_id=0){ + $where = []; + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } + // 特困人员 + $num1 = InformationUsermsg::where($where)->whereAge('>', 80)->count(); + //高龄老人 + $num2 = InformationUsermsg::where($where)->whereAge('>', 80)->count(); + //空巢老人 + $num4 = InformationUsermsg::where($where)->whereAge('<=', 80)->whereAge('>', 60)->count(); + //留守儿童 + $num6 = InformationUsermsg::where($where)->whereAge('<', 18)->count(); + + foreach ($where as $key =>$value){ + $where[$key][0] = 'm.'.$value[0]; + } + //低保人员 + $num3 = Db::name('szxc_information_usermsg') + ->alias('m') + ->where($where) + ->join('szxc_information_insurance i','m.user_id=i.user_id and i.insurance_type!=219')->count(); + //残疾人员 + $num5 = Db::name('szxc_information_usermsg') + ->alias('m') + ->where($where) + ->join('szxc_information_insurance i','m.user_id=i.user_id and i.whether_disabled!=112')->count(); + $return = [ + ['value'=>$num1,'name'=>'特困人员'], + ['value'=>$num2,'name'=>'高龄老人'], + ['value'=>$num3,'name'=>'空巢老人'], + ['value'=>$num4,'name'=>'留守儿童'], + ['value'=>$num5,'name'=>'低保人员'], + ['value'=>$num6,'name'=>'残疾人员'] + ]; + $this->success('获取成功',$return); + } + + //事项办理(待办事项、延期待办、总计办理、办理结率) + public function get_num($area_id=0,$street_id=0,$village_id=0){ + $where = []; + if ($area_id){ + $where[] = ['county', '=', $area_id]; + } + if ($street_id){ + $where[] = ['township', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village', '=', $village_id]; + } + $model = new Article(); +// 待办事项 + $shuqiu = $model->where('is_solve', 0)->where($where)->where('category_id', 'in',[165,150,149,148,147])->whereTime('end_time','>', date('Y-m-d h:i:s'))->count(); +// 延期待办 + $time = $model->where('is_solve', 0)->where($where)->where('category_id', 'in',[165,150,149,148,147])->whereTime('end_time','<=', date('Y-m-d h:i:s'))->count(); + // 总计办理 + $is_solve = $model->where('is_solve', 1)->where($where)->count(); + if($is_solve){ + // 办理结率 + $num4 = ($is_solve-$shuqiu-$time)/$is_solve; + }else{ + $num4 = 0; + } + + $num4 = round($num4*100,2).'%'; + $return = [ + ['value'=>$shuqiu,'name'=>'待办事项'], + ['value'=>$time,'name'=>'延期待办'], + ['value'=>$is_solve,'name'=>'总计办理'], + ['value'=>$num4,'name'=>'办理结率'] + ]; + $this->success('获取成功',$return); + } + + //资产情况(总耕地面积、人均耕地面积、退林还耕面积、种植物种类、养殖物种类) + public function get_num2($area_id=0,$street_id=0,$village_id=0){ + $where = $map = []; + if ($area_id){ + $where[] = ['b.area_id', '=', $area_id]; + $map[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['b.street_id', '=', $street_id]; + $map[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['b.village_id', '=', $village_id]; + $map[] = ['village_id', '=', $village_id]; + } + // 总耕地面积 + $num1 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where($where)->sum('land_area'); + //人均耕地面积 + $all = InformationUsermsg::where($map)->whereStatus('=', 1)->count(); + if($all){ + $num2 = $num1/$all; + }else{ + $num2 = 0; + } + + //退林还耕面积 + $num3 = $num1*0.3; + //种植物种类 + $num4 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where($where)->column('crops_msg'); + $a = 0; + foreach ($num4 as $k=>$v){ + $v = json_decode($v,true); + $v = array_filter(array_column($v,'name')); + $v = array_filter($v); + $a = $a + count($v); + } + $num4 = $a; + + //养殖物种类 + $num5 = 12; + $return = [ + ['value'=>$num1,'name'=>'总耕地面积'], + ['value'=>$num2,'name'=>'人均耕地面积'], + ['value'=>$num3,'name'=>'退林还耕面积'], + ['value'=>$num4,'name'=>'种植物种类'], + ['value'=>$num5,'name'=>'养殖物种类'] + ]; + $this->success('获取成功',$return); + } + + //消费数据(农资消费总计、食品消费总计、衣着消费总计、居住消费总计、生活用品及服务总计、交通通信总计、教育文化娱乐总计、医疗服务总计) + public function get_num3($area_id=0,$street_id=0,$village_id=0){ + $where = []; + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } +// 农资消费总计 + $num1 = 3687; +// 食品消费总计 + $num2 = 42222; +// 衣着消费总计 + $num3 = 25841; +// 居住消费总计 + $num4 = 84511; +// 生活用品及服务总计 + $num5 = 4254; +// 交通通信总计 + $num6 = 12631; +// 教育文化娱乐总计 + $num7 = 8571; + //医疗服务总计 + $num8 = 63541; + $return = [ + ['value'=>$num1,'name'=>'农资消费总计'], + ['value'=>$num2,'name'=>'食品消费总计'], + ['value'=>$num3,'name'=>'衣着消费总计'], + ['value'=>$num4,'name'=>'居住消费总计'], + ['value'=>$num5,'name'=>'生活用品及服务总计'], + ['value'=>$num6,'name'=>'交通通信总计'], + ['value'=>$num7,'name'=>'教育文化娱乐总计'], + ['value'=>$num8,'name'=>'医疗服务总计'] + ]; + $this->success('获取成功',$return); + } + + //收入构成(工资性收入、生产经营性收入、财产性收入、保险性收入) + public function get_num4($area_id=0,$street_id=0,$village_id=0){ + $where = []; + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } +// 工资性收入 + $num1 = 12; +// 生产经营性收入 + $num2 = 123; +// 财产性收入 + $num3 = 124; +// 保险性收入 + $num4 = 125; + + $return = [ + ['value'=>$num1,'name'=>'工资性收入'], + ['value'=>$num2,'name'=>'生产经营性收入'], + ['value'=>$num3,'name'=>'财产性收入'], + ['value'=>$num4,'name'=>'保险性收入'] + ]; + $this->success('获取成功',$return); + } + + + //土地性质(园地 + //耕地 + //林地 + //牧草地 + //养殖 + //坑塘 + //农田水利设施用地) + public function get_num5($area_id=0,$street_id=0,$village_id=0){ + $where = []; + if ($area_id){ + $where[] = ['b.area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['b.street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['b.village_id', '=', $village_id]; + } + // 总耕地面积 + $num1 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where('nature_of_land',70)->where($where)->sum('land_area'); + //园地 + $num2 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where('nature_of_land',69)->where($where)->sum('land_area'); + //林地 + $num3 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where('nature_of_land',214)->where($where)->sum('land_area'); + //牧草地 + $num4 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where('nature_of_land',215)->where($where)->sum('land_area'); + //养殖 + $num5 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where('nature_of_land',216)->where($where)->sum('land_area'); + //坑塘 + $num6 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where('nature_of_land',217)->where($where)->sum('land_area'); + //农田水利设施用地 + $num7 = Db::name('szxc_information_planting')->alias('a')->join('szxc_information_usermsg b','a.user_id = b.user_id')->where('nature_of_land',218)->where($where)->sum('land_area'); + $return = [ + ['value'=>$num1,'name'=>'耕地面积'], + ['value'=>$num2,'name'=>'园地'], + ['value'=>$num3,'name'=>'林地'], + ['value'=>$num4,'name'=>'牧草地'], + ['value'=>$num5,'name'=>'养殖'], + ['value'=>$num6,'name'=>'坑塘'], + ['value'=>$num7,'name'=>'农田水利设施用地'] + ]; + $this->success('获取成功',$return); + } + + //文化程度( + //文盲及半文盲 + //小学 + //初中 + //高中/技校/中专 + //大学专科及以上 + //不详) + public function get_num6($area_id=0,$street_id=0,$village_id=0){ + $where = []; + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } + // 文盲及半文盲 + $num1 = Db::name('szxc_information_usermsg')->where('education',71)->where($where)->count(); + //小学 + $num2 = Db::name('szxc_information_usermsg')->where('education',72)->where($where)->count(); + //初中 + $num3 = Db::name('szxc_information_usermsg')->where('education',73)->where($where)->count(); + //高中/技校/中专 + $num4 = Db::name('szxc_information_usermsg')->where('education',74)->where($where)->count(); + //大学专科及以上 + $num5 = Db::name('szxc_information_usermsg')->where('education',75)->where($where)->count(); + //不详 + $num6 = Db::name('szxc_information_usermsg')->where('education',76)->where($where)->count(); + + $return = [ + ['value'=>$num1,'name'=>'文盲及半文盲'], + ['value'=>$num2,'name'=>'小学'], + ['value'=>$num3,'name'=>'初中'], + ['value'=>$num4,'name'=>'高中/技校/中专'], + ['value'=>$num5,'name'=>'大学专科及以上'], + ['value'=>$num6,'name'=>'不详'], + ]; + $this->success('获取成功',$return); + } + + //政治面貌( + //群众 + //团员 + //党员 + //预备役) + public function get_num7($area_id=0,$street_id=0,$village_id=0){ + $where = []; + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } + // 群众 + $num1 = Db::name('szxc_information_usermsg')->where('political_outlook',2)->where($where)->count(); + //团员 + $num2 = Db::name('szxc_information_usermsg')->where('political_outlook',3)->where($where)->count(); + //党员 + $num3 = Db::name('szxc_information_usermsg')->where('political_outlook',4)->where($where)->count(); + //预备役 + $num4 = Db::name('szxc_information_usermsg')->where('political_outlook',177)->where($where)->count(); + + $return = [ + ['value'=>$num1,'name'=>'群众'], + ['value'=>$num2,'name'=>'团员'], + ['value'=>$num3,'name'=>'党员'], + ['value'=>$num4,'name'=>'预备役'], + ]; + $this->success('获取成功',$return); + } + + //优秀村民 + public function get_num8($area_id=0,$street_id=0,$village_id=0){ + $where[] = ['user_id','in','1,3,5']; + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } + // 群众 + $return = Db::name('szxc_information_usermsg') + ->field('name,age,address_name as address,gender') + ->where($where) + ->withAttr('gender', function ($value, $data) { + if($value == 1){ + return '男'; + }elseif($value == 2){ + return '女'; + }else{ + return '暂无'; + } + }) + ->select(); + $this->success('获取成功',$return); + } + + + //是否参保 + public function get_num9($area_id=0,$street_id=0,$village_id=0){ + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } + $return = [ + ['yi'=>346], + ['wei'=>30], + ['type' => [['value'=>30,'name'=>'未参保'], + ['value'=>60,'name'=>'低保'], + ['value'=>45,'name'=>'城乡居民基本医保'], + ['value'=>77,'name'=>'新农合'], + ['value'=>82,'name'=>'城镇居民医保'], + ['value'=>62,'name'=>'职工医保'], + ['value'=>20,'name'=>'其他']] + ] + ]; + $this->success('获取成功',$return); + } + + //农业结构 + public function get_num10($area_id=0,$street_id=0,$village_id=0){ + if ($area_id){ + $where[] = ['area_id', '=', $area_id]; + } + if ($street_id){ + $where[] = ['street_id', '=', $street_id]; + } + if ($village_id){ + $where[] = ['village_id', '=', $village_id]; + } + + $return = [ + ['value'=>135,'name'=>'萝卜'], + ['value'=>24,'name'=>'白菜'], + ['value'=>87,'name'=>'芋头'], + ['value'=>51,'name'=>'地瓜'], + ['value'=>102,'name'=>'土豆'], + ['value'=>24,'name'=>'山药'], + ]; + $this->success('获取成功',$return); + } + + + +} diff --git a/app/api/controller/Token.php b/app/api/controller/Token.php new file mode 100644 index 0000000..38050f7 --- /dev/null +++ b/app/api/controller/Token.php @@ -0,0 +1,40 @@ +auth->getToken(); + $tokenInfo = \app\common\library\Token::get($token); + $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]); + } + + /** + * 刷新Token. + */ + public function refresh() + { + //删除源Token + $token = $this->auth->getToken(); + \app\common\library\Token::delete($token); + //创建新Token + $token = Random::uuid(); + \app\common\library\Token::set($token, $this->auth->id, 2592000); + $tokenInfo = \app\common\library\Token::get($token); + $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]); + } +} diff --git a/app/api/controller/User.php b/app/api/controller/User.php new file mode 100644 index 0000000..f17af9a --- /dev/null +++ b/app/api/controller/User.php @@ -0,0 +1,425 @@ + ['except' => ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'] ] + ]; + + + /** + * 会员中心. + */ + public function index() + { + $this->apiSuccess('', ['nickname' => $this->auth->nickname,'avatar'=>$this->auth->avatar]); + } + + /** + * 会员登录. + * + * @param string $account 账号 + * @param string $password 密码 + */ + public function login() + { + $account = $this->request->request('account'); + $password = $this->request->request('password'); + if (! $account || ! $password) { + $this->apiError('参数不正确'); + } + $ret = $this->auth->login($account, $password); + if ($ret) { + $data = ['userinfo' => $this->auth->getUserinfo()]; + $this->apiSuccess('Logged in successful', $data); + } else { + $this->apiError($this->auth->getError()); + } + } + + /** + * 手机验证码登录. + * + * @param string $mobile 手机号 + * @param string $captcha 验证码 + */ + public function mobilelogin() + { + $param = get_params(); + if(empty($param['mobile']) || empty($param['captcha'])){ + $this->apiError('参数错误'); + } + $mobile = $param['mobile']; + $captcha = $param['captcha']; + if (! $mobile ) { + $this->apiError('参数不正确'); + } + if (! Validate::regex($mobile, "^1\d{10}$")) { + $this->apiError('手机格式不正确'); + } +// if (! Sms::check($mobile, $captcha, 'mobilelogin')) { +// $this->apiError('验证码错误')); +// } + + $user = Db::table('fa_user')->where('mobile',$mobile)->find(); + if ($user) { + if ($user['status'] != 'normal') { + $this->apiError('账户已经被锁定'); + } + //如果已经有账号则直接登录 + $token = self::getToken($user['id']); + $data = [ + 'prevtime' => time(), + 'loginip' => request()->ip(), + 'token' => $token + ]; + $ret = Db::table('fa_user')->where(['id' => $user['id']])->update($data); + add_user_log('api', '登录'); + + } else { + $ret = $this->reg($mobile,'123456'); + } + if ($ret) { +// Sms::flush($mobile, 'mobilelogin'); + $data = ['userinfo' => Db::table('fa_user')->where(['id' => $user['id']])->field('id,id as user_id, username, nickname, mobile, avatar,score,group_id,token,createtime')->find()]; + $find=Db::table('fa_szxc_information_usermsg')->where('user_id',$user['id'])->find(); + $group=Db::table('fa_user_group')->where('id',$data['userinfo']['group_id'])->find(); + $data['userinfo']['group_name']=$group['name']; + if ($find){ + $data['userinfo']['name']=$find['name']; + $data['userinfo']['no_update']=0; + $data['userinfo']['address_name']=$find['address_name']; + }else{ + $data['userinfo']['no_update']=1; + $data['userinfo']['address_name']=''; + } + + $this->apiSuccess('登录成功', $data); + } else { + $this->apiError('登录失败'); + } + } + + /** + * @param $user_id + * @return string + */ + public function getToken($user_id){ + $time = time(); //当前时间 + $conf = $this->jwt_conf; + $token = [ + 'iss' => $conf['iss'], //签发者 可选 + 'aud' => $conf['aud'], //接收该JWT的一方,可选 + 'iat' => $time, //签发时间 + 'nbf' => $time-1 , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用 + 'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时 + 'data' => [ + //自定义信息,不要定义敏感信息 + 'userid' =>$user_id, + ] + ]; + return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token 默认'HS256' + } + + /** + * @api {post} /index/reg 会员注册 + * @apiDescription 系统注册接口,返回是否成功的提示,需再次登录 + + * @apiParam (请求参数:) {string} username 用户名 + * @apiParam (请求参数:) {string} password 密码 + + * @apiSuccessExample {json} 成功示例 + * {"code":0,"msg":"注册成功","time":1627375117,"data":[]} + * @apiErrorExample {json} 失败示例 + * {"code":1,"msg":"该账户已经存在","time":1627374899,"data":[]} + */ + public function reg($mobile,$pwd) + { + $param = []; + if($mobile || $pwd){ + $this->apiError('参数错误'); + } + $user = Db::table('fa_user')->where(['username' => $mobile])->find(); + if (!empty($user)) { + $this->apiError('该账户已经存在'); + } + $param['salt'] = set_salt(20); + $param['password'] = set_password($param['pwd'], $param['salt']); + $param['register_time'] = time(); + $param['headimgurl'] = '/static/admin/images/icon.png'; + $param['register_ip'] = request()->ip(); + $char = mb_substr($mobile, 0, 1, 'utf-8'); + $uid = Db::table('fa_user')->strict(false)->field(true)->insertGetId($param); + if($uid){ + add_user_log('api', '注册'); + return true; + }else{ + return false; + } + } + + /** + * 注册会员. + * + * @param string $username 用户名 + * @param string $password 密码 + * @param string $email 邮箱 + * @param string $mobile 手机号 + * @param string $code 验证码 + */ + public function register() + { + $username = $this->request->request('username'); + $password = $this->request->request('password'); + $email = $this->request->request('email'); + $mobile = $this->request->request('mobile'); + $code = $this->request->request('code'); + if (! $username || ! $password) { + $this->apiError('参数不正确'); + } + if ($email && ! Validate::is($email, 'email')) { + $this->apiError('Email is incorrect'); + } + if ($mobile && ! Validate::regex($mobile, "^1\d{10}$")) { + $this->apiError('手机格式不正确'); + } + $ret = Sms::check($mobile, $code, 'register'); + if (!$ret) { + $this->apiError('Captcha is incorrect'); + } + if (empty($email)) { + $email = $username; + } + if (empty($mobile)) { + $mobile = $username; + } + $ret = $this->auth->register($username, $password, $email, $mobile, []); + if ($ret) { + $data = ['userinfo' => $this->auth->getUserinfo()]; + $this->apiSuccess('Sign up successful', $data); + } else { + $this->apiError($this->auth->getError()); + } + } + + /** + * 注销登录. + */ + public function logout() + { + $this->auth->logout(); + $this->apiSuccess('Logout successful'); + } + + /** + * 修改会员个人信息. + * + * @param string $avatar 头像地址 + * @param string $username 用户名 + * @param string $nickname 昵称 + * @param string $bio 个人简介 + */ + public function profile() + { + $user = Db::table('fa_user')->where(['id' => JWT_UID])->find(); +// $username = $this->request->request('username'); + $params = get_params(); + $nickname = $params['nickname']; +// $bio = $this->request->request('bio'); + $avatar = $params['avatar']; +// if ($username) { +// $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find(); +// if ($exists) { +// $this->apiError('Username already exists')); +// } +// $user->username = $username; +// } + if($nickname){ + $data['nickname'] = $nickname; + } + if($avatar){ + $data['avata'] = $avatar; + } +// $user->bio = $bio; +// $user->save(); + if($data){ + Db::table('fa_user')->where(['id' => JWT_UID])->update($data); + } + $user = Db::table('fa_user')->where(['id' => JWT_UID])->find(); + $new_user['nickname'] = $user['nickname']; + $new_user['avatar'] = $user['avatar']; + $this->apiSuccess('修改成功',$new_user); + } + + /** + * 修改邮箱. + * + * @param string $email 邮箱 + * @param string $captcha 验证码 + */ + public function changeemail() + { + $user = $this->auth->getUser(); + $email = $this->request->post('email'); + $captcha = $this->request->request('captcha'); + if (! $email || ! $captcha) { + $this->apiError('参数不正确'); + } + if (! Validate::is($email, 'email')) { + $this->apiError('Email is incorrect'); + } + if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) { + $this->apiError('Email already exists'); + } + $result = Ems::check($email, $captcha, 'changeemail'); + if (! $result) { + $this->apiError('Captcha is incorrect'); + } + $verification = $user->verification; + $verification->email = 1; + $user->verification = $verification; + $user->email = $email; + $user->save(); + + Ems::flush($email, 'changeemail'); + $this->apiSuccess(); + } + + /** + * 修改手机号. + * + * @param string $mobile 手机号 + * @param string $captcha 验证码 + */ + public function changemobile() + { + $user = $this->auth->getUser(); + $mobile = $this->request->request('mobile'); + $captcha = $this->request->request('captcha'); + if (! $mobile || ! $captcha) { + $this->apiError('参数不正确'); + } + if (! Validate::regex($mobile, "^1\d{10}$")) { + $this->apiError('手机格式不正确'); + } + if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) { + $this->apiError('Mobile already exists'); + } + $result = Sms::check($mobile, $captcha, 'changemobile'); + if (! $result) { + $this->apiError('Captcha is incorrect'); + } + $verification = $user->verification; + $verification->mobile = 1; + $user->verification = $verification; + $user->mobile = $mobile; + $user->save(); + + Sms::flush($mobile, 'changemobile'); + $this->apiSuccess(); + } + + /** + * 第三方登录. + * + * @param string $platform 平台名称 + * @param string $code Code码 + */ + public function third() + { + $url = url('user/index'); + $platform = $this->request->request('platform'); + $code = $this->request->request('code'); + $config = get_addon_config('third'); + if (! $config || ! isset($config[$platform])) { + $this->apiError('参数不正确'); + } + $app = new \addons\third\library\Application($config); + //通过code换access_token和绑定会员 + $result = $app->{$platform}->getUserInfo(['code' => $code]); + if ($result) { + $loginret = \addons\third\library\Service::connect($platform, $result); + if ($loginret) { + $data = [ + 'userinfo' => $this->auth->getUserinfo(), + 'thirdinfo' => $result, + ]; + $this->apiSuccess('Logged in successful', $data); + } + } + $this->apiError('Operation failed', $url); + } + + /** + * 重置密码 + * + * @param string $mobile 手机号 + * @param string $newpassword 新密码 + * @param string $captcha 验证码 + */ + public function resetpwd() + { + $type = $this->request->request('type'); + $mobile = $this->request->request('mobile'); + $email = $this->request->request('email'); + $newpassword = $this->request->request('newpassword'); + $captcha = $this->request->request('captcha'); + if (! $newpassword || ! $captcha) { + $this->apiError('参数不正确'); + } + if ($type == 'mobile') { + if (! Validate::regex($mobile, "^1\d{10}$")) { + $this->apiError('手机格式不正确'); + } + $user = \app\common\model\User::where('mobile',$mobile)->find(); + if (! $user) { + $this->apiError('User not found'); + } + $ret = Sms::check($mobile, $captcha, 'resetpwd'); + if (! $ret) { + $this->apiError('Captcha is incorrect'); + } + Sms::flush($mobile, 'resetpwd'); + } else { + if (! Validate::is($email, 'email')) { + $this->apiError('Email is incorrect'); + } + $user = \app\common\model\User::where('email',$email)->find(); + if (! $user) { + $this->apiError('User not found'); + } + $ret = Ems::check($email, $captcha, 'resetpwd'); + if (! $ret) { + $this->apiError('Captcha is incorrect'); + } + Ems::flush($email, 'resetpwd'); + } + //模拟一次登录 + $this->auth->direct($user->id); + $ret = $this->auth->changepwd($newpassword, '', true); + if ($ret) { + $this->apiSuccess('Reset password successful'); + } else { + $this->apiError($this->auth->getError()); + } + } +} diff --git a/app/api/controller/Userinfo.php b/app/api/controller/Userinfo.php new file mode 100644 index 0000000..2eb59d9 --- /dev/null +++ b/app/api/controller/Userinfo.php @@ -0,0 +1,351 @@ + ['except' => ['isBinding'] ] + ]; + /** + * 微信是否绑定用户 + * + * @ApiTitle (微信是否绑定用户) + * @ApiSummary (微信是否绑定用户) + * @ApiMethod (POST) + * @ApiRoute (/api/Userinfo/isBinding) + * @ApiParams (name="openid", type="integer", required=fasle, description="微信openID") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + * 'code':'1', + * 'msg':'返回成功' + * }) + */ + public function isBinding() + { + $post = get_params(); +// if(!$post['openid']){ +// $this->apiError('缺少参数'); +// } +// $where['openid'] = $post['openid']; +// $user = Db::table('fa_user')->where($where)->find(); +// if ($user) { +//// 调登录接口返回信息 +// $ret = $this->auth->login($user['mobile'], '123456'); +// if ($ret) { +// $Userinfo = $this->auth->getUserinfo(); +// $map['user_id'] = $user['id']; +// $is_wgy = Db::table('fa_szxc_information_usermsg')->where($map)->value('is_wgy'); +// $Userinfo['is_wgy'] = $is_wgy; +// $userinfo_data['userinfo'] = $Userinfo; +// $this->apiSuccess('已完善,登录成功', $userinfo_data,1); +// } else { +// $this->apiError($this->auth->getError()); +// } +// }else{ +// $this->apiError('未完善信息'); +// } + } + + /** + * 授权登录后完善用户信息 + * + * @ApiTitle (授权登录后完善用户信息) + * @ApiSummary (授权登录后完善用户信息) + * @ApiMethod (POST) + * @ApiRoute (/api/Userinfo/Binding) + * @ApiParams (name="openID", type="integer", required=fasle, description="微信openID") + * @ApiParams (name="phone", type="integer", required=fasle, description="电话") + * @ApiParams (name="nickname", type="integer", required=fasle, description="微信昵称") + * @ApiParams (name="avatar", type="integer", required=fasle, description="微信头像") + * @ApiParams (name="name", type="integer", required=fasle, description="姓名") + * @ApiParams (name="marital_status", type="integer", required=fasle, description="婚姻状态") + * @ApiParams (name="idcard", type="integer", required=fasle, description="身份证号") + * @ApiParams (name="address_name", type="string", required=true, description="地区中文") + * @ApiParams (name="area_id", type="string", required=true, description="区id") + * @ApiParams (name="street_id", type="string", required=true, description="镇id") + * @ApiParams (name="village_id", type="string", required=true, description="村id") + * @ApiParams (name="brigade_id", type="string", required=true, description="大队id") + * @ApiReturnParams (name="code", type="integer", required=true, sample="0") + * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") + * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回") + * @ApiReturn ({ + * 'code':'1', + * 'msg':'返回成功' + * }) + */ + public function Binding() + { + + $post = get_params(); + + if(!$post['idcard'] || !$post['area_id'] || !$post['street_id'] || !$post['village_id'] || !$post['name']){ + $this->apiError('缺少参数'); + } + $where['id'] = JWT_UID; + $post['user_id'] = JWT_UID; + $user = Db::table('fa_user')->where($where)->field('id,nickname,group_id,mobile,avatar,username,createtime,score')->find(); +// 已绑定 + if ($user) { + Db::startTrans(); + try { +// 判断手机号是否已经注册 +// $is_mobile = Db::table('fa_user')->getByMobile($post['phone']); +// if ($is_mobile) { +// $this->apiError('手机号已被使用'); +// } + //判断身份证号是否已被使用 + $InformationUsermsg = Db::table('fa_szxc_information_usermsg')->where('idcard',$post['idcard'])->field('idcard')->find(); + if ($InformationUsermsg) { + $this->apiError('身份证号已被使用'); + } + +// $validateIDCard = $this->validateIDCard($post['idcard']); +// if($validateIDCard == false){ +// $this->apiError('身份证号错误'); +// } +// $ip = request()->ip(); +// $time = +// $salt = Random::alnum(); //随机字符串 + + $userid = $post['user_id']; + + // 组装地址名 + if ($post['area_id'] != '') { + $area_name = Db::table('fa_geo_area')->where('area_code', $post['area_id'])->value('area_name'); + } + if ($post['street_id'] != '') { + $street_name = Db::table('fa_geo_street')->where('street_code', $post['street_id'])->value('street_name'); + } + if ($post['village_id'] != '') { + $village = Db::table('fa_geo_village')->where('village_code', $post['village_id'])->value('village_name'); + } + if ($post['brigade_id'] != '') { + $brigade_name = Db::table('fa_geo_brigade')->where('id', $post['brigade_id'])->value('brigade_name'); + } + $post['address_name'] = $area_name.$street_name.$village.$brigade_name; + + // 更新用户表 + $header = Request::header('x-Token'); + if(isset($header['x-token']) && !empty($header['x-token'])){ + $phone['mobile'] = $post['phone']??''; + Db::table('fa_user')->where($where)->update($phone); + }else{ + $post['phone'] = $user['mobile']??''; + } + + //写入用户信息表 + $data['user_id'] = $userid; + $data['area_id'] = $post['area_id']; + $data['name'] = $post['name']; + $data['gender'] = $this->get_sex($post['idcard']); + $data['age'] = $this->getAgeFromIdNo($post['idcard']); + $data['marital_status'] = $post['marital_status']; + $data['phone'] = $post['phone']??''; + $data['idcard'] = $post['idcard']; + $data['address_name'] = $post['address_name']; + $data['createtime'] = time(); + $data['street_id'] = $post['street_id']; + $data['village_id'] = $post['village_id_id']; + $data['brigade_id'] = $post['brigade_id']; + $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$post['user_id'])->find(); + if (!$msg){ + Db::table('fa_szxc_information_usermsg')->strict(false)->insert($data); + } +// else{ +// $this->apiError('请勿重新提交'); +// } + //写入用户地区表 + $address_data['user_id'] = $userid; + $address_data['area_id'] = $post['area_id']; + $address_data['street_id'] = $post['street_id']; + $address_data['village_id'] = $post['village_id_id']; + $address_data['village_code'] = $post['village_id']; + $address_data['brigade_id'] = $post['brigade_id']; + $address_data['createtime'] = date('Y-m-d H:i:s'); + $add=Db::table('fa_szxc_information_useraddress')->where('user_id',$post['user_id'])->find(); + if (!$add){ + Db::table('fa_szxc_information_useraddress')->strict(false)->insert($address_data); + } +// else{ +// $this->apiError('请勿重新提交'); +// } +// $ret = $this->auth->login($user['username'], ''); +// if ($ret) { + //创建新Token + $token = Random::uuid(); + \app\common\library\Token::set($token, $user['id'], 2592000); + $tokenInfo = \app\common\library\Token::get($token); + $Userinfo = $user; + $Userinfo['token'] = $tokenInfo['token']; + $Userinfo['expires_in'] = $tokenInfo['expires_in']; + $Userinfo['user_id'] = $post['user_id']; + $userinfo_data['userinfo'] = $Userinfo; + + $find=Db::table('fa_szxc_information_usermsg')->where('user_id',$post['user_id'])->find(); + $group=Db::table('fa_user_group')->where('id',$userinfo_data['userinfo']['group_id'])->find(); + $userinfo_data['userinfo']['group_name']=$group['name']; + if ($find){ + $userinfo_data['userinfo']['name']=$find['name']; + $userinfo_data['userinfo']['no_update']=0; + $userinfo_data['userinfo']['address_name']=$find['address_name']; + }else{ + $userinfo_data['userinfo']['no_update']=1; + $userinfo_data['userinfo']['address_name']=''; + } + Db::commit(); + $this->apiSuccess('已完善,登录成功', $userinfo_data); +// } else { +// $this->apiError($this->auth->getError()); +// } + } catch (ValidateException | PDOException | Exception $e) { + Db::rollback(); + $this->apiError($e->getMessage()); + } + } else { + $this->apiError('未注册不能绑定'); + } + } + + /** + + * 根据身份证号码获取性别 + + * author:xiaochuan + + * @param string $idcard 身份证号码 + + * @return int $sex 性别 1男 2女 0未知 + + */ + + function get_sex($idcard) { + + if(empty($idcard)) return null; + + $sexint = (int) substr($idcard, 16, 1); + + return $sexint % 2 === 0 ? '2' : '1'; + + } + + +// 计算年龄 + function getAgeFromIdNo($idno=''){ + + $btime = strtotime(substr($idno, 6, 8));//idno是身份证号 截取日期并转为时间戳 + $byear =date('Y',$btime ); + $bmonth =date('m',$btime ); + $bday =date('d',$btime ); + $curYear=date('Y'); + $curMoth = date('m'); + $curDay = date('d'); + $age = $curYear - $byear; + if( $curMoth < $bmonth || ($curMoth ==$bmonth && $curDay < $bday)){ + $age--; + } + return $age ??0; + + } + +//验证省份证格式是否正确 + + /** + * 获取密码加密后的字符串 + * @param string $password 密码 + * @param string $salt 密码盐 + * @return string + */ + private function getEncryptPassword($password, $salt = '') + { + return md5(md5($password) . $salt); + } + + /** + * 身份证号搜索户主 + * @return string + */ + public function getUserByIdcard() + { + //身份证号 + $idcard = get_params('idcard'); + if (empty($idcard)) { + $this->apiError('缺少参数'); + } + $where[] = ['idcard','like','%'.$idcard.'%']; + $where[] = ['status','=','1']; + $where[] = ['is_hz','=','1']; + $hz_arr = Db::table('fa_szxc_information_usermsg')->where($where)->field('user_id,name,idcard,gender,age,phone,address_name')->select()->toArray(); + if ($hz_arr) { + $this->apiSuccess('获取成功', $hz_arr); + } else { + $this->apiError('暂无数据'); + } + } + + public function validateIDCard($idcard) { + if(empty($idcard)){ + return false; + }else{ + $idcard = strtoupper($idcard); # 如果是小写x,转化为大写X + if(strlen($idcard) != 18 && strlen($idcard) != 15){ + return false; + } + # 如果是15位身份证,则转化为18位 + if(strlen($idcard) == 15){ + # 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码 + if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) { + $idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9); + } else { + $idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9); + } + # 加权因子 + $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); + # 校验码对应值 + $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); + $checksum = 0; + for ($i = 0; $i < strlen($idcard); $i++) { + $checksum += substr($idcard, $i, 1) * $factor[$i]; + } + $idcard = $idcard . $code[$checksum % 11]; + } + # 验证身份证开始 + $IDCardBody = substr($idcard, 0, 17); # 身份证主体 + $IDCardCode = strtoupper(substr($idcard, 17, 1)); # 身份证最后一位的验证码 + + # 加权因子 + $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); + # 校验码对应值 + $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); + $checksum = 0; + for ($i = 0; $i < strlen($IDCardBody); $i++) { + $checksum += substr($IDCardBody, $i, 1) * $factor[$i]; + } + $validateIdcard = $code[$checksum % 11]; # 判断身份证是否合理 + if($validateIdcard != $IDCardCode){ + return false; + }else{ + return true; + } + } + } + + +} diff --git a/app/api/controller/Validate.php b/app/api/controller/Validate.php new file mode 100644 index 0000000..c24527f --- /dev/null +++ b/app/api/controller/Validate.php @@ -0,0 +1,138 @@ +request->request('email'); + $id = (int) $this->request->request('id'); + $count = User::where('email', '=', $email)->where('id', '<>', $id)->count(); + if ($count > 0) { + $this->error(__('邮箱已经被占用')); + } + $this->success(); + } + + /** + * 检测用户名. + * + * @param string $username 用户名 + * @param string $id 排除会员ID + */ + public function check_username_available() + { + $email = $this->request->request('username'); + $id = (int) $this->request->request('id'); + $count = User::where('username', '=', $email)->where('id', '<>', $id)->count(); + if ($count > 0) { + $this->error(__('用户名已经被占用')); + } + $this->success(); + } + + /** + * 检测手机. + * + * @param string $mobile 手机号 + * @param string $id 排除会员ID + */ + public function check_mobile_available() + { + $mobile = $this->request->request('mobile'); + $id = (int) $this->request->request('id'); + $count = User::where('mobile', '=', $mobile)->where('id', '<>', $id)->count(); + if ($count > 0) { + $this->error(__('该手机号已经占用')); + } + $this->success(); + } + + /** + * 检测手机. + * + * @param string $mobile 手机号 + */ + public function check_mobile_exist() + { + $mobile = $this->request->request('mobile'); + $count = User::where('mobile', '=', $mobile)->count(); + if (! $count) { + $this->error(__('手机号不存在')); + } + $this->success(); + } + + /** + * 检测邮箱. + * + * @param string $mobile 邮箱 + */ + public function check_email_exist() + { + $email = $this->request->request('email'); + $count = User::where('email', '=', $email)->count(); + if (! $count) { + $this->error(__('邮箱不存在')); + } + $this->success(); + } + + /** + * 检测手机验证码 + * + * @param string $mobile 手机号 + * @param string $captcha 验证码 + * @param string $event 事件 + */ + public function check_sms_correct() + { + $mobile = $this->request->request('mobile'); + $captcha = $this->request->request('captcha'); + $event = $this->request->request('event'); + if (! \app\common\library\Sms::check($mobile, $captcha, $event)) { + $this->error(__('验证码不正确')); + } + $this->success(); + } + + /** + * 检测邮箱验证码 + * + * @param string $email 邮箱 + * @param string $captcha 验证码 + * @param string $event 事件 + */ + public function check_ems_correct() + { + $email = $this->request->request('email'); + $captcha = $this->request->request('captcha'); + $event = $this->request->request('event'); + if (! \app\common\library\Ems::check($email, $captcha, $event)) { + $this->error(__('验证码不正确')); + } + $this->success(); + } +} diff --git a/app/api/controller/Village.php b/app/api/controller/Village.php new file mode 100644 index 0000000..3e498bf --- /dev/null +++ b/app/api/controller/Village.php @@ -0,0 +1,402 @@ + ['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('点赞成功'); + } + } + + + +} diff --git a/app/api/controller/party/Article.php b/app/api/controller/party/Article.php new file mode 100644 index 0000000..f86054a --- /dev/null +++ b/app/api/controller/party/Article.php @@ -0,0 +1,126 @@ +request->request('order', 1); + //查询升降序 + if($order==1){ + $orders='desc'; + }else{ + $orders='asc'; + } + $model = new ArticleModel(); + $where=[ + ['status','=', 1], + ['category_id','=', $category_id] + ]; + //根据个人村id进行查询 + if ($this->auth->id) { + $find = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->find(); + if ($find) { + if ($find['auth_range']==1){ + $where[] = ['village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $where[] = ['township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $where[] = ['county', '=', $find['area_id']]; + } + } + } + $count=$model->where($where)->count(); + $month_count=$model->where($where)->whereMonth('view_time')->count(); + if ($search!=''){ + $where[]=['title','like','%'.$search.'%']; + } + $select=$model->with('user')->where($where)->page($page)->limit(20) + ->field('id,title,user_id,view,view_time,image')->order('id',$orders)->select(); + return $this->success('ok',['list'=>$select,'count'=>['count'=>$count,'month_count'=>$month_count]]); + } + public function details($id) + { + $model = new ArticleModel(); + $find=$model->where('id',$id)->find(); + if ($find){ + // 增加阅读数 +// $ip = 'party_article-details-'.$this->request->ip().'-'.$id; +// $ip_cache = Cache::get($ip); +// if(empty($ip_cache)){ +// Cache::set($ip,$id,3600*24); + $map[] =['id','=', $id]; + Db::name('szxc_party_article')->where($map)->inc('view','1')->update(); +// } + } + return $this->success('ok',$find); + } + public function add(){ + + } + public function post(){ + $input=$this->request->post(); + $valdate = new ArticleValdate(); + $res=$valdate->check($input); + if (!$res){ + return $this->error($valdate->getError()); + } + $model = new ArticleModel(); + $useraddress = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->where('status', 1)->find(); + if ($useraddress) { + $input['county'] = $useraddress['area_id']; + $input['township'] = $useraddress['street_id']; + $input['village'] = $useraddress['village_id']; + } + $input['add_time'] = date('Y-m-d H:i:s'); + $input['view_time'] = date('Y-m-d H:i:s'); + $input['user_id'] = $this->auth->id; + $res=$model->save($input); + if ($res){ + return $this->success('添加成功'); + }else{ + return $this->error('添加失败'); + } + } + public function edit($id){ + $model = new ArticleModel(); + $find=$model->where('id',$id)->find(); + return $this->success('ok',$find); + + } + public function put($id){ + $input=$this->request->post(); + $valdate = new ArticleValdate(); + $res=$valdate->check($input); + if (!$res){ + return $this->error($valdate->getError()); + } + $model = new ArticleModel(); + $input['user_id'] = $this->auth->id; + $res=$model->where('id',$id)->update($input); + if ($res){ + return $this->success('修改成功'); + }else{ + return $this->error('修改失败'); + } + } + public function delete($id){ + $model = new ArticleModel(); + $res=$model->where('id',$id)->update(['status'=>0]); + if ($res){ + return $this->success('删除成功'); + }else{ + return $this->error('删除失败'); + } + } +} diff --git a/app/api/controller/party/Branch.php b/app/api/controller/party/Branch.php new file mode 100644 index 0000000..7660beb --- /dev/null +++ b/app/api/controller/party/Branch.php @@ -0,0 +1,206 @@ +model = new BranchModel(); + $this->validate = new BranchValdate(); + } + + public function index($page=1) { + $where=[ + ['status','=', 1], + ]; + //根据个人村id进行查询 + if ($this->auth->id) { + $find = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->find(); + if ($find) { + if ($find['auth_range']==1){ + $where[] = ['village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $where[] = ['township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $where[] = ['county', '=', $find['area_id']]; + } + } + } + $select=Db::name('szxc_party_branch')->where($where) + ->withAttr('nickname',function ($value, $data){ + $find= Db::name('user')->where('id',$data['user_id'])->field('nickname')->find(); + return $find?$find['nickname']:''; + }) + ->withAttr('two_nickname',function ($value, $data){ + $find= Db::name('user')->where('id',$data['two_user_id'])->field('nickname')->find(); + return $find?$find['nickname']:''; + }) + ->withAttr('count',function ($value, $data){ + $find= Db::name('szxc_party_info')->where('party_branch',$data['id'])->where('branch_type',1)->count(); + return $find; + }) + ->page($page)->limit(20)->select(); + $branch_count=Db::name('szxc_party_branch')->where('status', 1)->count(); + $info_count=Db::name('szxc_party_info')->where('status', 1)->where('branch_type',1)->count(); + $info_count_two=Db::name('szxc_party_info')->where('status', 1)->where('branch_type',2)->count(); + $data=[ + 'page'=>$page, + 'branch_count'=>$branch_count,//支部总数 + 'info_count'=>$info_count,//党员总数 + 'info_count_two'=>$info_count_two,//预备役总数 + 'list' =>$select//党支部列表 + ]; + return $this->success('ok',$data); + } + /** 党员信息 + * @param $search + * @param $page + */ + public function info($id) { + $where=[ + ['status','=', 1], + ['user_id','=',$id], + ]; + + $user_info=Db::name('szxc_information_usermsg')->where($where) + ->withAttr('category_info',function ($value, $data){ + $find=Db::name('category')->where('id',$data['marital_status'])->find(); + $nation=Db::name('category')->where('id',$data['nation'])->find(); + $datas['marital']=$find['name']; + $datas['nation']=$nation['name']; + return $datas; + }) + ->withAttr('branch_info',function ($value, $data){ + $find=Db::name('szxc_party_branch')->where('id',$data['branch_id']) + ->withAttr('user_nickname',function ($value, $data){ + $find=Db::name('szxc_information_usermsg')->where('user_id',$data['user_id'])->field('name')->find(); + return $find?$find['name']:''; + }) + ->withAttr('two_user_nickname',function ($value, $data){ + $find=Db::name('szxc_information_usermsg')->where('user_id',$data['two_user_id'])->field('name')->find(); + return $find?$find['name']:''; + }) + ->find(); + return $find; + }) + ->withAttr('branch_user_info',function ($value, $data){ + $find=Db::name('szxc_party_info')->where('user_id',$data['user_id'])->find(); + return $find; + }) + ->find(); + return $this->success('ok',$user_info); + } + public function info_save($id,$branch_id) { +// $where=[ +// ['status','=', 1], +// ['user_id','=',$id], +// ['branch_id','=',$id], +// ]; + $input=$this->request->post(); + + $find=Db::name('szxc_party_info')->where('user_id',$id)->find(); + if ($find){ + Db::name('szxc_party_info')->where('user_id',$id)->update($input); + return $this->success('ok','修改成功'); + + }else{ + $input['party_branch']=$branch_id; + $input['user_id']=$id; + Db::name('szxc_party_info')->insert($input); + return $this->success('ok','添加成功'); + + }; + } + /** 党员管理 + * @param $search + * @param $page + */ + public function info_list($id,$search,$page=1) { + $where=[ + ['status','=', 1], + ['party_branch','=', $id], + ]; + if ($search!=''){ + $where[]=['title','like','%'.$search.'%']; + } + $branch=Db::name('szxc_party_branch')->where('id',$id)->find(); + + $select = Db::name('szxc_party_info')->withAttr('user_info', function ($value, $data) { + $find = Db::name('user')->where('id', $data['user_id'])->field('nickname,mobile,avatar')->find(); + return $find; + }) + ->where($where)->page($page)->limit(20)->select(); + + $info_count=Db::name('szxc_party_info')->where('status', 1)->where('branch_type',1)->count(); + $info_count_two=Db::name('szxc_party_info')->where('status', 1)->where('branch_type',2)->count(); + $data=[ + 'page'=>$page, + 'branch'=>$branch, + 'info_count'=>$info_count,//党员总数 + 'info_count_two'=>$info_count_two,//预备役总数 + 'list' =>$select//党员列表 + ]; + return $this->success('ok',$data); + } + public function add(){ + + } + public function post(){ + $input=$this->request->post(); + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $res=$this->model->save($input); + if ($res){ + return $this->success('添加成功'); + }else{ + return $this->error('添加失败'); + } + } + public function edit($id){ + $find=Db::name('szxc_party_branch')->where('id',$id)->where('status', 1) + ->withAttr('nickname',function ($value, $data){ + $find= Db::name('user')->where('id',$data['user_id'])->field('nickname')->find(); + return $find?$find['nickname']:''; + }) + ->withAttr('two_nickname',function ($value, $data){ + $find= Db::name('user')->where('id',$data['two_user_id'])->field('nickname')->find(); + return $find?$find['nickname']:''; + })->find(); + return $this->success('ok',$find); + + } + public function put($id){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $res=$this->model->where('id',$id)->update($input); + if ($res){ + return $this->success('修改成功'); + }else{ + return $this->error('修改失败'); + } + } + public function delete($id){ + $res=$this->model->where('id',$id)->update(['status'=>0]); + if ($res){ + return $this->success('删除成功'); + }else{ + return $this->error('删除失败'); + } + } +} diff --git a/app/api/controller/party/BranchPayList.php b/app/api/controller/party/BranchPayList.php new file mode 100644 index 0000000..bd0a10a --- /dev/null +++ b/app/api/controller/party/BranchPayList.php @@ -0,0 +1,116 @@ +model = new BranchPayListModel(); + $this->validate = new BranchPayListValdate(); + } + + + /**党组织党费清单 + * @param $search + * @param $page + * @return null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function index($search = '', $page = 1) + { + $select = Db::name('szxc_party_branch')->where('status', 1) + ->withAttr('pay_info', function ($data, $value) { + $find = Db::name('szxc_party_branch_pay_list')->where('branch_id',$value['id'])->where('status', 1)->field('pay,no_pay')->find(); + return $find; + })->withAttr('nickname', function ($datas, $value) { + $finds = Db::name('user')->where('id', $value['user_id'])->field('nickname')->find(); + return $finds?$finds['nickname']:''; + }) + ->withAttr('two_nickname', function ($datas, $value) { + $finds = Db::name('user')->where('id', $value['two_user_id'])->field('nickname')->find(); + return $finds?$finds['nickname']:''; + }) + ->page($page)->limit(20)->select(); + $branch_count = $this->model->where('status', 1)->count(); + $branch_pay_count = $this->model->where('status', 1)->sum('pay'); + $branch_no_pay_count = $this->model->where('status', 1)->sum('no_pay'); + return $this->success('ok', ['list' => $select, + 'branch' => ['branch_count' => $branch_count, 'branch_pay_count' => $branch_pay_count, 'branch_no_pay_count' => $branch_no_pay_count]]); + } + + public function branch_pay_list($id,$page=1){ + $select = $this->model->where('branch_id',$id)->where('status', 1)->page($page)->select(); + $count = $this->model->where('branch_id',$id)->where('status', 1)->count(); + return $this->success('ok', ['list' => $select, 'count' =>$count]); + + } + + public function add() + { + + } + + public function post() + { + $input = $this->request->post(); + $res = $this->validate->check($input); + if (!$res) { + return $this->error($this->validate->getError()); + } + $input['pay'] = 0; + $input['no_pay'] = 0; + $input['status'] = 0; + $res = BranchPayListModel::create($input); + if ($res) { + return $this->success('添加成功', ['id' => $res->id]); + } else { + return $this->error('添加失败'); + } + } + + public function edit($id) + { + $find = $this->model->with('branch')->where('id', $id)->find(); + return $this->success('ok', $find); + + } + + public function put($id) + { + $input = $this->request->post();; + $res = $this->validate->check($input); + if (!$res) { + return $this->error($this->validate->getError()); + } + $res = $this->model->where('id', $id)->update($input); + if ($res) { + return $this->success('修改成功'); + } else { + return $this->error('修改失败'); + } + } + + public function delete($id) + { + $res = $this->model->where('id', $id)->update(['status' => 0]); + if ($res) { + return $this->success('删除成功'); + } else { + return $this->error('删除失败'); + } + } +} diff --git a/app/api/controller/party/BranchPayUser.php b/app/api/controller/party/BranchPayUser.php new file mode 100644 index 0000000..1ea8004 --- /dev/null +++ b/app/api/controller/party/BranchPayUser.php @@ -0,0 +1,127 @@ +model = new BranchPayUsertModel(); + $this->validate = new BranchPayUserValdate(); + } + public function index($id,$page=1,$search='',$type=0) { + $find=BranchPayList::where('id',$id)->find(); + if (!$find) return $this->edit('清单数据不存在'); + $where=[ + ['branch_id','=',$find['branch_id']], + ['status','=', 1] + ]; + if ($search!=''){ + $userall=Db::name('user')->where('nickname','like', $search . '%')->field('id')->select(); + if ($userall){ + $ids=[]; + foreach ($userall as $user){ + $ids[]=$user['id']; + } + $where[]=['user_id','in',implode(',',$ids)]; + }else{ + return $this->error('查询内容不存在'); + } + + } + if ($type>0){ + $where[]=['is_pay','=',$type]; + } + $select=Db::name('szxc_party_branch_pay_user')->where($where) + ->withAttr('user',function ($value, $data){ + $user = Db::name('user')->where('id',$data['user_id'])->field('nickname,avatar')->find(); + return $user; + + }) + ->page($page)->limit(20)->select(); + $count=Db::name('szxc_party_info')->where('party_branch',$id)->count(); + + $data['info']=$find; + $data['list']=$select; + $data['count']=$count; + return $this->success('ok',$data); + } + + public function add($id,$page=1){ + $find=BranchPayList::where('id',$id)->find(); + $strtotime1=strtotime($find['start_time']); + $strtotime2=strtotime($find['end_time']); + $m=date('m',$strtotime1); + $ms=date('m',$strtotime2); + $m_count=$ms-$m; + if (!$find) return $this->edit('清单数据不存在'); + $select=Db::name('szxc_party_info')->where('party_branch',$find['branch_id']) + ->withAttr('user_info',function($data,$value){ + $user = Db::name('user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + return $user; + })->field('id,user_id,branch_pay') + ->select(); + $data_list=$select->toArray(); + foreach($data_list as $key=>$value){ + $data_list[$key]['month_pay_count']= $value['branch_pay']*$m_count; + $data_list[$key]['month_count']=$m_count; + } + $count=Db::name('szxc_party_info')->where('party_branch',$find['branch_id'])->count(); + $data['info']=$find; + $data['list']=$data_list; + $data['count']=$count; + return $this->success('ok',$data); + + } + public function post(){ + $input=$this->request->post(); + foreach ($input as $k=>$v){ + $res=$this->validate->check($v); + if (!$res){ + return $this->error($this->validate->getError()); + } + } + $res=$this->model->saveAll($input); + if ($res){ + return $this->success('添加成功'); + }else{ + return $this->error('添加失败'); + } + } + public function edit($id){ + $find=$this->model->where('id',$id)->find(); + return $this->success('ok',$find); + + } + public function put($id){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $res=$this->model->where('id',$id)->update($input); + if ($res){ + return $this->success('修改成功'); + }else{ + return $this->error('修改失败'); + } + } + public function delete($id){ + $res=$this->model->where('id',$id)->update(['status'=>0]); + if ($res){ + return $this->success('删除成功'); + }else{ + return $this->error('删除失败'); + } + } +} diff --git a/app/api/controller/party/Info.php b/app/api/controller/party/Info.php new file mode 100644 index 0000000..51ad97c --- /dev/null +++ b/app/api/controller/party/Info.php @@ -0,0 +1,104 @@ +model = new InfoModel(); + $this->validate = new InfoValdate(); + } + public function index($search='',$page=1,$type=0,$branch_id=0) { + $where=[ + ['status','=', 1] + ]; + if ($search!=''){ + $userall=Db::name('user')->where('nickname','like', $search . '%')->field('id')->select(); + if ($userall){ + $ids=[]; + foreach ($userall as $user){ + $ids[]=$user['id']; + } + $where[]=['user_id','in',implode(',',$ids)]; + }else{ + return $this->error('查询内容不存在'); + } + } + if ($type>0){ + $where[]=['is_pay','=',$type]; + } + if ($branch_id!=0){ + $where[]=['party_branch','=',$branch_id]; + } + $select=Db::name('szxc_party_info')->where($where) + ->withAttr('user_info',function ($value, $data){ + $user = Db::name('user')->where('id',$data['user_id'])->field('nickname,avatar,mobile')->find(); + $usermsg = Db::name('szxc_information_usermsg')->where('user_id',$data['user_id'])->field('idcard')->find(); + $user['idcard'] =$usermsg?$usermsg['idcard']:''; + return $user; + }) + ->withAttr('branch',function ($value, $data){ + $find = Db::name('szxc_party_branch')->where('id',$data['party_branch'])->field('company')->find(); + return $find['company']; + }) + ->page($page)->limit(20)->select(); + return $this->success('ok',$select); + } + + public function add($user_id){ + $user = Db::name('user')->where('id',$user_id)->field('nickname,avatar,mobile,gender')->find(); + $usermsg = Db::name('szxc_information_usermsg')->where('user_id',$user_id)->field('idcard,age,political_outlook,marriage')->find(); + return $this->success('ok',['userinfo'=>array_merge($user,$usermsg)]); + + } + public function post(){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $res=$this->model->save($input); + if ($res){ + return $this->success('添加成功'); + }else{ + return $this->error('添加失败'); + } + } + public function edit($id){ + $find=$this->model->where('id',$id)->find(); + return $this->success('ok',$find); + + } + public function put($id){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $input['user_id'] = 1; + $res=$this->model->where('id',$id)->update($input); + if ($res){ + return $this->success('修改成功'); + }else{ + return $this->error('修改失败'); + } + } + public function delete($id){ + $res=$this->model->where('id',$id)->update(['status'=>0]); + if ($res){ + return $this->success('删除成功'); + }else{ + return $this->error('删除失败'); + } + } +} diff --git a/app/api/controller/party/Vote.php b/app/api/controller/party/Vote.php new file mode 100644 index 0000000..2dcea67 --- /dev/null +++ b/app/api/controller/party/Vote.php @@ -0,0 +1,155 @@ +model = new VoteModel(); + $this->validate = new VoteValdate(); + } + public function index($search='',$page=1,$type=0,$screen=1) { + $where=[ + ['status','=', 1] + ]; + //根据个人村id进行查询 + if ($this->auth->id) { + $find = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->find(); + if ($find) { + if ($find['auth_range']==1){ + $where[] = ['village', '=', $find['village_id']]; + }elseif ($find['auth_range']==2){ + $where[] = ['township', '=', $find['street_id']]; + }elseif ($find['auth_range']==3){ + $where[] = ['county', '=', $find['area_id']]; + } + } + } + $count=Db::name('szxc_party_vote')->where($where)->count(); + $month_count=Db::name('szxc_party_vote')->where($where)->whereMonth('start_time')->count(); + + if ($search!=''){ + $where[]=['title','like',$search.'%']; + } + if ($screen==2){ + $where[]=['end_time','>=',date('Y-m-d')]; + } + if ($screen==3){ + $where[]=['end_time','<=',date('Y-m-d')]; + } + $select=Db::name('szxc_party_vote')->where($where)->page($page)->limit(20) + ->withAttr('percentage',function ($data,$value){ + $count=$value['agree']+$value['opposition']+$value['other']; + $find['agree_percentage']=0; + $find['opposition_percentage']=0; + if ($count!=0){ + if ($value['agree']!=0){ + $find['agree_percentage']=round(($value['agree']/$count)*100); + } + if ($value['opposition']!=0){ + $find['opposition_percentage']=round(($value['opposition']/$count)*100); + } + } + return $find; + }) + ->withAttr('nickname',function($value,$data){ + $find=Db::name('user')->where('id',$data['user_id'])->field('nickname')->find(); + return $find['nickname']; + })->order('id DESC') + ->field('id,title,image,user_id,view,start_time,end_time,agree,opposition,other')->select(); + return $this->success('ok',['list'=>$select,'count'=>['count'=>$count,'month_count'=>$month_count]]); + } + public function details($id) + { + $find=$this->model->where('id',$id)->find()->toArray(); + if ($find){ + // 增加阅读数 +// $ip = 'party_vote_article-details-'.$this->request->ip().'-'.$id; +// $ip_cache = Cache::get($ip); +// if(empty($ip_cache)){ +// Cache::set($ip,$id,3600*24); + $map[] =['id','=', $id]; + Db::name('szxc_party_vote')->where($map)->inc('view','1')->update(); +// } + $count=$find['agree']+$find['opposition']+$find['other']; + $find['agree_percentage']=0; + $find['opposition_percentage']=0; + if ($count!=0){ + if ($find['agree']!=0){ + $find['agree_percentage']=round(($find['agree']/$count)*100); + } + if ($find['opposition']!=0){ + $find['opposition_percentage']=round(($find['opposition']/$count)*100); + } + } + } + return $this->success('ok',$find); + } + public function add(){ + + } + public function post(){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $useraddress = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->where('status', 1)->find(); + if ($useraddress) { + $input['county'] = $useraddress['area_id']; + $input['township'] = $useraddress['street_id']; + $input['village'] = $useraddress['village_id']; + } + $input['user_id']=$this->auth->id; + $input['add_time']=date('Y-m-d H:i:s'); + $res=$this->model->save($input); + if ($res){ + return $this->success('添加成功'); + }else{ + return $this->error('添加失败'); + } + } + public function edit($id){ + $find=$this->model->where('id',$id)->find(); + return $this->success('ok',$find); + + } + public function put($id){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $useraddress = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->where('status', 1)->find(); + if ($useraddress) { + $input['county'] = $useraddress['area_id']; + $input['township'] = $useraddress['street_id']; + $input['village'] = $useraddress['village_id']; + } + $res=$this->model->where('id',$id)->update($input); + if ($res){ + return $this->success('修改成功'); + }else{ + return $this->error('修改失败'); + } + } + public function delete($id){ + $res=$this->model->where('id',$id)->update(['status'=>0]); + if ($res){ + return $this->success('删除成功'); + }else{ + return $this->error('删除失败'); + } + } +} diff --git a/app/api/controller/party/VoteComment.php b/app/api/controller/party/VoteComment.php new file mode 100644 index 0000000..717633c --- /dev/null +++ b/app/api/controller/party/VoteComment.php @@ -0,0 +1,126 @@ +model = new VoteCommentModel(); + $this->validate = new VoteCommentValdate(); + } + public function index($search='',$vote_id=1,$page=1) { + $select=Db::name('szxc_party_vote_comment') + ->withAttr('user_info',function ($data,$value){ + $find=Db::name('user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + $data = $find; + $data['user_type_info']='党员'; + return $data; + }) + ->withAttr('reply',function ($data,$value){ + if ($value['reply']==null){ + $find='无'; + }else{ + $find=$value['reply']; + } + return $find; + }) + ->where('vote_id', $vote_id)->where('status', 1)->where('type',1)->page($page)->limit(20)->select(); + $select_type2=Db::name('szxc_party_vote_comment') + ->withAttr('user_info',function ($data,$value){ + $find=Db::name('user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + $data = $find; + $data['user_type_info']='党员'; + return $data; + }) + ->withAttr('reply',function ($data,$value){ + if ($value['reply']==null){ + $find='无'; + }else{ + $find=$value['reply']; + } + return $find; + }) + ->where('vote_id', $vote_id)->where('status', 1)->where('type',2)->page($page)->limit(20)->select(); + $select_type3=Db::name('szxc_party_vote_comment') + ->withAttr('user_info',function ($data,$value){ + $find=Db::name('user')->where('id',$value['user_id'])->field('nickname,avatar')->find(); + $data = $find; + $data['user_type_info']='党员'; + return $data; + }) + ->withAttr('reply',function ($data,$value){ + if ($value['reply']==null){ + $find='无'; + }else{ + $find=$value['reply']; + } + return $find; + }) + ->where('vote_id', $vote_id)->where('status', 1)->where('type',3)->page($page)->limit(20)->select(); + return $this->success('ok',['list1'=>$select,'list2'=>$select_type2,'list3'=>$select_type3]); + } + + public function add(){ + + } + public function post(){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $useraddress = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->where('status', 1)->find(); + + if ($useraddress) { + $input['county'] = $useraddress['area_id']; + $input['township'] = $useraddress['street_id']; + $input['village'] = $useraddress['village_id']; + } + + $input['user_id']=1; + $input['add_time']=date('Y-m-d H:i:s'); + $res=$this->model->save($input); + if ($res){ + return $this->success('评论成功'); + }else{ + return $this->error('添加失败'); + } + } + public function edit($id){ + $find=$this->model->with('user')->where('id',$id)->find(); + return $this->success('ok',$find); + + } + public function put($id){ + $input=$this->request->post();; + $res=$this->validate->check($input); + if (!$res){ + return $this->error($this->validate->getError()); + } + $input['user_id'] = 1; + $res=$this->model->where('id',$id)->update($input); + if ($res){ + return $this->success('修改成功'); + }else{ + return $this->error('修改失败'); + } + } + public function delete($id){ + $res=$this->model->where('id',$id)->update(['status'=>0]); + if ($res){ + return $this->success('删除成功'); + }else{ + return $this->error('删除失败'); + } + } +} diff --git a/app/api/middleware/Auth.php b/app/api/middleware/Auth.php index f1c5bc9..a0c7cea 100644 --- a/app/api/middleware/Auth.php +++ b/app/api/middleware/Auth.php @@ -9,6 +9,7 @@ namespace app\api\middleware; use Firebase\JWT\JWT; use Firebase\JWT\Key; +use think\facade\Db; use think\facade\Request; use think\Response; @@ -16,8 +17,11 @@ class Auth { public function handle($request, \Closure $next) { - $token = Request::header('Token'); + $token = Request::header('x-Token'); if ($token) { + if (strpos($token, 'Bearer') === 0){ + $token = trim(substr($token, 6)); + } if (count(explode('.', $token)) != 3) { return json(['code'=>404,'msg'=>'非法请求']); } @@ -25,12 +29,15 @@ class Auth //var_dump($config);exit; try { JWT::$leeway = 60;//当前时间减去60,把时间留点余地 - $decoded = JWT::decode($token, new Key($config['secrect'], 'HS256')); //HS256方式,这里要和签发的时候对应 - //return (array)$decoded; - $decoded_array = json_decode(json_encode($decoded),TRUE); - $jwt_data = $decoded_array['data']; + $decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应 + + //return (array)$decoded; +// $decoded_array = json_decode(json_encode($decoded),TRUE); +// $jwt_data = $decoded_array['data']; + + //$request->uid = $jwt_data['userid']; - define('JWT_UID', $jwt_data['userid']); +// define('JWT_UID', $jwt_data['userid']); $response = $next($request); return $response; //return $next($request); diff --git a/app/api/route/party.php b/app/api/route/party.php new file mode 100644 index 0000000..3388df8 --- /dev/null +++ b/app/api/route/party.php @@ -0,0 +1,111 @@ + 'require', + 'gender' => 'require', + 'gender_id' => 'require', + 'age' => 'require|number|between:1,120', + 'phone' => 'require|mobile', + 'idcard' => 'require|idCard', + 'captcha' => 'require', + 'nation_id' => 'require', +// 'householder_id' => 'require', + 'family_relation' => 'require', + 'address_name' => 'require', + 'marital_status' => 'require', + 'marriage_id' => 'require', + 'political_outlook' => 'require', + 'zzmm_id' => 'require', + 'nation' => 'require', + 'area_id' => 'require', + 'street_id' => 'require', + 'village_id' => 'require', + 'endowment_insurance_status_id'=>'require', + 'insurance_type_id'=>'require', + 'disability_type_id'=>'require', + 'disability_level_id'=>'require', + 'ny_insurance_id'=>'require', + 'sy_insurance_id'=>'require', + ]; + + /** + * 提示消息. + */ + protected $message = [ + 'name' => '请输入姓名', + 'gender' => '请选择性别', + 'gender_id' => '请选择性别', + 'age' => '请输入年龄', + 'age.between' => '年龄必须在1~120之间', + 'age.number' => '年龄必须是数字', + 'phone' => '请输入手机号', + 'phone.mobile' => '手机号格式不正确', + 'idcard' => '请输入身份证号', + 'idcard.idCard' => '身份证号不正确', + 'captcha' => '请输入验证码', +// 'householder_id' => 'require', + 'family_relation' => '请选择家庭关系', + 'address_name' => '请选择村组信息', + 'marital_status' => '请选择婚姻状态', + 'marriage_id' => '请选择婚姻状态', + 'political_outlook' => '请选择政治面貌', + 'zzmm_id' => '请选择政治面貌', + 'nation' => '请选择民族', + 'nation_id' => '请选择民族', + 'area_id' => '请选择村组信息', + 'street_id' => '请选择村组信息', + 'village_id' => '请选择村组信息', + 'endowment_insurance_status_id'=>'请选择养老保险状态', + 'insurance_type_id'=>'请选择参保类型', + 'disability_type_id'=>'请选择是否残疾', + 'disability_level_id'=>'请选择残疾等级', + 'ny_insurance_id'=>'请选择农业保险', + 'sy_insurance_id'=>'请选择商业保险', + ]; + + /** + * 字段描述. + */ + protected $field = [ + ]; + + /** + * 验证场景. + */ + protected $scene = [ + 'add' => ['name', 'gender_id', 'age', 'phone','idcard', 'marriage_id', 'zzmm_id', 'nation_id'], + 'myadd' => ['name', 'gender_id', 'age', 'phone','idcard', 'marriage_id', 'zzmm_id', 'nation_id'], + 'edit' => ['name', 'gender', 'age', 'phone','idcard', 'marriage_id', 'zzmm_id', 'nation_id'], + 'insurancemsg' => ['endowment_insurance_status_id', 'insurance_type_id', 'disability_type_id','ny_insurance_id','sy_insurance_id'], + ]; + + public function __construct() + { + parent::__construct(); + } +} diff --git a/app/common.php b/app/common.php index 6d625c1..e2690e0 100644 --- a/app/common.php +++ b/app/common.php @@ -42,7 +42,7 @@ function get_system_config($name,$key='') if (get_cache('system_config' . $name)) { $config = get_cache('system_config' . $name); } else { - $conf = Db::name('config')->where('name',$name)->find(); + $conf = Db::table('cms_config')->where('name',$name)->find(); if($conf['content']){ $config = unserialize($conf['content']); } diff --git a/app/common/controller/Api.php b/app/common/controller/Api.php new file mode 100644 index 0000000..050321c --- /dev/null +++ b/app/common/controller/Api.php @@ -0,0 +1,345 @@ +request = is_null($app->request) ? \think\facade\Request::instance() : $app->request; + + // 控制器初始化 + $this->_initialize(); + + // 前置操作方法 + if ($this->beforeActionList) { + foreach ($this->beforeActionList as $method => $options) { + is_numeric($method) ? + $this->beforeAction($options) : + $this->beforeAction($method, $options); + } + } + } + /** + * 初始化操作. + */ + protected function _initialize() + { + + + //移除HTML标签 +// $this->request->filter('trim,strip_tags,htmlspecialchars'); + + $this->auth = app()->auth; + + $modulename = app()->http->getName(); + $controllername = strtolower($this->request->controller()); + $actionname = strtolower($this->request->action()); +// halt(\think\facade\Cookie::get('token')); + // token +// $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\facade\Cookie::get('token') ?: '')); + $header=$this->request->header(); + if(isset($header['x-token']) && !empty($header['x-token'])){ + $token = trim($header['x-token']); + if (strpos($token, 'Bearer') === 0){ + $token = trim(substr($token, 6)); + } + $is_shop = 1; + }else{ + $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\facade\Cookie::get('token') ?: '')); + $is_shop = 0; + } + $path = str_replace('.', '/', $controllername).'/'.$actionname; + // 设置当前请求的URI + $this->auth->setRequestUri($path); + // 检测是否需要验证登录 + if (! $this->auth->match($this->noNeedLogin)) { + //初始化 + $this->auth->init($token,$is_shop); + //检测是否登录 + if (! $this->auth->isLogin()) { + $this->error(__('Please login first'), null); + } + // 判断是否需要验证权限 + if (! $this->auth->match($this->noNeedRight)) { + // 判断控制器和方法判断是否有对应权限 + if (! $this->auth->check($path)) { + $this->error(__('You have no permission'), null); + } + } + } else { + // 如果有传递token才验证是否登录状态 +// if ($token) { +// $this->auth->init($token); +// } + if(isset($header['x-token']) && !empty($header['x-token'])){ + $token = trim($header['x-token']); + if (strpos($token, 'Bearer') === 0){ + $token = trim(substr($token, 6)); + } + $is_shop = 1; + }else{ + $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\facade\Cookie::get('token') ?: '')); + $is_shop = 0; + } + $this->auth->init($token,$is_shop); + } + $upload = \app\common\model\Config::upload(); + // 上传信息配置后 + Event::trigger('upload_config_init', $upload); + + Config::set(array_merge(Config::get('upload'), $upload), 'upload'); + + // 加载当前控制器语言包 + $this->loadlang($controllername); + } + + /** + * 加载语言文件. + * + * @param string $name + */ + protected function loadlang($name) + { + Lang::load(app()->getAppPath().'/lang/'.Lang::getLangset().'/'.str_replace('.', '/', + strtolower($name)).'.php'); + } + + /** + * 操作成功返回的数据. + * + * @param string $msg 提示信息 + * @param mixed $data 要返回的数据 + * @param int $code 错误码,默认为1 + * @param string $type 输出类型 + * @param array $header 发送的 Header 信息 + */ + protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = []) + { + $this->result($msg, $data, $code, $type, $header); + } + + /** + * 操作失败返回的数据. + * + * @param string $msg 提示信息 + * @param mixed $data 要返回的数据 + * @param int $code 错误码,默认为0 + * @param string $type 输出类型 + * @param array $header 发送的 Header 信息 + */ + protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = []) + { + $this->result($msg, $data, $code, $type, $header); + } + + /** + * 返回封装后的 API 数据到客户端. + * + * @param mixed $msg 提示信息 + * @param mixed $data 要返回的数据 + * @param int $code 错误码,默认为0 + * @param string $type 输出类型,支持json/xml/jsonp + * @param array $header 发送的 Header 信息 + * + * @throws HttpResponseException + * @return void + */ + protected function result($msg, $data = null, $code = 0, $type = null, array $header = []) + { + $result = [ + 'code' => $code, + 'msg' => $msg, + 'time' => time(), + 'data' => $data, + ]; + // 如果未设置类型则自动判断 + $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType); + + if (isset($header['statuscode'])) { + $code = $header['statuscode']; + unset($header['statuscode']); + } else { + //未设置状态码,根据code值判断 + $code = $code >= 1000 || $code < 200 ? 200 : $code; + } + + $response = Response::create($result, $type, $code)->header($header); + //halt($response); + throw new HttpResponseException($response); + } + + /** + * 前置操作. + * + * @param string $method 前置操作方法名 + * @param array $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]] + * + * @return void + */ + protected function beforeAction($method, $options = []) + { + if (isset($options['only'])) { + if (is_string($options['only'])) { + $options['only'] = explode(',', $options['only']); + } + + if (! in_array($this->request->action(), $options['only'])) { + return; + } + } elseif (isset($options['except'])) { + if (is_string($options['except'])) { + $options['except'] = explode(',', $options['except']); + } + + if (in_array($this->request->action(), $options['except'])) { + return; + } + } + + call_user_func([$this, $method]); + } + + /** + * 设置验证失败后是否抛出异常. + * + * @param bool $fail 是否抛出异常 + * + * @return $this + */ + protected function validateFailException($fail = true) + { + $this->failException = $fail; + + return $this; + } + + /** + * 验证数据. + * + * @param array $data 数据 + * @param string|array $validate 验证器名或者验证规则数组 + * @param array $message 提示信息 + * @param bool $batch 是否批量验证 + * @param mixed $callback 回调方法(闭包) + * + * @throws ValidateException + * @return array|string|true + */ + protected function validate($data, $validate, $message = [], $batch = false, $callback = null) + { + if (is_array($validate)) { + $v = Loader::validate(); + $v->rule($validate); + } else { + // 支持场景 + if (strpos($validate, '.')) { + [$validate, $scene] = explode('.', $validate); + } + + $v = Loader::validate($validate); + + ! empty($scene) && $v->scene($scene); + } + + // 批量验证 + if ($batch || $this->batchValidate) { + $v->batch(true); + } + // 设置错误信息 + if (is_array($message)) { + $v->message($message); + } + // 使用回调验证 + if ($callback && is_callable($callback)) { + call_user_func_array($callback, [$v, &$data]); + } + + if (! $v->check($data)) { + if ($this->failException) { + throw new ValidateException($v->getError()); + } + + return $v->getError(); + } + + return true; + } +} diff --git a/app/common/controller/Backend.php b/app/common/controller/Backend.php new file mode 100644 index 0000000..58b56a4 --- /dev/null +++ b/app/common/controller/Backend.php @@ -0,0 +1,567 @@ +http->getName(); + $controller = preg_replace_callback('/\.[A-Z]/', function ($d) { + return strtolower($d[0]); + }, $this->request->controller(), 1); + + $controllername = parseName($controller); + $actionname = strtolower($this->request->action()); + + $path = str_replace('.', '/', $controllername).'/'.$actionname; + + // 定义是否Addtabs请求 + ! defined('IS_ADDTABS') && define('IS_ADDTABS', request()->get('addtabs') ? true : false); + + // 定义是否Dialog请求 + ! defined('IS_DIALOG') && define('IS_DIALOG', request()->get('dialog') ? true : false); + + // 定义是否AJAX请求 + ! defined('IS_AJAX') && define('IS_AJAX', $this->request->isAjax()); + + $this->auth = Auth::instance(); + // 设置当前请求的URI + $this->auth->setRequestUri($path); + // 检测是否需要验证登录 + if (! $this->auth->match($this->noNeedLogin)) { + //检测是否登录 + if (! $this->auth->isLogin()) { + Event::trigger('admin_nologin', $this); + $url = Session::get('referer'); + $url = $url ? $url : $this->request->url(); + if ($url == '/') { + $this->redirect('index/login', [], 302, ['referer' => $url]); + exit; + } + $this->error(__('Please login first'), url('index/login', ['url' => $url])); + } + // 判断是否需要验证权限 + if (! $this->auth->match($this->noNeedRight)) { + // 判断控制器和方法判断是否有对应权限 + if (! $this->auth->check($path)) { + Event::trigger('admin_nopermission', $this); + $this->error(__('You have no permission'), ''); + } + } + } + + // 非选项卡时重定向 + if (! $this->request->isPost() && ! IS_AJAX && ! IS_ADDTABS && ! IS_DIALOG && request()->param('ref') == 'addtabs') { + $url = preg_replace_callback("/([\?|&]+)ref=addtabs(&?)/i", function ($matches) { + return $matches[2] == '&' ? $matches[1] : ''; + }, $this->request->url()); + if (Config::get('url_domain_deploy')) { + if (stripos($url, $this->request->server('SCRIPT_NAME')) === 0) { + $url = substr($url, strlen($this->request->server('SCRIPT_NAME'))); + } + $url = url($url, [], false); + } + $this->redirect(url('index/index'), [], 302, ['referer' => $url]); + exit; + } + + // 设置面包屑导航数据 + $breadcrumb = $this->auth->getBreadCrumb($path); + array_pop($breadcrumb); + $this->view->breadcrumb = $breadcrumb; + + // 如果有使用模板布局 + if ($this->layout) { + View::engine()->layout('layout/'.$this->layout); + } + + // 语言检测 + $lang = strip_tags(Lang::getLangSet()); + + $site = Config::get('site'); + $upload = \app\common\model\Config::upload(); + // 上传信息配置后 + $event_upload_config = Event::trigger('upload_config_init', $upload,true); + if($event_upload_config){ + $upload = array_merge($upload, $event_upload_config); + } + // 配置信息 + $config = [ + 'app_debug' => Env::get('APP_DEBUG'), + 'site' => array_intersect_key($site, + array_flip(['name', 'indexurl', 'cdnurl', 'version', 'timezone', 'languages'])), + 'upload' => $upload, + 'modulename' => $modulename, + 'controllername' => $controllername, + 'actionname' => $actionname, + 'jsname' => 'backend/'.str_replace('.', '/', $controllername), + 'moduleurl' => rtrim(request()->root(), '/'), + 'language' => $lang, + 'fastadmin' => Config::get('fastadmin'), + 'referer' => Session::get('referer'), + ]; + Config::set(array_merge(Config::get('upload'), $upload), 'upload'); + // 配置信息后 + $event_config = Event::trigger('config_init', $config,true); + if($event_config){ + $config = array_merge($config, $event_config); + } + //加载当前控制器语言包 + $this->loadlang($this->request->controller()); + //渲染站点配置 + $this->assign('site', $site); + //渲染配置信息 + $this->assign('config', $config); + //渲染权限对象 + $this->assign('auth', $this->auth); + //渲染管理员对象 + $this->assign('admin', Session::get('admin')); + } + + /** + * 加载语言文件. + * + * @param string $name + */ + protected function loadlang($name) + { + if (strpos($name, '.')) { + $_arr = explode('.', $name); + if (count($_arr) == 2) { + $path = $_arr[0].'/'.parseName($_arr[1]); + } else { + $path = strtolower($name); + } + } else { + $path = parseName($name); + } + Lang::load(app()->getAppPath().'/lang/'.Lang::getLangset().'/'.$path.'.php'); + } + + /** + * 渲染配置信息. + * + * @param mixed $name 键名或数组 + * @param mixed $value 值 + */ + protected function assignconfig($name, $value = '') + { + $this->view->config = array_merge($this->view->config ? $this->view->config : [], + is_array($name) ? $name : [$name => $value]); + } + + /** + * 生成查询所需要的条件,排序方式. + * + * @param mixed $searchfields 快速查询的字段 + * @param bool $relationSearch 是否关联查询 + * + * @return array + */ + protected function buildparams($searchfields = null, $relationSearch = null) + { + $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields; + $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch; + $search = $this->request->get('search', ''); + $filter = $this->request->get('filter', ''); + $op = $this->request->get('op', '', 'trim'); + $sort = $this->request->get('sort', + ! empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id'); + $order = $this->request->get('order', 'DESC'); + $offset = $this->request->get('offset', 0); + $limit = $this->request->get('limit', 0); + $filter = (array) json_decode($filter, true); + $op = (array) json_decode($op, true); + $filter = $filter ? $filter : []; + $where = []; + $tableName = ''; + if ($relationSearch) { + if (! empty($this->model)) { + //$name = parseName(trim(basename(str_replace('\\', ' / ', get_class($this->model))))); + $name = $this->model->getTable(); + $tableName = trim($name).'.'; + } + $sortArr = explode(',', $sort); + foreach ($sortArr as $index => &$item) { + $item = stripos($item, '.') === false ? $tableName.trim($item) : $item; + } + unset($item); + $sort = implode(',', $sortArr); + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) { + $where[] = [$tableName.$this->dataLimitField, 'in', $adminIds]; + } + if ($search) { + $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields); + foreach ($searcharr as $k => &$v) { + $v = stripos($v, '.') === false ? $tableName.$v : $v; + } + unset($v); + $where[] = [implode('|', $searcharr), 'LIKE', "%{$search}%"]; + } + foreach ($filter as $k => $v) { + $sym = isset($op[$k]) ? $op[$k] : ' = '; + if (stripos($k, '.') === false) { + $k = $tableName.$k; + } + $v = ! is_array($v) ? trim($v) : $v; + $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym); + switch ($sym) { + case ' = ': + case '=': + case ' <> ': + case '<>': + $where[] = [$k, $sym, (string) $v]; + break; + case 'LIKE': + case 'NOT LIKE': + case 'LIKE %...%': + case 'NOT LIKE %...%': + $where[] = [$k, trim(str_replace(' %...%', '', $sym)), "%{$v}%"]; + break; + case ' > ': + case '>': + case '>=': + case ' < ': + case '<': + case '<=': + $where[] = [$k, $sym, intval($v)]; + break; + case 'FINDIN': + case 'FINDINSET': + case 'FIND_IN_SET': + $where[] = "FIND_IN_SET('$v', ".($relationSearch ? $k : '`'.str_replace('.', '` . `', $k).'`').')'; + break; + case 'IN': + case 'IN(...)': + case 'NOT IN': + case 'NOT IN(...)': + $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)]; + break; + case 'BETWEEN': + case 'NOT BETWEEN': + $arr = array_slice(explode(',', $v), 0, 2); + if (stripos($v, ',') === false || !array_filter($arr, function($v){ + return $v != '' && $v !== false && $v !== null; + })) { + continue 2; + } + //当出现一边为空时改变操作符 + if ($arr[0] === '') { + $sym = $sym == 'BETWEEN' ? ' <= ' : '>'; + $arr = $arr[1]; + } elseif ($arr[1] === '') { + $sym = $sym == 'BETWEEN' ? ' >= ' : '<'; + $arr = $arr[0]; + } + $where[] = [$k, $sym, $arr]; + break; + case 'RANGE': + case 'NOT RANGE': + $v = str_replace(' - ', ',', $v); + $arr = array_slice(explode(',', $v), 0, 2); + if (stripos($v, ',') === false || ! array_filter($arr)) { + continue 2; + } + //当出现一边为空时改变操作符 + if ($arr[0] === '') { + $sym = $sym == 'RANGE' ? ' <= ' : '>'; + $arr = $arr[1]; + } elseif ($arr[1] === '') { + $sym = $sym == 'RANGE' ? ' >= ' : '<'; + $arr = $arr[0]; + } + $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym).' time', $arr]; + break; + case 'NULL': + case 'IS NULL': + case 'NOT NULL': + case 'IS NOT NULL': + $where[] = [$k, strtolower(str_replace('IS ', '', $sym))]; + break; + default: + break; + } + } + if (! empty($where)) { + $where = function ($query) use ($where) { + foreach ($where as $k => $v) { + if (is_array($v)) { + call_user_func_array([$query, 'where'], $v); + } else { + $query->where($v); + } + } + }; + } + + return [$where, trim($sort), trim($order), $offset, $limit]; + } + + /** + * 获取数据限制的管理员ID + * 禁用数据限制时返回的是null. + * + * @return mixed + */ + protected function getDataLimitAdminIds() + { + if (! $this->dataLimit) { + return; + } + if ($this->auth->isSuperAdmin()) { + return; + } + $adminIds = []; + if (in_array($this->dataLimit, ['auth', 'personal'])) { + $adminIds = $this->dataLimit == 'auth' ? $this->auth->getChildrenAdminIds(true) : [$this->auth->id]; + } + + return $adminIds; + } + + /** + * Selectpage的实现方法. + * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可 + * 这里示例了所有的参数,所以比较复杂,实现上自己实现只需简单的几行即可. + */ + protected function selectpage() + { + //设置过滤方法 + $this->request->filter(['strip_tags', 'htmlspecialchars']); + + //搜索关键词,客户端输入以空格分开,这里接收为数组 + $word = (array) $this->request->request('q_word/a'); + //当前页 + $page = $this->request->request('pageNumber', 1, 'int'); + //分页大小 + $pagesize = $this->request->request('pageSize'); + //搜索条件 + $andor = $this->request->request('andOr', 'and', 'strtoupper'); + //排序方式 + $orderby = (array) $this->request->request('orderBy/a'); + //显示的字段 + $field = $this->request->request('showField'); + //主键 + $primarykey = $this->request->request('keyField'); + //主键值 + $primaryvalue = $this->request->request('keyValue'); + //搜索字段 + $searchfield = (array) $this->request->request('searchField/a'); + //自定义搜索条件 + $custom = (array) $this->request->request('custom/a'); + //是否返回树形结构 + $istree = $this->request->request('isTree', 0); + $ishtml = $this->request->request('isHtml', 0); + if ($istree) { + $word = []; + $pagesize = 99999; + } + $order = []; + foreach ($orderby as $k => $v) { + $order[$v[0]] = $v[1]; + } + $field = $field ? $field : 'name'; + + //如果有primaryvalue,说明当前是初始化传值 + if ($primaryvalue !== null) { + $where = [$primarykey => explode(',', $primaryvalue)]; + $pagesize = null; + } else { + $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) { + $logic = $andor == ' AND ' ? ' & ' : ' | '; + $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield; + foreach ($word as $k => $v) { + $query->where(str_replace(',', $logic, $searchfield), 'like', "%{$v}%"); + } + if ($custom && is_array($custom)) { + foreach ($custom as $k => $v) { + if (is_array($v) && 2 == count($v)) { + $query->where($k, trim($v[0]), $v[1]); + } else { + $query->where($k, '=', $v); + } + } + } + }; + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $list = []; + $total = $this->model->where($where)->count(); + if ($total > 0) { + if (is_array($adminIds)) { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $datalist = $this->model->where($where) + ->order($order) + ->page($page, $pagesize) + ->field($this->selectpageFields) + ->select()->toArray(); + foreach ($datalist as $index => $item) { + unset($item['password'], $item['salt']); + $list[] = [ + $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '', + $field => isset($item[$field]) ? $item[$field] : '', + 'pid' => isset($item['pid']) ? $item['pid'] : 0, + ]; + } + if ($istree && ! $primaryvalue) { + $tree = Tree::instance(); + $tree->init($list, 'pid'); + $list = $tree->getTreeList($tree->getTreeArray(0), $field); + if (! $ishtml) { + foreach ($list as &$item) { + $item = str_replace(' & nbsp;', ' ', $item); + } + unset($item); + } + } + } + //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮 + return json(['list' => $list, 'total' => $total]); + } + + /** + * 刷新Token + */ + protected function token() + { + $token = $this->request->post('__token__'); + + //验证Token + if (! Validate::is($token, "token", ['__token__' => $token])) { + $this->error(__('Token verification error'), '', '', 3, ['__token__' => $this->request->buildToken()]); + } + + //刷新Token + $this->request->buildToken(); + } +} diff --git a/app/common/controller/BaseController.php b/app/common/controller/BaseController.php new file mode 100644 index 0000000..53a48bd --- /dev/null +++ b/app/common/controller/BaseController.php @@ -0,0 +1,122 @@ +app = $app; + $this->request = $this->app->request; + $this->view = $this->app->view; + // 控制器初始化 + $this->_initialize(); + } + + protected function assign($name, $value = null) + { + View::assign($name, $value); + } + + // 初始化 + protected function _initialize() + { + } + + /** + * 验证数据. + * + * @param array $data 数据 + * @param string|array $validate 验证器名或者验证规则数组 + * @param array $message 提示信息 + * @param bool $batch 是否批量验证 + * + * @throws ValidateException + * + * @return array|string|true + */ + protected function validate(array $data, $validate, array $message = [], bool $batch = false) + { + if (is_array($validate)) { + $v = new Validate(); + $v->rule($validate); + } else { + if (strpos($validate, '.')) { + // 支持场景 + [$validate, $scene] = explode('.', $validate); + } + $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate); + $v = new $class(); + if (! empty($scene)) { + $v->scene($scene); + } + } + + $v->message($message); + + // 是否批量验证 + if ($batch || $this->batchValidate) { + $v->batch(true); + } + + return $v->failException(true)->check($data); + } +} diff --git a/app/common/controller/EasyWeChat.php b/app/common/controller/EasyWeChat.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/app/common/controller/EasyWeChat.php @@ -0,0 +1 @@ +request->filter('trim,strip_tags,htmlspecialchars'); + $modulename = app()->http->getName(); + $controller = preg_replace_callback('/\.[A-Z]/', function ($d) { + return strtolower($d[0]); + }, $this->request->controller(), 1); + + $controllername = parseName($controller); + $actionname = strtolower($this->request->action()); + + // 如果有使用模板布局 + if ($this->layout) { + View::engine()->layout('layout/'.$this->layout); + } + $this->auth = app()->auth; + + // token + $token = $this->request->server('HTTP_TOKEN', + $this->request->request('token', \think\facade\Cookie::get('token')) ?: ''); + + $path = str_replace('.', '/', $controllername).'/'.$actionname; + // 设置当前请求的URI + $this->auth->setRequestUri($path); + // 检测是否需要验证登录 + if (! $this->auth->match($this->noNeedLogin)) { + //初始化 + $this->auth->init($token); + //检测是否登录 + if (! $this->auth->isLogin()) { + $this->error(__('Please login first'), 'index/user/login'); + } + // 判断是否需要验证权限 + if (! $this->auth->match($this->noNeedRight)) { + // 判断控制器和方法判断是否有对应权限 + if (! $this->auth->check($path)) { + $this->error(__('You have no permission')); + } + } + } else { + // 如果有传递token才验证是否登录状态 + if ($token) { + $this->auth->init($token); + } + } + + $this->view->assign('user', $this->auth->getUser()); + + // 语言检测 + $lang = strip_tags(Lang::getLangSet()); + + $site = Config::get('site'); + + $upload = \app\common\model\Config::upload(); + + // 上传信息配置后 + Event::trigger('upload_config_init', $upload); + + // 配置信息 + $config = [ + 'site' => array_intersect_key($site, + array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])), + 'upload' => $upload, + 'modulename' => $modulename, + 'controllername' => $controllername, + 'actionname' => $actionname, + 'jsname' => 'frontend/'.str_replace('.', '/', $controllername), + 'moduleurl' => rtrim(request()->root(), '/'), + 'language' => $lang, + ]; + + Config::set(array_merge(Config::get('upload'), $upload), 'upload'); + + // 配置信息后 + Event::trigger('config_init', $config); + // 加载当前控制器语言包 + $this->loadlang($this->request->controller()); + $this->assign('site', $site); + $this->assign('config', $config); + } + + /** + * 加载语言文件. + * + * @param string $name + */ + protected function loadlang($name) + { + if (strpos($name, '.')) { + $_arr = explode('.', $name); + if (count($_arr) == 2) { + $path = $_arr[0].'/'.parseName($_arr[1]); + } else { + $path = strtolower($name); + } + } else { + $path = parseName($name); + } + Lang::load(app()->getAppPath().'/lang/'.Lang::getLangset().'/'.$path.'.php'); + } + + /** + * 渲染配置信息. + * + * @param mixed $name 键名或数组 + * @param mixed $value 值 + */ + protected function assignconfig($name, $value = '') + { + $this->view->config = array_merge($this->view->config ? $this->view->config : [], + is_array($name) ? $name : [$name => $value]); + } + + /** + * 刷新Token + */ + protected function token() + { + $token = $this->request->post('__token__'); + + //验证Token + if (!Validate::is($token, "token", ['__token__' => $token])) { + $this->error(__('Token verification error'), '', ['__token__' => $this->request->buildToken()]); + } + + //刷新Token + $this->request->buildToken(); + } +} diff --git a/app/common/controller/WxTui.php b/app/common/controller/WxTui.php new file mode 100644 index 0000000..4de8276 --- /dev/null +++ b/app/common/controller/WxTui.php @@ -0,0 +1,104 @@ +getJson($url); + $token = json_decode($token, true); + + $uri ='https://api.weixin.qq.com/cgi-bin/message/template/send'; + $access_token = $token["access_token"]; + $uri = $uri.'?access_token='.$access_token; + + $data= array('touser'=>$openid, //发给谁 + 'template_id'=>'G_tFTFVPqY5krFuZ59MBybGENuLw22imaJgE7ilhwOU', //模板id + 'url'=>'#', //这个是你发送了模板消息之后,当用户点击时跳转的连接 + 'topcolor'=>"#FF0000", //颜色 + 'miniprogram' => '', + 'data'=>array( + + 'first'=>array( + 'value'=>$data['first'], + 'color'=>'#173177' + ), + 'keyword1'=>array( + 'value'=>$data['course_name'], + 'color'=>'#173177' + ), + 'keyword2'=>array( + 'value'=>$data['teacher_name'], + 'color'=>'#173177' + ), + 'keyword3'=>array( + 'value'=>$data['tel'], + 'color'=>'#173177' + ), + 'keyword4'=>array( + 'value'=>$data['time'], + 'color'=>'#173177' + ), + 'remark'=>array( + 'value'=>$data['remark'], + 'color'=>'#173177' + ) + ) + ); + $res_data = $this->getJson($uri,$data); + $res_data = json_decode($res_data, true); + if ($res_data['errcode'] != 0) { + return false; + } + return true; + } + + public function getJson($url = '', $param = [] ,$contentType = 'json'){ + $ch = curl_init(); + // 请求地址 + curl_setopt($ch, CURLOPT_URL, $url); + // 请求参数类型 + $param = $contentType == 'json' ? urldecode(json_encode($param)) : http_build_query($param); + // 关闭https验证 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + // post提交 + if($param){ + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $param); + } + // 返回的数据是否自动显示 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + // 执行并接收响应结果 + $output = curl_exec($ch); + // 关闭curl + curl_close($ch); + return $output !== false ? $output : false; + + } +} diff --git a/app/common/lang/zh-cn/addon.php b/app/common/lang/zh-cn/addon.php new file mode 100644 index 0000000..0491e57 --- /dev/null +++ b/app/common/lang/zh-cn/addon.php @@ -0,0 +1,97 @@ + '插件未找到', + 'addon %s is disabled' => '插件已禁用', + 'addon controller %s not found' => '插件控制器未找到', + 'addon action %s not found' => '插件控制器方法未找到', + 'addon can not be empty' => '插件不能为空', + 'Keep login' => '保持会话', + 'Forgot password' => '忘记密码?', + 'Username' => '用户名', + 'User id' => '会员ID', + 'Nickname' => '昵称', + 'Password' => '密码', + 'Sign up' => '注 册', + 'Sign in' => '登 录', + 'Sign out' => '注 销', + 'Guest' => '游客', + 'Welcome' => '%s,你好!', + 'Add' => '添加', + 'Edit' => '编辑', + 'Delete' => '删除', + 'Move' => '移动', + 'Name' => '名称', + 'Status' => '状态', + 'Weigh' => '权重', + 'Operate' => '操作', + 'Warning' => '温馨提示', + 'Default' => '默认', + 'Article' => '文章', + 'Page' => '单页', + 'OK' => '确定', + 'Cancel' => '取消', + 'Loading' => '加载中', + 'More' => '更多', + 'Normal' => '正常', + 'Hidden' => '隐藏', + 'Submit' => '提交', + 'Reset' => '重置', + 'Execute' => '执行', + 'Close' => '关闭', + 'Search' => '搜索', + 'Refresh' => '刷新', + 'First' => '首页', + 'Previous' => '上一页', + 'Next' => '下一页', + 'Last' => '末页', + 'None' => '无', + 'Online' => '在线', + 'Logout' => '注销', + 'Profile' => '个人资料', + 'Index' => '首页', + 'Hot' => '热门', + 'Recommend' => '推荐', + 'Dashboard' => '控制台', + 'Code' => '编号', + 'Message' => '内容', + 'Line' => '行号', + 'File' => '文件', + 'Menu' => '菜单', + 'Type' => '类型', + 'Title' => '标题', + 'Content' => '内容', + 'Append' => '追加', + 'Memo' => '备注', + 'Parent' => '父级', + 'Params' => '参数', + 'Permission' => '权限', + 'Begin time' => '开始时间', + 'End time' => '结束时间', + 'Create time' => '创建时间', + 'Flag' => '标志', + 'Home' => '首页', + 'Store' => '插件市场', + 'Services' => '服务', + 'Download' => '下载', + 'Demo' => '演示', + 'Donation' => '捐赠', + 'Forum' => '社区', + 'Docs' => '文档', + 'Go back' => '返回首页', + 'Jump now' => '立即跳转', + 'Please login first' => '请登录后再操作', + 'Send verification code' => '发送验证码', + 'Redirect now' => '立即跳转', + 'Operation completed' => '操作成功!', + 'Operation failed' => '操作失败!', + 'Unknown data format' => '未知的数据格式!', + 'Network error' => '网络错误!', + 'Advanced search' => '高级搜索', + 'Invalid parameters' => '未知参数', + 'No results were found' => '记录未找到', + 'Parameter %s can not be empty' => '参数%s不能为空', + 'You have no permission' => '你没有权限访问', + 'An unexpected error occurred' => '发生了一个意外错误,程序猿正在紧急处理中', + 'This page will be re-directed in %s seconds' => '页面将在 %s 秒后自动跳转', +]; diff --git a/app/common/library/Auth.php b/app/common/library/Auth.php new file mode 100644 index 0000000..5ffe583 --- /dev/null +++ b/app/common/library/Auth.php @@ -0,0 +1,672 @@ +config = array_merge($this->config, $config); + } + $this->options = array_merge($this->config, $options); + } + + /** + * @param array $options 参数 + * + * @return Auth + */ + public static function instance($options = []) + { + return app()->auth; + } + + /** + * 获取User模型. + * + * @return User + */ + public function getUser() + { + return $this->_user; + } + + /** + * 兼容调用user模型的属性. + * + * @param string $name + * + * @return mixed + */ + public function __get($name) + { + return $this->_user ? $this->_user->$name : null; + } + + /** + * 根据Token初始化. + * + * @param string $token Token + * + * @return bool + */ + public function init($token,$is_shop=0) + { + if ($this->_logined) { + return true; + } + if ($this->_error) { + return false; + } + if($is_shop){ + $jwt=JWT::decode($token, Env::get('app.app_key', 'default'), array('HS256')); + if ($jwt->jti[0] > 0) { + $user=Db::connect('shop')->name('nk_user')->where('user_id',$jwt->jti[0])->find(); + if (!$user){ + $this->setError('Account not exist'); + return false; + } + $user = User::find($user['n_user_id']); + if (! $user) { + $this->setError('Account not exist'); + + return false; + } + if ($user['status'] != 'normal') { + $this->setError('Account is locked'); + + return false; + } + $this->_user = $user; + $this->_logined = true; + $this->_token = $token; + + //初始化成功的事件 + Event::trigger('user_init_successed', $this->_user); + + return true; + } else { + $this->setError('You are not logged in'); + + return false; + } + }else{ + $data = Token::get($token); + if (! $data) { + return false; + } + $user_id = intval($data['user_id']); + if ($user_id > 0) { + $user = User::find($user_id); + if (! $user) { + $this->setError('Account not exist'); + + return false; + } + if ($user['status'] != 'normal') { + $this->setError('Account is locked'); + + return false; + } + $this->_user = $user; + $this->_logined = true; + $this->_token = $token; + + //初始化成功的事件 + Event::trigger('user_init_successed', $this->_user); + + return true; + } else { + $this->setError('You are not logged in'); + + return false; + } + } + } + + /** + * 注册用户. + * + * @param string $username 用户名 + * @param string $password 密码 + * @param string $email 邮箱 + * @param string $mobile 手机号 + * @param array $extend 扩展参数 + * + * @return bool + */ + public function register($username, $password, $email = '', $mobile = '', $extend = []) + { + // 检测用户名或邮箱、手机号是否存在 + if (User::getByUsername($username)) { + $this->setError('Username already exist'); + + return false; + } + if ($email && User::getByEmail($email)) { + $this->setError('Email already exist'); + + return false; + } + if ($mobile && User::getByMobile($mobile)) { + $this->setError('Mobile already exist'); + + return false; + } + + $ip = request()->ip(); + $time = time(); + + $data = [ + 'username' => $username, + 'password' => $password, + 'email' => $email, + 'mobile' => $mobile, + 'level' => 1, + 'group_id' => 1, + 'score' => 0, + 'avatar' => 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230111/58c84995456bc63b2660d9526f7062fc.png', + ]; + $params = array_merge($data, [ + 'nickname' => $username, + 'salt' => Random::alnum(), + 'jointime' => $time, + 'joinip' => $ip, + 'logintime' => $time, + 'loginip' => $ip, + 'prevtime' => $time, + 'status' => 'normal', + ]); + $params['password'] = $this->getEncryptPassword($password, $params['salt']); + $params = array_merge($params, $extend); + + //账号注册时需要开启事务,避免出现垃圾数据 + Db::startTrans(); + + try { + $user = User::create($params); + + $this->_user = User::find($user->id); + + //设置Token + $this->_token = Random::uuid(); + Token::set($this->_token, $user->id, $this->keeptime); + + //注册成功的事件 + Event::trigger('user_register_successed', $this->_user); + Db::commit(); + } catch (Exception $e) { + $this->setError($e->getMessage()); + Db::rollback(); + + return false; + } + + return true; + } + + /** + * 用户登录. + * + * @param string $account 账号,用户名、邮箱、手机号 + * @param string $password 密码 + * + * @return bool + */ + public function login($account, $password) + { + $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, + '/^1\d{10}$/') ? 'mobile' : 'username'); + $user = User::where([$field => $account])->find(); + if (! $user) { + $this->setError('Account is incorrect'); + + return false; + } + + if ($user->status != 'normal') { + $this->setError('Account is locked'); + + return false; + } + if ($user->password != $this->getEncryptPassword($password, $user->salt)) { + $this->setError('Password is incorrect'); + + return false; + } + + //直接登录会员 + $this->direct($user->id); + + return true; + } + + /** + * 注销 + * + * @return bool + */ + public function logout() + { + if (! $this->_logined) { + $this->setError('You are not logged in'); + + return false; + } + //设置登录标识 + $this->_logined = false; + //删除Token + //Token::delete($this->_token); + Token::clear($this->_user->id); + //注销成功的事件 + Event::trigger('user_logout_successed', $this->_user); + + return true; + } + + /** + * 修改密码 + * + * @param string $newpassword 新密码 + * @param string $oldpassword 旧密码 + * @param bool $ignoreoldpassword 忽略旧密码 + * + * @return bool + */ + public function changepwd($newpassword, $oldpassword = '', $ignoreoldpassword = false) + { + if (! $this->_logined) { + $this->setError('You are not logged in'); + + return false; + } + //判断旧密码是否正确 + if ($this->_user->password == $this->getEncryptPassword($oldpassword, + $this->_user->salt) || $ignoreoldpassword) { + Db::startTrans(); + + try { + $salt = Random::alnum(); + $newpassword = $this->getEncryptPassword($newpassword, $salt); + $this->_user->save(['loginfailure' => 0, 'password' => $newpassword, 'salt' => $salt]); + + //Token::delete($this->_token); + Token::clear($this->_user->id); + //修改密码成功的事件 + Event::trigger('user_changepwd_successed', $this->_user); + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + $this->setError($e->getMessage()); + + return false; + } + + return true; + } else { + $this->setError('Password is incorrect'); + + return false; + } + } + + /** + * 直接登录账号. + * + * @param int $user_id + * + * @return bool + */ + public function direct($user_id) + { + $user = User::find($user_id); + if ($user) { + Db::startTrans(); + + try { + $ip = request()->ip(); + $time = time(); + + //判断连续登录和最大连续登录 + if ($user->logintime < \fast\Date::unixtime('day')) { + $user->successions = $user->logintime < \fast\Date::unixtime('day', + -1) ? 1 : $user->successions + 1; + $user->maxsuccessions = max($user->successions, $user->maxsuccessions); + } + + $user->prevtime = $user->logintime; + //记录本次登录的IP和时间 + $user->loginip = $ip; + $user->logintime = $time; + //重置登录失败次数 + $user->loginfailure = 0; + + $user->save(); + + $this->_user = $user; + + $this->_token = Random::uuid(); + Token::set($this->_token, $user->id, $this->keeptime); + + $this->_logined = true; + + //登录成功的事件 + Event::trigger('user_login_successed', $this->_user); + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + $this->setError($e->getMessage()); + + return false; + } + + return true; + } else { + return false; + } + } + + /** + * 检测是否是否有对应权限. + * + * @param string $path 控制器/方法 + * @param string $module 模块 默认为当前模块 + * + * @return bool + */ + public function check($path = null, $module = null) + { + if (! $this->_logined) { + return false; + } + + $ruleList = $this->getRuleList(); + $rules = []; + foreach ($ruleList as $k => $v) { + $rules[] = $v['name']; + } + $url = ($module ? $module : app()->http->getName()).'/'.(is_null($path) ? $this->getRequestUri() : $path); + $url = strtolower(str_replace('.', '/', $url)); + + return in_array($url, $rules) ? true : false; + } + + /** + * 判断是否登录. + * + * @return bool + */ + public function isLogin() + { + if ($this->_logined) { + return true; + } + + return false; + } + + /** + * 获取当前Token. + * + * @return string + */ + public function getToken() + { + return $this->_token; + } + + /** + * 获取会员基本信息. + */ + public function getUserinfo() + { + $data = $this->_user->toArray(); + $allowFields = $this->getAllowFields(); + $userinfo = array_intersect_key($data, array_flip($allowFields)); + $userinfo = array_merge($userinfo, Token::get($this->_token)); + + return $userinfo; + } + + /** + * 获取会员组别规则列表. + * + * @return array + */ + public function getRuleList() + { + if ($this->rules) { + return $this->rules; + } + $group = $this->_user->group; + if (! $group) { + return []; + } + $rules = explode(',', $group->rules); + $this->rules = UserRule::where('status', 'normal')->where('id', 'in', + $rules)->field('id,pid,name,title,ismenu')->select(); + + return $this->rules; + } + + /** + * 获取当前请求的URI. + * + * @return string + */ + public function getRequestUri() + { + return $this->requestUri; + } + + /** + * 设置当前请求的URI. + * + * @param string $uri + */ + public function setRequestUri($uri) + { + $this->requestUri = $uri; + } + + /** + * 获取允许输出的字段. + * + * @return array + */ + public function getAllowFields() + { + return $this->allowFields; + } + + /** + * 设置允许输出的字段. + * + * @param array $fields + */ + public function setAllowFields($fields) + { + $this->allowFields = $fields; + } + + /** + * 删除一个指定会员. + * + * @param int $user_id 会员ID + * + * @return bool + */ + public function delete($user_id) + { + $user = User::find($user_id); + if (! $user) { + return false; + } + Db::startTrans(); + + try { + // 删除会员 + User::destroy($user_id); + // 删除会员指定的所有Token + Token::clear($user_id); + + Event::trigger('user_delete_successed', $user); + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + $this->setError($e->getMessage()); + + return false; + } + + return true; + } + + /** + * 获取密码加密后的字符串. + * + * @param string $password 密码 + * @param string $salt 密码盐 + * + * @return string + */ + public function getEncryptPassword($password, $salt = '') + { + return md5(md5($password).$salt); + } + + /** + * 检测当前控制器和方法是否匹配传递的数组. + * + * @param array $arr 需要验证权限的数组 + * + * @return bool + */ + public function match($arr = []) + { + $request = Request::instance(); + $arr = is_array($arr) ? $arr : explode(',', $arr); + if (! $arr) { + return false; + } + $arr = array_map('strtolower', $arr); + // 是否存在 + if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) { + return true; + } + + // 没找到匹配 + return false; + } + + /** + * 设置会话有效时间. + * + * @param int $keeptime 默认为永久 + */ + public function keeptime($keeptime = 0) + { + $this->keeptime = $keeptime; + } + + /** + * 渲染用户数据. + * + * @param array $datalist 二维数组 + * @param mixed $fields 加载的字段列表 + * @param string $fieldkey 渲染的字段 + * @param string $renderkey 结果字段 + * + * @return array + */ + public function render(&$datalist, $fields = [], $fieldkey = 'user_id', $renderkey = 'userinfo') + { + $fields = ! $fields ? ['id', 'nickname', 'level', 'avatar'] : (is_array($fields) ? $fields : explode(',', + $fields)); + $ids = []; + foreach ($datalist as $k => $v) { + if (! isset($v[$fieldkey])) { + continue; + } + $ids[] = $v[$fieldkey]; + } + $list = []; + if ($ids) { + if (! in_array('id', $fields)) { + $fields[] = 'id'; + } + $ids = array_unique($ids); + $selectlist = User::where('id', 'in', $ids)->column($fields); + foreach ($selectlist as $k => $v) { + $list[$v['id']] = $v; + } + } + foreach ($datalist as $k => &$v) { + $v[$renderkey] = isset($list[$v[$fieldkey]]) ? $list[$v[$fieldkey]] : null; + } + unset($v); + + return $datalist; + } + + /** + * 设置错误信息. + * + * @param $error 错误信息 + * + * @return Auth + */ + public function setError($error) + { + $this->_error = $error; + + return $this; + } + + /** + * 获取错误信息. + * + * @return string + */ + public function getError() + { + return $this->_error ? __($this->_error) : ''; + } +} diff --git a/app/common/library/Email.php b/app/common/library/Email.php new file mode 100644 index 0000000..023046f --- /dev/null +++ b/app/common/library/Email.php @@ -0,0 +1,215 @@ + 'utf-8', //编码格式 + 'debug' => 0, //调式模式 + ]; + + /** + * 初始化. + * + * @param array $options 参数 + * + * @return Email + */ + public static function instance($options = []) + { + if (is_null(self::$instance)) { + self::$instance = new static($options); + } + + return self::$instance; + } + + /** + * 构造函数. + * + * @param array $options + */ + public function __construct($options = []) + { + if ($config = Config::get('site')) { + $this->options = array_merge($this->options, $config); + } + $this->options = array_merge($this->options, $options); + + $securArr = [1 => 'tls', 2 => 'ssl']; + + $this->mail = new PHPMailer(true); + $this->mail->CharSet = $this->options['charset']; + $this->mail->SMTPDebug = $this->options['debug']; + $this->mail->isSMTP(); + $this->mail->SMTPAuth = true; + $this->mail->Host = $this->options['mail_smtp_host']; + $this->mail->Username = $this->options['mail_from']; + $this->mail->Password = $this->options['mail_smtp_pass']; + $this->mail->SMTPSecure = isset($securArr[$this->options['mail_verify_type']]) ? $securArr[$this->options['mail_verify_type']] : ''; + $this->mail->Port = $this->options['mail_smtp_port']; + + //设置发件人 + $this->from($this->options['mail_from'], $this->options['mail_smtp_user']); + } + + /** + * 设置邮件主题. + * + * @param string $subject + * + * @return $this + */ + public function subject($subject) + { + $this->options['subject'] = $subject; + + return $this; + } + + /** + * 设置发件人. + * + * @param string $email + * @param string $name + * + * @return $this + */ + public function from($email, $name = '') + { + $this->options['from'] = $email; + $this->options['from_name'] = $name; + + return $this; + } + + /** + * 设置收件人. + * + * @param string $email + * @param string $name + * + * @return $this + */ + public function to($email, $name = '') + { + $this->options['to'] = $email; + $this->options['to_name'] = $name; + + return $this; + } + + /** + * 设置邮件正文. + * + * @param string $body + * @param bool $ishtml + * + * @return $this + */ + public function message($body, $ishtml = true) + { + $this->options['body'] = $body; + $this->options['ishtml'] = $ishtml; + + return $this; + } + + /** + * 获取最后产生的错误. + * + * @return string + */ + public function getError() + { + return $this->_error; + } + + /** + * 设置错误. + * + * @param string $error 信息信息 + */ + protected function setError($error) + { + $this->_error = $error; + } + + /** + * 发送邮件. + * + * @return bool + */ + public function send() + { + $result = false; + switch ($this->options['mail_type']) { + case 1: + //使用phpmailer发送 + $this->mail->setFrom($this->options['from'], $this->options['from_name']); + $this->mail->addAddress($this->options['to'], $this->options['to_name']); + $this->mail->Subject = $this->options['subject']; + if ($this->options['ishtml']) { + $this->mail->msgHTML($this->options['body']); + } else { + $this->mail->Body = $this->options['body']; + } + try { + $result = $this->mail->send(); + } catch (Exception $e) { + $this->setError($e->getMessage()); + } + + $this->setError($result ? '' : $this->mail->ErrorInfo); + break; + case 2: + //使用mail方法发送邮件 + $headers = 'MIME-Version: 1.0'."\r\n"; + $headers .= 'Content-type: text/html; charset='.$this->options['charset']."\r\n"; + $headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人 + $headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人 + $result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers); + $this->setError($result ? '' : error_get_last()['message']); + break; + default: + //邮件功能已关闭 + $this->setError(__('Mail already closed')); + break; + } + + return $result; + } +} diff --git a/app/common/library/Ems.php b/app/common/library/Ems.php new file mode 100644 index 0000000..40a044d --- /dev/null +++ b/app/common/library/Ems.php @@ -0,0 +1,160 @@ + $email, 'event' => $event]) + ->order('id', 'DESC') + ->find(); + Event::trigger('ems_get', $ems, true); + + return $ems ? $ems : null; + } + + /** + * 发送验证码 + * + * @param int $email 邮箱 + * @param int $code 验证码,为空时将自动生成4位数字 + * @param string $event 事件 + * + * @return bool + */ + public static function send($email, $code = null, $event = 'default') + { + $code = is_null($code) ? mt_rand(1000, 9999) : $code; + $time = time(); + $ip = request()->ip(); + $ems = \app\common\model\Ems::create([ + 'event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, + 'createtime' => $time, + ]); + $result = Event::trigger('ems_send', $ems, true); + if (! $result) { + $ems->delete(); + + return false; + } + + return true; + } + + /** + * 发送通知. + * + * @param mixed $email 邮箱,多个以,分隔 + * @param string $msg 消息内容 + * @param string $template 消息模板 + * + * @return bool + */ + public static function notice($email, $msg = '', $template = null) + { + $params = [ + 'email' => $email, + 'msg' => $msg, + 'template' => $template, + ]; + $result = Event::trigger('ems_notice', $params, true); + + return $result ? true : false; + } + + /** + * 校验验证码 + * + * @param int $email 邮箱 + * @param int $code 验证码 + * @param string $event 事件 + * + * @return bool + */ + public static function check($email, $code, $event = 'default') + { + $time = time() - self::$expire; + $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event]) + ->order('id', 'DESC') + ->find(); + if ($ems) { + if ($ems['createtime'] > $time && $ems['times'] <= self::$maxCheckNums) { + $correct = $code == $ems['code']; + if (! $correct) { + $ems->times = $ems->times + 1; + $ems->save(); + + return false; + } else { + $result = Event::trigger('ems_check', $ems, true); + + return true; + } + } else { + // 过期则清空该邮箱验证码 + self::flush($email, $event); + + return false; + } + } else { + return false; + } + } + + /** + * 清空指定邮箱验证码 + * + * @param int $email 邮箱 + * @param string $event 事件 + * + * @return bool + */ + public static function flush($email, $event = 'default') + { + \app\common\model\Ems:: + where(['email' => $email, 'event' => $event]) + ->delete(); + Event::trigger('ems_flush'); + + return true; + } +} diff --git a/app/common/library/Menu.php b/app/common/library/Menu.php new file mode 100644 index 0000000..6bebf34 --- /dev/null +++ b/app/common/library/Menu.php @@ -0,0 +1,238 @@ +update(['status' => 'normal']); + + return true; + } + + /** + * 禁用菜单. + * + * @param string $name + * + * @return bool + */ + public static function disable($name) + { + $ids = self::getAuthRuleIdsByName($name); + if (! $ids) { + return false; + } + AuthRule::where('id', 'in', $ids)->update(['status' => 'hidden']); + + return true; + } + + /** + * 升级菜单 + * @param string $name 插件名称 + * @param array $menu 新菜单 + * @return bool + */ + public static function upgrade($name, $menu) + { + $ids = self::getAuthRuleIdsByName($name); + $old = AuthRule::where('id', 'in', $ids)->select(); + $old = $old ? $old->toArray() : []; + $old = array_column($old, null, 'name'); + Db::startTrans(); + try { + self::menuUpdate($menu, $old); + + $ids = []; + foreach ($old as $index => $item) { + if (!isset($item['keep'])) { + $ids[] = $item['id']; + } + } + if ($ids) { + //旧版本的菜单需要做删除处理 + $config = AddonService::config($name); + $menus = isset($config['menus']) ? $config['menus'] : []; + $where[] = ['id' ,'in', $ids]; + + if ($menus) { + //必须是旧版本中的菜单,可排除用户自主创建的菜单 + $where[] = ['name' ,'in', $menus]; + } + AuthRule::where($where)->delete(); + } + + Db::commit(); + } catch (\PDOException $e) { + Db::rollback(); + return false; + } + + Menu::refresh($name, $menu); + return true; + } + + /** + * 刷新插件菜单配置缓存 + * @param string $name + * @param array $menu + */ + public static function refresh($name, $menu = []) + { + if (!$menu) { + // $menu为空时表示首次安装,首次安装需刷新插件菜单标识缓存 + $menuIds = self::getAuthRuleIdsByName($name); + $menus = Db::name("auth_rule")->where('id', 'in', $menuIds)->column('name'); + } else { + // 刷新新的菜单缓存 + $getMenus = function ($menu) use (&$getMenus) { + $result = []; + foreach ($menu as $index => $item) { + $result[] = $item['name']; + $result = array_merge($result, isset($item['sublist']) && is_array($item['sublist']) ? $getMenus($item['sublist']) : []); + } + return $result; + }; + $menus = $getMenus($menu); + } + + //刷新新的插件核心菜单缓存 + AddonService::config($name, ['menus' => $menus]); + } + + /** + * 导出指定名称的菜单规则. + * + * @param string $name + * + * @return array + */ + public static function export($name) + { + $ids = self::getAuthRuleIdsByName($name); + if (! $ids) { + return []; + } + $menuList = []; + $menu = AuthRule::getByName($name); + if ($menu) { + $ruleList = AuthRule::where('id', 'in', $ids)->select()->toArray(); + $menuList = Tree::instance()->init($ruleList)->getTreeArray($menu['id']); + } + + return $menuList; + } + + /** + * 菜单升级 + * @param array $newMenu + * @param array $oldMenu + * @param int $parent + * @throws Exception + */ + private static function menuUpdate($newMenu, &$oldMenu, $parent = 0) + { + if (!is_numeric($parent)) { + $parentRule = AuthRule::getByName($parent); + $pid = $parentRule ? $parentRule['id'] : 0; + } else { + $pid = $parent; + } + $allow = array_flip(['file', 'name', 'title', 'icon', 'condition', 'remark', 'ismenu', 'weigh']); + foreach ($newMenu as $k => $v) { + $hasChild = isset($v['sublist']) && $v['sublist'] ? true : false; + $data = array_intersect_key($v, $allow); + $data['ismenu'] = isset($data['ismenu']) ? $data['ismenu'] : ($hasChild ? 1 : 0); + $data['icon'] = isset($data['icon']) ? $data['icon'] : ($hasChild ? 'fa fa-list' : 'fa fa-circle-o'); + $data['pid'] = $pid; + $data['status'] = 'normal'; + if (!isset($oldMenu[$data['name']])) { + $menu = AuthRule::create($data); + } else { + $menu = $oldMenu[$data['name']]; + //更新旧菜单 + AuthRule::update($data, ['id' => $menu['id']]); + $oldMenu[$data['name']]['keep'] = true; + } + if ($hasChild) { + self::menuUpdate($v['sublist'], $oldMenu, $menu['id']); + } + } + } + + /** + * 根据名称获取规则IDS. + * + * @param string $name + * + * @return array + */ + public static function getAuthRuleIdsByName($name) + { + $ids = []; + $menu = AuthRule::getByName($name); + if ($menu) { + // 必须将结果集转换为数组 + $ruleList = AuthRule::order('weigh', 'desc')->field('id,pid,name')->select()->toArray(); + // 构造菜单数据 + $ids = Tree::instance()->init($ruleList)->getChildrenIds($menu['id'], true); + } + + return $ids; + } +} diff --git a/app/common/library/Sms.php b/app/common/library/Sms.php new file mode 100644 index 0000000..1aa643e --- /dev/null +++ b/app/common/library/Sms.php @@ -0,0 +1,159 @@ + $mobile, 'event' => $event]) + ->order('id', 'DESC') + ->find(); + Event::trigger('sms_get', $sms, true); + + return $sms ? $sms : null; + } + + /** + * 发送验证码 + * + * @param int $mobile 手机号 + * @param int $code 验证码,为空时将自动生成4位数字 + * @param string $event 事件 + * + * @return bool + */ + public static function send($mobile, $code = null, $event = 'default') + { + $code = is_null($code) ? mt_rand(1000, 9999) : $code; + $time = time(); + $ip = request()->ip(); + $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]); + $result = Event::trigger('sms_send', $sms,true); + if (! $result) { + $sms->delete(); + + return false; + } + + return true; + } + + /** + * 发送通知. + * + * @param mixed $mobile 手机号,多个以,分隔 + * @param string $msg 消息内容 + * @param string $template 消息模板 + * @param array $data 模板变量 + * + * @return bool + */ + public static function notice($mobile, $msg = '', $template = null, $data = []) + { + $params = [ + 'mobile' => $mobile, + 'msg' => $msg, + 'template' => $template, + 'data' => $data, + ]; + $result = Event::trigger('sms_notice', $params, true); + + return $result ? true : false; + } + + /** + * 校验验证码 + * + * @param int $mobile 手机号 + * @param int $code 验证码 + * @param string $event 事件 + * + * @return bool + */ + public static function check($mobile, $code, $event = 'default') + { + $time = time() - self::$expire; + $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event]) + ->order('id', 'DESC') + ->find(); + if ($sms) { + if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) { + $correct = $code == $sms['code']; + if (! $correct) { + $sms->times = $sms->times + 1; + $sms->save(); + + return false; + } else { + $result = Event::trigger('sms_check', $sms, true); + + return $result; + } + } else { + // 过期则清空该手机验证码 + self::flush($mobile, $event); + + return false; + } + } else { + return false; + } + } + + /** + * 清空指定手机号验证码 + * + * @param int $mobile 手机号 + * @param string $event 事件 + * + * @return bool + */ + public static function flush($mobile, $event = 'default') + { + \app\common\model\Sms:: + where(['mobile' => $mobile, 'event' => $event]) + ->delete(); + Event::trigger('sms_flush'); + + return true; + } +} diff --git a/app/common/library/Token.php b/app/common/library/Token.php new file mode 100644 index 0000000..d83c9d6 --- /dev/null +++ b/app/common/library/Token.php @@ -0,0 +1,168 @@ +check($token, $user_id); + } + + /** + * 读取Token. + * + * @param string $token Token标识 + * @param mixed $default 默认值 + * + * @return mixed + */ + public static function get($token, $default = false) + { + return self::init()->get($token, $default); + } + + /** + * 写入Token. + * + * @param string $token Token标识 + * @param mixed $user_id 存储数据 + * @param int|null $expire 有效时间 0为永久 + * + * @return bool + */ + public static function set($token, $user_id, $expire = null) + { + return self::init()->set($token, $user_id, $expire); + } + + /** + * 删除Token(delete别名). + * + * @param string $token Token标识 + * + * @return bool + */ + public static function rm($token) + { + return self::delete($token); + } + + /** + * 删除Token. + * + * @param string $token 标签名 + * + * @return bool + */ + public static function delete($token) + { + return self::init()->delete($token); + } + + /** + * 清除Token. + * + * @param string $token Token标记 + * + * @return bool + */ + public static function clear($user_id = null) + { + return self::init()->clear($user_id); + } +} diff --git a/app/common/library/token/Driver.php b/app/common/library/token/Driver.php new file mode 100644 index 0000000..e77727b --- /dev/null +++ b/app/common/library/token/Driver.php @@ -0,0 +1,106 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\library\token; + +/** + * Token基础类. + */ +abstract class Driver +{ + protected $handler = null; + protected $options = []; + + /** + * 存储Token. + * + * @param string $token Token + * @param int $user_id 会员ID + * @param int $expire 过期时长,0表示无限,单位秒 + * + * @return bool + */ + abstract public function set($token, $user_id, $expire = 0); + + /** + * 获取Token内的信息. + * + * @param string $token + * + * @return array + */ + abstract public function get($token); + + /** + * 判断Token是否可用. + * + * @param string $token Token + * @param int $user_id 会员ID + * + * @return bool + */ + abstract public function check($token, $user_id); + + /** + * 删除Token. + * + * @param string $token + * + * @return bool + */ + abstract public function delete($token); + + /** + * 删除指定用户的所有Token. + * + * @param int $user_id + * + * @return bool + */ + abstract public function clear($user_id); + + /** + * 返回句柄对象,可执行其它高级方法. + * + * @return object + */ + public function handler() + { + return $this->handler; + } + + /** + * 获取加密后的Token. + * + * @param string $token Token标识 + * + * @return string + */ + protected function getEncryptedToken($token) + { + $config = \think\facade\Config::get('token'); + + return hash_hmac($config['hashalgo'], $token, $config['key']); + } + + /** + * 获取过期剩余时长 + * + * @param $expiretime + * + * @return float|int|mixed + */ + protected function getExpiredIn($expiretime) + { + return $expiretime ? max(0, $expiretime - time()) : 365 * 86400; + } +} diff --git a/app/common/library/token/driver/Mysql.php b/app/common/library/token/driver/Mysql.php new file mode 100644 index 0000000..10d3823 --- /dev/null +++ b/app/common/library/token/driver/Mysql.php @@ -0,0 +1,135 @@ + 'user_token', + 'expire' => 2592000, + 'connection' => [], + ]; + + /** + * 构造函数. + * + * @param array $options 参数 + */ + public function __construct($options = []) + { + if (! empty($options)) { + $this->options = array_merge($this->options, $options); + } + + } + + /** + * @return \think\db\BaseQuery|\think\facade\Db + */ + private function getHandler(){ + if ($this->options['connection']) { + return\think\facade\Db::connect($this->options['connection'])->name($this->options['table']); + } else { + return \think\facade\Db::name($this->options['table']); + } + } + /** + * 存储Token. + * + * @param string $token Token + * @param int $user_id 会员ID + * @param int $expire 过期时长,0表示无限,单位秒 + * + * @return bool + */ + public function set($token, $user_id, $expire = null) + { + $expiretime = ! is_null($expire) && $expire !== 0 ? time() + $expire : 0; + $token = $this->getEncryptedToken($token); + $this->getHandler()->insert([ + 'token' => $token, 'user_id' => $user_id, 'createtime' => time(), + 'expiretime' => $expiretime, + ]); + + return true; + } + + /** + * 获取Token内的信息. + * + * @param string $token + * + * @return array + */ + public function get($token) + { + $data = $this->getHandler()->where('token', $this->getEncryptedToken($token))->find(); + if ($data) { + if (! $data['expiretime'] || $data['expiretime'] > time()) { + //返回未加密的token给客户端使用 + $data['token'] = $token; + //返回剩余有效时间 + $data['expires_in'] = $this->getExpiredIn($data['expiretime']); + + return $data; + } else { + self::delete($token); + } + } + + return []; + } + + /** + * 判断Token是否可用. + * + * @param string $token Token + * @param int $user_id 会员ID + * + * @return bool + */ + public function check($token, $user_id) + { + $data = $this->get($token); + + return $data && $data['user_id'] == $user_id ? true : false; + } + + /** + * 删除Token. + * + * @param string $token + * + * @return bool + */ + public function delete($token) + { + $this->getHandler()->where('token', $this->getEncryptedToken($token))->delete(); + + return true; + } + + /** + * 删除指定用户的所有Token. + * + * @param int $user_id + * + * @return bool + */ + public function clear($user_id) + { + $this->getHandler()->where('user_id', $user_id)->delete(); + + return true; + } +} diff --git a/app/common/library/token/driver/Redis.php b/app/common/library/token/driver/Redis.php new file mode 100644 index 0000000..3425554 --- /dev/null +++ b/app/common/library/token/driver/Redis.php @@ -0,0 +1,184 @@ + '127.0.0.1', + 'port' => 6379, + 'password' => '', + 'select' => 0, + 'timeout' => 0, + 'expire' => 0, + 'persistent' => false, + 'userprefix' => 'up:', + 'tokenprefix' => 'tp:', + ]; + + /** + * 构造函数. + * + * @param array $options 缓存参数 + * + * @throws \BadFunctionCallException + */ + public function __construct($options = []) + { + if (! extension_loaded('redis')) { + throw new \BadFunctionCallException('not support: redis'); + } + if (! empty($options)) { + $this->options = array_merge($this->options, $options); + } + $this->handler = new \Redis(); + if ($this->options['persistent']) { + $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_'.$this->options['select']); + } else { + $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']); + } + + if ('' != $this->options['password']) { + $this->handler->auth($this->options['password']); + } + + if (0 != $this->options['select']) { + $this->handler->select($this->options['select']); + } + } + + /** + * 获取加密后的Token. + * + * @param string $token Token标识 + * + * @return string + */ + protected function getEncryptedToken($token) + { + $config = \think\Config::get('token'); + + return $this->options['tokenprefix'].hash_hmac($config['hashalgo'], $token, $config['key']); + } + + /** + * 获取会员的key. + * + * @param $user_id + * + * @return string + */ + protected function getUserKey($user_id) + { + return $this->options['userprefix'].$user_id; + } + + /** + * 存储Token. + * + * @param string $token Token + * @param int $user_id 会员ID + * @param int $expire 过期时长,0表示无限,单位秒 + * + * @return bool + */ + public function set($token, $user_id, $expire = 0) + { + if (is_null($expire)) { + $expire = $this->options['expire']; + } + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp() - time(); + } + $key = $this->getEncryptedToken($token); + if ($expire) { + $result = $this->handler->setex($key, $expire, $user_id); + } else { + $result = $this->handler->set($key, $user_id); + } + //写入会员关联的token + $this->handler->sAdd($this->getUserKey($user_id), $key); + + return $result; + } + + /** + * 获取Token内的信息. + * + * @param string $token + * + * @return array + */ + public function get($token) + { + $key = $this->getEncryptedToken($token); + $value = $this->handler->get($key); + if (is_null($value) || false === $value) { + return []; + } + //获取有效期 + $expire = $this->handler->ttl($key); + $expire = $expire < 0 ? 365 * 86400 : $expire; + $expiretime = time() + $expire; + //解决使用redis方式储存token时api接口Token刷新与检测因expires_in拼写错误报错的BUG + $result = ['token' => $token, 'user_id' => $value, 'expiretime' => $expiretime, 'expires_in' => $expire]; + + return $result; + } + + /** + * 判断Token是否可用. + * + * @param string $token Token + * @param int $user_id 会员ID + * + * @return bool + */ + public function check($token, $user_id) + { + $data = self::get($token); + + return $data && $data['user_id'] == $user_id ? true : false; + } + + /** + * 删除Token. + * + * @param string $token + * + * @return bool + */ + public function delete($token) + { + $data = $this->get($token); + if ($data) { + $key = $this->getEncryptedToken($token); + $user_id = $data['user_id']; + $this->handler->del($key); + $this->handler->sRem($this->getUserKey($user_id), $key); + } + + return true; + } + + /** + * 删除指定用户的所有Token. + * + * @param int $user_id + * + * @return bool + */ + public function clear($user_id) + { + $keys = $this->handler->sMembers($this->getUserKey($user_id)); + $this->handler->del($this->getUserKey($user_id)); + $this->handler->del($keys); + + return true; + } +} diff --git a/app/common/library/traits/Jump.php b/app/common/library/traits/Jump.php new file mode 100644 index 0000000..111cd78 --- /dev/null +++ b/app/common/library/traits/Jump.php @@ -0,0 +1,152 @@ +error(); + * $this->redirect(); + * } + * }. + */ + +namespace app\common\library\traits; + +use think\Response; +use think\facade\Config; +use think\facade\Request; +use think\response\Redirect; +use think\facade\View as ViewTemplate; +use think\exception\HttpResponseException; + +trait Jump +{ + /** + * 操作成功跳转的快捷方法. + * + * @param mixed $msg 提示信息 + * @param string $url 跳转的 URL 地址 + * @param mixed $data 返回的数据 + * @param int $wait 跳转等待时间 + * @param array $header 发送的 Header 信息 + * + * @throws \Exception + */ + protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = []) + { + if (is_null($url) && ! is_null(Request::server('HTTP_REFERER'))) { + $url = Request::server('HTTP_REFERER'); + } elseif ('' !== $url && ! strpos($url, '://') && 0 !== strpos($url, '/')) { + $url = url($url); + } + + $type = $this->getResponseType(); + $result = [ + 'code' => 1, + 'msg' => $msg, + 'data' => $data, + 'url' => $url, + 'wait' => $wait, + ]; + + if ('html' == strtolower($type)) { + $result = ViewTemplate::fetch(Config::get('app.dispatch_success_tmpl'), $result); + } + + $response = Response::create($result, $type)->header($header); + + throw new HttpResponseException($response); + } + + /** + * 操作错误跳转的快捷方法. + * + * @param mixed $msg 提示信息 + * @param string $url 跳转的 URL 地址 + * @param mixed $data 返回的数据 + * @param int $wait 跳转等待时间 + * @param array $header 发送的 Header 信息 + * + * @throws \Exception + */ + protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []) + { + if (is_null($url)) { + $url = Request::isAjax() ? '' : 'javascript:history.back(-1);'; + } elseif ('' !== $url && ! strpos($url, '://') && 0 !== strpos($url, '/')) { + $url = url($url); + } + + $type = $this->getResponseType(); + $result = [ + 'code' => 0, + 'msg' => $msg, + 'data' => $data, + 'url' => $url, + 'wait' => $wait, + ]; + + if ('html' == strtolower($type)) { + $result = ViewTemplate::fetch(Config::get('app.dispatch_success_tmpl'), $result); + } + $response = Response::create($result, $type)->header($header); + + throw new HttpResponseException($response); + } + + /** + * 返回封装后的 API 数据到客户端. + * + * @param mixed $data 要返回的数据 + * @param int $code 返回的 code + * @param mixed $msg 提示信息 + * @param string $type 返回数据格式 + * @param array $header 发送的 Header 信息 + */ + protected function result($data, $code = 0, $msg = '', $type = '', array $header = []) + { + $result = [ + 'code' => $code, + 'msg' => $msg, + 'time' => Request::server('REQUEST_TIME'), + 'data' => $data, + ]; + $type = $type ?: $this->getResponseType(); + $response = Response::create($result, $type)->header($header); + + throw new HttpResponseException($response); + } + + /** + * URL 重定向. + * + * @param string $url 跳转的 URL 表达式 + * @param array|int $params 其它 URL 参数 + * @param int $code http code + * @param array $with 隐式传参 + */ + protected function redirect($url, $params = [], $code = 302, $with = []) + { + if (is_int($params)) { + $code = $params; + } + $response = \redirect($url); + $response->code($code)->with($with); + + throw new HttpResponseException($response); + } + + /** + * 获取当前的 response 输出类型. + * + * @return string + */ + protected function getResponseType() + { + return Request::isAjax() + ? Config::get('app.default_ajax_return') + : Config::get('app.default_return_type'); + } +} diff --git a/app/common/middleware/Addon.php b/app/common/middleware/Addon.php new file mode 100644 index 0000000..5a90711 --- /dev/null +++ b/app/common/middleware/Addon.php @@ -0,0 +1,47 @@ +param('addon')?$request->param('addon'):''; + $tpl_replace_string['__ADDON__'] = $cdnurl . "/assets/addons/" . $addon; + Config::set(['tpl_replace_string'=>$tpl_replace_string],'view'); + + hook('addon_middleware', $request); + + return $next($request); + } +} diff --git a/app/common/middleware/AllowOriginMiddleware.php b/app/common/middleware/AllowOriginMiddleware.php new file mode 100644 index 0000000..2a472bf --- /dev/null +++ b/app/common/middleware/AllowOriginMiddleware.php @@ -0,0 +1,70 @@ + +// +---------------------------------------------------------------------- + +declare(strict_types=1); + + +namespace app\common\middleware; + + +use Closure; +use think\Config; +use think\Request; +use think\Response; + +/** + * 跨域中间件 + * Class AllowOriginMiddleware + * @package app\http\middleware + */ +class AllowOriginMiddleware +{ + + protected $cookieDomain; + + protected $header = [ + 'Access-Control-Allow-Credentials' => '*', + 'Access-Control-Max-Age' => 1800, + 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS', + 'Access-Control-Allow-Headers' => 'token,X-Token, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With,Form-type,Referer,Connection,Content-Length,Host,Origin,Authorization,Authori-zation,Accept,Accept-Encoding', + ]; + + public function __construct(\think\Config $config) + { + $this->cookieDomain = $config->get('cookie.domain', ''); + } + + /** + * 允许跨域请求 + * @access public + * @param \think\Request $request + * @param Closure $next + * @param array $header + * @return Response + */ + public function handle($request, Closure $next, ? array $header = []) + { + $header = !empty($header) ? array_merge($this->header, $header) : $this->header; + + if (!isset($header['Access-Control-Allow-Origin'])) { + $origin = $request->header('origin'); + + if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) { + $header['Access-Control-Allow-Origin'] = $origin; + } else { + $header['Access-Control-Allow-Origin'] = '*'; + } + } + + return $next($request)->header($header); + } + +} diff --git a/app/common/middleware/FastInit.php b/app/common/middleware/FastInit.php new file mode 100644 index 0000000..34e072c --- /dev/null +++ b/app/common/middleware/FastInit.php @@ -0,0 +1,105 @@ +lang = new \think\Lang(app(),\config('lang')); +// app()->lang->detect($request); + app()->lang->switchLangSet(Lang::getLangSet()); + + // 设置替换内容 + $this->initReplaceString(); + //设置DEBUG环境 + $this->initDebugEnv(); + // 切换多语言 + if ($request->get('lang')) { + Cookie::set('think_var', $request->get('lang')); + } + // Form别名 + if (! class_exists('Form')) { + class_alias('fast\\Form', 'Form'); + } + + return $next($request); + } + + /** + * 模板内容替换 + */ + private function initReplaceString(){ + // 设置替换字符串 + $url = ltrim(dirname(app()->request->root()), DIRECTORY_SEPARATOR); + // 如果未设置__CDN__则自动匹配得出 + $tpl_replace_string = Config::get('view.tpl_replace_string'); + if (!Config::get('view.tpl_replace_string.__CDN__')) { + $tpl_replace_string['__CDN__']=$url; + } + // 如果未设置__PUBLIC__则自动匹配得出 + if (!Config::get('view.tpl_replace_string.__PUBLIC__')) { + $tpl_replace_string['__PUBLIC__']= $url . '/'; + } + // 如果未设置__ROOT__则自动匹配得出 + if (!Config::get('view.tpl_replace_string.__ROOT__')) { + $tpl_replace_string['__ROOT__']= preg_replace("/\/public\/$/", '', $url . '/'); + } + Config::set(['tpl_replace_string'=>$tpl_replace_string],'view'); + if (! Config::get('site.cdnurl')) { + Config::set(['cdnurl' => $url], 'site'); + } + // 如果未设置cdnurl则自动匹配得出 + if (! Config::get('upload.cdnurl')) { + Config::set(['cdnurl' => $url], 'upload'); + } + } + + /** + * 调试模式缓存 + */ + private function initDebugEnv(){ + if (Env::get('APP_DEBUG')) { + // 如果是调试模式将version置为当前的时间戳可避免缓存 + Config::set(['version' => time()], 'site'); + //如果是调试模式将关闭视图缓存 + Config::set(['tpl_cache' => false], 'view'); + // 如果是开发模式那么将异常模板修改成官方的 + Config::set(['exception_tmpl' => app()->getThinkPath().'tpl/think_exception.tpl']); + } + } +} diff --git a/app/common/model/Area.php b/app/common/model/Area.php new file mode 100644 index 0000000..bd59d9b --- /dev/null +++ b/app/common/model/Area.php @@ -0,0 +1,94 @@ + 'geo:province', 2 => 'geo:city', 3 => 'geo:district']; + $rangearr = [1 => 15000, 2 => 1000, 3 => 200]; + $geoname = isset($namearr[$level]) ? $namearr[$level] : $namearr[3]; + $georange = isset($rangearr[$level]) ? $rangearr[$level] : $rangearr[3]; + // 读取范围内的ID + $redis = Cache::store('redis')->handler(); + $georadiuslist = []; + if (method_exists($redis, 'georadius')) { + $georadiuslist = $redis->georadius($geoname, $lng, $lat, $georange, 'km', + ['WITHDIST', 'COUNT' => 5, 'ASC']); + } + + if ($georadiuslist) { + [$id, $distance] = $georadiuslist[0]; + } + $id = isset($id) && $id ? $id : 3; + + return self::find($id); + } + + /** + * 根据经纬度获取省份. + * + * @param string $lng 经度 + * @param string $lat 纬度 + * + * @return Area + */ + public static function getProvinceFromLngLat($lng, $lat) + { + $provincedata = null; + $citydata = self::getCityFromLngLat($lng, $lat); + if ($citydata) { + $provincedata = self::find($citydata['pid']); + } + + return $provincedata; + } + + /** + * 根据经纬度获取城市 + * + * @param string $lng 经度 + * @param string $lat 纬度 + * + * @return Area + */ + public static function getCityFromLngLat($lng, $lat) + { + $citydata = null; + $districtdata = self::getDistrictFromLngLat($lng, $lat); + if ($districtdata) { + $citydata = self::find($districtdata['pid']); + } + + return $citydata; + } + + /** + * 根据经纬度获取地区. + * + * @param string $lng 经度 + * @param string $lat 纬度 + * + * @return Area + */ + public static function getDistrictFromLngLat($lng, $lat) + { + $districtdata = self::getAreaFromLngLat($lng, $lat, 3); + + return $districtdata; + } +} diff --git a/app/common/model/Attachment.php b/app/common/model/Attachment.php new file mode 100644 index 0000000..13bd979 --- /dev/null +++ b/app/common/model/Attachment.php @@ -0,0 +1,91 @@ +where('storage', $model['storage'])->find()) { + return false; + } + } + + protected static function onBeforeWrite($row) + { + if (isset($row['category']) && $row['category'] == 'unclassed') { + $row['category'] = ''; + } + } + + public function setUploadtimeAttr($value) + { + return is_numeric($value) ? $value : strtotime($value); + } + + public function getCategoryAttr($value) + { + return $value == '' ? 'unclassed' : $value; + } + + public function setCategoryAttr($value) + { + return $value == 'unclassed' ? '' : $value; + } + + /** + * 获取云储存的缩略图样式字符 + */ + public function getThumbStyleAttr($value, $data) + { + if (!isset($data['storage']) || $data['storage'] == 'local') { + return ''; + } else { + $config = get_addon_config($data['storage']); + if ($config && isset($config['thumbstyle'])) { + return $config['thumbstyle']; + } + } + return ''; + } + + public static function getMimetypeList() + { + $data = [ + "image/*" => __("Image"), + "audio/*" => __("Audio"), + "video/*" => __("Video"), + "text/*" => __("Text"), + "application/*" => __("Application"), + "zip,rar,7z,tar" => __("Zip"), + ]; + return $data; + } + + /** + * 获取定义的附件类别列表 + * @return array + */ + public static function getCategoryList() + { + $data = config('site.attachmentcategory') ?? []; + foreach ($data as $index => &$datum) { + $datum = __($datum); + } + $data['unclassed'] = __('Unclassed'); + return $data; + } +} diff --git a/app/common/model/BaseModel.php b/app/common/model/BaseModel.php new file mode 100644 index 0000000..656b9b9 --- /dev/null +++ b/app/common/model/BaseModel.php @@ -0,0 +1,90 @@ +find($data); + } + + /** + * 查找所有记录. + * + * @param mixed $data 主键列表或者查询条件(闭包) + * @param array|string $with 关联预查询 + * @param bool $cache 是否缓存 + * + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * + * @return \think\Collection + */ + public static function all($data = null, $with = [], $cache = false) + { + if (true === $with || is_int($with)) { + $cache = $with; + $with = []; + } + $query = static::parseQuery($data, $with, $cache); + + return $query->select($data); + } + + /** + * 分析查询表达式. + * + * @param mixed $data 主键列表或者查询条件(闭包) + * @param string $with 关联预查询 + * @param bool $cache 是否缓存 + * + * @return Query + */ + protected static function parseQuery(&$data, $with, $cache) + { + $result = self::withJoin($with)->cache($cache); + if (is_array($data) && key($data) !== 0) { + $result = $result->where($data); + $data = null; + } elseif ($data instanceof \Closure) { + call_user_func_array($data, [&$result]); + $data = null; + } elseif ($data instanceof Query) { + $result = $data->withJoin($with)->cache($cache); + $data = null; + } + + return $result; + } +} diff --git a/app/common/model/BreedInformation.php b/app/common/model/BreedInformation.php new file mode 100644 index 0000000..c146409 --- /dev/null +++ b/app/common/model/BreedInformation.php @@ -0,0 +1,20 @@ +save(['weigh' => $row['id']]); + } + + public function setFlagAttr($value, $data) + { + return is_array($value) ? implode(',', $value) : $value; + } + + /** + * 读取分类类型. + * + * @return array + */ + public static function getTypeList() + { + $typeList = config('site.categorytype'); + foreach ($typeList as $k => &$v) { + $v = __($v); + } + + return $typeList; + } + + public function getTypeTextAttr($value, $data) + { + $value = $value ? $value : $data['type']; + $list = $this->getTypeList(); + + return isset($list[$value]) ? $list[$value] : ''; + } + + public function getFlagList() + { + return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')]; + } + + public function getFlagTextAttr($value, $data) + { + $value = $value ? $value : $data['flag']; + $valueArr = explode(',', $value); + $list = $this->getFlagList(); + + return implode(',', array_intersect_key($list, array_flip($valueArr))); + } + + /** + * 读取分类列表. + * + * @param string $type 指定类型 + * @param string $status 指定状态 + * + * @return array + */ + public static function getCategoryArray($type = null, $status = null) + { + $list = self::where(function ($query) use ($type, $status) { + if (! is_null($type)) { + $query->where('type', '=', $type); + } + if (! is_null($status)) { + $query->where('status', '=', $status); + } + })->order('weigh', 'desc')->select()->toArray(); + + return $list; + } +} diff --git a/app/common/model/Config.php b/app/common/model/Config.php new file mode 100644 index 0000000..ca67788 --- /dev/null +++ b/app/common/model/Config.php @@ -0,0 +1,236 @@ + __('String'), + 'password' => __('Password'), + 'text' => __('Text'), + 'editor' => __('Editor'), + 'number' => __('Number'), + 'date' => __('Date'), + 'time' => __('Time'), + 'datetime' => __('Datetime'), + 'datetimerange' => __('Datetimerange'), + 'select' => __('Select'), + 'selects' => __('Selects'), + 'image' => __('Image'), + 'images' => __('Images'), + 'file' => __('File'), + 'files' => __('Files'), + 'switch' => __('Switch'), + 'checkbox' => __('Checkbox'), + 'radio' => __('Radio'), + 'city' => __('City'), + 'selectpage' => __('Selectpage'), + 'selectpages' => __('Selectpages'), + 'array' => __('Array'), + 'custom' => __('Custom'), + ]; + return $typeList; + } + + public static function getRegexList() + { + $regexList = [ + 'required' => '必选', + 'digits' => '数字', + 'letters' => '字母', + 'date' => '日期', + 'time' => '时间', + 'email' => '邮箱', + 'url' => '网址', + 'qq' => 'QQ号', + 'IDcard' => '身份证', + 'tel' => '座机电话', + 'mobile' => '手机号', + 'zipcode' => '邮编', + 'chinese' => '中文', + 'username' => '用户名', + 'password' => '密码' + ]; + return $regexList; + } + + public function getExtendHtmlAttr($value, $data) + { + $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) { + if (isset($data[$matches[1]])) { + return $data[$matches[1]]; + } + }, $data['extend']); + return $result; + } + + /** + * 读取分类分组列表 + * @return array + */ + public static function getGroupList() + { + $groupList = config('site.configgroup'); + foreach ($groupList as $k => &$v) { + $v = __($v); + } + return $groupList; + } + + public static function getArrayData($data) + { + if (!isset($data['value'])) { + $result = []; + foreach ($data as $index => $datum) { + $result['field'][$index] = $datum['key']; + $result['value'][$index] = $datum['value']; + } + $data = $result; + } + $fieldarr = $valuearr = []; + $field = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []); + $value = isset($data['value']) ? $data['value'] : []; + foreach ($field as $m => $n) { + if ($n != '') { + $fieldarr[] = $field[$m]; + $valuearr[] = $value[$m]; + } + } + return $fieldarr ? array_combine($fieldarr, $valuearr) : []; + } + + /** + * 将字符串解析成键值数组 + * @param string $text + * @return array + */ + public static function decode($text, $split = "\r\n") + { + $content = explode($split, $text); + $arr = []; + foreach ($content as $k => $v) { + if (stripos($v, "|") !== false) { + $item = explode('|', $v); + $arr[$item[0]] = $item[1]; + } + } + return $arr; + } + + /** + * 将键值数组转换为字符串 + * @param array $array + * @return string + */ + public static function encode($array, $split = "\r\n") + { + $content = ''; + if ($array && is_array($array)) { + $arr = []; + foreach ($array as $k => $v) { + $arr[] = "{$k}|{$v}"; + } + $content = implode($split, $arr); + } + return $content; + } + + /** + * 本地上传配置信息 + * @return array + */ + public static function upload() + { + $uploadcfg = config('upload'); + + $upload = [ + 'cdnurl' => $uploadcfg['cdnurl'], + 'uploadurl' => $uploadcfg['uploadurl'], + 'bucket' => 'local', + 'maxsize' => $uploadcfg['maxsize'], + 'mimetype' => $uploadcfg['mimetype'], + 'multipart' => [], + 'multiple' => $uploadcfg['multiple'], + ]; + + return $upload; + } + + /** + * 刷新配置文件 + */ + public static function refreshFile() + { + //如果没有配置权限无法进行修改 + if (!\app\admin\library\Auth::instance()->check('general/config/edit')) { + return false; + } + $config = []; + $configList = self::select(); + foreach ($configList as $k => $v) { + $value = $v->toArray(); + if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) { + $value['value'] = explode(',', $value['value']); + } + if ($value['type'] == 'array') { + $value['value'] = (array)json_decode($value['value'], true); + } + $config[$value['name']] = $value['value']; + } + file_put_contents( + app()->getConfigPath(). 'site.php', + ' 0, 'mobile' => 0], $value); + + return (object) $value; + } + + /** + * 设置验证字段. + * + * @param mixed $value + * + * @return string + */ + public function setVerificationAttr($value) + { + $value = is_object($value) || is_array($value) ? json_encode($value) : $value; + + return $value; + } + + /** + * 变更会员余额. + * + * @param int $money 余额 + * @param int $user_id 会员ID + * @param string $memo 备注 + */ + public static function money($money, $user_id, $memo) + { + $user = self::find($user_id); + if ($user && $money != 0) { + $before = $user->money; + //$after = $user->money + $money; + $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money; + //更新会员信息 + $user->save(['money' => $after]); + //写入日志 + MoneyLog::create([ + 'user_id' => $user_id, + 'money' => $money, + 'before' => $before, + 'after' => $after, + 'memo' => $memo, + ]); + } + } + + /** + * 变更会员积分. + * + * @param int $score 积分 + * @param int $user_id 会员ID + * @param string $memo 备注 + */ + public static function score($score, $user_id, $memo) + { + $user = self::find($user_id); + if ($user && $score != 0) { + $before = $user->score; + $after = $user->score + $score; + $level = self::nextlevel($after); + //更新会员信息 + $user->save(['score' => $after, 'level' => $level]); + //写入日志 + ScoreLog::create([ + 'user_id' => $user_id, + 'score' => $score, + 'before' => $before, + 'after' => $after, + 'memo' => $memo, + ]); + } + } + + /** + * 根据积分获取等级. + * + * @param int $score 积分 + * + * @return int + */ + public static function nextlevel($score = 0) + { + $lv = [ + 1 => 0, + 2 => 30, + 3 => 100, + 4 => 500, + 5 => 1000, + 6 => 2000, + 7 => 3000, + 8 => 5000, + 9 => 8000, + 10 => 10000, + ]; + $level = 1; + foreach ($lv as $key => $value) { + if ($score >= $value) { + $level = $key; + } + } + + return $level; + } +} diff --git a/app/common/model/UserAddress.php b/app/common/model/UserAddress.php new file mode 100644 index 0000000..1ba2dec --- /dev/null +++ b/app/common/model/UserAddress.php @@ -0,0 +1,20 @@ +cache('__version__')->order('weigh desc,id desc')->select(); + foreach ($versionlist as $k => $v) { + // 版本正常且新版本号不等于验证的版本号且找到匹配的旧版本 + if ($v['status'] == 'normal' && $v['newversion'] !== $version && \fast\Version::check($version, $v['oldversion'])) { + $updateversion = $v; + break; + } + } + if (isset($updateversion)) { + $search = ['{version}', '{newversion}', '{downloadurl}', '{url}', '{packagesize}']; + $replace = [$version, $updateversion['newversion'], $updateversion['downloadurl'], $updateversion['downloadurl'], $updateversion['packagesize']]; + $upgradetext = str_replace($search, $replace, $updateversion['content']); + + return [ + 'enforce' => $updateversion['enforce'], + 'version' => $version, + 'newversion' => $updateversion['newversion'], + 'downloadurl' => $updateversion['downloadurl'], + 'packagesize' => $updateversion['packagesize'], + 'upgradetext' => $upgradetext, + ]; + } + } +} diff --git a/app/common/service/AddonService.php b/app/common/service/AddonService.php new file mode 100644 index 0000000..75d88b7 --- /dev/null +++ b/app/common/service/AddonService.php @@ -0,0 +1,122 @@ +getRootPath().'addons'.DIRECTORY_SEPARATOR); + ! defined('DS') && define('DS', DIRECTORY_SEPARATOR); + // 如果插件目录不存在则创建 + if (! is_dir(ADDON_PATH)) { + @mkdir(ADDON_PATH, 0755, true); + } + //注册插件路由 + $this->addon_route(); + //注册插件事件 + $this->addon_event(); + } + + /** + * 注册插件事件. + */ + private function addon_event() + { + $hooks = Env::get('APP_DEBUG') ? [] : Cache::get('hooks', []); + if (empty($hooks)) { + $hooks = (array) Config::get('addons.hooks'); + // 初始化钩子 + foreach ($hooks as $key => $values) { + if (is_string($values)) { + $values = explode(',', $values); + } else { + $values = (array) $values; + } + $hooks[$key] = array_filter(array_map(function ($v) use ($key) { + return [get_addon_class($v), Str::camel($key)]; + }, $values)); + } + Cache::set('hooks', $hooks); + } + //如果在插件中有定义app_init,则直接执行 + Event::listenEvents($hooks); + if (isset($hooks['app_init'])) { + foreach ($hooks['app_init'] as $k => $v) { + Event::trigger('app_init', $v); + } + } + } + + /** + * 注册插件路由. + */ + private function addon_route() + { + Route::rule('addons/:addon/[:controller]/[:action]', '\\think\\addons\\Route::execute') + ->middleware([FastInit::class,Addon::class]); + + //注册路由 + $routeArr = (array) Config::get('addons.route'); + $execute = '\\think\\addons\\Route::execute'; + foreach ($routeArr as $k => $v) { + if (is_array($v)) { + $domain = $v['domain']; + $drules = []; + foreach ($v['rule'] as $m => $n) { + [$addon, $controller, $action] = explode('/', $n); + $drules[$m] = [ + 'addon' => $addon, 'controller' => $controller, 'action' => $action, + 'indomain' => 1, + ]; + } + Route::domain($domain, function () use ($drules, $execute) { + // 动态注册域名的路由规则 + foreach ($drules as $k => $rule) { + Route::rule($k, $execute) + ->middleware([FastInit::class,Addon::class]) + ->name($k) + ->completeMatch(true) + ->append($rule); + } + }); + } else { + if (! $v) { + continue; + } + [$addon, $controller, $action] = explode('/', $v); + Route::rule($k, $execute) + ->middleware([FastInit::class,Addon::class]) + ->name($k) + ->completeMatch(true) + ->append(['addon' => $addon, 'controller' => $controller, 'action' => $action]); + } + } + } +} diff --git a/app/common/service/AuthService.php b/app/common/service/AuthService.php new file mode 100644 index 0000000..c0131bf --- /dev/null +++ b/app/common/service/AuthService.php @@ -0,0 +1,27 @@ +app->bind('auth', Auth::class); + } +} diff --git a/app/common/view/tpl/dispatch_jump.tpl b/app/common/view/tpl/dispatch_jump.tpl new file mode 100644 index 0000000..dddf6cb --- /dev/null +++ b/app/common/view/tpl/dispatch_jump.tpl @@ -0,0 +1,66 @@ +{__NOLAYOUT__} + + + + {:__('Warning')} + + + + + +{php}$codeText=$code == 1 ? 'success' : ($code == 0 ? 'error' : 'info');{/php} +
+
+ +
+

{$msg}

+ {if $url} +

+ {:__('This page will be re-directed in %s seconds', '' . $wait . '')} +

+ {/if} +

+ {:__('Go back')} + {if $url} + {:__('Jump now')} + {/if} +

+
+ +{if $url} + +{/if} + + \ No newline at end of file diff --git a/app/common/view/tpl/think_exception.tpl b/app/common/view/tpl/think_exception.tpl new file mode 100644 index 0000000..f7e1778 --- /dev/null +++ b/app/common/view/tpl/think_exception.tpl @@ -0,0 +1,503 @@ +'.end($names).''; +} +} + +if (!function_exists('parse_file')) { +function parse_file($file, $line) +{ +return ''.basename($file)." line {$line}".''; +} +} + +if (!function_exists('parse_args')) { +function parse_args($args) +{ +$result = []; +foreach ($args as $key => $item) { +switch (true) { +case is_object($item): +$value = sprintf('object(%s)', parse_class(get_class($item))); +break; +case is_array($item): +if (count($item) > 3) { +$value = sprintf('[%s, ...]', parse_args(array_slice($item, 0, 3))); +} else { +$value = sprintf('[%s]', parse_args($item)); +} +break; +case is_string($item): +if (strlen($item) > 20) { +$value = sprintf( +'\'%s...\'', +htmlentities($item), +htmlentities(substr($item, 0, 20)) +); +} else { +$value = sprintf("'%s'", htmlentities($item)); +} +break; +case is_int($item): +case is_float($item): +$value = $item; +break; +case is_null($item): +$value = 'null'; +break; +case is_bool($item): +$value = '' . ($item ? 'true' : 'false') . ''; +break; +case is_resource($item): +$value = 'resource'; +break; +default: +$value = htmlentities(str_replace("\n", '', var_export(strval($item), true))); +break; +} + +$result[] = is_int($key) ? $value : "'{$key}' => {$value}"; +} + +return implode(', ', $result); +} +} +if (!function_exists('echo_value')) { +function echo_value($val) +{ +if (is_array($val) || is_object($val)) { +echo htmlentities(json_encode($val, JSON_PRETTY_PRINT)); +} elseif (is_bool($val)) { +echo $val ? 'true' : 'false'; +} elseif (is_scalar($val)) { +echo htmlentities($val); +} else { +echo 'Resource'; +} +} +} +?> + + + + + 系统发生错误 + + + + + + $trace) { ?> +
+
+
+
+

+
+

+
+
+ +
+
    $value) { ?>
  1. ">
+
+ +
+

Call Stack

+
    +
  1. + +
  2. + +
  3. + +
+
+
+ + +
+

+
+ + + +
+

Exception Datas

+ $value) { ?> + + + + + + + $val) { ?> + + + + + + + +
empty
+ +
+ + + +
+

Environment Variables

+ $value) { ?> + + + + + + + + $val) { ?> + + + + + + + +
empty
+ + +
+ + + + + + + + \ No newline at end of file diff --git a/app/middleware.php b/app/middleware.php index 64ec256..e17eb96 100644 --- a/app/middleware.php +++ b/app/middleware.php @@ -12,4 +12,6 @@ return [ // \think\middleware\LoadLangPack::class, // Session初始化 'think\middleware\SessionInit', +// 跨域请求 + \think\middleware\AllowCrossDomain::class ]; diff --git a/config/cache.php b/config/cache.php index a8d69d2..57e1da0 100644 --- a/config/cache.php +++ b/config/cache.php @@ -24,6 +24,11 @@ return [ // 序列化机制 例如 ['serialize', 'unserialize'] 'serialize' => [], ], + 'redis' => [ + 'host' => '127.0.0.1', + 'port' => 6379, + 'password' => '', + ], // 更多的缓存连接 ], ];