diff --git a/catch/wechat/command/SyncUsersCommand.php b/catch/wechat/command/SyncUsersCommand.php index d38933e..69d600a 100644 --- a/catch/wechat/command/SyncUsersCommand.php +++ b/catch/wechat/command/SyncUsersCommand.php @@ -12,13 +12,16 @@ namespace catchAdmin\wechat\command; use catchAdmin\wechat\model\WechatUsers; +use catcher\exceptions\FailedException; use catcher\facade\Trie; use catcher\library\ProgressBar; use catcher\library\WeChat; +use catcher\Utils; use think\Collection; use think\console\Command; use think\console\Input; use think\console\Output; +use think\Db; class SyncUsersCommand extends Command { @@ -34,9 +37,37 @@ class SyncUsersCommand extends Command { $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); + } + } + } /** * 同步 * diff --git a/catch/wechat/controller/Tags.php b/catch/wechat/controller/Tags.php index e703187..53e57ee 100644 --- a/catch/wechat/controller/Tags.php +++ b/catch/wechat/controller/Tags.php @@ -1,10 +1,94 @@ - * @copyright By CatchAdmin - * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt - */ \ No newline at end of file +// +---------------------------------------------------------------------- +// | 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\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()); + } + + +} \ No newline at end of file diff --git a/catch/wechat/controller/Users.php b/catch/wechat/controller/Users.php index 1363f48..3ad3fce 100644 --- a/catch/wechat/controller/Users.php +++ b/catch/wechat/controller/Users.php @@ -14,6 +14,7 @@ use catchAdmin\wechat\repository\WechatUsersRepository; use catcher\base\CatchController; use catcher\CatchResponse; use catcher\Utils; +use think\facade\Console; class Users extends CatchController { @@ -60,6 +61,13 @@ class Users extends CatchController return CatchResponse::success($this->user->block($id)); } + public function sync() + { + Console::call('sync:users'); + + return CatchResponse::success('', 'success'); + } + public function subscribe() { diff --git a/catch/wechat/database/migrations/20200621083413_wechat_user_has_tags.php b/catch/wechat/database/migrations/20200621083413_wechat_user_has_tags.php new file mode 100644 index 0000000..f3ca84d --- /dev/null +++ b/catch/wechat/database/migrations/20200621083413_wechat_user_has_tags.php @@ -0,0 +1,37 @@ +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(); + } +} diff --git a/catch/wechat/database/migrations/20200621144541_wechat_tags.php b/catch/wechat/database/migrations/20200621144541_wechat_tags.php new file mode 100644 index 0000000..59bd5f3 --- /dev/null +++ b/catch/wechat/database/migrations/20200621144541_wechat_tags.php @@ -0,0 +1,42 @@ +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(); + } +} diff --git a/catch/wechat/model/WechatTags.php b/catch/wechat/model/WechatTags.php new file mode 100644 index 0000000..0a4cac6 --- /dev/null +++ b/catch/wechat/model/WechatTags.php @@ -0,0 +1,38 @@ +belongsToMany(WechatUsers::class, 'wechat_user_has_tags', 'user_id', 'tag_id'); + } +} \ No newline at end of file diff --git a/catch/wechat/model/WechatUsers.php b/catch/wechat/model/WechatUsers.php index f422775..49d518e 100644 --- a/catch/wechat/model/WechatUsers.php +++ b/catch/wechat/model/WechatUsers.php @@ -46,4 +46,9 @@ class WechatUsers extends CatchModel const BlOCK = 2; // 拉黑 const UNBLOCK = 1; // 取消拉黑 + + public function hasManyTags() + { + return $this->belongsToMany(WechatTags::class, 'wechat_user_has_tags', 'tag_id', 'user_id'); + } } \ No newline at end of file diff --git a/catch/wechat/model/search/TagSearchTrait.php b/catch/wechat/model/search/TagSearchTrait.php new file mode 100644 index 0000000..42ea651 --- /dev/null +++ b/catch/wechat/model/search/TagSearchTrait.php @@ -0,0 +1,27 @@ +whereLike('name', $value); + } +} \ No newline at end of file diff --git a/catch/wechat/repository/WechatTagsRepository.php b/catch/wechat/repository/WechatTagsRepository.php new file mode 100644 index 0000000..b8a1302 --- /dev/null +++ b/catch/wechat/repository/WechatTagsRepository.php @@ -0,0 +1,110 @@ +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 + } +} \ No newline at end of file diff --git a/catch/wechat/route.php b/catch/wechat/route.php index 7cd4b45..5d448d9 100644 --- a/catch/wechat/route.php +++ b/catch/wechat/route.php @@ -12,8 +12,15 @@ $router->group('wechat', function () use ($router){ // 公众号粉丝 $router->group('official/users', function () use ($router){ - $router->get('', '\catchAdmin\wechat\controller\Users@index'); + $router->get('', '\catchAdmin\wechat\controller\Users@index'); $router->put('remark//', '\catchAdmin\wechat\controller\Users@remark'); $router->put('block/', '\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'); }); }); +