123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace api\modules\v1\controllers;
- use api\common\controllers\Controller;
- use api\modules\v1\models\Category;
- use api\modules\v1\models\Team;
- use api\modules\v1\models\TeamMember;
- use common\enums\CodeEnum;
- use common\helpers\Regexp;
- use common\helpers\Util;
- use common\models\Article;
- use common\models\Page;
- use common\modules\attachment\components\UploadedFile;
- use common\modules\attachment\models\Attachment;
- use common\modules\config\models\Config;
- use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use Yii;
- use yii\captcha\Captcha;
- use yii\helpers\ArrayHelper;
- use yii\helpers\StringHelper;
- use yii\web\Response;
- class CommonController extends Controller
- {
-
- public function actionPage()
- {
- $slug = request('slug');
- if (!$slug) {
- return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '页面未找到'];
- }
- $page = Page::findOne(['slug' => $slug]);
- if (empty($page)) {
- return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '页面未找到'];
- }
- return ['data' => $page];
- }
-
- public function actionConfig()
- {
- $config = ArrayHelper::map(Config::find()->where(['group' => ['site', 'mobile', 'pc']])->select(['name', 'value'])->all(), 'name', 'value');
-
- $config['hot_search'] = Yii::$app->config->get('hot_search');
- return ['data' => $config];
- }
-
- public function actionUpload()
- {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $files = UploadedFile::getInstancesByName('file');
- $attachment = Attachment::uploadFromPost(date('Ymd'), $files[0]);
- $transaction->commit();
- return ['data' => $attachment];
- } catch (\Exception $e) {
- $transaction->rollback();
- return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => $e->getMessage()];
- }
- }
-
- public function actionVersion()
- {
- return ['data' => Yii::$app->config->get('version')];
- }
-
- public function actionTest()
- {
- $list = \common\models\Category::find()->where([])->select('title')->column();
- Yii::$app->response->format = Response::FORMAT_JSON;
- return ['data' => $list];
-
-
- $cateList = Category::find()->where(['like', 'title', '表%', false])->orderBy(['id' => SORT_ASC])->all();
-
- $fileName = \Yii::getAlias('@storagePath/upload/data.xlsx');
- $inputFileType = IOFactory::identify($fileName);
- $reader = IOFactory::createReader($inputFileType);
- $spreadsheet = $reader->load($fileName);
- $reader->setReadDataOnly(true);
- $worksheetList = $spreadsheet->getAllSheets();
- $excelData = [];
- foreach ($worksheetList as $index => $worksheet) {
- $highestRow = $worksheet->getHighestRow();
- $highestColumn = $worksheet->getHighestColumn();
- $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);
- $category = $cateList[$index];
- for ($row = 1; $row <= $highestRow; $row++) {
-
- $data = [];
- for ($col = 1; $col <= $highestColumnIndex; $col++) {
- $val = (string)$worksheet->getCellByColumnAndRow($col, $row)->getValue();
-
- if ($row == 1 && $col == 1) {
- $attr = [];
- }
- if ($row == 1) {
- $attr[$col] = $this->getAttrByTitle($val);
- } else {
- if (!$data[$attr[$col]]) {
- $data[$attr[$col]] = $val;
- } else {
- $data[$attr[$col]] = $data[$attr[$col]] . $val;
- }
- }
- }
-
- if ($row != 1) {
- if ($data['sort'] != "") {
- $excelData[] = [
- 'sort' => $data['sort'],
- 'title' => $data['title'],
- 'company' => $data['company'] ? $data['company'] : '',
- 'position' => $data['position'] ? $data['position'] : '',
- 'chain' => $data['chain'] ? $data['chain'] : '',
- 'principal' => $data['principal'] ? $data['principal'] : '',
- 'intro' => $data['intro'] ? $data['intro'] : '',
- 'description' => $this->generateDesc($data['intro']),
- 'city' => $data['city'] ? $data['city'] : '',
- 'address' => $data['address'] ? $data['address'] : '',
- 'tel' => $data['tel'] ? $data['tel'] : '',
- 'category_id' => $category->id,
- 'category' => $category->title,
- 'module' => $category->module,
- ];
- } else {
-
- foreach ($data as $key => $value) {
- $excelData[count($excelData) - 1][$key] .= $value;
- }
- }
- }
- }
- }
- Yii::$app->db->createCommand()->batchInsert(Article::tableName(), ['sort', 'title', 'company', 'position', 'chain', 'principal', 'intro', 'description', 'city', 'address', 'tel', 'category_id', 'category', 'module'], $excelData)->execute();
- return ['data' => ['count' => count($excelData), 'list' => $excelData]];
- }
- private function getAttrByTitle($title)
- {
- switch ($title) {
-
- case '序号':
- return 'sort';
- case '姓名':
- case '名称':
- case '基地名称':
- case '产业集群':
- case '企业名称':
- case '商会名称':
- return 'title';
- case '城市':
- case '区域':
- case '国内城市分布':
- case '常住地':
- return 'city';
- case '地址':
- return 'address';
- case '联系方式':
- case '商会电话':
- return 'tel';
-
- case '公司':
- case '企业':
- case '单位':
- case '单位名称':
- return 'company';
- case '职务':
- return 'position';
- case '专长方向':
- case '研究领域':
- case '研究方向':
- case '简介':
- case '基地特色':
- case '科室/研究方向':
- case '专长方向/荣誉':
- case '投资产业领域':
- case '主要产品':
- case '个人简介':
- return 'intro';
- case '产业领域':
- case '产业类别':
- case '产业链':
- case '细分方向':
- case '细分领域':
- case '重点投资行业/研究领域':
- return 'chain';
- case '负责人':
- case '秘书长':
- case '法人':
- return 'principal';
- }
- }
-
- private function generateDesc($intro)
- {
- if (empty($intro)) {
- return '';
- }
- return StringHelper::truncate(preg_replace('/\s+/', ' ', strip_tags($intro)), 150);
- }
- }
|