ArticleController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 common\helpers\Cache;
  10. use common\helpers\Form;
  11. use common\models\Article;
  12. use common\models\ArticleCategory;
  13. use common\models\Countries;
  14. use common\models\forms\ArticleCategoryForm;
  15. use common\models\forms\ArticleForm;
  16. use common\models\forms\UploadForm;
  17. use Yii;
  18. use yii\web\UploadedFile;
  19. class ArticleController extends BaseController
  20. {
  21. public $modelClass = Article::class;
  22. public function behaviors() {
  23. $behaviors = parent::behaviors();
  24. //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  25. return $behaviors;
  26. }
  27. /**
  28. * 文章分类
  29. * @return mixed
  30. * @throws \yii\web\HttpException
  31. */
  32. public function actionCategory(){
  33. $data = ArticleCategory::lists('', [], [
  34. 'orderBy' => 'SORT ASC, CREATED_AT DESC',
  35. ]);
  36. return static::notice($data);
  37. }
  38. /**
  39. * 添加分类
  40. * @return mixed
  41. * @throws \yii\web\HttpException
  42. */
  43. public function actionCategoryAdd()
  44. {
  45. if (Yii::$app->request->isPost) {
  46. return parent::edit(ArticleCategoryForm::class, Yii::t('ctx', 'successfully'));
  47. }
  48. }
  49. /**
  50. * 删除文章分类
  51. * @return mixed
  52. * @throws \yii\db\Exception
  53. * @throws \yii\web\HttpException
  54. */
  55. public function actionCategoryDelete(){
  56. $result = static::delete(ArticleCategory::class);
  57. return $result;
  58. }
  59. /**
  60. * 分类排序
  61. * @return mixed
  62. * @throws \yii\web\HttpException
  63. */
  64. public function actionCategorySort(){
  65. if(Yii::$app->request->get('id')){
  66. $formModel = new ArticleCategoryForm();
  67. $formModel->scenario = 'sort';
  68. if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){
  69. return static::notice(Yii::t('ctx', 'successfully'));
  70. } else {
  71. return static::notice(Yii::t('ctx', 'failed'), 400);
  72. }
  73. }
  74. }
  75. /**
  76. * 文章
  77. * @return mixed
  78. * @throws \yii\web\HttpException
  79. */
  80. public function actionIndex()
  81. {
  82. $filter = $this->filterCondition([
  83. 'TITLE' => 'TITLE',
  84. 'COUNTRY_ID' => 'COUNTRY_ID',
  85. ]);
  86. $condition = $filter['condition'];
  87. $params = $filter['params'];
  88. $data = Article::lists($condition, $params, [
  89. 'select' => 'ID,TITLE,CID,COUNTRY_ID,STATUS,SORT,CREATED_AT',
  90. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  91. ]);
  92. // 全部分类
  93. $data['allCategory'] = ArticleCategory::getAllCategory();
  94. return static::notice($data);
  95. }
  96. /**
  97. * 添加文章
  98. * @return mixed
  99. * @throws \yii\web\HttpException
  100. */
  101. public function actionAdd()
  102. {
  103. if (Yii::$app->request->isPost) {
  104. return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
  105. }
  106. // 获取全部分类
  107. $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all();
  108. return static::notice(['allCategory'=>$allCategory]);
  109. }
  110. /**
  111. * 编辑文章
  112. * @return mixed
  113. * @throws \yii\base\Exception
  114. * @throws \yii\web\HttpException
  115. */
  116. public function actionEdit(){
  117. $id = Yii::$app->request->get('id');
  118. if(Yii::$app->request->isPost){
  119. return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
  120. }
  121. $oneData = Article::findOneAsArray(['ID'=>$id]);
  122. $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
  123. // 国家
  124. $oneData['COUNTRY'] = Countries::getCountry($oneData['COUNTRY_ID']);
  125. // 暂时先从文件中取内容
  126. $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
  127. if(!file_exists($path)){
  128. $oneData['CONTENT'] = '';
  129. } else {
  130. $oneData['CONTENT'] = file_get_contents($path);
  131. }
  132. // 获取全部分类
  133. $allCategory = ArticleCategory::getAllCategory();
  134. return static::notice(['oneData'=>$oneData, 'allCategory'=>$allCategory]);
  135. }
  136. /**
  137. * 删除文章
  138. * @return mixed
  139. * @throws \yii\db\Exception
  140. * @throws \yii\web\HttpException
  141. */
  142. public function actionArticleDelete(){
  143. $result = static::delete(Article::class);
  144. return $result;
  145. }
  146. /**
  147. * 隐藏
  148. * @return mixed
  149. * @throws \yii\db\Exception
  150. * @throws \yii\web\HttpException
  151. */
  152. public function actionArticleHide(){
  153. $adForm = new ArticleForm();
  154. $result = static::hide(Article::class, 'hide', function ($selected) use ($adForm) {
  155. }, function ($selected) use ($adForm) {
  156. }, true);
  157. return $result;
  158. }
  159. /**
  160. * 取消隐藏
  161. * @return mixed
  162. * @throws \yii\db\Exception
  163. * @throws \yii\web\HttpException
  164. */
  165. public function actionArticleUnHide(){
  166. $adForm = new ArticleForm();
  167. $result = static::hide(Article::class, 'un-hide', function ($selected) use ($adForm) {
  168. }, function ($selected) use ($adForm) {
  169. }, true);
  170. return $result;
  171. }
  172. /**
  173. * 获取文章详细
  174. * @return mixed
  175. * @throws \yii\web\HttpException
  176. */
  177. public function actionDetail()
  178. {
  179. $id = \Yii::$app->request->get('id');
  180. $data = null;
  181. if($id){
  182. $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,COUNTRY_ID,TITLE,CID,SORT,CREATED_AT');
  183. // 国家
  184. $data['COUNTRY'] = Countries::getCountry($data['COUNTRY_ID']);
  185. }
  186. if($data){
  187. // 暂时先从文件中取内容
  188. $path = \Yii::getAlias('@common/runtime/articleContent/') . $data['ID'];
  189. if(!file_exists($path)){
  190. $data['CONTENT'] = '';
  191. } else {
  192. $data['CONTENT'] = file_get_contents($path);
  193. }
  194. return static::notice($data);
  195. } else {
  196. return static::notice('Article not exists!', 400); // 文章不存在
  197. }
  198. }
  199. /**
  200. * 上传图片
  201. * @return mixed
  202. * @throws \yii\base\Exception
  203. * @throws \yii\db\Exception
  204. * @throws \yii\web\HttpException
  205. */
  206. public function actionUpload() {
  207. if (\Yii::$app->request->isPost) {
  208. $formModel = new UploadForm();
  209. $formModel->scenario = 'article';
  210. $formModel->file = UploadedFile::getInstanceByName('file');
  211. $formModel->token = \Yii::$app->request->request('uploadToken');;
  212. if($formModel->file && $uploader = $formModel->upload()){
  213. return static::notice($uploader->FILE_NAME);
  214. } else {
  215. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  216. }
  217. } else {
  218. $token = Cache::setUploadToken();
  219. return static::notice($token);
  220. }
  221. }
  222. /**
  223. * 排序
  224. * @return mixed
  225. * @throws \yii\web\HttpException
  226. */
  227. public function actionSort()
  228. {
  229. if (Yii::$app->request->get('id')) {
  230. $formModel = new ArticleForm();
  231. $formModel->scenario = 'sort';
  232. if ($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()) {
  233. return static::notice(Yii::t('ctx', 'successfully'));
  234. } else {
  235. return static::notice(Yii::t('ctx', 'failed'), 400);
  236. }
  237. }
  238. }
  239. }