ArticleController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace backendApi\modules\v1\controllers;
  9. use backendApi\modules\v1\models\Admin;
  10. use backendApi\modules\v1\models\AdminCountry;
  11. use common\helpers\Cache;
  12. use common\helpers\Form;
  13. use common\models\Article;
  14. use common\models\ArticleCategory;
  15. use common\models\Countries;
  16. use common\models\forms\ArticleCategoryForm;
  17. use common\models\forms\ArticleForm;
  18. use common\models\forms\UploadForm;
  19. use Yii;
  20. use yii\web\UploadedFile;
  21. class ArticleController extends BaseController
  22. {
  23. public $modelClass = Article::class;
  24. public function behaviors() {
  25. $behaviors = parent::behaviors();
  26. //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  27. return $behaviors;
  28. }
  29. /**
  30. * 文章分类
  31. * @return mixed
  32. * @throws \yii\web\HttpException
  33. */
  34. public function actionCategory(){
  35. $data = ArticleCategory::lists('', [], [
  36. 'orderBy' => 'SORT ASC, CREATED_AT DESC',
  37. ]);
  38. return static::notice($data);
  39. }
  40. /**
  41. * 添加分类
  42. * @return mixed
  43. * @throws \yii\web\HttpException
  44. */
  45. public function actionCategoryAdd()
  46. {
  47. if (Yii::$app->request->isPost) {
  48. return parent::edit(ArticleCategoryForm::class, Yii::t('ctx', 'successfully'));
  49. }
  50. }
  51. /**
  52. * 删除文章分类
  53. * @return mixed
  54. * @throws \yii\db\Exception
  55. * @throws \yii\web\HttpException
  56. */
  57. public function actionCategoryDelete(){
  58. $result = static::delete(ArticleCategory::class);
  59. return $result;
  60. }
  61. /**
  62. * 分类排序
  63. * @return mixed
  64. * @throws \yii\web\HttpException
  65. */
  66. public function actionCategorySort(){
  67. if(Yii::$app->request->get('id')){
  68. $formModel = new ArticleCategoryForm();
  69. $formModel->scenario = 'sort';
  70. if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){
  71. return static::notice(Yii::t('ctx', 'successfully'));
  72. } else {
  73. return static::notice(Yii::t('ctx', 'failed'), 400);
  74. }
  75. }
  76. }
  77. /**
  78. * 文章
  79. * @return mixed
  80. * @throws \yii\web\HttpException
  81. */
  82. public function actionIndex()
  83. {
  84. // 如果admin不是超管,只允许查询自己关联的国家
  85. $admin = Admin::findOne(Yii::$app->user->id);
  86. $roleId = $admin->ROLE_ID;
  87. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  88. $countries = Countries::find()->asArray()->all();
  89. } else {
  90. // 关联国家
  91. $countries = Countries::find()
  92. ->select('COU.ID, COU.CODE, COU.NAME')
  93. ->from(['COU' => Countries::tableName()])
  94. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  95. ->where(['ADL.ADMIN_ID' => $admin->ID])
  96. ->asArray()
  97. ->all();
  98. }
  99. $filter = $this->filterCondition([
  100. 'TITLE' => 'TITLE',
  101. 'COUNTRY_ID' => 'COUNTRY_ID',
  102. ]);
  103. $condition = $filter['condition'];
  104. $params = $filter['params'];
  105. $countryIds = array_column($countries, 'ID');
  106. if (!empty($countryIds)) {
  107. $placeholders = [];
  108. foreach ($countryIds as $key => $id) {
  109. $paramName = ':countryId_' . $key;
  110. $placeholders[] = $paramName;
  111. $params[$paramName] = $id;
  112. }
  113. $condition .= ' AND AD.COUNTRY_ID IN (' . implode(',', $placeholders) . ')';
  114. }
  115. $data = Article::lists($condition, $params, [
  116. 'select' => 'ART.ID,ART.TITLE,ART.CID,ART.COUNTRY_ID,ART.STATUS,ART.SORT,ART.CREATED_AT,,ADC.NAME AS COUNTRY_NAME, ADC.CODE AS COUNTRY_CODE',
  117. 'from' => Article::tableName().' AS ART',
  118. 'join' => [
  119. ['INNER JOIN', Countries::tableName() . ' AS ADC', 'ADC.ID=ART..COUNTRY_ID'],
  120. ],
  121. 'orderBy' => 'ART.SORT ASC,ART.CREATED_AT DESC',
  122. ]);
  123. // 全部分类
  124. $data['allCategory'] = ArticleCategory::getAllCategory();
  125. return static::notice($data);
  126. }
  127. /**
  128. * 添加文章
  129. * @return mixed
  130. * @throws \yii\web\HttpException
  131. */
  132. public function actionAdd()
  133. {
  134. if (Yii::$app->request->isPost) {
  135. return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
  136. }
  137. // 获取全部分类
  138. $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all();
  139. return static::notice(['allCategory'=>$allCategory]);
  140. }
  141. /**
  142. * 编辑文章
  143. * @return mixed
  144. * @throws \yii\base\Exception
  145. * @throws \yii\web\HttpException
  146. */
  147. public function actionEdit(){
  148. $id = Yii::$app->request->get('id');
  149. if(Yii::$app->request->isPost){
  150. return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
  151. }
  152. $oneData = Article::findOneAsArray(['ID'=>$id]);
  153. $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
  154. // 国家
  155. $oneData['COUNTRY'] = Countries::getCountry($oneData['COUNTRY_ID']);
  156. // 暂时先从文件中取内容
  157. $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
  158. if(!file_exists($path)){
  159. $oneData['CONTENT'] = '';
  160. } else {
  161. $oneData['CONTENT'] = file_get_contents($path);
  162. }
  163. // 获取全部分类
  164. $allCategory = ArticleCategory::getAllCategory();
  165. return static::notice(['oneData'=>$oneData, 'allCategory'=>$allCategory]);
  166. }
  167. /**
  168. * 删除文章
  169. * @return mixed
  170. * @throws \yii\db\Exception
  171. * @throws \yii\web\HttpException
  172. */
  173. public function actionArticleDelete(){
  174. $result = static::delete(Article::class);
  175. return $result;
  176. }
  177. /**
  178. * 隐藏
  179. * @return mixed
  180. * @throws \yii\db\Exception
  181. * @throws \yii\web\HttpException
  182. */
  183. public function actionArticleHide(){
  184. $adForm = new ArticleForm();
  185. $result = static::hide(Article::class, 'hide', function ($selected) use ($adForm) {
  186. }, function ($selected) use ($adForm) {
  187. }, true);
  188. return $result;
  189. }
  190. /**
  191. * 取消隐藏
  192. * @return mixed
  193. * @throws \yii\db\Exception
  194. * @throws \yii\web\HttpException
  195. */
  196. public function actionArticleUnHide(){
  197. $adForm = new ArticleForm();
  198. $result = static::hide(Article::class, 'un-hide', function ($selected) use ($adForm) {
  199. }, function ($selected) use ($adForm) {
  200. }, true);
  201. return $result;
  202. }
  203. /**
  204. * 获取文章详细
  205. * @return mixed
  206. * @throws \yii\web\HttpException
  207. */
  208. public function actionDetail()
  209. {
  210. $id = \Yii::$app->request->get('id');
  211. $data = null;
  212. if($id){
  213. $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,COUNTRY_ID,TITLE,CID,SORT,CREATED_AT');
  214. // 国家
  215. $data['COUNTRY'] = Countries::getCountry($data['COUNTRY_ID']);
  216. }
  217. if($data){
  218. // 暂时先从文件中取内容
  219. $path = \Yii::getAlias('@common/runtime/articleContent/') . $data['ID'];
  220. if(!file_exists($path)){
  221. $data['CONTENT'] = '';
  222. } else {
  223. $data['CONTENT'] = file_get_contents($path);
  224. }
  225. return static::notice($data);
  226. } else {
  227. return static::notice('Article not exists!', 400); // 文章不存在
  228. }
  229. }
  230. /**
  231. * 上传图片
  232. * @return mixed
  233. * @throws \yii\base\Exception
  234. * @throws \yii\db\Exception
  235. * @throws \yii\web\HttpException
  236. */
  237. public function actionUpload() {
  238. if (\Yii::$app->request->isPost) {
  239. $formModel = new UploadForm();
  240. $formModel->scenario = 'article';
  241. $formModel->file = UploadedFile::getInstanceByName('file');
  242. $formModel->token = \Yii::$app->request->request('uploadToken');;
  243. if($formModel->file && $uploader = $formModel->upload()){
  244. return static::notice($uploader->FILE_NAME);
  245. } else {
  246. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  247. }
  248. } else {
  249. $token = Cache::setUploadToken();
  250. return static::notice($token);
  251. }
  252. }
  253. /**
  254. * 排序
  255. * @return mixed
  256. * @throws \yii\web\HttpException
  257. */
  258. public function actionSort()
  259. {
  260. if (Yii::$app->request->get('id')) {
  261. $formModel = new ArticleForm();
  262. $formModel->scenario = 'sort';
  263. if ($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()) {
  264. return static::notice(Yii::t('ctx', 'successfully'));
  265. } else {
  266. return static::notice(Yii::t('ctx', 'failed'), 400);
  267. }
  268. }
  269. }
  270. }