/** * Api for Weibo callback **/ public function actionCreateWeibo() { try { $currentDomain = Yii::$app->request->hostInfo; $accountId = $this->getAccountId(); //get authorization code code $authCode = $this->getQuery('code'); // get oauth2/access_token $token = Yii::$app->weiboConnect->getAccessToken($authCode); // get oauth2/get_token_info, such as uid, appkey, create_at, expire_in... $tokenInfo = Yii::$app->weiboConnect->getBindWeiboUUID($token["access_token"]); // get bind weibo info $weiboAccount = Yii::$app->weiboConnect->getBindWeiboInfo($token["access_token"], $tokenInfo['uid']); $account = Account::findOne(['_id' => $accountId]); $channelAccountIds = Channel::getWeiboByAccount($accountId); $updateAccount = null; if (count($channelAccountIds) > 0) { $result = Yii::$app->weConnect->getAccounts($channelAccountIds); foreach ($result as $item) { if (!empty($item['channel']) && $weiboAccount['id'] == $item['channelAccount']) { $updateAccount = $item; } } } $info = null; // In order to make sure a logical token expire time (subtract 1 day) $weiboTokenExpireTime = ($tokenInfo["create_at"] + $tokenInfo["expire_in"] - 24 * 60 * 60) * 1000; $account = ["channelAccount" => $weiboAccount['id'], "appId" => $weiboAccount['id'], "weiboToken" => $token["access_token"], "weiboTokenExpireTime" => $weiboTokenExpireTime, "name" => $weiboAccount['screen_name'], "channel" => "WEIBO", "headImageUrl" => $weiboAccount['profile_image_url'], "weiboAccountType" => $weiboAccount["verified"] ? self::WEIBO_AUTH_ACCOUNT : self::WEIBO_NORMAL_ACCOUNT]; if (!empty($updateAccount)) { //update a account $info = Yii::$app->weConnect->updateAccount($updateAccount["id"], $account); } else { //validate if the count of the channel has exceeded the limitation $limit = $this->_getChannelLimit(); $channelCount = Channel::getEnableCountByAccountId($accountId); if ($channelCount >= $limit) { throw new BadRequestHttpException(Yii::t('channel', 'channel_count_limit')); } //create a new account $info = Yii::$app->weConnect->createAccount($account); //initialize the default rules $types = ['SUBSCRIBE', 'RESUBSCRIBE', 'DEFAULT']; $this->_initDefaultRules($info['id'], $types, $account['name'], self::SOCIAL_CHANNEL_WEIBO); } $channelId = $info['id']; //update account information and insert the new channel $createChannelResult = Channel::upsert($accountId, $channelId, Channel::WEIBO, $account['name'], '', false, $weiboAccount['id']); if (!$createChannelResult) { throw new \yii\web\ServerErrorHttpException("update channel failed"); } return ['data' => $info]; } catch (\Exception $e) { return ['errmsg' => urlencode($e->getMessage())]; } }