ArticleServices.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\article;
  12. use app\dao\article\ArticleDao;
  13. use app\services\BaseServices;
  14. use app\services\wechat\WechatNewsCategoryServices;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. * Class ArticleServices
  18. * @package app\services\article
  19. */
  20. class ArticleServices extends BaseServices
  21. {
  22. /**
  23. * ArticleServices constructor.
  24. * @param ArticleDao $dao
  25. */
  26. public function __construct(ArticleDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 获取列表
  32. * @param array $where
  33. * @param int $page
  34. * @param int $limit
  35. * @return array
  36. * @throws \ReflectionException
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getList(array $where, int $page = 0, int $limit = 0)
  42. {
  43. if (!$page && !$limit) {
  44. [$page, $limit] = $this->getPageValue();
  45. }
  46. $where['ids'] = app()->make(WechatNewsCategoryServices::class)->getNewIds();
  47. $list = $this->dao->getList($where, $page, $limit);
  48. foreach ($list as &$item) {
  49. $item['store_name'] = $item['storeInfo']['store_name'] ?? '';
  50. $item['copy_url'] = sys_config('site_url') . '/pages/extension/news_details/index?id=' . $item['id'];
  51. $item['copy_url_pc'] = sys_config('site_url') . '/news_detail?id=' . $item['id'];
  52. unset($item['content']);
  53. }
  54. $count = $this->dao->count($where);
  55. return compact('list', 'count');
  56. }
  57. /**
  58. * 新增编辑文章
  59. * @param array $data
  60. * @return mixed
  61. */
  62. public function save(array $data)
  63. {
  64. /** @var ArticleContentServices $articleContentService */
  65. $articleContentService = app()->make(ArticleContentServices::class);
  66. $content['content'] = htmlspecialchars($data['content']);
  67. $id = $data['id'];
  68. unset($data['content'], $data['id']);
  69. $info = $this->transaction(function () use ($id, $data, $articleContentService, $content) {
  70. if ($id) {
  71. $info = $this->dao->update($id, $data);
  72. $content['nid'] = $id;
  73. $res = $info && $articleContentService->update($id, $content, 'nid');
  74. } else {
  75. unset($data['id']);
  76. $data['add_time'] = time();
  77. $info = $this->dao->save($data);
  78. $content['nid'] = $info->id;
  79. $res = $info && $articleContentService->save($content);
  80. }
  81. if (!$res) {
  82. throw new AdminException('保存失败');
  83. } else {
  84. return $info;
  85. }
  86. });
  87. return $info;
  88. }
  89. /**
  90. * 获取文章详情
  91. * @param int $id
  92. * @return array
  93. * @throws \ReflectionException
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function read(int $id)
  99. {
  100. $info = $this->dao->read($id);
  101. $info['cid'] = (int)$info['cid'];
  102. $info['content'] = htmlspecialchars_decode($info['content']);
  103. return compact('info');
  104. }
  105. /**
  106. * 删除文章
  107. * @param int $id
  108. */
  109. public function del(int $id)
  110. {
  111. /** @var ArticleContentServices $articleContentService */
  112. $articleContentService = app()->make(ArticleContentServices::class);
  113. $this->transaction(function () use ($id, $articleContentService) {
  114. $res = $this->dao->delete($id);
  115. $res = $res && $articleContentService->del($id);
  116. if (!$res) {
  117. throw new AdminException('删除失败');
  118. }
  119. });
  120. }
  121. /**
  122. * 文章关联商品
  123. * @param int $id
  124. * @param int $product_id
  125. * @return mixed
  126. */
  127. public function bindProduct(int $id, int $product_id = 0)
  128. {
  129. return $this->dao->update($id, ['product_id' => $product_id]);
  130. }
  131. /**
  132. * 获取数量
  133. * @param array $where
  134. * @param bool $search
  135. * @return int
  136. */
  137. public function count(array $where = [], bool $search = true): int
  138. {
  139. return $this->search($where, $search)->count();
  140. }
  141. /**
  142. * 获取一条数据
  143. * @param int $id
  144. * @return mixed
  145. * @throws \ReflectionException
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. */
  150. public function getInfo(int $id)
  151. {
  152. $info = $this->dao->read($id);
  153. $info->visit = intval($info['visit']) + 1;
  154. if (!$info->save())
  155. throw new AdminException('请稍后查看');
  156. if ($info) {
  157. $info = $info->toArray();
  158. $info['visit'] = (int)$info['visit'];
  159. $info['add_time'] = date('Y-m-d', $info['add_time']);
  160. $info['content'] = htmlspecialchars_decode($info['content']);
  161. }
  162. return $info;
  163. }
  164. /**
  165. * 获取文章列表
  166. * @param $new_id
  167. * @return int
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\DbException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. */
  172. public function articleList($new_id)
  173. {
  174. return $this->dao->articleLists($new_id);
  175. }
  176. /**
  177. * 图文详情
  178. * @param $new_id
  179. * @return mixed
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function articlesList($new_id)
  185. {
  186. return $this->dao->articleContentList($new_id);
  187. }
  188. /**
  189. * 自定义组件-文章
  190. * @param $where
  191. * @return array
  192. * @throws \ReflectionException
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. * @author wuhaotian
  197. * @email 442384644@qq.com
  198. * @date 2026/1/12
  199. */
  200. public function getThemeArticle($where)
  201. {
  202. $sort = $where['sort'] ? 'desc' : 'asc';
  203. switch ($where['order']) {
  204. case 0:
  205. $order = 'visit ' . $sort;
  206. break;
  207. case 1:
  208. $order = 'add_time ' . $sort;
  209. break;
  210. default:
  211. $order = 'sort desc';
  212. break;
  213. }
  214. if ($where['ids'] != '') {
  215. $where['in_ids'] = explode(',', $where['ids']);
  216. $where['limit'] = 1000;
  217. } else {
  218. $where['in_ids'] = [];
  219. }
  220. $limit = (int)$where['limit'];
  221. unset($where['order'], $where['sort'], $where['limit'], $where['ids']);
  222. $list = $this->dao->getList($where, 1, $limit, $order);
  223. $data = [];
  224. foreach ($list as &$item) {
  225. $data[] = [
  226. 'title' => $item['title'],
  227. 'id' => $item['id'],
  228. 'image' => $item['image_input'][0],
  229. 'cid_name' => $item['catename'],
  230. 'synopsis' => $item['synopsis'],
  231. 'visit' => $item['visit'],
  232. 'add_time' => date('Y-m-d H:i:s', $item['add_time']),
  233. ];
  234. }
  235. if (empty($where['in_ids'])) return $data;
  236. // 将$list转换为以id为键的数组
  237. $list = array_column($data, null, 'id');
  238. $data = [];
  239. // 遍历where中的ids,按顺序取出对应文章
  240. foreach ($where['in_ids'] as $id) {
  241. if (isset($list[$id])) {
  242. $data[] = $list[$id];
  243. }
  244. }
  245. return $data;
  246. }
  247. }