ArticleController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. 'TITLE' => 'ART.TITLE',
  102. 'COUNTRY_ID' => 'ART.COUNTRY_ID',
  103. 'STATUS' => 'ART.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. // 管理员关联国家
  136. $admin = Admin::findOne(Yii::$app->user->id);
  137. $roleId = $admin->ROLE_ID;
  138. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  139. $countries = Countries::find()->asArray()->all();
  140. } else {
  141. // 关联国家
  142. $countries = Countries::find()
  143. ->select('COU.ID, COU.CODE, COU.NAME')
  144. ->from(['COU' => Countries::tableName()])
  145. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  146. ->where(['ADL.ADMIN_ID' => $admin->ID])
  147. ->asArray()
  148. ->all();
  149. }
  150. return static::notice([
  151. 'allCategory' => $allCategory,
  152. 'countries' => $countries,
  153. ]);
  154. }
  155. /**
  156. * 编辑文章
  157. * @return mixed
  158. * @throws \yii\base\Exception
  159. * @throws \yii\web\HttpException
  160. */
  161. public function actionEdit(){
  162. $id = Yii::$app->request->get('id');
  163. if(Yii::$app->request->isPost){
  164. return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
  165. }
  166. $oneData = Article::findOneAsArray(['ID'=>$id]);
  167. $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
  168. // 国家
  169. $oneData['COUNTRY'] = Countries::getCountry($oneData['COUNTRY_ID']);
  170. // 暂时先从文件中取内容
  171. $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
  172. if(!file_exists($path)){
  173. $oneData['CONTENT'] = '';
  174. } else {
  175. $oneData['CONTENT'] = file_get_contents($path);
  176. }
  177. // 获取全部分类
  178. $allCategory = ArticleCategory::getAllCategory();
  179. // 管理员关联国家
  180. $admin = Admin::findOne(Yii::$app->user->id);
  181. $roleId = $admin->ROLE_ID;
  182. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  183. $countries = Countries::find()->asArray()->all();
  184. } else {
  185. // 关联国家
  186. $countries = Countries::find()
  187. ->select('COU.ID, COU.CODE, COU.NAME')
  188. ->from(['COU' => Countries::tableName()])
  189. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  190. ->where(['ADL.ADMIN_ID' => $admin->ID])
  191. ->asArray()
  192. ->all();
  193. }
  194. return static::notice([
  195. 'oneData' => $oneData,
  196. 'allCategory' => $allCategory,
  197. 'countries' => $countries
  198. ]);
  199. }
  200. /**
  201. * 删除文章
  202. * @return mixed
  203. * @throws \yii\db\Exception
  204. * @throws \yii\web\HttpException
  205. */
  206. public function actionArticleDelete(){
  207. $result = static::delete(Article::class);
  208. return $result;
  209. }
  210. /**
  211. * 隐藏
  212. * @return mixed
  213. * @throws \yii\db\Exception
  214. * @throws \yii\web\HttpException
  215. */
  216. public function actionArticleHide(){
  217. $adForm = new ArticleForm();
  218. $result = static::hide(Article::class, 'hide', function ($selected) use ($adForm) {
  219. }, function ($selected) use ($adForm) {
  220. }, true);
  221. return $result;
  222. }
  223. /**
  224. * 取消隐藏
  225. * @return mixed
  226. * @throws \yii\db\Exception
  227. * @throws \yii\web\HttpException
  228. */
  229. public function actionArticleUnHide(){
  230. $adForm = new ArticleForm();
  231. $result = static::hide(Article::class, 'un-hide', function ($selected) use ($adForm) {
  232. }, function ($selected) use ($adForm) {
  233. }, true);
  234. return $result;
  235. }
  236. /**
  237. * 获取文章详细
  238. * @return mixed
  239. * @throws \yii\web\HttpException
  240. */
  241. public function actionDetail()
  242. {
  243. $id = \Yii::$app->request->get('id');
  244. $data = null;
  245. if($id){
  246. $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,COUNTRY_ID,TITLE,CID,SORT,CREATED_AT');
  247. // 国家
  248. $data['COUNTRY'] = Countries::getCountry($data['COUNTRY_ID']);
  249. }
  250. if($data){
  251. // 暂时先从文件中取内容
  252. $path = \Yii::getAlias('@common/runtime/articleContent/') . $data['ID'];
  253. if(!file_exists($path)){
  254. $data['CONTENT'] = '';
  255. } else {
  256. $data['CONTENT'] = file_get_contents($path);
  257. }
  258. return static::notice($data);
  259. } else {
  260. return static::notice('Article not exists!', 400); // 文章不存在
  261. }
  262. }
  263. /**
  264. * 上传图片
  265. * @return mixed
  266. * @throws \yii\base\Exception
  267. * @throws \yii\db\Exception
  268. * @throws \yii\web\HttpException
  269. */
  270. public function actionUpload() {
  271. if (\Yii::$app->request->isPost) {
  272. $formModel = new UploadForm();
  273. $formModel->scenario = 'article';
  274. $formModel->file = UploadedFile::getInstanceByName('file');
  275. $formModel->token = \Yii::$app->request->request('uploadToken');;
  276. if($formModel->file && ($uploader = $formModel->upload()) !== false){
  277. if (is_object($uploader) && isset($uploader->FILE_NAME)) {
  278. return static::notice($uploader->FILE_NAME);
  279. } else {
  280. // 处理非对象返回值的情况
  281. return static::notice('Upload successful');
  282. }
  283. } else {
  284. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  285. }
  286. } else {
  287. $token = Cache::setUploadToken();
  288. return static::notice($token);
  289. }
  290. }
  291. /**
  292. * 排序
  293. * @return mixed
  294. * @throws \yii\web\HttpException
  295. */
  296. public function actionSort()
  297. {
  298. if (Yii::$app->request->get('id')) {
  299. $formModel = new ArticleForm();
  300. $formModel->scenario = 'sort';
  301. if ($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()) {
  302. return static::notice(Yii::t('ctx', 'successfully'));
  303. } else {
  304. return static::notice(Yii::t('ctx', 'failed'), 400);
  305. }
  306. }
  307. }
  308. public function actionCountry()
  309. {
  310. $admin = Admin::findOne(Yii::$app->user->id);
  311. $roleId = $admin->ROLE_ID;
  312. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  313. $countries = Countries::find()->asArray()->all();
  314. } else {
  315. // 关联国家
  316. $countries = Countries::find()
  317. ->select('COU.ID, COU.CODE, COU.NAME')
  318. ->from(['COU' => Countries::tableName()])
  319. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  320. ->where(['ADL.ADMIN_ID' => $admin->ID])
  321. ->asArray()
  322. ->all();
  323. }
  324. return static::notice($countries);
  325. }
  326. }