CommonController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2017/4/14
  6. * Time: 下午10:49
  7. */
  8. namespace api\modules\v1\controllers;
  9. use api\common\controllers\Controller;
  10. use api\modules\v1\models\Category;
  11. use api\modules\v1\models\Team;
  12. use api\modules\v1\models\TeamMember;
  13. use common\enums\CodeEnum;
  14. use common\helpers\Regexp;
  15. use common\helpers\Util;
  16. use common\models\Article;
  17. use common\models\Page;
  18. use common\modules\attachment\components\UploadedFile;
  19. use common\modules\attachment\models\Attachment;
  20. use common\modules\config\models\Config;
  21. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  22. use PhpOffice\PhpSpreadsheet\IOFactory;
  23. use Yii;
  24. use yii\captcha\Captcha;
  25. use yii\helpers\ArrayHelper;
  26. use yii\helpers\StringHelper;
  27. use yii\web\Response;
  28. class CommonController extends Controller
  29. {
  30. /**
  31. * 单页内容
  32. * @return array
  33. */
  34. public function actionPage()
  35. {
  36. $slug = request('slug');
  37. if (!$slug) {
  38. return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '页面未找到'];
  39. }
  40. $page = Page::findOne(['slug' => $slug]);
  41. if (empty($page)) {
  42. return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '页面未找到'];
  43. }
  44. return ['data' => $page];
  45. }
  46. /**
  47. * @return array
  48. * @api {get} /v1/common/configs 平台配置
  49. * @apiGroup 通用接口
  50. * @apiVersion 1.0.0
  51. *
  52. * @apiSuccess {Number} errcode 状态码,0:请求成功
  53. * @apiSuccess {String} errmsg 提示信息
  54. * @apiSuccess {Object} data 配置信息
  55. *
  56. * @apiError {Number} errcode 状态码,500:请求失败
  57. * @apiError {String} errmsg 提示信息
  58. *
  59. * @apiSuccessExample {json} 成功响应:
  60. * {"errcode":0,"errmsg":"ok","data":{}}
  61. *
  62. * @apiErrorExample {json} 失败响应:
  63. * {"errcode":500,"errmsg":"请求失败"}
  64. *
  65. * @author nodelog
  66. */
  67. public function actionConfig()
  68. {
  69. $config = ArrayHelper::map(Config::find()->where(['group' => ['site', 'mobile', 'pc']])->select(['name', 'value'])->all(), 'name', 'value');
  70. //自动判断数组
  71. $config['hot_search'] = Yii::$app->config->get('hot_search');
  72. return ['data' => $config];
  73. }
  74. /**
  75. * @return array
  76. * @throws \yii\db\Exception
  77. * @api {post} /v1/common/upload 上传文件
  78. * @apiGroup 通用接口
  79. * @apiVersion 1.0.0
  80. *
  81. * @apiHeader {String} Content-Type 内容类型.
  82. * @apiHeader {String} accept 传输方式.
  83. * @apiHeaderExample {json} Header-Example:
  84. * {
  85. * "Content-Type": "multipart/form-data",
  86. * 'accept': 'application/json',
  87. * }
  88. *
  89. * @apiParam {String} file 上传文件表单名
  90. *
  91. * @apiSuccess {Number} errcode 状态码,0:请求成功
  92. * @apiSuccess {String} errmsg 提示信息
  93. * @apiSuccess {Object} data 服务器上文件信息
  94. *
  95. * @apiError {Number} errcode 状态码,500:请求失败
  96. * @apiError {String} errmsg 提示信息
  97. *
  98. * @apiSuccessExample {json} 成功响应:
  99. * {"errcode":0,"errmsg":"ok","data":{}}
  100. *
  101. * @apiErrorExample {json} 失败响应:
  102. * {"errcode":500,"errmsg":"请求失败"}
  103. *
  104. * @author nodelog
  105. */
  106. public function actionUpload()
  107. {
  108. $transaction = Yii::$app->db->beginTransaction();
  109. try {
  110. $files = UploadedFile::getInstancesByName('file');
  111. $attachment = Attachment::uploadFromPost(date('Ymd'), $files[0]);
  112. $transaction->commit();
  113. return ['data' => $attachment];
  114. } catch (\Exception $e) {
  115. $transaction->rollback();
  116. return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => $e->getMessage()];
  117. }
  118. }
  119. /**
  120. * 平台版本号
  121. * @return array|mixed
  122. * @author nodelog
  123. */
  124. public function actionVersion()
  125. {
  126. return ['data' => Yii::$app->config->get('version')];
  127. }
  128. /**
  129. * @return array
  130. * @throws \yii\web\ServerErrorHttpException
  131. * @author nodelog
  132. */
  133. public function actionTest()
  134. {
  135. $list = \common\models\Category::find()->where([])->select('title')->column();
  136. Yii::$app->response->format = Response::FORMAT_JSON;
  137. return ['data' => $list];
  138. //分类
  139. /* @var $cateList \common\models\Category[] */
  140. $cateList = Category::find()->where(['like', 'title', '表%', false])->orderBy(['id' => SORT_ASC])->all();
  141. /* 读取excel */
  142. $fileName = \Yii::getAlias('@storagePath/upload/data.xlsx');//path
  143. $inputFileType = IOFactory::identify($fileName);
  144. $reader = IOFactory::createReader($inputFileType);
  145. $spreadsheet = $reader->load($fileName);
  146. $reader->setReadDataOnly(true); // 设置后无法获取excel中的图片
  147. $worksheetList = $spreadsheet->getAllSheets();
  148. $excelData = [];
  149. foreach ($worksheetList as $index => $worksheet) {
  150. $highestRow = $worksheet->getHighestRow(); // 取得总行数
  151. $highestColumn = $worksheet->getHighestColumn(); // 取得总列数
  152. $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn); // 取得总列数
  153. $category = $cateList[$index];
  154. for ($row = 1; $row <= $highestRow; $row++) {
  155. //一行一条数据
  156. $data = [];
  157. for ($col = 1; $col <= $highestColumnIndex; $col++) {
  158. $val = (string)$worksheet->getCellByColumnAndRow($col, $row)->getValue();
  159. //第一行初始化
  160. if ($row == 1 && $col == 1) {
  161. $attr = [];
  162. }
  163. if ($row == 1) {
  164. $attr[$col] = $this->getAttrByTitle($val);
  165. } else {
  166. if (!$data[$attr[$col]]) {
  167. $data[$attr[$col]] = $val;
  168. } else {
  169. $data[$attr[$col]] = $data[$attr[$col]] . $val;
  170. }
  171. }
  172. }
  173. //一行完整数据取完
  174. if ($row != 1) {
  175. if ($data['sort'] != "") {
  176. $excelData[] = [
  177. 'sort' => $data['sort'],
  178. 'title' => $data['title'],
  179. 'company' => $data['company'] ? $data['company'] : '',
  180. 'position' => $data['position'] ? $data['position'] : '',
  181. 'chain' => $data['chain'] ? $data['chain'] : '',
  182. 'principal' => $data['principal'] ? $data['principal'] : '',
  183. 'intro' => $data['intro'] ? $data['intro'] : '',
  184. 'description' => $this->generateDesc($data['intro']),
  185. 'city' => $data['city'] ? $data['city'] : '',
  186. 'address' => $data['address'] ? $data['address'] : '',
  187. 'tel' => $data['tel'] ? $data['tel'] : '',
  188. 'category_id' => $category->id,
  189. 'category' => $category->title,
  190. 'module' => $category->module,
  191. ];
  192. } else {
  193. //合并数据
  194. foreach ($data as $key => $value) {
  195. $excelData[count($excelData) - 1][$key] .= $value;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. Yii::$app->db->createCommand()->batchInsert(Article::tableName(), ['sort', 'title', 'company', 'position', 'chain', 'principal', 'intro', 'description', 'city', 'address', 'tel', 'category_id', 'category', 'module'], $excelData)->execute();
  202. return ['data' => ['count' => count($excelData), 'list' => $excelData]];
  203. }
  204. private function getAttrByTitle($title)
  205. {
  206. switch ($title) {
  207. //基本信息
  208. case '序号':
  209. return 'sort';
  210. case '姓名':
  211. case '名称':
  212. case '基地名称':
  213. case '产业集群':
  214. case '企业名称':
  215. case '商会名称':
  216. return 'title';
  217. case '城市':
  218. case '区域':
  219. case '国内城市分布':
  220. case '常住地':
  221. return 'city';
  222. case '地址':
  223. return 'address';
  224. case '联系方式':
  225. case '商会电话':
  226. return 'tel';
  227. //扩展信息
  228. case '公司':
  229. case '企业':
  230. case '单位':
  231. case '单位名称':
  232. return 'company';
  233. case '职务':
  234. return 'position';
  235. case '专长方向':
  236. case '研究领域':
  237. case '研究方向':
  238. case '简介':
  239. case '基地特色':
  240. case '科室/研究方向':
  241. case '专长方向/荣誉':
  242. case '投资产业领域':
  243. case '主要产品':
  244. case '个人简介':
  245. return 'intro';
  246. case '产业领域':
  247. case '产业类别':
  248. case '产业链':
  249. case '细分方向':
  250. case '细分领域':
  251. case '重点投资行业/研究领域':
  252. return 'chain';
  253. case '负责人':
  254. case '秘书长':
  255. case '法人':
  256. return 'principal';
  257. }
  258. }
  259. // 摘要生成方式
  260. private function generateDesc($intro)
  261. {
  262. if (empty($intro)) {
  263. return '';
  264. }
  265. return StringHelper::truncate(preg_replace('/\s+/', ' ', strip_tags($intro)), 150);
  266. }
  267. }