ArticleServices.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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'] = $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(100006);
  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. return compact('info');
  103. }
  104. /**
  105. * 删除文章
  106. * @param int $id
  107. */
  108. public function del(int $id)
  109. {
  110. /** @var ArticleContentServices $articleContentService */
  111. $articleContentService = app()->make(ArticleContentServices::class);
  112. $this->transaction(function () use ($id, $articleContentService) {
  113. $res = $this->dao->delete($id);
  114. $res = $res && $articleContentService->del($id);
  115. if (!$res) {
  116. throw new AdminException(100008);
  117. }
  118. });
  119. }
  120. /**
  121. * 文章关联商品
  122. * @param int $id
  123. * @param int $product_id
  124. * @return mixed
  125. */
  126. public function bindProduct(int $id, int $product_id = 0)
  127. {
  128. return $this->dao->update($id, ['product_id' => $product_id]);
  129. }
  130. /**
  131. * 获取数量
  132. * @param array $where
  133. * @param bool $search
  134. * @return int
  135. */
  136. public function count(array $where = [], bool $search = true): int
  137. {
  138. return $this->search($where, $search)->count();
  139. }
  140. /**
  141. * 获取一条数据
  142. * @param int $id
  143. * @return mixed
  144. * @throws \ReflectionException
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\DbException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. */
  149. public function getInfo(int $id)
  150. {
  151. $info = $this->dao->read($id);
  152. $info->visit = intval($info['visit']) + 1;
  153. if (!$info->save())
  154. throw new AdminException(400456);
  155. if ($info) {
  156. $info = $info->toArray();
  157. $info['visit'] = (int)$info['visit'];
  158. $info['add_time'] = date('Y-m-d', $info['add_time']);
  159. }
  160. return $info;
  161. }
  162. /**
  163. * 获取文章列表
  164. * @param $new_id
  165. * @return int
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public function articleList($new_id)
  171. {
  172. return $this->dao->articleLists($new_id);
  173. }
  174. /**
  175. * 图文详情
  176. * @param $new_id
  177. * @return mixed
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public function articlesList($new_id)
  183. {
  184. return $this->dao->articleContentList($new_id);
  185. }
  186. }