コード例 #1
0
 /**
  * Follower bind to be a member.
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/mobile/bind<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for follower bind.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     mobile: string<br/>
  *     openId: string<br/>
  *     unionId: string<br/>
  *     channelId: string<br/>
  *     captcha: string<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     <br/><br/>
  *
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *  "message": "OK",
  *  "data": "http://wm.com?memberId=55d29e86d6f97f72618b4569"
  * </pre>
  */
 public function actionBind()
 {
     //set language zh_cn when bind
     Yii::$app->language = LanguageUtil::LANGUAGE_ZH;
     $params = $this->getParams();
     if (empty($params['mobile']) || empty($params['openId']) || empty($params['channelId']) || empty($params['captcha'])) {
         throw new BadRequestHttpException('missing param');
     }
     $isTest = false;
     if ($params['mobile'] == self::TEST_PHONE && $params['captcha'] == self::TEST_CODE) {
         $isTest = true;
     } else {
         $this->attachBehavior('CaptchaBehavior', new CaptchaBehavior());
         $this->checkCaptcha($params['mobile'], $params['captcha']);
     }
     //get accountId
     $openId = $params['openId'];
     $channel = Channel::getEnableByChannelId($params['channelId']);
     $origin = $channel->origin;
     $accountId = $channel->accountId;
     //create accessToken
     $token = Token::createForMobile($accountId);
     if (empty($token['accessToken'])) {
         throw new ServerErrorHttpException('Failed to create token for unknown reason.');
     }
     $accessToken = $token['accessToken'];
     $this->setAccessToken($accessToken);
     //if member has bind
     if (empty($params['unionId'])) {
         $member = Member::getByOpenId($openId);
     } else {
         $unionId = $params['unionId'];
         $member = Member::getByUnionid($unionId);
     }
     if (!empty($member)) {
         $memberId = (string) $member->_id;
         $url = $this->buildBindRedirect($memberId, $params);
         return ['message' => 'OK', 'data' => $url];
     }
     //check mobile has been bind
     $member = Member::getByMobile($params['mobile'], new \MongoId($accountId));
     if (!empty($member)) {
         throw new InvalidParameterException(['phone' => Yii::t('common', 'phone_has_been_bound')]);
     }
     $follower = Yii::$app->weConnect->getFollowerByOriginId($openId, $params['channelId']);
     $originScene = empty($follower['firstSubscribeSource']) ? '' : $follower['firstSubscribeSource'];
     //init avatar and location
     $avatar = !empty($follower['headerImgUrl']) ? $follower['headerImgUrl'] : Yii::$app->params['defaultAvatar'];
     $location = [];
     !empty($follower['city']) ? $location['city'] = $follower['city'] : null;
     !empty($follower['province']) ? $location['province'] = $follower['province'] : null;
     !empty($follower['country']) ? $location['country'] = $follower['country'] : null;
     LogUtil::info(['message' => 'get follower info', 'follower' => $follower], 'channel');
     //init member properties
     $memberProperties = MemberProperty::getByAccount($accountId);
     $propertyMobile = [];
     $propertyGender = [];
     $propertyName = [];
     $properties = [];
     foreach ($memberProperties as $memberProperty) {
         if ($memberProperty['name'] == Member::DEFAULT_PROPERTIES_MOBILE) {
             $propertyMobile['id'] = $memberProperty['_id'];
             $propertyMobile['name'] = $memberProperty['name'];
             $propertyMobile['value'] = $params['mobile'];
             $properties[] = $propertyMobile;
         }
         if ($memberProperty['name'] == Member::DEFAULT_PROPERTIES_GENDER) {
             $propertyGender['id'] = $memberProperty['_id'];
             $propertyGender['name'] = $memberProperty['name'];
             $propertyGender['value'] = !empty($follower['gender']) ? strtolower($follower['gender']) : 'male';
             $properties[] = $propertyGender;
         }
         if ($memberProperty['name'] == Member::DEFAULT_PROPERTIES_NAME) {
             $propertyName['id'] = $memberProperty['_id'];
             $propertyName['name'] = $memberProperty['name'];
             $propertyName['value'] = $follower['nickname'];
             $properties[] = $propertyName;
         }
     }
     //get default card
     $card = MemberShipCard::getDefault($accountId);
     $memberId = new \MongoId();
     $memberAttribute = ['$set' => ['_id' => $memberId, 'avatar' => $avatar, 'cardId' => $card['_id'], 'location' => $location, 'score' => 0, 'socialAccountId' => $params['channelId'], 'properties' => $properties, 'openId' => $openId, 'origin' => $origin, 'originScene' => $originScene, 'unionId' => empty($unionId) ? '' : $unionId, 'accountId' => $accountId, 'cardNumber' => Member::generateCardNumber($card['_id']), 'cardProvideTime' => new \MongoDate(), 'createdAt' => new \MongoDate(), 'socials' => [], 'isDeleted' => false]];
     if (!$isTest) {
         $result = Member::updateAll($memberAttribute, ['openId' => $openId], ['upsert' => true]);
     } else {
         $result = true;
     }
     if ($result) {
         // reword score first bind card
         $this->attachBehavior('MemberBehavior', new MemberBehavior());
         $this->updateItemByScoreRule(Member::findByPk($memberId));
         Yii::$app->qrcode->create(Yii::$app->request->hostInfo, Qrcode::TYPE_MEMBER, $memberId, $accountId);
         $memberId = (string) $memberId;
         $url = $this->buildBindRedirect($memberId, $params);
         return ['message' => 'OK', 'data' => $url];
     } else {
         LogUtil::error(['error' => 'member save error', 'params' => Json::encode($member)], 'member');
         throw new ServerErrorHttpException("bind fail");
     }
 }