コード例 #1
0
 public function actionAnswer()
 {
     $params = $this->getParams();
     if (empty($params['questionnaireId']) || empty($params['answers'])) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $user = [];
     if (!empty($params['user']['channelId']) && !empty($params['user']['openId'])) {
         $channelId = $params['user']['channelId'];
         $openId = $params['user']['openId'];
         $user = $params['user'];
         try {
             $follower = Yii::$app->weConnect->getFollowerByOriginId($openId, $channelId);
             $user['name'] = empty($follower['nickname']) ? '' : $follower['nickname'];
         } catch (ApiDataException $e) {
             LogUtil::error(['message' => 'Answer questionnaire failed to get follower info', 'param' => ['openId' => $openId, 'channelId' => $channelId]]);
         }
     }
     $questionnaireId = new MongoId($params['questionnaireId']);
     $questionnaire = Questionnaire::findByPk($questionnaireId);
     //error questionnaire id or un published questionnaire
     if (empty($questionnaire) || !$questionnaire->isPublished) {
         throw new InvalidParameterException(Yii::t('content', 'invalid_questionnaire'));
     }
     //questionnaire has not begun
     $now = time();
     if (MongodbUtil::MongoDate2TimeStamp($questionnaire->startTime) > $now) {
         throw new InvalidParameterException(Yii::t('content', 'questionnaire_not_began'));
     }
     //questionnaire expired
     if (MongodbUtil::MongoDate2TimeStamp($questionnaire->endTime) < $now) {
         throw new InvalidParameterException(Yii::t('content', 'questionnaire_expired'));
     }
     if (!empty($user)) {
         $questionnaireLog = QuestionnaireLog::getByQuestionnaireAndUser($questionnaireId, $user);
         if (!empty($questionnaireLog)) {
             throw new InvalidParameterException(Yii::t('content', 'user_has_answered_questionnaire'));
         }
     }
     $quertionnaireLog = new QuestionnaireLog();
     $quertionnaireLog->questionnaireId = $questionnaireId;
     $quertionnaireLog->user = $user;
     $quertionnaireLog->answers = $params['answers'];
     $quertionnaireLog->accountId = $questionnaire->accountId;
     if ($quertionnaireLog->save()) {
         return ['message' => 'OK', 'data' => ''];
     } else {
         throw new ServerErrorHttpException(Yii::t('common', 'save_fail'));
     }
 }
コード例 #2
0
ファイル: Campaign.php プロジェクト: timelessmemory/uhkklp
 /**
  * The default implementation returns the names of the columns whose values have been populated into campaign.
  */
 public function fields()
 {
     return array_merge(parent::fields(), ['name', 'startTime' => function () {
         return MongodbUtil::MongoDate2String($this->startTime, 'Y-m-d H:i:00');
     }, 'endTime' => function () {
         return MongodbUtil::MongoDate2String($this->endTime, 'Y-m-d H:i:00');
     }, 'participantCount', 'limitTimes', 'promotion' => function () {
         $promotion = $this->promotion;
         $data = [];
         if (!empty($promotion['data'])) {
             $products = Product::getByIds($promotion['data']);
             foreach ($products as $product) {
                 $data[] = ['id' => $product->_id . '', 'name' => $product->name];
             }
             $promotion['data'] = $data;
         }
         if (is_array($promotion['products'])) {
             foreach ($promotion['products'] as &$products) {
                 $products .= '';
             }
         }
         return $promotion;
     }, 'isActivated', 'isExpired' => function () {
         $nextMinute = strtotime('+ 1 minute - ' . date('s') . 'seconds');
         return MongodbUtil::MongoDate2TimeStamp($this->endTime) < $nextMinute;
     }]);
 }
コード例 #3
0
 /**
  * Send mobile captcha.
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/mobile/send-captcha<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for send mobile captcha.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     mobile: string, phone number<br/>
  *     unionId: string<br/>
  *     language: 'zh_cn' or 'en_us', This param is just for update mobile<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     message: OK or Fail
  *     data: string, if success, It is verification code<br/>
  *     <br/><br/>
  *
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *  "message": "OK",
  *  "data": "456787"
  * }
  * </pre>
  */
 public function actionSendCaptcha()
 {
     $params = $this->getParams();
     if (empty($params['type']) || empty($params['mobile']) || empty($params['codeId']) || empty($params['code'])) {
         throw new BadRequestHttpException('Missing params');
     }
     $type = $params['type'];
     $mobile = $params['mobile'];
     if (in_array($type, [self::CAPTCHA_TYPE_COMPANY_INFO, self::CAPTCHA_TYPE_EXCHANGE])) {
         $params['accountId'] = $this->getAccountId();
     } else {
         if (!in_array($type, [self::CAPTCHA_TYPE_BIND, self::CAPTCHA_TYPE_SIGNUP])) {
             throw new BadRequestHttpException('Invalid type');
         }
     }
     $this->attachBehavior('CaptchaBehavior', new CaptchaBehavior());
     $companyInfo = $this->{$type}($params);
     $company = $companyInfo['company'] === null ? self::DEFAULT_COMPANY : $companyInfo['company'];
     $accountId = $companyInfo['accountId'];
     //limit captcha send by ip
     $ip = Yii::$app->request->userIp;
     $captcha = Captcha::getByIP($ip);
     $now = time();
     if (!empty($captcha)) {
         $sendTimeInt = MongodbUtil::MongoDate2TimeStamp($captcha->createdAt);
         $nextTime = $sendTimeInt + Yii::$app->params['captcha_send_interval'];
         if ($nextTime > $now) {
             throw new InvalidParameterException(['phone' => Yii::t('common', 'send_too_frequently')]);
         } else {
             $captcha->isExpired = true;
             $captcha->save();
         }
     }
     //get random string, length = 6, charlist = '0123456789'
     $code = StringUtil::rndString(6, 0, '0123456789');
     $text = str_replace('#code#', $code, Yii::$app->params['mobile_message_text']);
     $text = str_replace('#company#', $company, $text);
     $captcha = new Captcha();
     $captcha->ip = $ip;
     $captcha->code = $code;
     $captcha->mobile = $mobile;
     $captcha->isExpired = false;
     $captcha->accountId = $accountId;
     if (MessageUtil::sendMobileMessage($mobile, $text) && $captcha->save()) {
         MessageUtil::recoreMessageCount('omni_record_message_' . $type);
         $result = ['message' => 'OK', 'data' => ''];
     } else {
         $result = ['message' => 'Error', 'data' => 'unknow error'];
     }
     return $result;
 }
コード例 #4
0
 /**
  * The default implementation returns the names of the columns whose values have been populated into applications.
  */
 public function fields()
 {
     return array_merge(parent::fields(), ['name', 'privateKey', 'icon', 'content', 'createdAt' => function () {
         return MongodbUtil::MongoDate2TimeStamp($this->createdAt);
     }]);
 }
コード例 #5
0
ファイル: Account.php プロジェクト: timelessmemory/uhkklp
 /**
  * Get account access key and sercet key
  */
 public function getKey()
 {
     return ['accessKey' => $this->accessKey, 'secretKey' => $this->secretKey, 'keyCreatedAt' => MongodbUtil::MongoDate2TimeStamp($this->keyCreatedAt)];
 }
コード例 #6
0
 /**
  * Check message captch
  * @param string $mobile
  * @param string $code
  * @throws InvalidParameterException
  */
 public function checkCaptcha($mobile, $code)
 {
     $now = time();
     //get available captcha by mobile
     $captcha = Captcha::getByMobile($mobile);
     if (!empty($captcha)) {
         $sendTimeInt = MongodbUtil::MongoDate2TimeStamp($captcha->createdAt);
         $availabTime = $sendTimeInt + Yii::$app->params['captcha_availab_time'];
         if ($captcha['code'] != $code) {
             throw new InvalidParameterException(['captcha' => Yii::t('common', 'captcha_error')]);
         }
         $captcha->isExpired = true;
         $captcha->save(true, ['isExpired']);
         if ($now > $availabTime) {
             throw new InvalidParameterException(['captcha' => Yii::t('common', 'captcha_expired')]);
         }
     } else {
         throw new InvalidParameterException(['phone' => Yii::t('common', 'mobile_error')]);
     }
 }
コード例 #7
0
 public static function createDailyData($members, $campaignLogs, $propertyIdStrs, $accountId)
 {
     $statsRows = [];
     foreach ($campaignLogs as $campaignLog) {
         $campaignLog = $campaignLog['_id'];
         if ($campaignLog['redeemTime'] == $campaignLog['createdAt']) {
             $createdAt = $campaignLog['createdAt'];
         } else {
             $createdAt = $campaignLog['redeemTime'];
         }
         //check the redeem time whether exists
         $createdAt = MongodbUtil::MongoDate2TimeStamp($createdAt);
         $month = date('Y-m', $createdAt);
         $memberId = $campaignLog['member']['id'];
         $memProperty = [];
         if (isset($members[(string) $memberId])) {
             $member = $members[(string) $memberId];
             foreach ($member->properties as $property) {
                 $propertyId = (string) $property['id'];
                 if (in_array($propertyId, $propertyIdStrs)) {
                     $memProperty[$propertyId] = $property['value'];
                 }
             }
         }
         $statsRows[] = ['memberId' => $memberId, 'memProperty' => $memProperty, 'productId' => $campaignLog['productId'], 'code' => $campaignLog['code'], 'year' => date('Y', $createdAt), 'month' => $month, 'quarter' => TimeUtil::getQuarter($createdAt), 'accountId' => $accountId, 'createdAt' => $campaignLog['createdAt']];
     }
     StatsMemberCampaignLogDaily::batchInsert($statsRows);
     unset($statsRows, $members, $campaignLog, $campaignLogs);
 }
コード例 #8
0
 public static function getHistoryByProduct($productId)
 {
     $raws = self::getCollection()->aggregate([['$match' => ['productId' => $productId]], ['$group' => ['_id' => ['createdAt' => '$createdAt', 'isUsed' => '$isUsed'], 'count' => ['$sum' => 1]]], ['$sort' => ['createdAt' => -1]]]);
     $raws = empty($raws) ? [] : $raws;
     //check th campaign
     $enable = true;
     $campaignWhere = ['promotion.data' => ['$all' => [$productId]], 'isActivated' => true];
     $campaign = Campaign::findOne($campaignWhere);
     if (!empty($campaign)) {
         $enable = false;
     }
     $result = [];
     foreach ($raws as $raw) {
         $createdAt = $raw['_id']['createdAt'];
         $timestampCreatedAt = MongodbUtil::MongoDate2TimeStamp($createdAt);
         $strCreatedAt = MongodbUtil::MongoDate2String($createdAt, 'Y-m-d H:i:s');
         $result[$timestampCreatedAt]['createdAt'] = $strCreatedAt;
         $result[$timestampCreatedAt]['all'] = empty($result[$timestampCreatedAt]['all']) ? 0 : $result[$timestampCreatedAt]['all'];
         $result[$timestampCreatedAt]['timestamp'] = $timestampCreatedAt;
         $result[$timestampCreatedAt]['used'] = empty($result[$timestampCreatedAt]['used']) ? 0 : $result[$timestampCreatedAt]['used'];
         $result[$timestampCreatedAt]['rest'] = empty($result[$timestampCreatedAt]['rest']) ? 0 : $result[$timestampCreatedAt]['rest'];
         if ($raw['_id']['isUsed']) {
             $result[$timestampCreatedAt]['used'] = $raw['count'];
         } else {
             $result[$timestampCreatedAt]['rest'] = $raw['count'];
         }
         $result[$timestampCreatedAt]['all'] += $raw['count'];
         $result[$timestampCreatedAt]['enable'] = $enable;
     }
     $result = array_values($result);
     ArrayHelper::multisort($result, 'createdAt', SORT_DESC);
     return $result;
 }
コード例 #9
0
 /**
  * @return int
  */
 public static function getRedeemTime($campaignLog)
 {
     if ($campaignLog['redeemTime'] == $campaignLog['createdAt']) {
         $redeemTime = $campaignLog['createdAt'];
     } else {
         $redeemTime = $campaignLog['redeemTime'];
     }
     return MongodbUtil::MongoDate2TimeStamp($redeemTime);
 }
コード例 #10
0
ファイル: WebHook.php プロジェクト: timelessmemory/uhkklp
 /**
  * The default implementation returns the names of the columns whose values have been populated into webHooks.
  */
 public function fields()
 {
     return array_merge(parent::fields(), ['url', 'isEnabled', 'channels', 'createdAt' => function () {
         return MongodbUtil::MongoDate2TimeStamp($this->createdAt);
     }]);
 }
コード例 #11
0
 /**
  * Verify if time is true.
  */
 public function validateTime($attribute)
 {
     $time = $this->{$attribute};
     if ($attribute == 'startTime') {
         $now = time();
         if ($time < $now) {
             throw new InvalidParameterException(['beginDatePicker' => \Yii::t('product', 'invalid_start_time')]);
         }
     } else {
         if ($attribute == 'endTime') {
             if ($time <= MongodbUtil::MongoDate2TimeStamp($this->startTime)) {
                 throw new InvalidParameterException(['endDatePicker' => \Yii::t('product', 'invalid_end_time')]);
             }
         }
     }
 }