فهرست منبع

师傅端重要公告

whitefang 1 سال پیش
والد
کامیت
8e9af6aba2
2فایلهای تغییر یافته به همراه151 افزوده شده و 0 حذف شده
  1. 38 0
      app/workerapi/controller/ArticleController.php
  2. 113 0
      app/workerapi/lists/ArticleLists.php

+ 38 - 0
app/workerapi/controller/ArticleController.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace app\workerapi\controller;
+
+use app\workerapi\lists\ArticleLists;
+use app\api\logic\ArticleLogic;
+
+/**
+ * 文章管理
+ * Class ArticleController
+ * @package app\api\controller
+ */
+class ArticleController extends \app\workerapi\controller\BaseApiController
+{
+    /**
+     * @notes 文章列表
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/20 15:30
+     */
+    public function lists()
+    {
+        return $this->dataLists(new ArticleLists());
+    }
+
+    /**
+     * @notes 文章详情
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/20 17:09
+     */
+    public function detail()
+    {
+        $id = $this->request->get('id/d');
+        $result = ArticleLogic::detail($id, $this->userId);
+        return $this->data($result);
+    }
+}

+ 113 - 0
app/workerapi/lists/ArticleLists.php

@@ -0,0 +1,113 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\workerapi\lists;
+
+use app\api\lists\BaseApiDataLists;
+use app\common\enum\YesNoEnum;
+use app\common\lists\ListsSearchInterface;
+use app\common\model\article\Article;
+use app\common\model\article\ArticleCollect;
+
+
+/**
+ * 文章列表
+ * Class ArticleLists
+ * @package app\api\lists\article
+ */
+class ArticleLists extends BaseApiDataLists implements ListsSearchInterface
+{
+
+    /**
+     * @notes 搜索条件
+     * @return \string[][]
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['cid']
+        ];
+    }
+
+
+    /**
+     * @notes 自定查询条件
+     * @return array
+     */
+    public function queryWhere()
+    {
+        $where[] = ['is_show', '=', 1];
+        $where['cid'] = 3;//工程师端-重要公告
+        if (!empty($this->params['keyword'])) {
+            $where[] = ['title', 'like', '%' . $this->params['keyword'] . '%'];
+        }
+        return $where;
+    }
+
+
+    /**
+     * @notes 获取文章列表
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function lists(): array
+    {
+        $orderRaw = 'sort desc, id desc';
+        $sortType = $this->params['sort'] ?? 'default';
+        // 最新排序
+        if ($sortType == 'new') {
+            $orderRaw = 'id desc';
+        }
+        // 最热排序
+        if ($sortType == 'hot') {
+            $orderRaw = 'click_actual + click_virtual desc, id desc';
+        }
+
+        $field = 'id,cid,title,desc,image,click_virtual,click_actual,create_time';
+        $result = Article::field($field)
+            ->where($this->queryWhere())
+            ->where($this->searchWhere)
+            ->orderRaw($orderRaw)
+            ->append(['click'])
+            ->hidden(['click_virtual', 'click_actual'])
+            ->limit($this->limitOffset, $this->limitLength)
+            ->select()->toArray();
+
+        $articleIds = array_column($result, 'id');
+
+        $collectIds = ArticleCollect::where(['user_id' => $this->userId, 'status' => YesNoEnum::YES])
+            ->whereIn('article_id', $articleIds)
+            ->column('article_id');
+
+        foreach ($result as &$item) {
+            $item['collect'] = in_array($item['id'], $collectIds);
+        }
+
+        return $result;
+    }
+
+
+    /**
+     * @notes 获取文章数量
+     * @return int
+     */
+    public function count(): int
+    {
+        return Article::where($this->searchWhere)
+            ->where($this->queryWhere())
+            ->count();
+    }
+}