ArticleController.php 8.8 KB

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