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;

上張效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM