ArticleController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16-1-28
  6. * Time: 下午6:40
  7. */
  8. namespace api\modules\v1\controllers;
  9. use api\common\controllers\Controller;
  10. use api\modules\v1\models\Article;
  11. use common\enums\CodeEnum;
  12. use common\models\ArticleModule;
  13. use common\models\Footprint;
  14. use common\models\SearchRecord;
  15. use Yii;
  16. use yii\base\Exception;
  17. use yii\data\ActiveDataProvider;
  18. use api\common\behaviors\QueryParamAuth;
  19. use yii\helpers\ArrayHelper;
  20. use yii\web\NotFoundHttpException;
  21. class ArticleController extends Controller
  22. {
  23. public function behaviors()
  24. {
  25. return ArrayHelper::merge(parent::behaviors(), [
  26. [
  27. 'class' => QueryParamAuth::className(),
  28. 'tokenParam' => 'token',
  29. 'optional' => []
  30. ]
  31. ]);
  32. }
  33. /**
  34. * @param $cid
  35. * @param $chain
  36. * @param $keyword
  37. * @return ActiveDataProvider
  38. * @author jiang
  39. */
  40. public function actionIndex($cid = null, $chain = null, $keyword = null)
  41. {
  42. $query = Article::find()->published()
  43. ->andFilterWhere(['category_id' => $cid]);
  44. //产业链
  45. if (!empty($chain) && $chain != '全部') {
  46. $query->andWhere([
  47. 'or',
  48. ['like', 'chain', $chain],
  49. ['like', 'title', $chain],
  50. ['like', 'company', $chain],
  51. ['like', 'description', $chain],
  52. ['like', 'intro', $chain],
  53. ['like', 'address', $chain],
  54. ['like', 'city', $chain],
  55. ['like', 'tel', $chain],
  56. ['like', 'principal', $chain],
  57. ['like', 'position', $chain],
  58. ]);
  59. }
  60. //搜索关键字
  61. if (!empty($keyword)) {
  62. $query->andWhere([
  63. 'or',
  64. ['like', 'chain', $keyword],
  65. ['like', 'title', $keyword],
  66. ['like', 'company', $keyword],
  67. ['like', 'description', $keyword],
  68. ['like', 'intro', $keyword],
  69. ['like', 'address', $keyword],
  70. ['like', 'city', $keyword],
  71. ['like', 'tel', $keyword],
  72. ['like', 'principal', $keyword],
  73. ['like', 'position', $keyword],
  74. ]);
  75. }
  76. $dataProvider = new ActiveDataProvider([
  77. 'query' => $query,
  78. 'pagination' => [
  79. // 'validatePage' => true,
  80. 'pageParam' => 'page_no',
  81. 'pageSizeParam' => 'page_size',
  82. ],
  83. 'sort' => [
  84. 'defaultOrder' => [
  85. 'id' => SORT_ASC
  86. ]
  87. ]
  88. ]);
  89. //记录搜索关键词
  90. self::recordKeyword($keyword);
  91. return $dataProvider;
  92. }
  93. /**
  94. * 详情
  95. * @param $id
  96. * @return array
  97. * @throws NotFoundHttpException
  98. * @author jiang
  99. */
  100. public function actionView($id)
  101. {
  102. // request()->setQueryParams(['expand' => 'data']);
  103. $model = Article::find()->published()->where(['id' => $id])->one();
  104. if ($model === null) {
  105. throw new NotFoundHttpException('not found');
  106. }
  107. $model->addView();
  108. //添加足迹记录
  109. Footprint::add(\common\models\Article::className(), $id);
  110. return ['data' => $model];
  111. }
  112. /**
  113. * 看板统计
  114. * @return array
  115. * @throws NotFoundHttpException
  116. * @author jiang
  117. */
  118. public function actionCount()
  119. {
  120. $list = Article::find()->select(['module', 'count(module) as count'])->published()->groupBy('module')->orderBy(['count' => SORT_DESC])->asArray()->all();
  121. $citylist = Article::find()->select(['city', 'count(city) as count'])->where(['!=', 'city', ''])->published()->groupBy('city')->having(['>', 'count', 5])->orderBy(['count' => SORT_DESC])->asArray()->all();
  122. $data = [];
  123. foreach ($list as $item) {
  124. $data['normal'][] = [
  125. 'name' => array_get(\common\models\ArticleModule::getTypeEnum(), $item['module']),
  126. 'centerText' => $item['count'],
  127. 'value' => (int)$item['count']
  128. ];
  129. }
  130. foreach ($citylist as $item) {
  131. $size = 10;
  132. if ($item['count'] >= 100) {
  133. $size = 50;
  134. }
  135. if ($item['count'] >= 50 && $item['count'] < 100) {
  136. $size = 40;
  137. }
  138. if ($item['count'] >= 40 && $item['count'] < 50) {
  139. $size = 30;
  140. }
  141. if ($item['count'] >= 30 && $item['count'] < 40) {
  142. $size = 25;
  143. }
  144. if ($item['count'] >= 20 && $item['count'] < 30) {
  145. $size = 20;
  146. }
  147. if ($item['count'] >= 10 && $item['count'] < 20) {
  148. $size = 15;
  149. }
  150. // if ($item['count'] < 10) {
  151. // $size = 10;
  152. // }
  153. $data['word'][] = [
  154. 'name' => $item['city'],
  155. 'textSize' => $size
  156. ];
  157. }
  158. return ['data' => $data];
  159. }
  160. /**
  161. * 浏览足迹
  162. * @return ActiveDataProvider
  163. * @author jiang
  164. */
  165. public function actionFootprint()
  166. {
  167. $ids = Footprint::find()->where(['entity' => \common\models\Article::className()])->my()->active()->select('entity_id')->orderBy(['id' => SORT_DESC])->column();
  168. $query = Article::find()->where(['in', 'id', $ids])->published();
  169. $dataProvider = new ActiveDataProvider([
  170. 'query' => $query,
  171. 'pagination' => [
  172. 'pageParam' => 'page_no',
  173. 'pageSizeParam' => 'page_size',
  174. ],
  175. // 'sort' => [
  176. // 'defaultOrder' => [
  177. // 'created_at' => SORT_DESC,
  178. // ]
  179. // ]
  180. ]);
  181. return $dataProvider;
  182. }
  183. /**
  184. * 数据模型列表
  185. * @return array
  186. * @author jiang
  187. */
  188. public function actionModule()
  189. {
  190. $data = ArticleModule::find()->select(['name', 'title'])->all();
  191. return ['data' => $data];
  192. }
  193. /**
  194. * 企业反馈
  195. * @return array|string[]
  196. * @throws \yii\db\Exception
  197. * @author jiang
  198. */
  199. public function actionCreate()
  200. {
  201. $transaction = Yii::$app->db->beginTransaction();
  202. try {
  203. $model = new \common\models\Article();
  204. $model->load(request()->post(), '');
  205. $model->status = \common\models\Article::STATUS_PENDING;//待审核
  206. $model->save();
  207. if ($model->hasErrors()) {
  208. throw new Exception(current($model->getErrors())[0]);
  209. }
  210. $transaction->commit();
  211. } catch (\Exception $e) {
  212. $transaction->rollBack();
  213. return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '提交失败:' . $e->getMessage()];
  214. }
  215. return ['errmsg' => '提交成功'];
  216. }
  217. /**
  218. * 历史搜索
  219. * @return array
  220. * @author jiang
  221. */
  222. public function actionHistorySearch()
  223. {
  224. $where = [
  225. 'del' => 0,
  226. 'user_id' => \Yii::$app->user->id
  227. ];
  228. $order = [
  229. 'count' => SORT_DESC,
  230. 'updated_at' => SORT_DESC,
  231. 'id' => SORT_DESC
  232. ];
  233. $data = SearchRecord::find()->where($where)->orderBy($order)->limit(50)->select('keyword')->column();
  234. return ['data' => $data];
  235. }
  236. /**
  237. * @author jiang
  238. */
  239. public function actionClearSearch()
  240. {
  241. $where = [
  242. 'del' => 0,
  243. 'user_id' => \Yii::$app->user->id
  244. ];
  245. $data = [
  246. 'del' => 1,
  247. ];
  248. SearchRecord::updateAll($data, $where);
  249. return ['errmsg' => '清空成功'];
  250. }
  251. /**
  252. * 记录关键词
  253. */
  254. public static function recordKeyword($keyword)
  255. {
  256. if (empty($keyword)) {
  257. return false;
  258. }
  259. $where = [
  260. 'del' => 0,
  261. 'user_id' => \Yii::$app->user->id,
  262. 'keyword' => $keyword
  263. ];
  264. $record = SearchRecord::find()->where($where)->one();
  265. if ($record) {
  266. // 有该关键词记录, 更新
  267. $data = [
  268. 'count' => 1,
  269. ];
  270. SearchRecord::updateAllCounters($data, $where);
  271. return true;
  272. }
  273. $record = new SearchRecord();
  274. $record->load($where, '');
  275. $record->count = 1;
  276. $record->save();
  277. return true;
  278. }
  279. }