123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\wechat;
- use app\services\BaseServices;
- use app\dao\wechat\WechatNewsCategoryDao;
- use app\services\article\ArticleServices;
- use crmeb\services\wechat\Messages;
- use crmeb\services\wechat\OfficialAccount;
- use think\facade\Log;
- /**
- *
- * Class UserWechatuserServices
- * @package app\services\user
- * @mixin WechatNewsCategoryDao
- */
- class WechatNewsCategoryServices extends BaseServices
- {
- /**
- * UserWechatuserServices constructor.
- * @param WechatNewsCategoryDao $dao
- */
- public function __construct(WechatNewsCategoryDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 获取配置分类
- * @param array $where
- * @return array
- */
- public function getAll($where = array())
- {
- [$page, $limit] = $this->getPageValue();
- $model = $this->dao->getNewCtae($where);
- $count = $model->count();
- $list = $model->page($page, $limit)
- ->select()
- ->each(function ($item) {
- /** @var ArticleServices $services */
- $services = app()->make(ArticleServices::class);
- $new = $services->articleList($item['new_id']);
- if ($new) $new = $new->toArray();
- $item['new'] = $new;
- });
- return compact('count', 'list');
- }
- /**
- * 获取一条图文
- * @param int $id
- * @return array|false|\PDOStatement|string|\think\Model
- */
- public function getWechatNewsItem($id = 0)
- {
- if (!$id) return [];
- $list = $this->dao->getOne(['id' => $id, 'status' => 1], 'cate_name as title,new_id');
- if ($list) {
- $list = $list->toArray();
- /** @var ArticleServices $services */
- $services = app()->make(ArticleServices::class);
- $new = $services->articleList($list['new_id']);
- if ($new) $new = $new->toArray();
- $list['new'] = $new;
- }
- return $list;
- }
- /**
- * 发送客服消息选择文章列表
- * @param $where
- * @return array
- */
- public function list($where)
- {
- $list = $this->dao->getNewCtae($where)
- ->page((int)$where['page'], (int)$where['limit'])
- ->select()
- ->each(function ($item) {
- /** @var ArticleServices $services */
- $services = app()->make(ArticleServices::class);
- $item['new'] = $services->articleList($item['new_id']);
- });
- return ['list' => $list];
- }
- /**整理图文资源
- * @param $wechatNews
- * @return bool
- */
- public function wechatPush($wechatNews)
- {
- /** @var WechatReplyServices $services */
- $services = app()->make(WechatReplyServices::class);
- return $services->tidyNews($wechatNews);
- }
- /**发送的用户
- * @param $user_ids
- * @param $column
- * @param $key
- * @return array
- */
- public function getWechatUser($user_ids, $column, $key)
- {
- /** @var WechatUserServices $services */
- $services = app()->make(WechatUserServices::class);
- return $services->getColumnUser($user_ids, $column, $key);
- }
- /**
- * 获取文章id
- * @return array
- */
- public function getNewIds()
- {
- return $this->dao->getColumn([], 'new_id');
- }
- /**
- * 执行发送
- * @param int $uids
- * @param array $message
- * @return bool
- */
- public function runPush(int $uid, array $message)
- {
- if (!$uid || !$message) {
- return true;
- }
- $message = Messages::newsMessage($message);
- $user = $this->getWechatUser([$uid], 'nickname,subscribe,openid', 'uid');
- if ($user) {
- foreach ($user as $v) {
- if ($v['subscribe'] && $v['openid']) {
- try {
- OfficialAccount::staffService()->message($message)->to($v['openid'])->send();
- } catch (\Exception $e) {
- Log::error($v['nickname'] . '发送失败,原因:' . $e->getMessage());
- }
- }
- }
- }
- return true;
- }
- }
|