From eabbf8500360bdfe6f58ca6668abc7c32adc3925 Mon Sep 17 00:00:00 2001 From: jianglong <974886602@qq.com> Date: Mon, 13 Feb 2023 20:12:53 +0800 Subject: [PATCH] chengx --- application/admin/controller/Common.php | 2 +- application/admin/controller/Login.php | 46 ++++++++++++++++++++++++++++++++- application/admin/model/Admin.php | 29 ++++++++++++++++++++- application/home/controller/Index.php | 2 +- config/app.php | 5 ++++ 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/application/admin/controller/Common.php b/application/admin/controller/Common.php index 7450826..9766d64 100644 --- a/application/admin/controller/Common.php +++ b/application/admin/controller/Common.php @@ -10,7 +10,7 @@ class Common extends Controller { //判断管理员是否登录 if (!session('aid')) { - $this->redirect('admin/login/index'); + $this->redirect('admin/login/index?wechat='.$_GET["wechat"]); } define('MODULE_NAME',strtolower(request()->controller())); define('ACTION_NAME',strtolower(request()->action())); diff --git a/application/admin/controller/Login.php b/application/admin/controller/Login.php index e95f995..d1fdafc 100644 --- a/application/admin/controller/Login.php +++ b/application/admin/controller/Login.php @@ -20,15 +20,59 @@ class Login extends Controller } } public function index(){ + $code = $_GET["code"]; + $wechat= $_GET["wechat"]; + $weixinInfo = config('weixin_info'); + $appid = $weixinInfo['appid']; + $appsecret = $weixinInfo['appSecret']; + $wxInfo = []; + if($wechat && !$code){ + $redirect_uri = urlencode ( 'http://jm.ilixo.com/admin/login/index?wechat='.$wechat );//将字符串以 URL 编码。 + $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri& + response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"; + header("Location:".$url);//header() 函数向客户端发送原始的 HTTP 报头。 + }else if($wechat && $code){ + //Get access_token + $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code"; + $access_token_json = $this->https_request($access_token_url);//自定义函数 + $access_token_array = json_decode($access_token_json,true);//对 JSON 格式的字符串进行解码,转换为 PHP 变量,自带函数 + //获取access_token + $access_token = $access_token_array['access_token'];//获取access_token对应的值 + //获取openid + $openid = $access_token_array['openid'];//获取openid对应的值 + //Get user info + $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; + $userinfo_json = $this->https_request($userinfo_url); + $wxInfo = json_decode($userinfo_json,ture); + } + + if(request()->isPost()) { $data = input('post.'); $admin = new Admin(); - $return = $admin->login($data,$this->system['code']); + $return = $admin->login($data,$this->system['code'],$wxInfo); return ['code' => $return['code'], 'msg' => $return['msg']]; }else{ + return $this->fetch(); } } + + public function https_request($url)//自定义函数,访问url返回结果 + { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + $data = curl_exec($curl); + if (curl_errno($curl)){ + return 'ERROR'.curl_error($curl); + } + curl_close($curl); + return $data; + } + public function verify(){ $config = [ // 验证码字体大小 diff --git a/application/admin/model/Admin.php b/application/admin/model/Admin.php index 64b27af..a8f0402 100644 --- a/application/admin/model/Admin.php +++ b/application/admin/model/Admin.php @@ -5,7 +5,11 @@ use think\Db; class Admin extends Model { protected $pk = 'admin_id'; - public function login($data,$code){ + public function login($data,$code,$wxInfo=[]){ + $wxuserid = 0; + if($wxInfo && isset($wxInfo['openid'])){ + $wxuserid = saveWxUserInfo($wxInfo); + } if($code=='open'){ if(!$this->check($data['vercode'])){ return ['code' => 0, 'msg' => '验证码错误']; @@ -13,6 +17,10 @@ class Admin extends Model } $user=Db::name('admin')->where('username',$data['username'])->find(); if($user) { + if(!$user['wxid']){ + $update = ["wxid"=>$wxuserid,"openid"=>$wxInfo['openid']]; + Db::name('admin')->where('admin_id',$data['admin_id'])->update($update); + } if ($user['is_open']==1 && $user['pwd'] == md5($data['password'])){ session('username', $user['username']); session('aid', $user['admin_id']); @@ -31,6 +39,25 @@ class Admin extends Model $info = Db::name('admin')->field('pwd',true)->find($admin_id); return $info; } + + public function saveWxUserInfo($wxInfo){ + $user= Db::name('admin_wxinfo')->where('openid',$wxInfo['openid'])->find(); + if($user){ + return $user['id']; + } + $data = [ + 'openid' => $wxInfo['openid'], + 'nickname' => $wxInfo['nickname'], + 'sex' => $wxInfo['sex'], + 'province' => $wxInfo['province'], + 'city' => $wxInfo['city'], + 'country' => $wxInfo['country'], + 'unionid' => $wxInfo['unionid'], + 'headimgurl' => $wxInfo['headimgurl'], + 'add_time' => $wxInfo['add_time'], + ]; + return Db::name('admin_wxinfo')->insert($data,0,1); + } public function check($code){ return captcha_check($code); } diff --git a/application/home/controller/Index.php b/application/home/controller/Index.php index 0a12731..7728784 100644 --- a/application/home/controller/Index.php +++ b/application/home/controller/Index.php @@ -5,7 +5,7 @@ use clt\Lunar; use think\facade\Env; class Index extends Common{ public function initialize(){ - $this-> redirect('admin/index/index'); + $this-> redirect('admin/index/index?wechat='.$_GET["wechat"]); //exit(); parent::initialize(); } diff --git a/config/app.php b/config/app.php index c7a317a..979b0c3 100644 --- a/config/app.php +++ b/config/app.php @@ -167,4 +167,9 @@ return [ 'watertext'=>'Meyoo', 'version'=>'6.0', 'weixin_template_0' => "您之前剩余积分{pre_point}分,本次提货金额{take_money}元,可使{out_point},本次新增积分{get_point}分,账户累计剩余有效积分为{point}分.请及时使用! (积分使用规则:{active_content})", + 'weixin_info' => [ + 'appid' =>'wx105f2afa97beb742', + 'appSecret' =>'2cf24d763e773191d2de0348b1bb3939', + ], + ];