Exemplo n.º 1
0
 /**
  * 获取微信code
  */
 public function oauth2Action()
 {
     $code = $this->request->getQuery('code');
     $weixin = new \Weixin();
     //获取授权token
     $data = json_decode($weixin->oauth2($code), true);
     //设置cookie
     $this->setCookie('openid', $data['openid'], time() + 3600 * 24 * 30);
     if ($weixin->is_set($data['openid'])) {
         $basic_token = $this->getToken();
         //获取用户信息
         $user = json_decode($weixin->user($basic_token, $data['openid']));
         if ($this->getCookie('f_openid')) {
             $f_openid = $this->getCookie('f_openid');
             $code = $weixin->addUser($user, $f_openid);
             $this->setCookie('f_openid', '', time() - 1);
         } else {
             $code = $weixin->addUser($user);
         }
         if ($code != 200) {
             $this->response->redirect('http://www.51sxwang.com/weixin/guanzhu')->send();
         }
     }
     $this->response->redirect(HOST . '/weixin/login')->send();
     $this->view->disable();
 }