ArticleController.php 7.1 KB

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