ArticleServices.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. }
  53. $count = $this->dao->count($where);
  54. return compact('list', 'count');
  55. }
  56. /**
  57. * 新增编辑文章
  58. * @param array $data
  59. * @return mixed
  60. */
  61. public function save(array $data)
  62. {
  63. /** @var ArticleContentServices $articleContentService */
  64. $articleContentService = app()->make(ArticleContentServices::class);
  65. $content['content'] = $data['content'];
  66. $id = $data['id'];
  67. unset($data['content'], $data['id']);
  68. $info = $this->transaction(function () use ($id, $data, $articleContentService, $content) {
  69. if ($id) {
  70. $info = $this->dao->update($id, $data);
  71. $content['nid'] = $id;
  72. $res = $info && $articleContentService->update($id, $content, 'nid');
  73. } else {
  74. unset($data['id']);
  75. $data['add_time'] = time();
  76. $info = $this->dao->save($data);
  77. $content['nid'] = $info->id;
  78. $res = $info && $articleContentService->save($content);
  79. }
  80. if (!$res) {
  81. throw new AdminException(100006);
  82. } else {
  83. return $info;
  84. }
  85. });
  86. return $info;
  87. }
  88. /**
  89. * 获取文章详情
  90. * @param int $id
  91. * @return array
  92. * @throws \ReflectionException
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function read(int $id)
  98. {
  99. $info = $this->dao->read($id);
  100. $info['cid'] = (int)$info['cid'];
  101. return compact('info');
  102. }
  103. /**
  104. * 删除文章
  105. * @param int $id
  106. */
  107. public function del(int $id)
  108. {
  109. /** @var ArticleContentServices $articleContentService */
  110. $articleContentService = app()->make(ArticleContentServices::class);
  111. $this->transaction(function () use ($id, $articleContentService) {
  112. $res = $this->dao->delete($id);
  113. $res = $res && $articleContentService->del($id);
  114. if (!$res) {
  115. throw new AdminException(100008);
  116. }
  117. });
  118. }
  119. /**
  120. * 文章关联商品
  121. * @param int $id
  122. * @param int $product_id
  123. * @return mixed
  124. */
  125. public function bindProduct(int $id, int $product_id = 0)
  126. {
  127. return $this->dao->update($id, ['product_id' => $product_id]);
  128. }
  129. /**
  130. * 获取数量
  131. * @param array $where
  132. * @param bool $search
  133. * @return int
  134. */
  135. public function count(array $where = [], bool $search = true): int
  136. {
  137. return $this->search($where, $search)->count();
  138. }
  139. /**
  140. * 获取一条数据
  141. * @param int $id
  142. * @return mixed
  143. * @throws \ReflectionException
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function getInfo(int $id)
  149. {
  150. $info = $this->dao->read($id);
  151. $info->visit = intval($info['visit']) + 1;
  152. if (!$info->save())
  153. throw new AdminException(400456);
  154. if ($info) {
  155. $info = $info->toArray();
  156. $info['visit'] = (int)$info['visit'];
  157. $info['add_time'] = date('Y-m-d', $info['add_time']);
  158. }
  159. return $info;
  160. }
  161. /**
  162. * 获取文章列表
  163. * @param $new_id
  164. * @return int
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function articleList($new_id)
  170. {
  171. return $this->dao->articleLists($new_id);
  172. }
  173. /**
  174. * 图文详情
  175. * @param $new_id
  176. * @return mixed
  177. * @throws \think\db\exception\DataNotFoundException
  178. * @throws \think\db\exception\DbException
  179. * @throws \think\db\exception\ModelNotFoundException
  180. */
  181. public function articlesList($new_id)
  182. {
  183. return $this->dao->articleContentList($new_id);
  184. }
  185. }