WechatNewsCategoryServices.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. declare (strict_types=1);
  12. namespace app\services\wechat;
  13. use app\services\BaseServices;
  14. use app\dao\wechat\WechatNewsCategoryDao;
  15. use app\services\article\ArticleServices;
  16. use crmeb\services\wechat\Messages;
  17. use crmeb\services\wechat\OfficialAccount;
  18. use think\facade\Log;
  19. /**
  20. *
  21. * Class UserWechatuserServices
  22. * @package app\services\user
  23. * @mixin WechatNewsCategoryDao
  24. */
  25. class WechatNewsCategoryServices extends BaseServices
  26. {
  27. /**
  28. * UserWechatuserServices constructor.
  29. * @param WechatNewsCategoryDao $dao
  30. */
  31. public function __construct(WechatNewsCategoryDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取配置分类
  37. * @param array $where
  38. * @return array
  39. */
  40. public function getAll($where = array())
  41. {
  42. [$page, $limit] = $this->getPageValue();
  43. $model = $this->dao->getNewCtae($where);
  44. $count = $model->count();
  45. $list = $model->page($page, $limit)
  46. ->select()
  47. ->each(function ($item) {
  48. /** @var ArticleServices $services */
  49. $services = app()->make(ArticleServices::class);
  50. $new = $services->articleList($item['new_id']);
  51. if ($new) $new = $new->toArray();
  52. $item['new'] = $new;
  53. });
  54. return compact('count', 'list');
  55. }
  56. /**
  57. * 获取一条图文
  58. * @param int $id
  59. * @return array|false|\PDOStatement|string|\think\Model
  60. */
  61. public function getWechatNewsItem($id = 0)
  62. {
  63. if (!$id) return [];
  64. $list = $this->dao->getOne(['id' => $id, 'status' => 1], 'cate_name as title,new_id');
  65. if ($list) {
  66. $list = $list->toArray();
  67. /** @var ArticleServices $services */
  68. $services = app()->make(ArticleServices::class);
  69. $new = $services->articleList($list['new_id']);
  70. if ($new) $new = $new->toArray();
  71. $list['new'] = $new;
  72. }
  73. return $list;
  74. }
  75. /**
  76. * 发送客服消息选择文章列表
  77. * @param $where
  78. * @return array
  79. */
  80. public function list($where)
  81. {
  82. $list = $this->dao->getNewCtae($where)
  83. ->page((int)$where['page'], (int)$where['limit'])
  84. ->select()
  85. ->each(function ($item) {
  86. /** @var ArticleServices $services */
  87. $services = app()->make(ArticleServices::class);
  88. $item['new'] = $services->articleList($item['new_id']);
  89. });
  90. return ['list' => $list];
  91. }
  92. /**整理图文资源
  93. * @param $wechatNews
  94. * @return bool
  95. */
  96. public function wechatPush($wechatNews)
  97. {
  98. /** @var WechatReplyServices $services */
  99. $services = app()->make(WechatReplyServices::class);
  100. return $services->tidyNews($wechatNews);
  101. }
  102. /**发送的用户
  103. * @param $user_ids
  104. * @param $column
  105. * @param $key
  106. * @return array
  107. */
  108. public function getWechatUser($user_ids, $column, $key)
  109. {
  110. /** @var WechatUserServices $services */
  111. $services = app()->make(WechatUserServices::class);
  112. return $services->getColumnUser($user_ids, $column, $key);
  113. }
  114. /**
  115. * 获取文章id
  116. * @return array
  117. */
  118. public function getNewIds()
  119. {
  120. return $this->dao->getColumn([], 'new_id');
  121. }
  122. /**
  123. * 执行发送
  124. * @param int $uids
  125. * @param array $message
  126. * @return bool
  127. */
  128. public function runPush(int $uid, array $message)
  129. {
  130. if (!$uid || !$message) {
  131. return true;
  132. }
  133. $message = Messages::newsMessage($message);
  134. $user = $this->getWechatUser([$uid], 'nickname,subscribe,openid', 'uid');
  135. if ($user) {
  136. foreach ($user as $v) {
  137. if ($v['subscribe'] && $v['openid']) {
  138. try {
  139. OfficialAccount::staffService()->message($message)->to($v['openid'])->send();
  140. } catch (\Exception $e) {
  141. Log::error($v['nickname'] . '发送失败,原因:' . $e->getMessage());
  142. }
  143. }
  144. }
  145. }
  146. return true;
  147. }
  148. }