Delphi 开发微信公众平台 (二)- 用户管理

一、用户标签管理

开发者可以使用用户标签管理的相关接口,实现对公众号的标签进行创建、查询、修改、删除等操作,也可以对用户进行打标签、取消标签等操作。

1、创建标签

/// <summary>
/// 创建标签
/// </summary>
/// <remarks>
/// 一个公众号,最多可以创建100个标签。
/// </remarks>
function CreateTag(const ATagName: string): TWechatTag;
function TWechatRequest.CreateTag(const ATagName: string): TWechatTag;
var
  Content, Response: TJSONObject;
begin
  Result := nil;
  Content := TJSONObject.Create.AddPair('tag', TJSONObject.Create.AddPair('name', ATagName));
  try
    Response := HttpPost(Content, 'tags/create');
    try
      if ParseResponse(Response) then
      begin
        Result := TJson.Json2Object<TWechatTag>(Response.Values['tag'].ToJSON);
        Result.Count := 0;
      end;
    finally
      FreeAndNil(Response);
    end;
  finally
    FreeAndNil(Content);
  end;
end;

2、获取标签

/// <summary>
/// 获取公众号已创建的标签
/// </summary>
function GetTags: TWechatTags;
function TWechatRequest.GetTags: TWechatTags;
var
  JsonString: string;
  Response: TJSONObject;
begin
  Response := HttpGet('tags/get');
  try
    if ParseResponse(Response) then
    begin
      JsonString := Response.GetValue<TJSONArray>('tags').ToJSON;
      Result := TJson.Json2Object<TWechatTags>(JsonString);
    end;
  finally
    FreeAndNil(Response);
  end;
end;

3、删除标签

/// <summary>
/// 删除标签
/// </summary>
/// <remarks>
/// 当某个标签下的粉丝超过10w时,后台不可直接删除标签。
/// 此时,开发者可以对该标签下的openid列表 ,
/// 先进行取消标签的操作,直到粉丝数不超过10w后,才可直接删除该标签。
/// </remarks>
function DeleteTag(ATagId: Integer): Boolean;
function TWechatRequest.DeleteTag(ATagId: Integer): Boolean;
var
  Content, Response: TJSONObject;
begin
  Content := TJSONObject.Create.AddPair('tag', TJSONObject.Create.AddPair('id', TJSONNumber.Create(ATagId)));
  try
    Response := HttpPost(Content, 'tags/delete');
    try
      Result := ParseResponse(Response);
    finally
      FreeAndNil(Response);
    end;
  finally
    FreeAndNil(Content);
  end;
end;

4、编辑标签

/// <summary>
/// 编辑标签
/// </summary>
function UpdateTag(ATagId: Integer; ANewName: string): Boolean;
function TWechatRequest.UpdateTag(ATagId: Integer; ANewName: string): Boolean;
var
  Content, Response: TJSONObject;
begin
  Content := TJSONObject.Create.AddPair('tag', TJSONObject.Create
      .AddPair('id', TJSONNumber.Create(ATagId))
      .AddPair('name', ANewName)
    );
  try
    Response := HttpPost(Content, 'tags/update');
    try
      Result := ParseResponse(Response);
    finally
      FreeAndNil(Response);
    end;
  finally
    FreeAndNil(Content);
  end;
end;

二、设置用户备注名

/// <summary>
/// 设置用户备注名
/// </summary>
/// <remarks>
/// https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140838
/// </remarks>
function UpdateRemark(const AOpenID, ARemark: string): Boolean;
function TWechatRequest.UpdateRemark(const AOpenID, ARemark: string): Boolean;
var
  Content, Response: TJSONObject;
begin
  Content := TJSONObject.Create.AddPair('openid', AOpenID).AddPair('remark', ARemark);
  try
    Response := HttpPost(Content, 'user/info/updateremark');
    try
      Result := ParseResponse(Response);
    finally
      FreeAndNil(Response);
    end;
  finally
    FreeAndNil(Content);
  end;
end;

三、获取用户基本信息(UnionID机制)

在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的。对于不同公众号,同一用户的openid不同)。公众号可通过本接口来根据OpenID获取用户基本信息,包括昵称、头像、性别、所在城市、语言和关注时间。

请注意,如果开发者有在多个公众号,或在公众号、移动应用之间统一用户帐号的需求,需要前往微信开放平台(open.weixin.qq.com)绑定公众号后,才可利用UnionID机制来满足上述需求。

UnionID机制说明:

开发者可通过OpenID来获取用户基本信息。特别需要注意的是,如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号,用户的unionid是唯一的。换句话说,同一用户,对同一个微信开放平台下的不同应用,unionid是相同的。

/// <summary>
/// 获取单个用户基本信息
/// </summary>
function GetUserInfo(const AOpenID: string): TWechatUser;
function TWechatRequest.GetUserInfo(const AOpenID: String): TWechatUser;
var
  Response: TJSONObject;
begin
  Result := nil;
  Response := HttpGet('user/info', Format('openid=%s&lang=zh_CN', [AOpenID]));
  try
    if ParseResponse(Response) then
      Result := TWechatUser.FromJsonString(Response.ToJSON);
  finally
    FreeAndNil(Response);
  end;
end;

上张效果图

posted @ 2019-05-06 11:06  塞翁失身  阅读(1069)  评论(0编辑  收藏  举报