ArticleLogic.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\logic;
  15. use app\common\enum\YesNoEnum;
  16. use app\common\logic\BaseLogic;
  17. use app\common\model\article\Article;
  18. use app\common\model\article\ArticleCate;
  19. use app\common\model\article\ArticleCollect;
  20. /**
  21. * 文章逻辑
  22. * Class ArticleLogic
  23. * @package app\api\logic
  24. */
  25. class ArticleLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 文章详情
  29. * @param $articleId
  30. * @param $userId
  31. * @return array
  32. * @author 段誉
  33. * @date 2022/9/20 17:09
  34. */
  35. public static function detail($articleId, $userId)
  36. {
  37. // 文章详情
  38. $article = Article::getArticleDetailArr($articleId);
  39. // 关注状态
  40. $article['collect'] = ArticleCollect::isCollectArticle($userId, $articleId);
  41. return $article;
  42. }
  43. /**
  44. * @notes 加入收藏
  45. * @param $userId
  46. * @param $articleId
  47. * @author 段誉
  48. * @date 2022/9/20 16:52
  49. */
  50. public static function addCollect($articleId, $userId)
  51. {
  52. $where = ['user_id' => $userId, 'article_id' => $articleId];
  53. $collect = ArticleCollect::where($where)->findOrEmpty();
  54. if ($collect->isEmpty()) {
  55. ArticleCollect::create([
  56. 'user_id' => $userId,
  57. 'article_id' => $articleId,
  58. 'status' => YesNoEnum::YES
  59. ]);
  60. } else {
  61. ArticleCollect::update([
  62. 'id' => $collect['id'],
  63. 'status' => YesNoEnum::YES
  64. ]);
  65. }
  66. }
  67. /**
  68. * @notes 取消收藏
  69. * @param $articleId
  70. * @param $userId
  71. * @author 段誉
  72. * @date 2022/9/20 16:59
  73. */
  74. public static function cancelCollect($articleId, $userId)
  75. {
  76. ArticleCollect::update(['status' => YesNoEnum::NO], [
  77. 'user_id' => $userId,
  78. 'article_id' => $articleId,
  79. 'status' => YesNoEnum::YES
  80. ]);
  81. }
  82. /**
  83. * @notes 文章分类
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @author 段誉
  89. * @date 2022/9/23 14:11
  90. */
  91. public static function cate()
  92. {
  93. return ArticleCate::field('id,name')
  94. ->where('is_show', '=', 1)
  95. ->order(['sort' => 'desc', 'id' => 'desc'])
  96. ->select()->toArray();
  97. }
  98. }