2020-06-21 13:23:22 +08:00
|
|
|
<?php
|
|
|
|
/**
|
2020-06-21 14:41:22 +08:00
|
|
|
* @filename WechatUsersRepository.php
|
2020-06-21 13:23:22 +08:00
|
|
|
* @createdAt 2020/6/21
|
|
|
|
* @project https://github.com/yanwenwu/catch-admin
|
|
|
|
* @document http://doc.catchadmin.com
|
|
|
|
* @author JaguarJack <njphper@gmail.com>
|
|
|
|
* @copyright By CatchAdmin
|
|
|
|
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
|
|
|
|
*/
|
|
|
|
namespace catchAdmin\wechat\repository;
|
|
|
|
|
2020-06-22 07:55:16 +08:00
|
|
|
use catchAdmin\wechat\model\WechatTags;
|
2020-06-21 13:23:22 +08:00
|
|
|
use catchAdmin\wechat\model\WechatUsers;
|
|
|
|
use catcher\base\CatchRepository;
|
2020-06-21 14:41:22 +08:00
|
|
|
use catcher\library\WeChat;
|
2020-06-22 07:55:16 +08:00
|
|
|
use catcher\Utils;
|
2020-06-21 13:23:22 +08:00
|
|
|
|
|
|
|
class WechatUsersRepository extends CatchRepository
|
|
|
|
{
|
2020-06-21 14:41:22 +08:00
|
|
|
protected $wechatUser;
|
2020-06-21 13:23:22 +08:00
|
|
|
|
|
|
|
public function __construct(WechatUsers $users)
|
|
|
|
{
|
2020-06-21 14:41:22 +08:00
|
|
|
$this->wechatUser = $users;
|
2020-06-21 13:23:22 +08:00
|
|
|
}
|
|
|
|
|
2020-06-21 14:41:22 +08:00
|
|
|
/**
|
|
|
|
* 模型
|
|
|
|
*
|
|
|
|
* @time 2020年06月21日
|
|
|
|
* @return WechatUsers
|
|
|
|
*/
|
2020-06-21 13:23:22 +08:00
|
|
|
protected function model()
|
|
|
|
{
|
2020-06-21 14:41:22 +08:00
|
|
|
return $this->wechatUser;
|
|
|
|
}
|
|
|
|
|
2020-06-22 07:55:16 +08:00
|
|
|
/**
|
|
|
|
* 获取列表
|
|
|
|
*
|
|
|
|
* @time 2020年06月21日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getList()
|
|
|
|
{
|
|
|
|
return $this->wechatUser
|
|
|
|
->catchSearch()
|
|
|
|
->field('*')
|
|
|
|
->tags()
|
|
|
|
->catchOrder()
|
|
|
|
->paginate();
|
|
|
|
}
|
|
|
|
|
2020-06-21 14:41:22 +08:00
|
|
|
/**
|
|
|
|
* 拉黑用户
|
|
|
|
*
|
|
|
|
* @time 2020年06月21日
|
|
|
|
* @param $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function block($id)
|
|
|
|
{
|
|
|
|
$user = $this->wechatUser->findBy($id);
|
|
|
|
|
|
|
|
$blockMethod = $user->block == WechatUsers::UNBLOCK ? 'block' : 'unblock';
|
|
|
|
|
|
|
|
WeChat::throw(WeChat::officialAccount()->user->{$blockMethod}([$user->openid]));
|
|
|
|
|
|
|
|
$user->block = $user->block == WechatUsers::BlOCK ? WechatUsers::UNBLOCK : WechatUsers::BlOCK;
|
|
|
|
|
|
|
|
return $user->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 粉丝备注
|
|
|
|
*
|
|
|
|
* @time 2020年06月21日
|
|
|
|
* @param $id
|
|
|
|
* @param string $remark
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function remark($id, string $remark)
|
|
|
|
{
|
|
|
|
$user = $this->wechatUser->findBy($id);
|
|
|
|
|
|
|
|
WeChat::throw(WeChat::officialAccount()->user->remark($user->openid, $remark));
|
|
|
|
|
|
|
|
$user->remark = $remark;
|
2020-06-21 13:23:22 +08:00
|
|
|
|
2020-06-21 14:41:22 +08:00
|
|
|
return $user->save();
|
2020-06-21 13:23:22 +08:00
|
|
|
}
|
2020-06-22 07:55:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 给用户打标签
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2020-06-21 13:23:22 +08:00
|
|
|
}
|