微信标签管理

This commit is contained in:
JaguarJack 2020-06-21 18:04:30 +08:00
parent 4392f25e97
commit fbf9c11191
10 changed files with 400 additions and 11 deletions

View File

@ -12,13 +12,16 @@
namespace catchAdmin\wechat\command; namespace catchAdmin\wechat\command;
use catchAdmin\wechat\model\WechatUsers; use catchAdmin\wechat\model\WechatUsers;
use catcher\exceptions\FailedException;
use catcher\facade\Trie; use catcher\facade\Trie;
use catcher\library\ProgressBar; use catcher\library\ProgressBar;
use catcher\library\WeChat; use catcher\library\WeChat;
use catcher\Utils;
use think\Collection; use think\Collection;
use think\console\Command; use think\console\Command;
use think\console\Input; use think\console\Input;
use think\console\Output; use think\console\Output;
use think\Db;
class SyncUsersCommand extends Command class SyncUsersCommand extends Command
{ {
@ -34,9 +37,37 @@ class SyncUsersCommand extends Command
{ {
$this->officialAccount = WeChat::officialAccount(); $this->officialAccount = WeChat::officialAccount();
$this->sync(null); $latest = WechatUsers::order('subscribe_time')->find();
if ($latest) {
throw new FailedException('暂时无法增量同步');
} }
$this->sync($latest ? $latest->openid : null);
$this->syncTags();
}
protected function syncTags()
{
$users = WechatUsers::cursor();
foreach ($users as $user) {
if ($user->tag_list) {
$tagIds = Utils::stringToArrayBy($user->tag_list);
$relate = [];
foreach ($tagIds as $id) {
$relate[] = [
'user_id' => $user->id,
'tag_id' => $id,
];
}
Db::name('wechat_user_has_tags')->insertAll($relate);
}
}
}
/** /**
* 同步 * 同步
* *

View File

@ -1,10 +1,94 @@
<?php <?php
/** // +----------------------------------------------------------------------
* @filename Tags.php // | CatchAdmin [Just Like ]
* @createdAt 2020/6/21 // +----------------------------------------------------------------------
* @project https://github.com/yanwenwu/catch-admin // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
* @document http://doc.catchadmin.com // +----------------------------------------------------------------------
* @author JaguarJack <njphper@gmail.com> // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
* @copyright By CatchAdmin // +----------------------------------------------------------------------
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt // | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\controller;
use think\Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\wechat\repository\WechatTagsRepository;
class Tags extends CatchController
{
protected $repository;
public function __construct(WechatTagsRepository $repository)
{
$this->repository = $repository;
}
/**
* 列表
*
* @time 2020/06/21 14:45
*
* @return \think\Response
*/ */
public function index()
{
return CatchResponse::paginate($this->repository->getList());
}
/**
* 保存
*
* @time 2020/06/21 14:45
* @param Request Request
* @return \think\Response
*/
public function save(Request $request)
{
return CatchResponse::success($this->repository->storeBy($request->post()));
}
/**
* 读取
*
* @time 2020/06/21 14:45
* @param $id
* @return \think\Response
*/
public function read($id)
{
return CatchResponse::success($this->repository->findBy($id));
}
/**
* 更新
*
* @time 2020/06/21 14:45
* @param Request $request
* @return \think\Response
*/
public function update(Request $request, $id)
{
return CatchResponse::success($this->repository->updateBy($id, $request->post()));
}
/**
* 删除
*
* @time 2020/06/21 14:45
* @param $id
* @return \think\Response
*/
public function delete($id)
{
return CatchResponse::success($this->repository->deleteBy($id));
}
public function sync()
{
return CatchResponse::success($this->repository->sync());
}
}

View File

@ -14,6 +14,7 @@ use catchAdmin\wechat\repository\WechatUsersRepository;
use catcher\base\CatchController; use catcher\base\CatchController;
use catcher\CatchResponse; use catcher\CatchResponse;
use catcher\Utils; use catcher\Utils;
use think\facade\Console;
class Users extends CatchController class Users extends CatchController
{ {
@ -60,6 +61,13 @@ class Users extends CatchController
return CatchResponse::success($this->user->block($id)); return CatchResponse::success($this->user->block($id));
} }
public function sync()
{
Console::call('sync:users');
return CatchResponse::success('', 'success');
}
public function subscribe() public function subscribe()
{ {

View File

@ -0,0 +1,37 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class WechatUserHasTags extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('wechat_user_has_tags', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '用户标签关联表' ,'signed' => true ]);
$table->addColumn('tag_id', 'integer', ['limit' => MysqlAdapter::INT_SMALL,'null' => true,'signed' => false,'comment' => '微信 tagId',])
->addColumn('user_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',])
->create();
}
}

View File

@ -0,0 +1,42 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class WechatTags extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('wechat_tags', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '微信标签表' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]);
$table->addColumn('tag_id', 'integer', ['limit' => MysqlAdapter::INT_SMALL,'null' => true,'signed' => false,'comment' => '微信 tagId',])
->addColumn('name', 'string', ['limit' => 30,'null' => true,'comment' => '标签名称',])
->addColumn('fans_amount', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '粉丝数量',])
->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',])
->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',])
->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',])
->addIndex(['tag_id'], ['unique' => true,'name' => 'unique_tag_id'])
->create();
}
}

View File

@ -0,0 +1,38 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\model;
use catchAdmin\wechat\model\search\TagSearchTrait;
use catcher\base\CatchModel as Model;
class WechatTags extends Model
{
use TagSearchTrait;
protected $name = 'wechat_tags';
protected $field = [
'id', //
'tag_id', // 微信 tagId
'name', // 标签名称
'fans_amount', // 粉丝数量
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 软删除
];
public function hasManyUsers()
{
return $this->belongsToMany(WechatUsers::class, 'wechat_user_has_tags', 'user_id', 'tag_id');
}
}

View File

@ -46,4 +46,9 @@ class WechatUsers extends CatchModel
const BlOCK = 2; // 拉黑 const BlOCK = 2; // 拉黑
const UNBLOCK = 1; // 取消拉黑 const UNBLOCK = 1; // 取消拉黑
public function hasManyTags()
{
return $this->belongsToMany(WechatTags::class, 'wechat_user_has_tags', 'tag_id', 'user_id');
}
} }

View File

@ -0,0 +1,27 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\model\search;
trait TagSearchTrait
{
/**
* 昵称搜索
* @time 2020年06月21日
* @param $query
* @param $value
* @param $data
* @return mixed
*/
public function searchNameAttr($query, $value, $data)
{
return $query->whereLike('name', $value);
}
}

View File

@ -0,0 +1,110 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\repository;
use catchAdmin\wechat\model\WechatTags;
use catchAdmin\wechat\model\WechatUsers;
use catcher\base\CatchRepository;
use catcher\library\WeChat;
class WechatTagsRepository extends CatchRepository
{
protected $wechatTag;
public function __construct(WechatTags $tags)
{
$this->wechatTag = $tags;
}
/**
* 模型
*
* @time 2020年06月21日
* @return WechatTags
*/
protected function model()
{
return $this->wechatTag;
}
/**
* 同步微信标签
*
* @time 2020年06月21日
* @return int
*/
public function sync()
{
$tags = $this->wechatTag->column('name');
$wechatTags = WeChat::officialAccount()->user_tag->list()['tags'];
$_tags = [];
foreach ($wechatTags as $key => &$tag) {
if (in_array($tag['name'], $tags)) {
continue;
}
$_tags[] = [
'tag_id' => $tag['id'],
'name' => $tag['name'],
'fans_amount' => $tag['count'],
'created_at' => time(),
'updated_at' => time()
];
}
return $this->wechatTag->insertAll($_tags);
}
public function storeBy(array $data)
{
$res = WeChat::throw(WeChat::officialAccount()->user_tag->create($data['name']));
$data['tag_id'] = $res['tag']['id'];
return $this->wechatTag->storeBy($data);
}
/**
* 更新
*
* @time 2020年06月21日
* @param int $id
* @param array $data
* @return mixed
*/
public function updateBy(int $id, array $data)
{
$tag = $this->findBy($id);
WeChat::throw(WeChat::officialAccount()->user_tag->update($tag->tag_id,$data['name']));
return parent::updateBy($id, $data); // TODO: Change the autogenerated stub
}
/**
* 删除
*
* @time 2020年06月21日
* @param int $id
* @return mixed
*/
public function deleteBy(int $id)
{
$tag = $this->findBy($id);
WeChat::throw(WeChat::officialAccount()->user_tag->delete($tag->tag_id));
return parent::deleteBy($id); // TODO: Change the autogenerated stub
}
}

View File

@ -12,8 +12,15 @@
$router->group('wechat', function () use ($router){ $router->group('wechat', function () use ($router){
// 公众号粉丝 // 公众号粉丝
$router->group('official/users', function () use ($router){ $router->group('official/users', function () use ($router){
$router->get('<nextOpenid?>', '\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->get('sync', '\catchAdmin\wechat\controller\Users@sync');
});
// 粉丝标签
$router->group('official/tags', function () use ($router){
$router->resource('', '\catchAdmin\wechat\controller\Tags');
$router->get('sync', '\catchAdmin\wechat\controller\Tags@sync');
}); });
}); });