PcLogic.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. use app\common\model\decorate\DecoratePage;
  21. use app\common\service\ConfigService;
  22. use app\common\service\FileService;
  23. /**
  24. * index
  25. * Class IndexLogic
  26. * @package app\api\logic
  27. */
  28. class PcLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 首页数据
  32. * @return array
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. * @author 段誉
  37. * @date 2022/9/21 19:15
  38. */
  39. public static function getIndexData()
  40. {
  41. // 装修配置
  42. $decoratePage = DecoratePage::findOrEmpty(4);
  43. // 最新资讯
  44. $newArticle = self::getLimitArticle('new', 7);
  45. // 全部资讯
  46. $allArticle = self::getLimitArticle('all', 5);
  47. // 热门资讯
  48. $hotArticle = self::getLimitArticle('hot', 8);
  49. return [
  50. 'page' => $decoratePage,
  51. 'all' => $allArticle,
  52. 'new' => $newArticle,
  53. 'hot' => $hotArticle
  54. ];
  55. }
  56. /**
  57. * @notes 获取文章
  58. * @param string $sortType
  59. * @param int $limit
  60. * @return mixed
  61. * @author 段誉
  62. * @date 2022/10/19 9:53
  63. */
  64. public static function getLimitArticle(string $sortType, int $limit = 0, int $cate = 0, int $excludeId = 0)
  65. {
  66. // 查询字段
  67. $field = [
  68. 'id', 'cid', 'title', 'desc', 'abstract', 'image',
  69. 'author', 'click_actual', 'click_virtual', 'create_time'
  70. ];
  71. // 排序条件
  72. $orderRaw = 'sort desc, id desc';
  73. if ($sortType == 'new') {
  74. $orderRaw = 'id desc';
  75. }
  76. if ($sortType == 'hot') {
  77. $orderRaw = 'click_actual + click_virtual desc, id desc';
  78. }
  79. // 查询条件
  80. $where[] = ['is_show', '=', YesNoEnum::YES];
  81. if (!empty($cate)) {
  82. $where[] = ['cid', '=', $cate];
  83. }
  84. if (!empty($excludeId)) {
  85. $where[] = ['id', '<>', $excludeId];
  86. }
  87. $article = Article::field($field)
  88. ->where($where)
  89. ->append(['click'])
  90. ->orderRaw($orderRaw)
  91. ->hidden(['click_actual', 'click_virtual']);
  92. if ($limit) {
  93. $article->limit($limit);
  94. }
  95. return $article->select()->toArray();
  96. }
  97. /**
  98. * @notes 获取配置
  99. * @return array
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @author 段誉
  104. * @date 2022/9/21 19:38
  105. */
  106. public static function getConfigData()
  107. {
  108. // 登录配置
  109. $loginConfig = [
  110. // 登录方式
  111. 'login_way' => ConfigService::get('login', 'login_way', config('project.login.login_way')),
  112. // 注册强制绑定手机
  113. 'coerce_mobile' => ConfigService::get('login', 'coerce_mobile', config('project.login.coerce_mobile')),
  114. // 政策协议
  115. 'login_agreement' => ConfigService::get('login', 'login_agreement', config('project.login.login_agreement')),
  116. // 第三方登录 开关
  117. 'third_auth' => ConfigService::get('login', 'third_auth', config('project.login.third_auth')),
  118. // 微信授权登录
  119. 'wechat_auth' => ConfigService::get('login', 'wechat_auth', config('project.login.wechat_auth')),
  120. // qq授权登录
  121. 'qq_auth' => ConfigService::get('login', 'qq_auth', config('project.login.qq_auth')),
  122. ];
  123. // 网站信息
  124. $website = [
  125. 'shop_name' => ConfigService::get('website', 'shop_name'),
  126. 'shop_logo' => FileService::getFileUrl(ConfigService::get('website', 'shop_logo')),
  127. 'pc_logo' => FileService::getFileUrl(ConfigService::get('website', 'pc_logo')),
  128. 'pc_title' => ConfigService::get('website', 'pc_title'),
  129. 'pc_ico' => FileService::getFileUrl(ConfigService::get('website', 'pc_ico')),
  130. 'pc_desc' => ConfigService::get('website', 'pc_desc'),
  131. 'pc_keywords' => ConfigService::get('website', 'pc_keywords'),
  132. ];
  133. // 站点统计
  134. $siteStatistics = [
  135. 'clarity_code' => ConfigService::get('siteStatistics', 'clarity_code'),
  136. ];
  137. // 备案信息
  138. $copyright = ConfigService::get('copyright', 'config', []);
  139. // 公众号二维码
  140. $oaQrCode = ConfigService::get('oa_setting', 'qr_code', '');
  141. $oaQrCode = empty($oaQrCode) ? $oaQrCode : FileService::getFileUrl($oaQrCode);
  142. // 小程序二维码
  143. $mnpQrCode = ConfigService::get('mnp_setting', 'qr_code', '');
  144. $mnpQrCode = empty($mnpQrCode) ? $mnpQrCode : FileService::getFileUrl($mnpQrCode);
  145. return [
  146. 'domain' => FileService::getFileUrl(),
  147. 'login' => $loginConfig,
  148. 'website' => $website,
  149. 'siteStatistics' => $siteStatistics,
  150. 'version' => config('project.version'),
  151. 'copyright' => $copyright,
  152. 'admin_url' => request()->domain() . '/admin',
  153. 'qrcode' => [
  154. 'oa' => $oaQrCode,
  155. 'mnp' => $mnpQrCode,
  156. ]
  157. ];
  158. }
  159. /**
  160. * @notes 资讯中心
  161. * @return array
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @author 段誉
  166. * @date 2022/10/19 16:55
  167. */
  168. public static function getInfoCenter()
  169. {
  170. $data = ArticleCate::field(['id', 'name'])
  171. ->with(['article' => function ($query) {
  172. $query->hidden(['content', 'click_virtual', 'click_actual'])
  173. ->order(['sort' => 'desc', 'id' => 'desc'])
  174. ->append(['click'])
  175. ->limit(10);
  176. }])
  177. ->where(['is_show' => YesNoEnum::YES])
  178. ->order(['sort' => 'desc', 'id' => 'desc'])
  179. ->select()
  180. ->toArray();
  181. return $data;
  182. }
  183. /**
  184. * @notes 获取文章详情
  185. * @param $userId
  186. * @param $articleId
  187. * @param string $source
  188. * @return array
  189. * @author 段誉
  190. * @date 2022/10/20 15:18
  191. */
  192. public static function getArticleDetail($userId, $articleId, $source = 'default')
  193. {
  194. // 文章详情
  195. $detail = Article::getArticleDetailArr($articleId);
  196. // 根据来源列表查找对应列表
  197. $nowIndex = 0;
  198. $lists = self::getLimitArticle($source, 0, $detail['cid']);
  199. foreach ($lists as $key => $item) {
  200. if ($item['id'] == $articleId) {
  201. $nowIndex = $key;
  202. }
  203. }
  204. // 上一篇
  205. $detail['last'] = $lists[$nowIndex - 1] ?? [];
  206. // 下一篇
  207. $detail['next'] = $lists[$nowIndex + 1] ?? [];
  208. // 最新资讯
  209. $detail['new'] = self::getLimitArticle('new', 8, $detail['cid'], $detail['id']);
  210. // 关注状态
  211. $detail['collect'] = ArticleCollect::isCollectArticle($userId, $articleId);
  212. // 分类名
  213. $detail['cate_name'] = ArticleCate::where('id', $detail['cid'])->value('name');
  214. return $detail;
  215. }
  216. }