ArticleServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. * 文章
  18. * Class ArticleServices
  19. * @package app\services\article
  20. * @mixin ArticleDao
  21. */
  22. class ArticleServices extends BaseServices
  23. {
  24. /**
  25. * ArticleServices constructor.
  26. * @param ArticleDao $dao
  27. */
  28. public function __construct(ArticleDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取列表
  34. * @param array $where
  35. * @return array
  36. */
  37. public function getList(array $where, int $page = 0, int $limit = 0)
  38. {
  39. if (!$limit) {
  40. [$page, $limit] = $this->getPageValue();
  41. }
  42. /** @var WechatNewsCategoryServices $services */
  43. $services = app()->make(WechatNewsCategoryServices::class);
  44. $where['ids'] = $services->getNewIds();
  45. $list = $this->dao->getList($where, $page, $limit);
  46. foreach ($list as &$item) {
  47. $item['store_name'] = $item['storeInfo']['store_name'] ?? '';
  48. }
  49. $count = $this->dao->count($where);
  50. return compact('list', 'count');
  51. }
  52. /**
  53. * 新增编辑文章
  54. * @param array $data
  55. */
  56. public function save(array $data)
  57. {
  58. /** @var ArticleContentServices $articleContentService */
  59. $articleContentService = app()->make(ArticleContentServices::class);
  60. $content['content'] = $data['content'];
  61. $id = $data['id'];
  62. unset($data['content'], $data['id']);
  63. $info = $this->transaction(function () use ($id, $data, $articleContentService, $content) {
  64. if ($id) {
  65. $info = $this->dao->update($id, $data);
  66. $content['nid'] = $id;
  67. $res = $info && $articleContentService->update($id, $content, 'nid');
  68. } else {
  69. unset($data['id']);
  70. $data['add_time'] = time();
  71. $info = $this->dao->save($data);
  72. $content['nid'] = $info->id;
  73. $res = $info && $articleContentService->save($content);
  74. }
  75. if (!$res) {
  76. throw new AdminException('保存失败');
  77. } else {
  78. return $info;
  79. }
  80. });
  81. return $info;
  82. }
  83. /**
  84. * 获取商品详情
  85. * @param $id
  86. * @return array
  87. */
  88. public function read(int $id)
  89. {
  90. $info = $this->dao->read($id);
  91. $info['cid'] = (int)$info['cid'];
  92. return compact('info');
  93. }
  94. /**
  95. * 删除商品
  96. * @param int $id
  97. */
  98. public function del(int $id)
  99. {
  100. /** @var ArticleContentServices $articleContentService */
  101. $articleContentService = app()->make(ArticleContentServices::class);
  102. $this->transaction(function () use ($id, $articleContentService) {
  103. $res = $this->dao->delete($id);
  104. $res = $res && $articleContentService->del($id);
  105. if (!$res) {
  106. throw new AdminException('删除失败');
  107. }
  108. });
  109. }
  110. /**
  111. * 文章关联商品
  112. * @param int $id
  113. * @param int $product_id
  114. * @return mixed
  115. */
  116. public function bindProduct(int $id, int $product_id = 0)
  117. {
  118. return $this->dao->update($id, ['product_id' => $product_id]);
  119. }
  120. /**
  121. * 获取数量
  122. * @param array $where
  123. * @return int
  124. */
  125. public function count(array $where)
  126. {
  127. return $this->dao->count($where);
  128. }
  129. /**
  130. * 获取一条数据
  131. * @param int $id
  132. * @return mixed
  133. */
  134. public function getInfo(int $id)
  135. {
  136. $info = $this->dao->read($id);
  137. if (!$info) {
  138. throw new AdminException('文章不存在或已删除');
  139. }
  140. $info->visit = ($info->visit ?? 0) + 1;
  141. if (!$info->save())
  142. throw new AdminException('请稍后查看');
  143. if ($info) {
  144. $info = $info->toArray();
  145. $info['visit'] = (int)$info['visit'];
  146. $info['add_time'] = date('Y-m-d', $info['add_time']);
  147. }
  148. return $info;
  149. }
  150. /**
  151. * 获取文章列表
  152. * @param $new_id
  153. * @return int
  154. */
  155. public function articleList($new_id)
  156. {
  157. return $this->dao->articleLists($new_id);
  158. }
  159. /**图文详情
  160. * @param $new_id
  161. * @return mixed
  162. */
  163. public function articlesList($new_id)
  164. {
  165. return $this->dao->articleContentList($new_id);
  166. }
  167. }