Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

处理企业微信服务商功能和在离职分配接口 #2614

Closed
wants to merge 16 commits into from
12 changes: 10 additions & 2 deletions Samples/All/net6-mvc/Senparc.Weixin.Sample.Net6/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Senparc.Weixin.Sample.Net6
{
Expand Down Expand Up @@ -253,6 +254,13 @@ public void ConfigureServices(IServiceCollection services)
.RegisterWorkAccount(senparcWeixinSetting.Value, "【盛派网络】企业微信")
.RegisterWorkAccount(senparcWeixinSetting.Value["企业微信审批"], "【盛派网络】企业微信审批")
//.RegisterWorkAccount(senparcWeixinSetting.Value["企业微信审批"].WeixinCorpAgentId, senparcWeixinSetting.Value["企业微信审批"].WeixinCorpSecret, "【盛派网络】企业微信审批应用-自建应用")
.RegisterWorkAccount(senparcWeixinSetting.Value, "【盛派网络】企业微信(或服务商)",
getSuiteTicketFunc:suiteId => {
return Task.FromResult("");
},
getSuiteByCorpPermanentCodeFunc: permanentCode => {
return Task.FromResult(new Work.Entities.FuncGetIdSecretResult { CorpId = "", Secret = "" });
})

//除此以外,仍然可以在程序任意地方注册企业微信:
//AccessTokenContainer.Register(corpId, corpSecret, name);//命名空间:Senparc.Weixin.Work.Containers
Expand Down Expand Up @@ -353,8 +361,8 @@ public void ConfigureServices(IServiceCollection services)

#region 设置自定义 ApiHandlerWapper 参数(可选,一般不需要设置) --DPBMARK MP

.SetMP_InvalidCredentialValues(new[] { ReturnCode.获取access_token时AppSecret错误或者access_token无效 })
//.SetMP_AccessTokenContainer_GetAccessTokenResultFunc((appId, getNewToken)=> { return xxx })
.SetWork_InvalidCredentialValues(new[] { ReturnCode_Work.不合法的access_token, ReturnCode_Work.access_token过期, ReturnCode_Work.不合法的suitetoken })
//.SetMP_AccessTokenContainer_GetAccessTokenResultFunc((appId, getNewToken)=> { return xxx })

#endregion // DPBMARK_END

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public static WxJsonResult SendNews(string accessTokenOrAppId, string openId, Li
/// <summary>
/// 发送图文消息(点击跳转到图文消息页面)
/// 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。
/// 草稿接口灰度完成后,将不再支持此前客服接口中带 media_id 的 mpnews 类型的图文消息
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="openId"></param>
Expand Down Expand Up @@ -467,6 +468,54 @@ public static WxJsonResult SendMpNews(string accessTokenOrAppId, string openId,

}, accessTokenOrAppId);
}
/// <summary>
/// 发送图文消息(点击跳转到图文消息页面)使用通过 “发布” 系列接口得到的 article_id
/// 草稿接口灰度完成后,将不再支持此前客服接口中带 media_id 的 mpnews 类型的图文消息
/// </summary>
/// <param name="accessTokenOrAppId"></param>
/// <param name="openId"></param>
/// <param name="article_id"></param>
/// <param name="timeOut"></param>
/// <param name="kfAccount"></param>
/// <returns></returns>
public static WxJsonResult SendMpNewsArticle(string accessTokenOrAppId, string openId, string article_id, int timeOut = Config.TIME_OUT, string kfAccount = "")
{
object data = null;
if (kfAccount.IsNullOrWhiteSpace())
{
data = new
{
touser = openId,
msgtype = "mpnewsarticle",
mpnewsarticle = new
{
article_id = article_id
}
};
}
else
{
data = new
{
touser = openId,
msgtype = "mpnewsarticle",
mpnewsarticle = new
{
article_id = article_id
},
CustomService = new
{
kf_account = kfAccount
}
};
}
return ApiHandlerWapper.TryCommonApi(accessToken =>
{

return CommonJsonSend.Send(accessToken, UrlFormat, data, timeOut: timeOut);

}, accessTokenOrAppId);
}

/// <summary>
/// 发送卡券
Expand Down Expand Up @@ -997,6 +1046,56 @@ public static async Task<WxJsonResult> SendMpNewsAsync(string accessTokenOrAppId
}, accessTokenOrAppId).ConfigureAwait(false);
}

/// <summary>
/// 发送图文消息(点击跳转到图文消息页面)使用通过 “发布” 系列接口得到的 article_id
/// 草稿接口灰度完成后,将不再支持此前客服接口中带 media_id 的 mpnews 类型的图文消息
/// </summary>
/// <param name="accessTokenOrAppId"></param>
/// <param name="openId"></param>
/// <param name="article_id"></param>
/// <param name="timeOut"></param>
/// <param name="kfAccount"></param>
/// <returns></returns>
public static async Task<WxJsonResult> SendMpNewsArticleAsync(string accessTokenOrAppId, string openId, string article_id, int timeOut = Config.TIME_OUT, string kfAccount = "")
{
object data = null;
if (kfAccount.IsNullOrWhiteSpace())
{
data = new
{
touser = openId,
msgtype = "mpnewsarticle",
mpnewsarticle = new
{
article_id = article_id
}
};
}
else
{
data = new
{
touser = openId,
msgtype = "mpnewsarticle",
mpnewsarticle = new
{
article_id = article_id
},
CustomService = new
{
kf_account = kfAccount
}
};
}
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{

return await CommonJsonSend.SendAsync(accessToken, UrlFormat, data, timeOut: timeOut).ConfigureAwait(false);

}, accessTokenOrAppId).ConfigureAwait(false);
}


/// <summary>
/// 【异步方法】发送卡券
/// </summary>
Expand Down
208 changes: 208 additions & 0 deletions src/Senparc.Weixin.MP/Senparc.Weixin.MP/AdvancedAPIs/Media/MediaApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.AdvancedAPIs.GroupMessage;
using Senparc.Weixin.MP.AdvancedAPIs.Media;
using Senparc.Weixin.MP.AdvancedAPIs.Media.MediaJson;
using Senparc.Weixin.MP.CommonAPIs;

namespace Senparc.Weixin.MP.AdvancedAPIs
Expand Down Expand Up @@ -494,6 +495,108 @@ public static UploadImgResult UploadImg(string accessTokenOrAppId, string file,

}, accessTokenOrAppId);
}
#region 草稿
/// <summary>
/// 获取草稿图文列表
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="offset">从全部素材的该偏移位置开始返回,0表示从第一个素材 返回</param>
/// <param name="count">返回素材的数量,取值在1到20之间</param>
/// <param name="no_content">1 表示不返回 content 字段,0 表示正常返回,默认为 0</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static DraftList_NewsResult GetDraftNewsList(string accessTokenOrAppId, int offset, int count,int no_content = 0, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/draft/batchget?access_token={0}", accessToken.AsUrlData());

var date = new
{
offset = offset,
count = count,
no_content = no_content
};

return CommonJsonSend.Send<DraftList_NewsResult>(null, url, date, CommonJsonSendType.POST, timeOut);

}, accessTokenOrAppId);

}

/// <summary>
/// 删除草稿图文
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="mediaId">要删除的草稿的media_id</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static WxJsonResult DeleteDraftNews(string accessTokenOrAppId, string mediaId, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/draft/delete?access_token={0}", accessToken.AsUrlData());
var data = new
{
media_id = mediaId
};
return CommonJsonSend.Send<WxJsonResult>(accessToken, url, data, CommonJsonSendType.POST, timeOut);
}, accessTokenOrAppId);
}


#endregion

#region 发布图文
/// <summary>
/// 获取发布图文列表
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="offset">从全部素材的该偏移位置开始返回,0表示从第一个素材 返回</param>
/// <param name="count">返回素材的数量,取值在1到20之间</param>
/// <param name="no_content">1 表示不返回 content 字段,0 表示正常返回,默认为 0</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static PublishList_NewsResult GetPublishNewsList(string accessTokenOrAppId, int offset, int count, int no_content = 0, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/freepublish/batchget?access_token={0}", accessToken.AsUrlData());

var date = new
{
offset = offset,
count = count,
no_content = no_content
};

return CommonJsonSend.Send<PublishList_NewsResult>(null, url, date, CommonJsonSendType.POST, timeOut);

}, accessTokenOrAppId);
}
/// <summary>
/// 发布成功之后,随时可以通过该接口删除。此操作不可逆,请谨慎操作。
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="article_id">成功发布时返回的 article_id</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <param name="timeOut">要删除的文章在图文消息中的位置,第一篇编号为1,该字段不填或填0会删除全部文章</param>
/// <returns></returns>
public static WxJsonResult DeletePublishNews(string accessTokenOrAppId, string article_id, int index = 0, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/freepublish/delete?access_token={0}", accessToken.AsUrlData());
var data = new
{
article_id = article_id,
index = index
};
return CommonJsonSend.Send<WxJsonResult>(accessToken, url, data, CommonJsonSendType.POST, timeOut);
}, accessTokenOrAppId);
}

#endregion

#endregion

Expand Down Expand Up @@ -1046,6 +1149,111 @@ public static async Task<TranslateContentResultJson> TranslateContentAsync(strin

#endregion



#region 草稿
/// <summary>
/// 获取草稿图文列表
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="offset">从全部素材的该偏移位置开始返回,0表示从第一个素材 返回</param>
/// <param name="count">返回素材的数量,取值在1到20之间</param>
/// <param name="no_content">1 表示不返回 content 字段,0 表示正常返回,默认为 0</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<DraftList_NewsResult> GetDraftNewsListAsync(string accessTokenOrAppId, int offset, int count, int no_content = 0, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/draft/batchget?access_token={0}", accessToken.AsUrlData());

var date = new
{
offset = offset,
count = count,
no_content = no_content
};

return await CommonJsonSend.SendAsync<DraftList_NewsResult>(null, url, date, CommonJsonSendType.POST, timeOut).ConfigureAwait(false);

}, accessTokenOrAppId).ConfigureAwait(false);

}

/// <summary>
/// 删除草稿图文
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="mediaId">要删除的草稿的media_id</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<WxJsonResult> DeleteDraftNewsAsync(string accessTokenOrAppId, string mediaId, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/draft/delete?access_token={0}", accessToken.AsUrlData());
var data = new
{
media_id = mediaId
};
return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, url, data, CommonJsonSendType.POST, timeOut).ConfigureAwait(false);
}, accessTokenOrAppId).ConfigureAwait(false);
}

#endregion

#region 发布图文
/// <summary>
/// 获取发布图文列表
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="offset">从全部素材的该偏移位置开始返回,0表示从第一个素材 返回</param>
/// <param name="count">返回素材的数量,取值在1到20之间</param>
/// <param name="no_content">1 表示不返回 content 字段,0 表示正常返回,默认为 0</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<PublishList_NewsResult> GetPublishNewsListAsync(string accessTokenOrAppId, int offset, int count, int no_content = 0, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/freepublish/batchget?access_token={0}", accessToken.AsUrlData());

var date = new
{
offset = offset,
count = count,
no_content = no_content
};

return await CommonJsonSend.SendAsync<PublishList_NewsResult>(null, url, date, CommonJsonSendType.POST, timeOut).ConfigureAwait(false);

}, accessTokenOrAppId).ConfigureAwait(false);

}

/// <summary>
/// 发布成功之后,随时可以通过该接口删除。此操作不可逆,请谨慎操作。
/// </summary>
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
/// <param name="article_id">成功发布时返回的 article_id</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <param name="timeOut">要删除的文章在图文消息中的位置,第一篇编号为1,该字段不填或填0会删除全部文章</param>
/// <returns></returns>
public static async Task<WxJsonResult> DeletePublishNewsAsync(string accessTokenOrAppId, string article_id, int index = 0, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cgi-bin/freepublish/delete?access_token={0}", accessToken.AsUrlData());
var data = new
{
article_id = article_id,
index = index
};
return await CommonJsonSend.SendAsync<WxJsonResult>(accessToken, url, data, CommonJsonSendType.POST, timeOut).ConfigureAwait(false);
}, accessTokenOrAppId).ConfigureAwait(false);
}

#endregion
#endregion
}
}