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;

二、獲取標籤spa

/// <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;

三、刪除標籤code

/// <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;

四、編輯標籤orm

/// <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;

2、設置用戶備註名blog

/// <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;

3、獲取用戶基本信息(UnionID機制)接口

在關注者與公衆號產生消息交互後,公衆號可得到關注者的OpenID(加密後的微信號,每一個用戶對每一個公衆號的OpenID是惟一的。對於不一樣公衆號,同一用戶的openid不一樣)。公衆號可經過本接口來根據OpenID獲取用戶基本信息,包括暱稱、頭像、性別、所在城市、語言和關注時間。開發

請注意,若是開發者有在多個公衆號,或在公衆號、移動應用之間統一用戶賬號的需求,須要前往微信開放平臺(open.weixin.qq.com)綁定公衆號後,纔可利用UnionID機制來知足上述需求。rem

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;

上張效果圖

相關文章
相關標籤/搜索