private function buildRedirectUrl($channelId, $oauthRedirect, $originRequired = false) { if (empty($channelId)) { throw new BadRequestHttpException('missing channel id'); } $weChannels = Yii::$app->weConnect->getAccounts($channelId); if (empty($weChannels)) { throw new BadRequestHttpException('Invalid channel id'); } $channelType = $weChannels[0]['channel']; $appId = $weChannels[0]['appId']; $state = $channelId; $redirectUrl = $oauthRedirect; switch ($channelType) { case Account::WECONNECT_CHANNEL_WEIXIN: $channel = Channel::getByChannelId($channelId); $redirectUrl = urlencode($redirectUrl); if ($channel->isTest) { $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appId}&redirect_uri={$redirectUrl}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect"; } else { $component = Yii::$app->weConnect->getComponentToken(); $componentToken = $component['componentToken']; $componentAppId = $component['componentAppId']; $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appId}&redirect_uri={$redirectUrl}&response_type=code&scope=snsapi_base&state={$state}&component_appid={$componentAppId}#wechat_redirect"; } break; case Account::WECONNECT_CHANNEL_WEIBO: $redirectUrl .= false === strpos($redirectUrl, '?') ? "?state={$state}" : "&state={$state}"; $weiboAppKey = WEIBO_APP_KEY; $url = "https://api.weibo.com/oauth2/authorize?client_id={$weiboAppKey}&response_type=code&redirect_uri={$redirectUrl}&scope=snsapi_base"; break; case Account::WECONNECT_CHANNEL_ALIPAY: $redirectUrl .= false === strpos($redirectUrl, '?') ? "?state={$state}" : "&state={$state}"; $redirectUrl = $redirectUrl . '&appId=' . $appId; $redirectUrl = urlencode($redirectUrl); $url = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?" . "app_id={$appId}&auth_skip=false&scope=auth_base&redirect_uri={$redirectUrl}"; break; } LogUtil::info(['message' => 'oauth2 snsapi_base redirecturl', 'url' => $redirectUrl], 'channel'); return $url; }
/** * create a attention qrcode * @return string. qrcode id */ public static function createAttentionQrcode($channelId, $accessWeibo = false) { try { //defined a special name for qrcodeName,this param must be pass $qrcodeName = Channel::ATTENTION_QRCODE . '_' . $channelId; $channelInfo = Channel::getByChannelId($channelId); if (empty($channelInfo['qrcodeId'])) { if ($channelInfo['origin'] != self::WEIBO || $accessWeibo) { $qrcode = ['type' => 'CHANNEL', 'name' => $qrcodeName]; $qrcodeInfo = Yii::$app->weConnect->createQrcode($channelId, $qrcode); if (!empty($qrcodeInfo)) { return $qrcodeInfo['id']; } } return ''; } else { return $channelInfo['qrcodeId']; } } catch (Exception $e) { return ''; } }
/** * The default implementation returns the names of the columns whose values have been populated into Member. */ public function fields() { return array_merge(parent::fields(), ['socials', 'card' => function () { $card = MemberShipCard::findByPk($this->cardId); return $card; }, 'createdAt' => function () { return MongodbUtil::MongoDate2String($this->createdAt, 'Y-m-d H:i:s'); }, 'socialAccount' => function () { if (!empty($this->socialAccountId)) { $channel = Channel::getByChannelId($this->socialAccountId, $this->accountId); } $result = ['id' => $this->socialAccountId, 'origin' => $this->origin]; $result['name'] = empty($channel->name) ? '' : $channel->name; $result['type'] = empty($channel->type) ? '' : $channel->type; $result['status'] = empty($channel->status) ? '' : $channel->status; return $result; }, 'socialMember' => function () { if (!empty($this->openId) && !empty($this->socialAccountId)) { try { $socialMember = \Yii::$app->weConnect->getFollowerByOriginId($this->openId, $this->socialAccountId); } catch (ApiDataException $e) { $socialMember = ''; } } $result = null; if (!empty($socialMember) && !empty($socialMember['nickname'])) { $result = $socialMember['nickname']; } return $result; }, 'properties' => function () { $result = []; foreach ($this->properties as $property) { $property['id'] = $property['id'] . ''; $result[] = $property; } return $result; }, 'cardProvideTime' => function () { return MongodbUtil::MongoDate2String($this->cardProvideTime, 'Y-m-d H:i:s'); }, 'cardExpired' => function () { if (!empty($this->cardExpiredAt) && $this->cardExpiredAt < TimeUtil::msTime()) { return 1; } else { return 0; } }, 'avatar', 'location', 'tags', 'score', 'remarks', 'cardNumber', 'unionId', 'totalScore', 'cardExpiredAt' => function () { return empty($this->cardExpiredAt) ? '' : TimeUtil::msTime2String($this->cardExpiredAt); }, 'birth', 'openId', 'qrcodeViewed', 'totalScoreAfterZeroed', 'isDisabled']); }
/** * Get by channelId * @param string $channelId * @return \yii\db\static * @author Rex Chen */ public function getByChannelId($channelId) { return ModelChannel::getByChannelId($channelId, $this->accountId); }
public static function getChannelInfo($params) { $scoreHistoryChannel = []; if (!empty($params['channelId'])) { $channelInfo = Channel::getByChannelId($params['channelId']); $scoreHistoryChannel = ['id' => $channelInfo->channelId, 'name' => $channelInfo->name, 'origin' => $channelInfo->origin]; $channelType = $channelInfo->origin; $channelName = $channelInfo->name; } else { $scoreHistoryChannel = ['origin' => PromotionCode::PORTAL]; $channelName = ''; $channelType = PromotionCode::PROMOTION_CODE_EXCHANGE; } return ['scoreHistoryChannel' => $scoreHistoryChannel, 'channelName' => $channelName, 'channelType' => $channelType]; }
private function _saveLog(Member $member, $exchanges, $params, $user = null) { $goodsExchangeLog = new GoodsExchangeLog(); $allGoods = []; $totalCount = 0; $scoreHistoryDescription = ''; foreach ($exchanges as $exchange) { $goods = $exchange['goods']; $count = $exchange['count']; $pictures = $goods->pictures; $allGoods[] = ['id' => $goods->_id, 'productId' => $goods->productId, 'sku' => $goods->sku, 'picture' => empty($pictures[0]) ? '' : $pictures[0], 'productName' => $goods->productName, 'count' => $count]; $totalCount += $count; $scoreHistoryDescription .= $goods->productName . "({$count}); "; } $scoreHistoryDescription = trim($scoreHistoryDescription, '; '); $goodsExchangeLog->goods = $allGoods; $goodsExchangeLog->memberId = $member->_id; $properties = $member->properties; $name = ''; foreach ($properties as $property) { if ($property['name'] == Member::DEFAULT_PROPERTIES_NAME) { $name = $property['value']; } if ($property['name'] == Member::DEFAULT_PROPERTIES_MOBILE) { $mobile = $property['value']; } } $goodsExchangeLog->memberName = $name; $goodsExchangeLog->telephone = empty($params['phone']) ? $mobile : $params['phone']; $goodsExchangeLog->usedScore = $params['usedScore']; $goodsExchangeLog->expectedScore = $params['expectedScore']; $goodsExchangeLog->count = $totalCount; $goodsExchangeLog->address = $params['address']; $goodsExchangeLog->receiveMode = $params['receiveMode']; $goodsExchangeLog->postcode = empty($params['postcode']) ? '' : $params['postcode']; $goodsExchangeLog->isDelivered = false; $scoreHistoryChannel = []; if (!empty($params['channelId'])) { $channelInfo = Channel::getByChannelId($params['channelId'], $member->accountId); $scoreHistoryChannel = ['id' => $channelInfo->channelId, 'name' => $channelInfo->name, 'origin' => $channelInfo->origin]; $goodsExchangeLog->usedFrom = ['id' => $params['channelId'], 'type' => $channelInfo->origin, 'name' => $channelInfo->name]; } else { $scoreHistoryChannel = ['origin' => GoodsExchangeLog::PORTAL]; $goodsExchangeLog->usedFrom = ['id' => '', 'type' => GoodsExchangeLog::PORTAL, 'name' => GoodsExchangeLog::OFFLINE_EXCHANGE]; } $goodsExchangeLog->accountId = $member->accountId; $scoreHistory = new ScoreHistory(); $scoreHistory->assigner = ScoreHistory::ASSIGNER_EXCHAGE_GOODS; $scoreHistory->increment = 0 - $params['usedScore']; $scoreHistory->memberId = $member->_id; $scoreHistory->brief = ScoreHistory::ASSIGNER_EXCHAGE_GOODS; $scoreHistory->description = $scoreHistoryDescription; $scoreHistory->accountId = $member->accountId; $scoreHistory->channel = $scoreHistoryChannel; $scoreHistory->user = $user; if ($goodsExchangeLog->save(true) && $scoreHistory->save(true)) { MemberLogs::record($member->_id, $member->accountId, MemberLogs::OPERATION_REDEEM); return true; } else { LogUtil::error(['exchange fail' => [$scoreHistory->getErrors(), $goodsExchangeLog->getErrors()]]); return false; } }