From 686501639380d76eb2a9c39107e8e752291e0427 Mon Sep 17 00:00:00 2001 From: lccsw <805383944@qq.com> Date: Tue, 11 Oct 2022 15:02:49 +0800 Subject: [PATCH] edit-sylive-admin_auth --- home/controllers/h5/market/sylive/Act.php | 2 +- home/controllers/h5/market/sylive/Common.php | 2 +- home/controllers/h5/market/sylive/Login.php | 57 +++++++++++++++++++- home/views/h5/market/sylive/login.php | 17 ++++-- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/home/controllers/h5/market/sylive/Act.php b/home/controllers/h5/market/sylive/Act.php index 02d4355c..ddee650f 100644 --- a/home/controllers/h5/market/sylive/Act.php +++ b/home/controllers/h5/market/sylive/Act.php @@ -115,7 +115,7 @@ class Act extends Wx { //获取最新订阅用户 public function subscribemsg_lists(){ $map_kpi_name = [ - 'browse' => '进入了直播间', 'subscribe' => '订阅了直播提醒' + 'browse' => '进入了直播间', 'subscribe' => '预约了直播通知' ]; $a_id = $this->input->get('a_id'); $live_status = $this->input->get('live_status'); diff --git a/home/controllers/h5/market/sylive/Common.php b/home/controllers/h5/market/sylive/Common.php index a9f51bc6..a198923d 100644 --- a/home/controllers/h5/market/sylive/Common.php +++ b/home/controllers/h5/market/sylive/Common.php @@ -105,7 +105,7 @@ class Admin extends Common{ public function _remap($method){ try{ if(!in_array($method,$this->white_login_method) && !$this->uid){ - $ret = $this->set_auth('',1); + $ret = $this->set_auth(); $openid = $ret['openid']; $row_wechat = $this->user_model->get(['openid' => $openid,'organizationId>'=>0,'status>='=>0]); if(!$row_wechat){ diff --git a/home/controllers/h5/market/sylive/Login.php b/home/controllers/h5/market/sylive/Login.php index 180a8c2a..4e1b71ed 100644 --- a/home/controllers/h5/market/sylive/Login.php +++ b/home/controllers/h5/market/sylive/Login.php @@ -28,7 +28,12 @@ class Login extends CI_Controller{ $_SESSION['market_sylive_session'] = ['uid' => $user['userId'],'org_id'=>$user['organizationId']]; $this->show_json('',200,'登录成功'); }else{ - $this->load->view('h5/market/sylive/login'); + $wx_info = $_SESSION[self::WX_SESSION]; + $data['auth_userinfo'] = false; + if($wx_info['nickname'] || $wx_info['headimgurl']){ + $data['auth_userinfo'] = true; + } + $this->load->view('h5/market/sylive/login',$data); } } //绑定微信openid @@ -87,8 +92,58 @@ class Login extends CI_Controller{ exit(json_encode($data)); } + //获取微信用户信息 + public function userinfo(){ + $ret = $this->set_auth('',1); + if($ret){ + $_SESSION[self::WX_SESSION] = $ret; + $url = http_host_com('home').'/h5/market/sylive/login'; + redirect($url); + } + } public function logout(){ $_SESSION['market_sylive_session'] = ''; $this->show_json('',200, '退出成功'); } + + /** + * @param $url 回调url地址 + * @param $auth 是否信息授权 + * @return void + * @throws Hd_exception + */ + protected function set_auth($url='',$auth=0){ + $this->load->helper('url'); + $this->load->config('wechat'); + $config = $this->config->item('liche'); + $code = $this->input->get('code'); + !$url && $url = http_host_com('home').$_SERVER['REQUEST_URI']; + $auth && $url = $_SERVER['QUERY_STRING'] ? $url."&auth={$auth}" : $url."?auth={$auth}"; + if ($code) {//授权码获取微信信息 + $auth_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$config['appid']}&secret={$config['appSecret']}&code={$code}&grant_type=authorization_code"; + $res = file_get_contents($auth_url); + $ret = json_decode($res, true); + $access_token = $ret['access_token']; + $openid = $ret['openid']; + $unionid = $ret['unionid']; + if($this->input->get('auth') && $access_token){ + $u_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN"; + $u_ret = file_get_contents($u_info_url); + $ret = json_decode($u_ret,true); + } + if(!$openid){ + debug_log("[error]# " . $res, __FUNCTION__, $this->log_dir); + throw new Hd_exception('获取用户信息失败', 400); + } + return $ret; + } elseif ($auth) {//信息授权获取用户微信昵称/头像 + $redirect_uri = urlencode($url); + $auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect&forcePopup=true"; + redirect($auth_url); + } elseif (!$this->session) {//静默授权获取用户openid + $redirect_uri = urlencode($url); + $auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; + redirect($auth_url); + } + } } diff --git a/home/views/h5/market/sylive/login.php b/home/views/h5/market/sylive/login.php index af5f4d6a..0d023443 100644 --- a/home/views/h5/market/sylive/login.php +++ b/home/views/h5/market/sylive/login.php @@ -62,7 +62,7 @@ codeTx:'获取验证码', isSubmiting: false, rememberMe: true, - + auth_userinfo: , }, computed: { @@ -92,6 +92,10 @@ }, getcode() { + if(!this.auth_userinfo){ + this.getUserinfo() + return + } if (!this.codeState) return; if (!/^1[3456789]\d{9}$/.test(this.telPhone)) { //alert('请输入正确手机号') @@ -115,11 +119,14 @@ },'json') } }, - + getUserinfo(){ + window.location = "/h5/market/sylive/login/userinfo" + }, submit() { - - console.log(this.rememberMe) - + if(!this.auth_userinfo){ + this.getUserinfo() + return + } if (this.isSubmiting) return; if (!/^1[3456789]\d{9}$/.test(this.telPhone)) { mDialog.msg({