public static function unsubscribe($fromUsername)
 {
     // do something
     $curUser = WkWechatUserService::getWeChatUserByOpenId($fromUsername);
     if (isset($curUser)) {
         $curUser->subscribe = 0;
         WkWechatUserService::saveWeChatUser($curUser);
     }
 }
Exemple #2
0
 /**
  * 获取微信openid,用户信息(snsapi_userinfo授权)
  * @apiMethod get|post
  * @apiParam string code 微信授权code
  * @return array
  * @throws Wk_Exception
  */
 public function getWeChatOpenIdUserInfoAction()
 {
     $code = Wk_Request::getRequestString('code', null, false);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
     curl_setopt($ch, CURLOPT_TIMEOUT, 15);
     $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . Wk::$config['wechat']['WX_AKEY'] . '&secret=' . Wk::$config['wechat']['WX_SKEY'] . '&code=' . $code . '&grant_type=authorization_code';
     curl_setopt($ch, CURLOPT_URL, $url);
     $data = curl_exec($ch);
     if ($data === false) {
         throw new Wk_Exception('', TErrorConstants::E_SNS_LOGIN);
     }
     $data = json_decode($data);
     if (!empty($data->errcode)) {
         throw new Wk_Exception('', TErrorConstants::E_SNS_LOGIN);
     }
     if (empty($data->access_token)) {
         throw new Wk_Exception('', TErrorConstants::E_SNS_LOGIN);
     }
     $access_token = $data->access_token;
     $expires_in = $data->expires_in;
     $refresh_token = $data->refresh_token;
     $openid = $data->openid;
     $scope = $data->scope;
     $unionid = $data->unionid;
     $url2 = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . "&lang=zh_CN";
     curl_setopt($ch, CURLOPT_URL, $url2);
     $data2 = curl_exec($ch);
     curl_close($ch);
     if ($data2 === false) {
         throw new Wk_Exception('', TErrorConstants::E_SNS_LOGIN);
     }
     $data2 = json_decode($data2);
     if (!empty($data2->errcode)) {
         throw new Wk_Exception('', TErrorConstants::E_SNS_LOGIN);
     }
     if (empty($data2->openid)) {
         throw new Wk_Exception('', TErrorConstants::E_SNS_LOGIN);
     }
     $user = new TAR_WechatUser();
     $user->nickname = $data2->nickname;
     $user->sex = $data2->sex;
     $user->province = $data2->province;
     $user->city = $data2->city;
     $user->country = $data2->country;
     $user->headImgUrl = $data2->headimgurl;
     $user->openId = $data2->openid;
     $user->unionId = $data2->unionid;
     $tempUser = WkWxExternalService::getInstance()->getUserInfoByOpenId($openid);
     $user->subscribe = $tempUser['subscribe'];
     if ($user->subscribe == 1) {
         $user->unionId = $tempUser['unionId'];
         $user->subscribeTime = $tempUser['subscribeTime'];
     }
     $_SESSION['tmpAvatar'] = $user->headImgUrl;
     WkWechatUserService::saveWeChatUser($user);
     return ['openid' => $openid, 'weChatUserInfo' => $user, 'auth' => 'userinfo'];
 }