完善微信粉丝和标签
This commit is contained in:
parent
317a9ef595
commit
a3dd8282b3
@ -30,11 +30,19 @@ class Tags extends CatchController
|
|||||||
*
|
*
|
||||||
* @time 2020/06/21 14:45
|
* @time 2020/06/21 14:45
|
||||||
*
|
*
|
||||||
|
* @param Request $request
|
||||||
* @return \think\Response
|
* @return \think\Response
|
||||||
|
* @throws \think\db\exception\DbException
|
||||||
|
* @throws \think\db\exception\ModelNotFoundException
|
||||||
|
* @throws \think\db\exception\DataNotFoundException
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
return CatchResponse::paginate($this->repository->getList());
|
if ($request->has('all')) {
|
||||||
|
return CatchResponse::success($this->repository->getList($request->param()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return CatchResponse::paginate($this->repository->getList($request->param()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,8 +13,10 @@ namespace catchAdmin\wechat\controller;
|
|||||||
use catchAdmin\wechat\repository\WechatUsersRepository;
|
use catchAdmin\wechat\repository\WechatUsersRepository;
|
||||||
use catcher\base\CatchController;
|
use catcher\base\CatchController;
|
||||||
use catcher\CatchResponse;
|
use catcher\CatchResponse;
|
||||||
|
use catcher\library\WeChat;
|
||||||
use catcher\Utils;
|
use catcher\Utils;
|
||||||
use think\facade\Console;
|
use think\facade\Console;
|
||||||
|
use think\Request;
|
||||||
|
|
||||||
class Users extends CatchController
|
class Users extends CatchController
|
||||||
{
|
{
|
||||||
@ -61,6 +63,11 @@ class Users extends CatchController
|
|||||||
return CatchResponse::success($this->user->block($id));
|
return CatchResponse::success($this->user->block($id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function tag($id, Request $request)
|
||||||
|
{
|
||||||
|
return CatchResponse::success($this->user->tag($id, $request->post()));
|
||||||
|
}
|
||||||
|
|
||||||
public function sync()
|
public function sync()
|
||||||
{
|
{
|
||||||
Console::call('sync:users');
|
Console::call('sync:users');
|
||||||
|
@ -18,6 +18,8 @@ class WechatTags extends Model
|
|||||||
{
|
{
|
||||||
use TagSearchTrait;
|
use TagSearchTrait;
|
||||||
|
|
||||||
|
protected $pk = 'tag_id';
|
||||||
|
|
||||||
protected $name = 'wechat_tags';
|
protected $name = 'wechat_tags';
|
||||||
|
|
||||||
protected $field = [
|
protected $field = [
|
||||||
@ -30,8 +32,7 @@ class WechatTags extends Model
|
|||||||
'deleted_at', // 软删除
|
'deleted_at', // 软删除
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function hasUsers()
|
||||||
public function hasManyUsers()
|
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(WechatUsers::class, 'wechat_user_has_tags', 'user_id', 'tag_id');
|
return $this->belongsToMany(WechatUsers::class, 'wechat_user_has_tags', 'user_id', 'tag_id');
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ namespace catchAdmin\wechat\model;
|
|||||||
|
|
||||||
use catchAdmin\wechat\model\search\UserSearchTrait;
|
use catchAdmin\wechat\model\search\UserSearchTrait;
|
||||||
use catcher\base\CatchModel;
|
use catcher\base\CatchModel;
|
||||||
use catcher\traits\db\BaseOptionsTrait;
|
use think\facade\Db;
|
||||||
|
|
||||||
class WechatUsers extends CatchModel
|
class WechatUsers extends CatchModel
|
||||||
{
|
{
|
||||||
@ -47,8 +47,19 @@ class WechatUsers extends CatchModel
|
|||||||
const BlOCK = 2; // 拉黑
|
const BlOCK = 2; // 拉黑
|
||||||
const UNBLOCK = 1; // 取消拉黑
|
const UNBLOCK = 1; // 取消拉黑
|
||||||
|
|
||||||
public function hasManyTags()
|
public function hasTags()
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(WechatTags::class, 'wechat_user_has_tags', 'tag_id', 'user_id');
|
return $this->belongsToMany(WechatTags::class, 'wechat_user_has_tags', 'tag_id', 'user_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function scopeTags($query)
|
||||||
|
{
|
||||||
|
return $query->addSelectSub(function () {
|
||||||
|
return Db::name('wechat_user_has_tags')
|
||||||
|
->whereColumn('wechat_user_has_tags.user_id', $this->aliasField('id'))
|
||||||
|
->leftJoin('wechat_tags','wechat_user_has_tags.tag_id=wechat_tags.tag_id')
|
||||||
|
->field(Db::raw('group_concat(`wechat_tags`.name)'));
|
||||||
|
}, 'tags');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -36,6 +36,25 @@ class WechatTagsRepository extends CatchRepository
|
|||||||
return $this->wechatTag;
|
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'];
|
$wechatTags = WeChat::officialAccount()->user_tag->list()['tags'];
|
||||||
|
|
||||||
$_tags = [];
|
$_tags = [];
|
||||||
foreach ($wechatTags as $key => &$tag) {
|
foreach ($wechatTags as $key => $tag) {
|
||||||
if (in_array($tag['name'], $tags)) {
|
if (in_array($tag['name'], $tags)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,11 @@
|
|||||||
*/
|
*/
|
||||||
namespace catchAdmin\wechat\repository;
|
namespace catchAdmin\wechat\repository;
|
||||||
|
|
||||||
|
use catchAdmin\wechat\model\WechatTags;
|
||||||
use catchAdmin\wechat\model\WechatUsers;
|
use catchAdmin\wechat\model\WechatUsers;
|
||||||
use catcher\base\CatchRepository;
|
use catcher\base\CatchRepository;
|
||||||
use catcher\library\WeChat;
|
use catcher\library\WeChat;
|
||||||
|
use catcher\Utils;
|
||||||
|
|
||||||
class WechatUsersRepository extends CatchRepository
|
class WechatUsersRepository extends CatchRepository
|
||||||
{
|
{
|
||||||
@ -34,6 +36,22 @@ class WechatUsersRepository extends CatchRepository
|
|||||||
return $this->wechatUser;
|
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();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -15,6 +15,7 @@ $router->group('wechat', function () use ($router){
|
|||||||
$router->get('', '\catchAdmin\wechat\controller\Users@index');
|
$router->get('', '\catchAdmin\wechat\controller\Users@index');
|
||||||
$router->put('remark/<id>/<remark>', '\catchAdmin\wechat\controller\Users@remark');
|
$router->put('remark/<id>/<remark>', '\catchAdmin\wechat\controller\Users@remark');
|
||||||
$router->put('block/<id>', '\catchAdmin\wechat\controller\Users@block');
|
$router->put('block/<id>', '\catchAdmin\wechat\controller\Users@block');
|
||||||
|
$router->put('tag/<id>', '\catchAdmin\wechat\controller\Users@tag');
|
||||||
$router->get('sync', '\catchAdmin\wechat\controller\Users@sync');
|
$router->get('sync', '\catchAdmin\wechat\controller\Users@sync');
|
||||||
});
|
});
|
||||||
// 粉丝标签
|
// 粉丝标签
|
||||||
|
Loading…
x
Reference in New Issue
Block a user