moonsflyer 9 hónapja
szülő
commit
0fc2fe3550

+ 114 - 0
app/adminapi/controller/agricultural_machinery/ArticleController.php

@@ -0,0 +1,114 @@
+<?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\adminapi\controller\article;
+
+use app\adminapi\controller\BaseAdminController;
+use app\adminapi\lists\article\ArticleLists;
+use app\adminapi\logic\article\ArticleLogic;
+use app\adminapi\validate\article\ArticleValidate;
+
+/**
+ * 资讯管理控制器
+ * Class ArticleController
+ * @package app\adminapi\controller\article
+ */
+class ArticleController extends BaseAdminController
+{
+
+    /**
+     * @notes  查看资讯列表
+     * @return \think\response\Json
+     * @author heshihu
+     * @date 2022/2/22 9:47
+     */
+    public function lists()
+    {
+        return $this->dataLists(new ArticleLists());
+    }
+
+    /**
+     * @notes  添加资讯
+     * @return \think\response\Json
+     * @author heshihu
+     * @date 2022/2/22 9:57
+     */
+    public function add()
+    {
+        $params = (new ArticleValidate())->post()->goCheck('add');
+        ArticleLogic::add($params);
+        return $this->success('添加成功', [], 1, 1);
+    }
+
+    /**
+     * @notes  编辑资讯
+     * @return \think\response\Json
+     * @author heshihu
+     * @date 2022/2/22 10:12
+     */
+    public function edit()
+    {
+        $params = (new ArticleValidate())->post()->goCheck('edit');
+        $result = ArticleLogic::edit($params);
+        if (true === $result) {
+            return $this->success('编辑成功', [], 1, 1);
+        }
+        return $this->fail(ArticleLogic::getError());
+    }
+
+    /**
+     * @notes  删除资讯
+     * @return \think\response\Json
+     * @author heshihu
+     * @date 2022/2/22 10:17
+     */
+    public function delete()
+    {
+        $params = (new ArticleValidate())->post()->goCheck('delete');
+        ArticleLogic::delete($params);
+        return $this->success('删除成功', [], 1, 1);
+    }
+
+    /**
+     * @notes  资讯详情
+     * @return \think\response\Json
+     * @author heshihu
+     * @date 2022/2/22 10:15
+     */
+    public function detail()
+    {
+        $params = (new ArticleValidate())->goCheck('detail');
+        $result = ArticleLogic::detail($params);
+        return $this->data($result);
+    }
+
+
+    /**
+     * @notes  更改资讯状态
+     * @return \think\response\Json
+     * @author heshihu
+     * @date 2022/2/22 10:18
+     */
+    public function updateStatus()
+    {
+        $params = (new ArticleValidate())->post()->goCheck('status');
+        $result = ArticleLogic::updateStatus($params);
+        if (true === $result) {
+            return $this->success('修改成功', [], 1, 1);
+        }
+        return $this->fail(ArticleLogic::getError());
+    }
+
+
+}

+ 99 - 0
app/adminapi/lists/agricultural_machinery/ArticleLists.php

@@ -0,0 +1,99 @@
+<?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\adminapi\lists\article;
+
+use app\adminapi\lists\BaseAdminDataLists;
+use app\common\lists\ListsSearchInterface;
+use app\common\lists\ListsSortInterface;
+use app\common\model\article\Article;
+
+/**
+ * 资讯列表
+ * Class ArticleLists
+ * @package app\adminapi\lists\article
+ */
+class ArticleLists extends BaseAdminDataLists implements ListsSearchInterface, ListsSortInterface
+{
+
+    /**
+     * @notes  设置搜索条件
+     * @return array
+     * @author heshihu
+     * @date 2022/2/8 18:39
+     */
+    public function setSearch(): array
+    {
+        return [
+            '%like%' => ['title'],
+            '=' => ['cid', 'is_show']
+        ];
+    }
+
+    /**
+     * @notes  设置支持排序字段
+     * @return array
+     * @author heshihu
+     * @date 2022/2/9 15:11
+     */
+    public function setSortFields(): array
+    {
+        return ['create_time' => 'create_time', 'id' => 'id'];
+    }
+
+    /**
+     * @notes  设置默认排序
+     * @return array
+     * @author heshihu
+     * @date 2022/2/9 15:08
+     */
+    public function setDefaultOrder(): array
+    {
+        return ['sort' => 'desc', 'id' => 'desc'];
+    }
+
+    /**
+     * @notes  获取管理列表
+     * @return array
+     * @author heshihu
+     * @date 2022/2/21 17:11
+     */
+    public function lists(): array
+    {
+        $ArticleLists = Article::where($this->searchWhere)
+            ->append(['cate_name', 'click'])
+            ->limit($this->limitOffset, $this->limitLength)
+            ->order($this->sortOrder)
+            ->select()
+            ->toArray();
+
+        return $ArticleLists;
+    }
+
+    /**
+     * @notes  获取数量
+     * @return int
+     * @author heshihu
+     * @date 2022/2/9 15:12
+     */
+    public function count(): int
+    {
+        return Article::where($this->searchWhere)->count();
+    }
+
+    public function extend()
+    {
+        return [];
+    }
+}

+ 121 - 0
app/adminapi/logic/agricultural_machinery/ArticleLogic.php

@@ -0,0 +1,121 @@
+<?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\adminapi\logic\article;
+
+use app\common\logic\BaseLogic;
+use app\common\model\article\Article;
+use app\common\service\FileService;
+
+/**
+ * 资讯管理逻辑
+ * Class ArticleLogic
+ * @package app\adminapi\logic\article
+ */
+class ArticleLogic extends BaseLogic
+{
+
+    /**
+     * @notes  添加资讯
+     * @param array $params
+     * @author heshihu
+     * @date 2022/2/22 9:57
+     */
+    public static function add(array $params)
+    {
+        Article::create([
+            'title' => $params['title'],
+            'desc' => $params['desc'] ?? '',
+            'author' => $params['author'] ?? '', //作者
+            'sort' => $params['sort'] ?? 0, // 排序
+            'abstract' => $params['abstract'], // 文章摘要
+            'click_virtual' => $params['click_virtual'] ?? 0,
+            'image' => $params['image'] ? FileService::setFileUrl($params['image']) : '',
+            'cid' => $params['cid'],
+            'is_show' => $params['is_show'],
+            'content' => $params['content'] ?? '',
+        ]);
+    }
+
+
+    /**
+     * @notes  编辑资讯
+     * @param array $params
+     * @return bool
+     * @author heshihu
+     * @date 2022/2/22 10:12
+     */
+    public static function edit(array $params) : bool
+    {
+        try {
+            Article::update([
+                'id' => $params['id'],
+                'title' => $params['title'],
+                'desc' => $params['desc'] ?? '', // 简介
+                'author' => $params['author'] ?? '', //作者
+                'sort' => $params['sort'] ?? 0, // 排序
+                'abstract' => $params['abstract'], // 文章摘要
+                'click_virtual' => $params['click_virtual'] ?? 0,
+                'image' => $params['image'] ? FileService::setFileUrl($params['image']) : '',
+                'cid' => $params['cid'],
+                'is_show' => $params['is_show'],
+                'content' => $params['content'] ?? '',
+            ]);
+            return true;
+        } catch (\Exception $e) {
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes  删除资讯
+     * @param array $params
+     * @author heshihu
+     * @date 2022/2/22 10:17
+     */
+    public static function delete(array $params)
+    {
+        Article::destroy($params['id']);
+    }
+
+    /**
+     * @notes  查看资讯详情
+     * @param $params
+     * @return array
+     * @author heshihu
+     * @date 2022/2/22 10:15
+     */
+    public static function detail($params) : array
+    {
+        return Article::findOrEmpty($params['id'])->toArray();
+    }
+
+    /**
+     * @notes  更改资讯状态
+     * @param array $params
+     * @return bool
+     * @author heshihu
+     * @date 2022/2/22 10:18
+     */
+    public static function updateStatus(array $params)
+    {
+        Article::update([
+            'id' => $params['id'],
+            'is_show' => $params['is_show']
+        ]);
+        return true;
+    }
+}

+ 108 - 0
app/adminapi/validate/agricultural_machinery/ArticleValidate.php

@@ -0,0 +1,108 @@
+<?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\adminapi\validate\article;
+
+use app\common\validate\BaseValidate;
+use app\common\model\article\Article;
+
+/**
+ * 资讯管理验证
+ * Class ArticleValidate
+ * @package app\adminapi\validate\article
+ */
+class ArticleValidate extends BaseValidate
+{
+    protected $rule = [
+        'id' => 'require|checkArticle',
+        'title' => 'require|length:1,255',
+//        'image' => 'require',
+        'cid' => 'require',
+        'is_show' => 'require|in:0,1',
+    ];
+
+    protected $message = [
+        'id.require' => '资讯id不能为空',
+        'title.require' => '标题不能为空',
+        'title.length' => '标题长度须在1-255位字符',
+//        'image.require' => '封面图必须存在',
+        'cid.require' => '所属栏目必须存在',
+    ];
+
+    /**
+     * @notes  添加场景
+     * @return ArticleValidate
+     * @author heshihu
+     * @date 2022/2/22 9:57
+     */
+    public function sceneAdd()
+    {
+        return $this->remove(['id'])
+            ->remove('id','require|checkArticle');
+    }
+
+    /**
+     * @notes  详情场景
+     * @return ArticleValidate
+     * @author heshihu
+     * @date 2022/2/22 10:15
+     */
+    public function sceneDetail()
+    {
+        return $this->only(['id']);
+    }
+
+    /**
+     * @notes  更改状态场景
+     * @return ArticleValidate
+     * @author heshihu
+     * @date 2022/2/22 10:18
+     */
+    public function sceneStatus()
+    {
+        return $this->only(['id', 'is_show']);
+    }
+
+    public function sceneEdit()
+    {
+    }
+
+    /**
+     * @notes  删除场景
+     * @return ArticleValidate
+     * @author heshihu
+     * @date 2022/2/22 10:17
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['id']);
+    }
+
+    /**
+     * @notes  检查指定资讯是否存在
+     * @param $value
+     * @return bool|string
+     * @author heshihu
+     * @date 2022/2/22 10:11
+     */
+    public function checkArticle($value)
+    {
+        $article = Article::findOrEmpty($value);
+        if ($article->isEmpty()) {
+            return '资讯不存在';
+        }
+        return true;
+    }
+
+}

+ 112 - 0
app/common/model/agricultural_machinery/Article.php

@@ -0,0 +1,112 @@
+<?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\common\model\article;
+
+use app\common\enum\YesNoEnum;
+use app\common\model\BaseModel;
+use think\model\concern\SoftDelete;
+
+/**
+ * 资讯管理模型
+ * Class Article
+ * @package app\common\model\article;
+ */
+class Article extends BaseModel
+{
+    use SoftDelete;
+
+    protected $deleteTime = 'delete_time';
+
+    /**
+     * @notes  获取分类名称
+     * @param $value
+     * @param $data
+     * @return string
+     * @author heshihu
+     * @date 2022/2/22 9:53
+     */
+    public function getCateNameAttr($value, $data)
+    {
+        return ArticleCate::where('id', $data['cid'])->value('name');
+    }
+
+    /**
+     * @notes 浏览量
+     * @param $value
+     * @param $data
+     * @return mixed
+     * @author 段誉
+     * @date 2022/9/15 11:33
+     */
+    public function getClickAttr($value, $data)
+    {
+        return $data['click_actual'] + $data['click_virtual'];
+    }
+
+
+    /**
+     * @notes 设置图片域名
+     * @param $value
+     * @param $data
+     * @return array|string|string[]|null
+     * @author 段誉
+     * @date 2022/9/28 10:17
+     */
+    public function getContentAttr($value, $data)
+    {
+        return get_file_domain($value);
+    }
+
+
+    /**
+     * @notes 清除图片域名
+     * @param $value
+     * @param $data
+     * @return array|string|string[]
+     * @author 段誉
+     * @date 2022/9/28 10:17
+     */
+    public function setContentAttr($value, $data)
+    {
+        return clear_file_domain($value);
+    }
+
+
+    /**
+     * @notes 获取文章详情
+     * @param $id
+     * @return array
+     * @author 段誉
+     * @date 2022/10/20 15:23
+     */
+    public static function getArticleDetailArr(int $id)
+    {
+        $article = Article::where(['id' => $id, 'is_show' => YesNoEnum::YES])
+            ->findOrEmpty();
+
+        if ($article->isEmpty()) {
+            return [];
+        }
+
+        // 增加点击量
+        $article->click_actual += 1;
+        $article->save();
+
+        return $article->append(['click'])
+            ->hidden(['click_virtual', 'click_actual'])
+            ->toArray();
+    }
+
+}

+ 52 - 0
app/common/model/agricultural_machinery/ArticleCollect.php

@@ -0,0 +1,52 @@
+<?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\common\model\article;
+
+use app\common\enum\YesNoEnum;
+use app\common\model\BaseModel;
+use think\model\concern\SoftDelete;
+
+/**
+ * 资讯收藏
+ * Class ArticleCollect
+ * @package app\common\model\article
+ */
+class ArticleCollect extends BaseModel
+{
+    use SoftDelete;
+
+    protected $deleteTime = 'delete_time';
+
+
+    /**
+     * @notes 是否已收藏文章
+     * @param $userId
+     * @param $articleId
+     * @return bool (true=已收藏, false=未收藏)
+     * @author 段誉
+     * @date 2022/10/20 15:13
+     */
+    public static function isCollectArticle($userId, $articleId)
+    {
+        $collect = ArticleCollect::where([
+            'user_id' => $userId,
+            'article_id' => $articleId,
+            'status' => YesNoEnum::YES
+        ])->findOrEmpty();
+
+        return !$collect->isEmpty();
+    }
+
+}