ArticleServices.php 4.9 KB

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