/**
  * @param $data array eg: [['mobile'=>'0912345678', 'smsContent'=>'恭喜中奖了'], ...]
  * @param $smsName string eg: 'cny_winners'
  * @param $smsRecord MongoId
  */
 public static function sendSms($data, $smsName, $smsRecordId, $accountId)
 {
     BulkSmsRecord::updateProcessById($smsRecordId, 1);
     // 正在發送
     try {
         if (!empty($data)) {
             foreach ($data as $sms) {
                 $mobile = self::processSmsMobile($accountId, $sms['mobile']);
                 $response = MessageUtil::sendMobileMessage($mobile, $sms['smsContent'], $accountId);
                 BulkSmsLog::createSmsLog($sms['mobile'], $sms['smsContent'], $response, $smsRecordId, $accountId);
                 if (!$response) {
                     LogUtil::error(['message' => '群發簡訊失敗', 'mobile' => $mobile, 'SMSContent' => $sms['smsContent']], 'bulkSms');
                     BulkSmsFailed::createSmsFailed($sms['mobile'], $sms['smsContent'], $smsRecordId, $accountId);
                 }
                 unset($response, $mobile);
             }
             BulkSmsRecord::updateProcessById($smsRecordId, 2);
             // 發送完成
         }
     } catch (\Exception $e) {
         LogUtil::error(['message' => 'EarlyBirdSms發送失敗', 'error' => $e], 'earlybird');
         BulkSmsRecord::updateProcessById($smsRecordId, 3);
         // 發送故障
         throw $e;
     }
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['accountId']) || empty($args['key']) || empty($args['header']) || empty($args['type']) || empty($args['smsRecordId'])) {
         ResqueUtil::log(['status' => 'fail to export bulk sms record', 'message' => 'missing params', 'args' => $args]);
         return false;
     }
     $accountId = new \MongoId($args['accountId']);
     $smsRecordId = new \MongoId($args['smsRecordId']);
     $header = $args['header'];
     $fileName = $args['key'];
     $filePath = ExcelUtil::getFile($fileName, 'csv');
     $rows = null;
     if ($args['type'] == 'all') {
         $rows = BulkSmsLog::preProcessBulkSmsRecordData($smsRecordId, $accountId);
     } else {
         if ($args['type'] == 'faild') {
             // 暂时还没用到
         }
     }
     ExcelUtil::exportCsv($header, $rows, $filePath, 1);
     $hashKey = ExcelUtil::setQiniuKey($filePath, $fileName);
     if ($hashKey) {
         //notice frontend the job is finished
         Yii::$app->tuisongbao->triggerEvent(Message::EVENT_EXPORT_FINISH, ['key' => $fileName], [Message::CHANNEL_GLOBAL . $args['accountId']]);
         return true;
     } else {
         ResqueUtil::log(['status' => 'fail to export early bird sms details', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]);
         return false;
     }
 }
 public static function updateSmsRecordById($id)
 {
     $smsCount = BulkSmsLog::getCountBySmsRecordId($id);
     $smsRecord = BulkSmsRecord::findByPk($id);
     $smsRecord->successful = $smsCount['successful'];
     $smsRecord->failed = $smsCount['failed'];
     $smsRecord->save();
     return $smsRecord;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['data']) || empty($args['accountId']) || empty($args['modelContent']) || empty($args['smsBatch'])) {
         ResqueUtil::log(['status' => 'fail to send sms', 'message' => 'missing params', 'args' => $args]);
         LogUtil::error(['message' => 'missing params in job', 'args' => $args], 'Sms');
     }
     $data = $args['data'];
     $accountId = $args['accountId'];
     $modelContent = $args['modelContent'];
     $smsBatch = $args['smsBatch'];
     $failureCount = 0;
     $successCount = 0;
     $totalCount = 0;
     try {
         if (!empty($data)) {
             foreach ($data as $sms) {
                 if ($sms['mobile'] != '') {
                     $response = MessageUtil::sendMobileMessage($sms['mobile'], $sms['content'], $accountId);
                     // $response = MessageUtil::sendMobileMessage($sms['mobile'], $sms['content']);
                     BulkSmsLog::createSmsLog($sms['mobile'], $sms['content'], $response, $smsBatch, $accountId);
                     if (!$response) {
                         $failureCount++;
                         LogUtil::error(['message' => '群發簡訊失敗', 'mobile' => $sms['mobile'], 'SMSContent' => $sms['content']], 'bulkSms');
                         BulkSmsFailed::createSmsFailed($sms['mobile'], $sms['content'], $smsBatch, $accountId);
                     } else {
                         $successCount++;
                         LogUtil::error(['message' => '群發簡訊成功', 'mobile' => $sms['mobile'], 'SMSContent' => $sms['content']], 'bulkSms');
                         BulkSmsSuccess::createSmsSuccess($sms['mobile'], $sms['content'], $smsBatch, $accountId);
                     }
                     unset($response);
                 } else {
                     LogUtil::error(date('Y-m-d h:i:s') . '号码为空.');
                     $failureCount++;
                 }
             }
             $totalCount = $successCount + $failureCount;
             //record result
             $SmsResultModel = new SmsResultModel();
             $SmsResultModel->successRecord = $successCount;
             $SmsResultModel->failureRecord = $failureCount;
             $SmsResultModel->totalRecord = $totalCount;
             $SmsResultModel->smsBatch = $smsBatch;
             $SmsResultModel->accountId = $accountId;
             $SmsResultModel->modelContent = $modelContent;
             $SmsResultModel->save();
         }
     } catch (\Exception $e) {
         LogUtil::error(['message' => 'Sms發送失敗', 'error' => $e], 'sms');
         throw $e;
     }
 }
Exemple #5
0
 public static function preProcessBulkSmsRecordData($smsRecordId, $accountId)
 {
     $logs = BulkSmsLog::find()->where(['smsRecordId' => $smsRecordId, 'accountId' => $accountId])->orderBy(['createdAt' => SORT_ASC])->all();
     $rows = array();
     if (!empty($logs)) {
         foreach ($logs as $log) {
             $row = ['mobile' => "'" . $log->mobile, 'smsContent' => $log->smsContent, 'status' => $log->status ? '成功' : '失敗', 'createdAt' => MongodbUtil::MongoDate2String($log->createdAt, 'Y-m-d H:i:s', null)];
             $rows[] = $row;
             unset($row);
         }
     }
     return $rows;
 }