Пример #1
0
 function index()
 {
     // 直接把自己作为授权页面
     include 'WxOAuth2.class.php';
     $oauth2 = new WxOAuth2();
     $v_token = $this->_get('token');
     $code = $this->_get('code');
     $state = $this->_get('state');
     // 如果没有授权,就去授权
     if (!$state) {
         $appid = M('wxuser')->where(array('token' => $v_token))->getField('appid');
         $oauth2->app_id = $appid;
         $callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
         $url = $oauth2->get_authorize_url($callback, 'snsapi_base', $v_token);
         header('location:' . $url);
     } else {
         if ($code) {
             // 如果用户同意授权
             // 根据用户标识来确定app_id
             $wxuser = M('wxuser')->where(array('token' => $v_token))->find();
             // dump($wxuser);
             $oauth2->app_id = $wxuser['appid'];
             $oauth2->app_secret = $wxuser['appsecret'];
             // 确认授权后会,根据返回的code获取token
             $token_arr = $oauth2->get_access_token($wxuser['appid'], $wxuser['appsecret'], $code);
             if (!$token_arr) {
                 die;
             } else {
                 // header('location:' . implode(',', $token_arr));
             }
             // $user_info = $oauth2->get_user_info($token_arr['access_token'], $token_arr['openid']); //获取用户信息
             $this->assign('openid', $token_arr['openid']);
             if (!$this->wecha_id) {
                 $this->openid = $this->wecha_id = $token_arr['openid'];
                 $this->assign('reopenid', $this->wecha_id);
             }
             $this->assign('is_start', $this->is_start($this->packet_info['id']));
             $this->display();
         }
     }
 }
Пример #2
0
 public function wxrurl()
 {
     include 'WxOAuth2.class.php';
     $oauth2 = new WxOAuth2();
     // 授权成功后跳转到此页面获取的信息
     // dump($_GET);
     // var_dump($this);
     // echo $this->app_id . '<br>';
     // 用户同意授权,返回用户标识
     $state = $_GET['state'];
     // 根据用户标识来确定app_id
     $wxuser = M('wxuser')->where(array('token' => $state))->find();
     // dump($wxuser);
     $oauth2->app_id = $wxuser['appid'];
     $oauth2->app_secret = $wxuser['appsecret'];
     // 用户取消授权
     if ($_GET['code'] == '') {
         // echo '用户取消授权';
         return false;
     }
     // 确认授权后会,根据返回的code获取token
     $token_arr = $oauth2->get_access_token('', '', $_GET['code']);
     // dump($token_arr);
     // 保存授权信息
     // 把当前公众号的标识写到数组
     $token_arr['token'] = $_GET['state'];
     // 时间戳
     $token_arr['creatime'] = time();
     // 把失效时间也存到数据库,7200-120,提前2分钟,为了防止出错吧
     $token_arr['expire_time'] = $token_arr['creatime'] + 7200 - 120;
     session('token', $token_arr);
     // $wxoauth2 = M('wxoauth2')->where(array('token' => $_GET['token']))->order('desc')->find();
     // 添加新的数据
     $r = M('wxoauth2')->data($token_arr)->add();
     // echo '<br>access_token => ' . $token_arr['access_token'];
     // die;
     $user_info = $oauth2->get_user_info($token_arr['access_token'], $token_arr['openid']);
     //获取用户信息
     // print_r($user_info);
     $user_info['addtime'] = time();
     // 把是数组的值转成json字符串,方便存数据库
     foreach ($user_info as $key => $value) {
         if (is_array($value)) {
             $user_info[$key] = json_encode($value);
         }
     }
     // 存用户信息到数据库
     $user_info['v_token'] = $token_arr['token'];
     $wx_openid = M('wxoauth2_openid');
     $r = $wx_openid->data($user_info)->add();
     if ($r < 0) {
         // 写入失败
     } else {
         echo $user_info['headimgurl'];
     }
 }