123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- /**
- * Created by PhpStorm.
- * User: NODELOG
- * Date: 2017/4/14
- * Time: 下午10:49
- */
- 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
- {
- /**
- * 单页内容
- * @return array
- */
- 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];
- }
- /**
- * @return array
- * @api {get} /v1/common/configs 平台配置
- * @apiGroup 通用接口
- * @apiVersion 1.0.0
- *
- * @apiSuccess {Number} errcode 状态码,0:请求成功
- * @apiSuccess {String} errmsg 提示信息
- * @apiSuccess {Object} data 配置信息
- *
- * @apiError {Number} errcode 状态码,500:请求失败
- * @apiError {String} errmsg 提示信息
- *
- * @apiSuccessExample {json} 成功响应:
- * {"errcode":0,"errmsg":"ok","data":{}}
- *
- * @apiErrorExample {json} 失败响应:
- * {"errcode":500,"errmsg":"请求失败"}
- *
- * @author nodelog
- */
- 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];
- }
- /**
- * @return array
- * @throws \yii\db\Exception
- * @api {post} /v1/common/upload 上传文件
- * @apiGroup 通用接口
- * @apiVersion 1.0.0
- *
- * @apiHeader {String} Content-Type 内容类型.
- * @apiHeader {String} accept 传输方式.
- * @apiHeaderExample {json} Header-Example:
- * {
- * "Content-Type": "multipart/form-data",
- * 'accept': 'application/json',
- * }
- *
- * @apiParam {String} file 上传文件表单名
- *
- * @apiSuccess {Number} errcode 状态码,0:请求成功
- * @apiSuccess {String} errmsg 提示信息
- * @apiSuccess {Object} data 服务器上文件信息
- *
- * @apiError {Number} errcode 状态码,500:请求失败
- * @apiError {String} errmsg 提示信息
- *
- * @apiSuccessExample {json} 成功响应:
- * {"errcode":0,"errmsg":"ok","data":{}}
- *
- * @apiErrorExample {json} 失败响应:
- * {"errcode":500,"errmsg":"请求失败"}
- *
- * @author nodelog
- */
- 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()];
- }
- }
- /**
- * 平台版本号
- * @return array|mixed
- * @author nodelog
- */
- public function actionVersion()
- {
- return ['data' => Yii::$app->config->get('version')];
- }
- /**
- * @return array
- * @throws \yii\web\ServerErrorHttpException
- * @author nodelog
- */
- public function actionTest()
- {
- $list = \common\models\Category::find()->where([])->select('title')->column();
- Yii::$app->response->format = Response::FORMAT_JSON;
- return ['data' => $list];
- //分类
- /* @var $cateList \common\models\Category[] */
- $cateList = Category::find()->where(['like', 'title', '表%', false])->orderBy(['id' => SORT_ASC])->all();
- /* 读取excel */
- $fileName = \Yii::getAlias('@storagePath/upload/data.xlsx');//path
- $inputFileType = IOFactory::identify($fileName);
- $reader = IOFactory::createReader($inputFileType);
- $spreadsheet = $reader->load($fileName);
- $reader->setReadDataOnly(true); // 设置后无法获取excel中的图片
- $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);
- }
- }
|