微信授权登录
-
微信的相关的接口写在父类控制器中
private function _checkLogin(){ if(!isset($_SESSION['uid']) || empty($_SESSION['uid'])){ $this->wx_login(); } } public function wx_login(){ $is_inweixin = $this->is_inweixin(); //判断是否在微信打开 //获取微信信息 if ($is_inweixin) { $user_wx_arr = array(); if (!isset($_SESSION['wx_info']) || !$_SESSION['wx_info']) { $codes = $this->input->get('code',true); if (!$codes) { $wx_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->config->item('wx')['appid'] . '&redirect_uri='; //$back_url = urlencode($this->config->item('wx')['back_url'] . $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']); $back_url = urlencode($this->config->item('wx')['back_url'] . $_SERVER['REQUEST_URI']); $url_open = $wx_url . $back_url . '&response_type=code&scope=snsapi_userinfo&state=' . mt_rand(100, 1000) . '#wechat_redirect'; $this->load->helper('url'); redirect($url_open); } $user_wx_arr = $this->get_weixin_user_access($codes); if (!empty($user_wx_arr)) { $url_acc = 'https://api.weixin.qq.com/sns/userinfo'; $data_acc = array(); $data_acc['access_token'] = $user_wx_arr['access_token']; $data_acc['openid'] = $user_wx_arr['openid']; $data_acc['lang'] = 'zh_CN'; $this->load->helper('curl'); $user_info = json_decode(http($url_acc, $data_acc, 'GET'), true); $user_wx_arr['headimgurl'] = $user_info['headimgurl']; $user_wx_arr['nickname'] = $user_info['nickname']; $user_wx_arr['sex'] = $user_info['sex']; } $this->session->set_userdata('wx_info', $user_wx_arr,7100); } else { $user_wx_arr = $_SESSION['wx_info']; } if (isset($user_wx_arr['openid'])) { $wid = $user_wx_arr['openid']; $where_arr['wid'] = $wid; $where_arr['status'] = '0'; 伪代码:获取用户信息 $user_value if ($user_value['uid']) { //自动登录 if($user_wx_arr['headimgurl']){ $data_user['img'] = $user_wx_arr['headimgurl']; } if($user_wx_arr['nickname']) { $data_user['name'] = $user_wx_arr['nickname']; } if($user_wx_arr['sex']) { $data_user['sex'] = $user_wx_arr['sex']; } $user_session = array(); $user_session['uid'] = $user_value['uid']; $user_session['mobile'] = $user_value['mobile']?$user_value['mobile']:''; $user_session['wid'] = $wid; $user_session['yg_id'] = $yg_id; $this->session->set_userdata($user_session); 伪代码:修改用户信息参数$data_user $login_status = true; } else { $data = array(); $data['wid'] = $wid; $data['img'] = $user_wx_arr['headimgurl']; $data['source'] = $business_id; $data['name'] = $user_wx_arr['nickname']; $data['sex'] = (string)$user_wx_arr['sex']; $yg_id = $data['yg_id']; 伪代码:插入用户信息 参数 $data,返回$uid //自动登录 $user_session = array(); $user_session['uid'] = $uid; $user_session['mobile'] = ''; $user_session['wid'] = $wid; $this->session->set_userdata($user_session); $login_status = true; } } unset($user_wx_arr); } if (isset($_SESSION['uid']) && $_SESSION['uid']) { $this->input->set_cookie('uid', $_SESSION['uid'], '86400'); } } public function is_inweixin() { if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == false) { return false; }else{ return true; } } /** * 获取微信用户access */ public function get_weixin_user_access($code){ $url = 'https://api.weixin.qq.com/sns/oauth2/access_token'; $wx = $this->config->item('wx'); $data = array(); $data['appid'] = trim($wx['appid']); $data['secret'] = trim($wx['secret']); $data['code'] = $code; $data['grant_type'] = 'authorization_code'; $this->load->helper('curl'); return json_decode(http($url,$data,'GET'),true); }