ArticleController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace backend\controllers;
  3. use common\services\xhArticleService;
  4. use Yii;
  5. use yii\web\Controller;
  6. use yii\filters\AccessControl;
  7. use yii\helpers\Json;
  8. use yii\data\Pagination;
  9. use common\components\qrCodeUtil;
  10. use common\components\util;
  11. use common\models\xhArticle;
  12. use common\services\xhCategoryService;
  13. use common\services\xhArticleCategoryService;
  14. class ArticleController extends BackendController
  15. {
  16. /**
  17. * 文章管理
  18. */
  19. public function actionList()
  20. {
  21. $get = Yii::$app->request->get();
  22. $query = xhArticle::find();
  23. $aId = $this->adminId;
  24. $status = isset($get['status']) ? $get['status'] : -1;
  25. $query->where(['merchantId' =>$this->merchantId]);
  26. if(isset($get['status']) && $get['status'] != -1){
  27. $query->andWhere(['status' => $get['status']]);
  28. }
  29. if(isset($get['title']) && !empty($get['title'])){
  30. $query->andWhere(['like', 'title', $get['title']]);
  31. }
  32. $query->orderBy('id desc');
  33. $countQuery = clone $query;
  34. $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => 20]);
  35. $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  36. return $this->render('list', ['models' => $models,'pages' => $pages,'status' => $status]);
  37. }
  38. public function actionAdd()
  39. {
  40. //取分类
  41. $categoryList = xhCategoryService::getAll($this->merchantId,1);
  42. return $this->render('add',['categoryList' => $categoryList]);
  43. }
  44. public function actionSaveAdd()
  45. {
  46. $post = Yii::$app->request->post();
  47. $date = date("Y-m-d H:i:s");
  48. $post['createTime'] = $date;
  49. $post['merchantId'] = $this->merchantId;
  50. xhArticleService::addByAdmin($post);
  51. util::successInfo('添加成功!');
  52. }
  53. public function actionEdit()
  54. {
  55. $get = Yii::$app->request->get();
  56. $id = isset($get['id']) ? $get['id'] : 0;
  57. $article = xhArticleService::getById($id);
  58. if(empty($article) || $article['merchantId'] != $this->merchantId){
  59. util::stop('非法访问');
  60. }
  61. $articleCategory = xhArticleCategoryService::getCategoryByArticleId($id);
  62. $categoryList = xhCategoryService::getAll($this->merchantId,1);
  63. return $this->render('edit',['categoryList' => $categoryList,'article' => $article,'articleCategory' => $articleCategory]);
  64. }
  65. public function actionSaveEdit()
  66. {
  67. $post = Yii::$app->request->post();
  68. $id = $post['id'];
  69. $post['merchantId'] = $this->merchantId;
  70. unset($post['_csrf']);
  71. xhArticleService::updateAllById($id,$post,$this->merchantId);
  72. util::successInfo('修改成功!');
  73. }
  74. /**
  75. * 生成文章的二维码地址
  76. */
  77. public function actionShowArticleQrcode()
  78. {
  79. $get = Yii::$app->request->get();
  80. $id = isset($get['id']) ? $get['id'] : 0;
  81. $url = Yii::$app->params['frontUrl'].'/article/article-detail?id='.$id.'&account='.$this->merchant['id'];
  82. $merchantId = $this->merchantId;
  83. $article = xhArticle::getById($id);
  84. if(empty($article)){
  85. util::failInfo();
  86. }
  87. $logo = Yii::getAlias("@app").'/../images'.$this->merchant['logo'];//取商家的logo
  88. $imgUrl = qrCodeUtil::generate($url,'backend_article_url_'.$merchantId.'_'.$id,$logo);
  89. xhArticleService::updateById($id, ['urlQrCode'=>$imgUrl]);
  90. util::successInfo('success','A0001',['url' => $imgUrl]);
  91. }
  92. }