ArticleController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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\user\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. 'TITLE' => 'TITLE',
  102. 'COUNTRY_ID' => 'COUNTRY_ID',
  103. ]);
  104. $condition = $filter['condition'];
  105. $params = $filter['params'];
  106. $countryIds = array_column($countries, 'ID');
  107. if (!empty($countryIds)) {
  108. $placeholders = [];
  109. foreach ($countryIds as $key => $id) {
  110. $paramName = ':countryId_' . $key;
  111. $placeholders[] = $paramName;
  112. $params[$paramName] = $id;
  113. }
  114. $condition .= ' AND ART.COUNTRY_ID IN (' . implode(',', $placeholders) . ')';
  115. }
  116. $obj = new Article();
  117. $data = $obj->getList(['condition' => $condition, 'params' => $params]);
  118. // 全部分类
  119. $data['allCategory'] = ArticleCategory::getAllCategory();
  120. return static::notice($data);
  121. }
  122. /**
  123. * 添加文章
  124. * @return mixed
  125. * @throws \yii\web\HttpException
  126. */
  127. public function actionAdd()
  128. {
  129. if (Yii::$app->request->isPost) {
  130. return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
  131. }
  132. // 获取全部分类
  133. $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all();
  134. return static::notice(['allCategory'=>$allCategory]);
  135. }
  136. /**
  137. * 编辑文章
  138. * @return mixed
  139. * @throws \yii\base\Exception
  140. * @throws \yii\web\HttpException
  141. */
  142. public function actionEdit(){
  143. $id = Yii::$app->request->get('id');
  144. if(Yii::$app->request->isPost){
  145. return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
  146. }
  147. $oneData = Article::findOneAsArray(['ID'=>$id]);
  148. $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
  149. // 国家
  150. $oneData['COUNTRY'] = Countries::getCountry($oneData['COUNTRY_ID']);
  151. // 暂时先从文件中取内容
  152. $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
  153. if(!file_exists($path)){
  154. $oneData['CONTENT'] = '';
  155. } else {
  156. $oneData['CONTENT'] = file_get_contents($path);
  157. }
  158. // 获取全部分类
  159. $allCategory = ArticleCategory::getAllCategory();
  160. return static::notice(['oneData'=>$oneData, 'allCategory'=>$allCategory]);
  161. }
  162. /**
  163. * 删除文章
  164. * @return mixed
  165. * @throws \yii\db\Exception
  166. * @throws \yii\web\HttpException
  167. */
  168. public function actionArticleDelete(){
  169. $result = static::delete(Article::class);
  170. return $result;
  171. }
  172. /**
  173. * 隐藏
  174. * @return mixed
  175. * @throws \yii\db\Exception
  176. * @throws \yii\web\HttpException
  177. */
  178. public function actionArticleHide(){
  179. $adForm = new ArticleForm();
  180. $result = static::hide(Article::class, 'hide', function ($selected) use ($adForm) {
  181. }, function ($selected) use ($adForm) {
  182. }, true);
  183. return $result;
  184. }
  185. /**
  186. * 取消隐藏
  187. * @return mixed
  188. * @throws \yii\db\Exception
  189. * @throws \yii\web\HttpException
  190. */
  191. public function actionArticleUnHide(){
  192. $adForm = new ArticleForm();
  193. $result = static::hide(Article::class, 'un-hide', function ($selected) use ($adForm) {
  194. }, function ($selected) use ($adForm) {
  195. }, true);
  196. return $result;
  197. }
  198. /**
  199. * 获取文章详细
  200. * @return mixed
  201. * @throws \yii\web\HttpException
  202. */
  203. public function actionDetail()
  204. {
  205. $id = \Yii::$app->request->get('id');
  206. $data = null;
  207. if($id){
  208. $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,COUNTRY_ID,TITLE,CID,SORT,CREATED_AT');
  209. // 国家
  210. $data['COUNTRY'] = Countries::getCountry($data['COUNTRY_ID']);
  211. }
  212. if($data){
  213. // 暂时先从文件中取内容
  214. $path = \Yii::getAlias('@common/runtime/articleContent/') . $data['ID'];
  215. if(!file_exists($path)){
  216. $data['CONTENT'] = '';
  217. } else {
  218. $data['CONTENT'] = file_get_contents($path);
  219. }
  220. return static::notice($data);
  221. } else {
  222. return static::notice('Article not exists!', 400); // 文章不存在
  223. }
  224. }
  225. /**
  226. * 上传图片
  227. * @return mixed
  228. * @throws \yii\base\Exception
  229. * @throws \yii\db\Exception
  230. * @throws \yii\web\HttpException
  231. */
  232. public function actionUpload() {
  233. if (\Yii::$app->request->isPost) {
  234. $formModel = new UploadForm();
  235. $formModel->scenario = 'article';
  236. $formModel->file = UploadedFile::getInstanceByName('file');
  237. $formModel->token = \Yii::$app->request->request('uploadToken');;
  238. if($formModel->file && $uploader = $formModel->upload()){
  239. return static::notice($uploader->FILE_NAME);
  240. } else {
  241. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  242. }
  243. } else {
  244. $token = Cache::setUploadToken();
  245. return static::notice($token);
  246. }
  247. }
  248. /**
  249. * 排序
  250. * @return mixed
  251. * @throws \yii\web\HttpException
  252. */
  253. public function actionSort()
  254. {
  255. if (Yii::$app->request->get('id')) {
  256. $formModel = new ArticleForm();
  257. $formModel->scenario = 'sort';
  258. if ($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()) {
  259. return static::notice(Yii::t('ctx', 'successfully'));
  260. } else {
  261. return static::notice(Yii::t('ctx', 'failed'), 400);
  262. }
  263. }
  264. }
  265. }