微信公众号根据不同的用户提供标签管理,给用户贴上标签,类似用户分组,为不同的用户提供个性化服务。
如:企业管理中,不同角色看到的菜单不同,不同客户享受的优惠和赠送的礼品不同。这些,都需要标签管理。
在Applications/Wechat/views下创建一个文件,名为:WechatTag.py,用于对微信公众号的标签操作。
1 创建标签
class CreateWechatTag(APIView): schema = CustomSchema(manual_fields=[ Field(name="name", required=True, location="query", schema=String(description=‘标签名称‘)) ]) @classmethod def get(cls, request): """ 创建标签</br> GET /ThirdParts/wechatpytag/create/?name=<标签名称></br> """ name = request.query_params.get(‘name‘) logger.info("name:%s" % name) wechat_client = get_WeChatClient() wechat_client_tag = wechat_client.tag # 创建标签 data = wechat_client_tag.create(name) logger.info("data:%s" % data) return Response(data=data, status=status.HTTP_200_OK)
2 删除标签
class DeleteWechatTag(APIView): schema = CustomSchema(manual_fields=[ Field(name="tag_id", required=True, location="query", schema=String(description=‘标签ID‘)) ]) @classmethod def get(cls, request): """ 删除标签</br> GET /ThirdParts/wechatpytag/delete/?tag_id=<标签id></br> """ tag_id = request.query_params.get(‘tag_id‘) logger.info("tag_id:%s" % tag_id) wechat_client = get_WeChatClient() wechat_client_tag = wechat_client.tag # 删除标签 data = wechat_client_tag.delete(tag_id) logger.info("data:%s" % data) return Response(data=data, status=status.HTTP_200_OK)
3 获取标签列表
class GetWechatTag(APIView): @classmethod def get(cls, request): """ 获取公众号已创建的标签</br> GET /ThirdParts/wechatpytag/select/</br> """ wechat_client = get_WeChatClient() wechat_client_tag = wechat_client.tag # 获取公众号已创建的标签 tag_list = wechat_client_tag.get() logger.info("tag_list:%s" % tag_list) return Response(data=tag_list, status=status.HTTP_200_OK)
4 批量为用户打标签
class BatchMarkWechatTag(APIView): schema = CustomSchema(manual_fields=[ Field(name="tag_id", required=True, location="query", schema=String(description=‘标签id‘)), Field(name="openid", required=True, location="query", schema=String(description=‘openID‘)) ]) @classmethod def get(cls, request): """ 批量为用户打标签</br> GET /ThirdParts/wechatpytaguser/create/?tag_id=<标签id>&openid=<openid></br> """ tag_id = request.query_params.get(‘tag_id‘) openid = request.query_params.get(‘openid‘) logger.info("tag_id:%s" % tag_id) logger.info("openid:%s" % openid) wechat_client = get_WeChatClient() wechat_client_tag = wechat_client.tag # 批量为用户打标签 data = wechat_client_tag.tag_user(tag_id, openid) logger.info("data:%s" % data) return Response(data=data, status=status.HTTP_200_OK)
5 批量取消用户标签
class BatchUnmarkWechatTag(APIView): schema = CustomSchema(manual_fields=[ Field(name="tag_id", required=True, location="query", schema=String(description=‘标签id‘)), Field(name="openid", required=True, location="query", schema=String(description=‘openID‘)) ]) @classmethod def get(cls, request): """ 批量为用户取消标签</br> GET /ThirdParts/wechatpytaguser/delete/?tag_id=<标签id>&openid=<openid></br> """ tag_id = request.query_params.get(‘tag_id‘) openid = request.query_params.get(‘openid‘) logger.info("tag_id:%s" % tag_id) logger.info("openid:%s" % openid) wechat_client = get_WeChatClient() wechat_client_tag = wechat_client.tag # 批量为用户取消标签 data = wechat_client_tag.untag_user(tag_id, openid) logger.info("data:%s" % data) return Response(data=data, status=status.HTTP_200_OK)
6 获取用户身上的标签列表
class GetUserWechatTagList(APIView): schema = CustomSchema(manual_fields=[ Field(name="openid", required=True, location="query", schema=String(description=‘openID‘)) ]) @classmethod def get(cls, request): """ 获取用户身上的标签列表</br> GET /ThirdParts/wechatpytaguser/select/?openid=<openid></br> """ openid = request.query_params.get(‘openid‘) logger.info("openid:%s" % openid) wechat_client = get_WeChatClient() wechat_client_tag = wechat_client.tag # 获取用户身上的标签列表 data = wechat_client_tag.get_user_tag(openid) logger.info("data:%s" % data) return Response(data=data, status=status.HTTP_200_OK)