From a3dd8282b3af27c7c6cbbe26dbcd690eda6cbf71 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 22 Jun 2020 07:55:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BE=AE=E4=BF=A1=E7=B2=89?= =?UTF-8?q?=E4=B8=9D=E5=92=8C=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/wechat/controller/Tags.php | 16 +++-- catch/wechat/controller/Users.php | 7 ++ catch/wechat/model/WechatTags.php | 5 +- catch/wechat/model/WechatUsers.php | 15 ++++- .../repository/WechatTagsRepository.php | 21 +++++- .../repository/WechatUsersRepository.php | 64 +++++++++++++++++++ catch/wechat/route.php | 1 + 7 files changed, 120 insertions(+), 9 deletions(-) diff --git a/catch/wechat/controller/Tags.php b/catch/wechat/controller/Tags.php index 53e57ee..570d769 100644 --- a/catch/wechat/controller/Tags.php +++ b/catch/wechat/controller/Tags.php @@ -24,17 +24,25 @@ class Tags extends CatchController { $this->repository = $repository; } - + /** * 列表 * * @time 2020/06/21 14:45 - * + * + * @param Request $request * @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())); } /** diff --git a/catch/wechat/controller/Users.php b/catch/wechat/controller/Users.php index 3ad3fce..670966d 100644 --- a/catch/wechat/controller/Users.php +++ b/catch/wechat/controller/Users.php @@ -13,8 +13,10 @@ namespace catchAdmin\wechat\controller; use catchAdmin\wechat\repository\WechatUsersRepository; use catcher\base\CatchController; use catcher\CatchResponse; +use catcher\library\WeChat; use catcher\Utils; use think\facade\Console; +use think\Request; class Users extends CatchController { @@ -61,6 +63,11 @@ class Users extends CatchController 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() { Console::call('sync:users'); diff --git a/catch/wechat/model/WechatTags.php b/catch/wechat/model/WechatTags.php index 0a4cac6..144a0bf 100644 --- a/catch/wechat/model/WechatTags.php +++ b/catch/wechat/model/WechatTags.php @@ -18,6 +18,8 @@ class WechatTags extends Model { use TagSearchTrait; + protected $pk = 'tag_id'; + protected $name = 'wechat_tags'; protected $field = [ @@ -29,9 +31,8 @@ class WechatTags extends Model 'updated_at', // 更新时间 'deleted_at', // 软删除 ]; - - public function hasManyUsers() + public function hasUsers() { return $this->belongsToMany(WechatUsers::class, 'wechat_user_has_tags', 'user_id', 'tag_id'); } diff --git a/catch/wechat/model/WechatUsers.php b/catch/wechat/model/WechatUsers.php index 49d518e..b12e562 100644 --- a/catch/wechat/model/WechatUsers.php +++ b/catch/wechat/model/WechatUsers.php @@ -13,7 +13,7 @@ namespace catchAdmin\wechat\model; use catchAdmin\wechat\model\search\UserSearchTrait; use catcher\base\CatchModel; -use catcher\traits\db\BaseOptionsTrait; +use think\facade\Db; class WechatUsers extends CatchModel { @@ -47,8 +47,19 @@ class WechatUsers extends CatchModel const BlOCK = 2; // 拉黑 const UNBLOCK = 1; // 取消拉黑 - public function hasManyTags() + public function hasTags() { 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'); + } + } \ No newline at end of file diff --git a/catch/wechat/repository/WechatTagsRepository.php b/catch/wechat/repository/WechatTagsRepository.php index b8a1302..87d51ea 100644 --- a/catch/wechat/repository/WechatTagsRepository.php +++ b/catch/wechat/repository/WechatTagsRepository.php @@ -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; } diff --git a/catch/wechat/repository/WechatUsersRepository.php b/catch/wechat/repository/WechatUsersRepository.php index 46dde5a..90a1ff7 100644 --- a/catch/wechat/repository/WechatUsersRepository.php +++ b/catch/wechat/repository/WechatUsersRepository.php @@ -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; + } } \ No newline at end of file diff --git a/catch/wechat/route.php b/catch/wechat/route.php index 5d448d9..159b9f2 100644 --- a/catch/wechat/route.php +++ b/catch/wechat/route.php @@ -15,6 +15,7 @@ $router->group('wechat', function () use ($router){ $router->get('', '\catchAdmin\wechat\controller\Users@index'); $router->put('remark//', '\catchAdmin\wechat\controller\Users@remark'); $router->put('block/', '\catchAdmin\wechat\controller\Users@block'); + $router->put('tag/', '\catchAdmin\wechat\controller\Users@tag'); $router->get('sync', '\catchAdmin\wechat\controller\Users@sync'); }); // 粉丝标签