完善微信粉丝和标签

This commit is contained in:
JaguarJack
2020-06-22 07:55:16 +08:00
parent 317a9ef595
commit a3dd8282b3
7 changed files with 120 additions and 9 deletions

View File

@@ -36,6 +36,25 @@ class WechatTagsRepository extends CatchRepository
return $this->wechatTag;
}
/**
* 获取列表
*
* @time 2020年06月21日
* @param array $params
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return mixed
*/
public function getList($params)
{
if (isset($params['all'])) {
return $this->wechatTag->select();
}
return parent::getList(); // TODO: Change the autogenerated stub
}
/**
* 同步微信标签
*
@@ -49,7 +68,7 @@ class WechatTagsRepository extends CatchRepository
$wechatTags = WeChat::officialAccount()->user_tag->list()['tags'];
$_tags = [];
foreach ($wechatTags as $key => &$tag) {
foreach ($wechatTags as $key => $tag) {
if (in_array($tag['name'], $tags)) {
continue;
}

View File

@@ -10,9 +10,11 @@
*/
namespace catchAdmin\wechat\repository;
use catchAdmin\wechat\model\WechatTags;
use catchAdmin\wechat\model\WechatUsers;
use catcher\base\CatchRepository;
use catcher\library\WeChat;
use catcher\Utils;
class WechatUsersRepository extends CatchRepository
{
@@ -34,6 +36,22 @@ class WechatUsersRepository extends CatchRepository
return $this->wechatUser;
}
/**
* 获取列表
*
* @time 2020年06月21日
* @return mixed
*/
public function getList()
{
return $this->wechatUser
->catchSearch()
->field('*')
->tags()
->catchOrder()
->paginate();
}
/**
* 拉黑用户
*
@@ -72,4 +90,50 @@ class WechatUsersRepository extends CatchRepository
return $user->save();
}
/**
* 给用户打标签
*
* @time 2020年06月21日
* @param $id
* @param $data
* @return mixed
*/
public function tag($id, $data)
{
$tagIds = WechatTags::whereIn('name', Utils::stringToArrayBy($data['tag']))->column('tag_id');
$user = $this->findBy($id);
$hasTagIds = $user->hasTags()->select()->column('tag_id');
// 已存在的标签
$existedTagIds = [];
foreach ($tagIds as $tagId) {
if (in_array($tagId, $hasTagIds)) {
$existedTagIds[] = $tagId;
}
}
$detachIds = array_diff($hasTagIds, $existedTagIds);
$attachIds = array_diff($tagIds, $existedTagIds);
$officialUserTag = WeChat::officialAccount()->user_tag;
// 删除标签
if (!empty($detachIds)) {
foreach ($detachIds as $detachId) {
$officialUserTag->untagUsers([$user->openid], $detachId);
}
$user->hasTags()->detach($detachIds);
}
// 新增标签
if (!empty($attachIds)) {
foreach ($attachIds as $attachId) {
$officialUserTag->tagUsers([$user->openid], $attachId);
}
$user->hasTags()->saveAll($attachIds);
}
return true;
}
}