public function actionValidateNumber()
 {
     $mobile = Yii::$app->request->post("mobile");
     $accountId = $this->getAccountId();
     $result = Member::getByMobile($mobile, null);
     return $result;
 }
Пример #2
0
 public static function preProcessCnyWinnerData($condition)
 {
     // TODO refine smsContent
     $smsTemplate = BulkSmsUtil::CNY_WINNERS_SMS_TEMPLATE;
     $winners = LuckyDrawWinner::find()->where($condition)->all();
     $rows = array();
     foreach ($winners as $winner) {
         $smsContent = str_replace("%username%", $winner->name, $smsTemplate);
         $smsContent = str_replace("%awardName%", $winner->awardName, $smsContent);
         $member = Member::getByMobile($winner->mobile, $condition['accountId']);
         if ($member != null) {
             $city = null;
             $site = null;
             foreach ($member->properties as $property) {
                 if ($property['name'] == '縣市') {
                     $city = $property['value'];
                 }
                 if ($property['name'] == '地址') {
                     $site = $property['value'];
                 }
             }
             $row = ['mobile' => " " . $winner->mobile, 'name' => $winner->name, 'city' => $city, 'site' => $site, 'awardName' => $winner->awardName, 'scoreAdded' => $winner->winInfo['scoreAdded'], 'createdAt' => MongodbUtil::MongoDate2String($winner->createdAt, 'Y-m-d H:i', null), 'remark' => $winner->winInfo['boughtAllProducts'] ? '已包含全部3支品項' : '', 'smsContent' => $smsContent];
             $rows[] = $row;
             unset($row, $smsContent, $member, $city, $site);
         }
     }
     return $rows;
 }
 public function actionGetTestSms()
 {
     $smsTag = $this->getQuery('smsTag');
     $accountId = $this->getAccountId();
     if (empty($smsTag)) {
         throw new BadRequestHttpException('param is missing.(smsTag)');
     }
     $member = Member::getByMobile(EarlybirdSmsUtil::MOBILE_FOR_TEST);
     $sms = EarlybirdSmsUtil::getSms($member, $smsTag, $accountId);
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     if (empty($member)) {
         return ['code' => 1000];
     }
     return ['code' => 200, 'sms' => $sms];
 }
Пример #4
0
 /**
  * Get accountId and company when bind
  * @param array $params
  * @throws BadRequestHttpException
  * @throws InvalidParameterException
  * @return array
  */
 public function bind($params)
 {
     \Yii::$app->language = LanguageUtil::LANGUAGE_ZH;
     $this->checkCode($params);
     $mobile = $params['mobile'];
     if (empty($params['channelId']) || empty($params['openId'])) {
         throw new BadRequestHttpException('Missing params');
     }
     $channelId = $params['channelId'];
     $channel = Channel::getEnableByChannelId($channelId);
     $member = Member::getByMobile($mobile, $channel->accountId);
     if (!empty($member)) {
         throw new InvalidParameterException(['phone' => \Yii::t('common', 'phone_has_been_bound')]);
     }
     $account = Account::findByPk($channel->accountId);
     return ['accountId' => $channel->accountId, 'company' => empty($account->company) ? null : $account->company];
 }
Пример #5
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");
     }
 }
 private function _prizeDraw($activityId, $mobile, $deviceId)
 {
     $bar = ActivityBar::findOne($activityId);
     $probalility = $bar->probability;
     //未中奖几率
     $drawArr = array();
     //抽奖阵列
     // 加入中奖的比率
     $i = 100 - (int) $probalility;
     while ($i > 0) {
         $drawArr[] = 'Y';
         $i--;
     }
     // 加入沒中奖的比率
     $i = (int) $probalility;
     while ($i > 0) {
         $drawArr[] = 'N';
         $i--;
     }
     shuffle($drawArr);
     // 抽奖 (随机打乱阵列)
     $hasPrize = $drawArr[0];
     // 抽出其中一个看是否中奖
     $resultPrize = null;
     $thankPrize = array('name' => "銘謝惠顧", 'prizeImgUrl' => "", '_id' => 'thanks');
     //给中奖用户抽奖品
     if ($hasPrize == 'Y') {
         $prizeList = ActivityPrize::getValidPrizesByActivityId($activityId);
         if (!empty($prizeList)) {
             $prizeArr = array();
             //奖品阵列
             $today = strtotime(date('Y-m-d H:i:s'));
             $todayMstime = MongodbUtil::MongoDate2msTimeStamp(new \MongoDate($today));
             foreach ($prizeList as $prize) {
                 if ($prize['type'] == 'topPrize') {
                     if ($prize['startDate'] <= $todayMstime and $todayMstime < $prize['endDate']) {
                         $prizeArr[] = $prize;
                     }
                 } else {
                     $prizeArr[] = $prize;
                 }
             }
             unset($prizeList);
             shuffle($prizeArr);
             $resultPrize = $prizeArr[0];
         } else {
             //奖项列表为空,改发鸣谢惠顾
             $hasPrize = 'N';
             $resultPrize = $thankPrize;
         }
     } else {
         $resultPrize = $thankPrize;
     }
     //判断奖品数量是否足够
     $activityPrize = ActivityPrize::findOne($resultPrize['_id']);
     if (!empty($activityPrize)) {
         $quantity = $activityPrize->quantity - 1;
         $activityPrize->quantity = $quantity;
         if ($quantity >= 0) {
             if (!$activityPrize->save()) {
                 //更新奖品数量(减1), 失敗,改发鸣谢惠顾
                 $hasPrize = 'N';
                 $resultPrize = $thankPrize;
             }
         } else {
             //奖品不够,改发鸣谢惠顾
             $hasPrize = 'N';
             $resultPrize = $thankPrize;
         }
     }
     if ($resultPrize['_id'] != 'thanks' and $resultPrize['isPoint'] == 'Y') {
         //add points
         try {
             $service = Yii::$app->service;
             $service->accountId = $this->getAccountId();
             $member = Member::getByMobile($mobile, $this->getAccountId());
             $rewardResult = $service->member->rewardScore([$member['_id']], $resultPrize['points'], '拉霸活動中獎');
             if (is_bool($rewardResult) && $rewardResult) {
                 $resultPrize['name'] = $resultPrize['name'] . '(已發獎' . $resultPrize['points'] . '積分)';
             } else {
                 $hasPrize = 'N';
                 $resultPrize = $thankPrize;
                 //Failed,改发鸣谢惠顾
             }
             unset($service, $member, $rewardResult);
         } catch (Exception $e) {
             $hasPrize = 'N';
             $resultPrize = $thankPrize;
             LogUtil::error(['message' => '获奖者加积分捕获未知错误', 'result' => '改发鸣谢惠顾', 'mobile' => $mobile, 'exception' => $e], 'activitybar');
         }
     }
     //save in DB
     $params = array('activityId' => $activityId, 'prizeId' => $resultPrize['_id'], 'deviceId' => $deviceId, 'mobile' => $mobile, 'prizeContent' => $resultPrize['name']);
     $isOK = ActivityUser::createUser($params);
     if (!$isOK) {
         $hasPrize = 'N';
         $resultPrize = $thankPrize;
     }
     $result = ['hasPrize' => $hasPrize, 'prizeName' => $resultPrize['name'], 'prizeImage' => $resultPrize['prizeImgUrl']];
     unset($thankPrize, $resultPrize, $params);
     return ['code' => 200, 'msg' => 'ok', 'result' => $result];
 }
 public function actionGetDrawTestSms()
 {
     $params = $this->getQuery();
     if (empty($params) || count($params) < 8) {
         throw new BadRequestHttpException('params are missing');
     }
     $member = Member::getByMobile(EarlybirdSmsUtil::MOBILE_FOR_TEST);
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     if (empty($member)) {
         return ['code' => 1000];
     }
     $condition = array_merge($params, ['accountId' => $this->getAccountId()]);
     $smsArr = EarlybirdSmsUtil::getSmsForMemberCanDraw($condition);
     $testSms = array();
     foreach ($smsArr as $sms) {
         if (!empty($sms) && $sms['mobile'] == EarlybirdSmsUtil::MOBILE_FOR_TEST) {
             $testSms['mobile'] = $sms['mobile'];
             $testSms['smsContent'] = $sms['content'];
         }
     }
     if (empty($testSms)) {
         return ['code' => 2000];
         //测试号码不满足抽奖条件
     }
     return ['code' => 200, 'sms' => $testSms];
 }
Пример #8
0
 /**
  * check the member
  */
 public function actionCheckMember()
 {
     $params = $this->getQuery();
     if (empty($params['searchKey'])) {
         throw new BadRequestHttpException('missing params');
     }
     $accountId = $this->getAccountId();
     $result = Member::getByMobile($params['searchKey'], $accountId);
     return $result;
 }