public function actionExportSmsModel() { $id = $this->getQuery('modelGroupId'); // LogUtil::error(date('Y-m-d h:i:s') . ' $id: ' . $id); $condition = ['groupId' => $id]; $result = SmsModel::find()->where($condition)->one(); if (!empty($result)) { $key = 'Sms發送手機號及內容列表' . date('YmdHis'); $header = ['groupId' => '導入批號', 'mobile' => '手機號碼', 'content' => '內容']; $exportArgs = ['key' => $key, 'header' => $header, 'condition' => serialize($condition)]; $jobId = Yii::$app->job->create('backend\\modules\\uhkklp\\job\\ExportSmsModel', $exportArgs); return ['result' => 'success', 'message' => 'exporting SmsModel', 'data' => ['jobId' => $jobId, 'key' => $key]]; } else { LogUtil::error(['message' => 'smsModel記錄表失败', 'reason' => '没有数据(no data)', 'condition' => $condition], 'sms'); return ['result' => 'error', 'message' => 'no datas', 'data' => []]; } }
public function perform() { $args = $this->args; if (empty($args['key']) || empty($args['header']) || empty($args['condition'])) { ResqueUtil::log(['status' => 'fail to export sms model record', 'message' => 'missing params', 'args' => $args]); return false; } $condition = unserialize($args['condition']); $header = $args['header']; $fileName = $args['key']; $filePath = ExcelUtil::getFile($fileName, 'csv'); $rows = SmsModel::find()->where($condition)->all(); // LogUtil::error(date('Y-m-d h:i:s') . ' $content....: ' . count($rows)); 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]); return true; } else { ResqueUtil::log(['status' => 'fail to export sms model', 'message' => 'fail to setQiniuKey', 'filePath' => $filePath]); return false; } }
public function actionReadNumberExcel() { sleep(2); Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $request = Yii::$app->request; $fileB64 = $request->post('fileB64'); $content = $request->post('content'); $modelGroupId = $request->post('modelGroupId'); //delete all sms model by groupId //根据groupid删除各自短信模板 但是导入之后前台刷新会丢失groupid 会造成数据库数据累积 //表中含有accountid字段可以根据该字段删除所有模板 if ($modelGroupId != -1) { SmsModel::deleteAll(['groupId' => $modelGroupId]); } $file = base64_decode(substr($fileB64, strpos($fileB64, ";base64,") + 8)); $filePath = Yii::getAlias('@runtime') . '/numberExcel' . date('his') . '.csv'; file_put_contents($filePath, $file); //csv 由xlsx转换 第一列为号码 第二列替换param1 第三列param2 $phpReader = new \PHPExcel_Reader_Excel2007(); if (!$phpReader->canRead($filePath)) { $phpReader = new \PHPExcel_Reader_Excel5(); if (!$phpReader->canRead($filePath)) { $phpReader = new \PHPExcel_Reader_CSV(); if (!$phpReader->canRead($filePath)) { unlink($filePath); return ['fileError' => true]; } } } $phpExcel = $phpReader->load($filePath); $sheets = $phpExcel->getAllSheets(); $recordCount = -1; for ($si = 0; $si < sizeof($sheets); $si++) { $sheet = $sheets[$si]; $ingredientFinished = false; $rowCount = $sheet->getHighestRow(); $recordCount = $rowCount; $highestCol = $sheet->getHighestColumn(); // LogUtil::error(date('Y-m-d h:i:s') . ' $rowCount: ' . $rowCount . ' $highestCol: ' . $highestCol); $colCount = ord($highestCol) - 65; $currentGroupId = $this->guid(); // LogUtil::error(date('Y-m-d h:i:s') . ' $getAccountId: ' . $this->getAccountId()); for ($row = 1; $row <= $rowCount; $row++) { $realModel = $content; $number = ''; for ($col = 0; $col <= $colCount; $col++) { $val = $sheet->getCellByColumnAndRow($col, $row)->getValue(); $val = trim((string) $val); if ($val === '') { continue; } if ($col == 0) { $number = $val; } if ($col > 0) { $realModel = str_replace('%param' . $col . '%', $val, $realModel); } } //如果存在空白行则会把模板存入 $SmsModel = new SmsModel(); $SmsModel->groupId = $currentGroupId; $SmsModel->mobile = $number; $SmsModel->content = $realModel; $SmsModel->accountId = $this->getAccountId(); $SmsModel->save(); } } LogUtil::error(date('Y-m-d h:i:s') . ' $modelGroupId: ' . $currentGroupId); return ['recordCount' => $recordCount, 'modelGroupId' => $currentGroupId]; }