The current project is mainly aimed at the interface encapsulation of social networking sites. In the early stage of the project, it mainly focuses on the development of WeChat platform SDK, which is simply classified according to the interface function level.
This project provides services in the form of a standard library, which means it can support both .Net Framework (4.6 and above) and .Net Core.
If you have any questions, you can also ask in the official account (osscore):
1. Authorization docking module (Oauth)
Installation command under nuget: Install-Package OSS.Clients.Oauth.WX
User authorization (oauth2.0), basic user authorization information
2. Session message module (msg)
Installation command under nuget: Install-Package OSS.Clients.Msg.Wechat
Session management, receiving user session information and corresponding responses
3. Advanced functions of public accounts (Platform)
1.AccessToken
nuget下安装命令:**Install-Package OSS.Clients.Platform.WX.AccessToken**
这里主要是获取AccessToken接口的封装
WXPlatTokenApi - 公众号的正常Token接口
WXAgentPlatTokenApi - 代理平台的Token的接口
2. Basic information part
nuget下安装命令:**Install-Package OSS.Clients.Platform.WX.Basic**
这里主要是基础信息相关接口
WXPlatUserApi - 微信公众号用户信息相关接口
WXPlatIpApi - 微信服务器信息接口
WXPlatKfApi - 微信客服接口
WXPlatMassApi - 微信群发消息相关接口
WXPlatMediaApi - 微信素材相关接口
WXPlatMenuApi - 微信菜单相关接口
WXPlatQrApi - 微信二维码相关接口
3.Other interfaces
nuget下安装命令:**Install-Package OSS.Clients.Platform.WX**
这里主要是公众号其他相关接口
WXPlatAssistApi - 微信jssdk辅助接口
WXPlatCardApi - 微信卡信息相关接口
WXPlatShakeApi - 微信摇一摇相关接口
WXPlatAppApi - 微信小程序相关接口
WXPlatStatApi - 微信统计相关接口
WXPlatStoreApi - 微信门店管理相关接口
4.Agent platform interface
nuget下安装命令:**Install-Package OSS.Clients.Platform.WX.Agent**
这里主要是公众号代理服务平台的相关接口
1. Basic authorization call (under the sns folder)
//声明配置信息
private static AppConfig m_Config = new AppConfig ( )
{
AppSource = "11" ,
AppId = "你的appId" ,
AppSecret = "你的secretkey"
} ;
// 接口api实例
private static WXOauthApi m_AuthApi = new WXOauthApi ( m_Config ) ;
// 获取微信授权地址
public ActionResult auth ( )
{
var res = m_AuthApi . GetAuthorizeUrl ( "http://www.social.com/wxoauth/callback" ,
AuthClientType . WXPlat ) ;
return Redirect ( res ) ;
}
// 微信回调页,此页面获取accesstoken 获取用户基础信息
public ActionResult callback ( string code , string state )
{
var tokecRes = m_AuthApi . GetAuthAccessToken ( code ) ;
if ( tokecRes . IsSuccess ( ) )
{
var userInfoRes = m_AuthApi . GetWXAuthUserInfo ( tokecRes . AccessToken , tokecRes . OpenId ) ;
return Content ( "你已成功获取用户信息!" ) ;
}
return Content ( "获取用户授权信息失败!" ) ;
}
2. Session call (under Msg folder)
a. First declare the configuration information
// 声明配置
private static readonly WXChatServerConfig config = new WXChatServerConfig ( )
{
Token = "你的token" ,
EncodingAesKey = "你的加密key" ,
SecurityType = WXSecurityType . Safe , // 在微信段设置的安全模式
AppId = "你的appid" //
} ;
b. Define a processing handle (you can implement your own Handler, just inherit from WXChatHandler)
private static readonly WXChatHandler msgService = new WXChatHandler ( config ) ;
c. When calling, just pass the current requested content into the program entrance:
using ( StreamReader reader = new StreamReader ( Request . InputStream ) )
{
requestXml = reader . ReadToEnd ( ) ;
}
try
{
var res = msgService . Process ( requestXml , signature , timestamp , nonce , echostr ) ;
if ( res . IsSuccess ( ) )
return Content ( res . data ) ;
}
catch ( Exception ex )
{
}
return Content ( "success" ) ;
Among them, WXChatHandler can be a specific processing class that inherits the implementation of WXChatHandler, and returns the corresponding results by rewriting relevant user events.
3. Advanced function call (under the Platform folder)
Other advanced functional interfaces of the WeChat official account require a global accesstoken interface, such as push module information, etc. The automatic acquisition of accesstoken has been encapsulated in the underlying request processing of the SDK. By default, it will be saved in the system cache and will be automatically updated when it expires. If it needs to be saved, In redis, it can be injected through the cache module in oscommon. Just add a cache module implementation for sns (an example will be given later). Access and appid correspond one to one, so there is no need to worry about conflicts between multiple official accounts.
a. Declare configuration information:
//声明配置信息
private static AppConfig m_Config = new AppConfig ( )
{
AppSource = "11" ,
AppId = "你的appId" ,
AppSecret = "你的secretkey"
} ;
b. Declare an instance:
private static readonly WXPlatMassApi m_OffcialApi = new WXPlatMassApi ( m_Config ) ;
c. Specific use
m_OffcialApi . SendTemplateAsync ( "openid" , "templateId" , "url" , new { } )
At present, the logic part of this part of the interface framework has been processed. The main functions of the official account have been completed. The interfaces of the store and equipment management are still to be perfected. They will be updated soon. If necessary, you can implement it yourself or contribute it. Add A new interface only requires a few lines of code, see Contributing Code for details.
In view of the actual usage scenarios of the business, the WeChat official account interface has been simply disassembled, and AccessToken and others are independent SDKs. All independent SDKs reference the OSS.Clients.Platform.WX.Base class library, and provide unified configuration of WXPlatConfigProvider under this type of library, mainly to facilitate users to achieve unified acquisition of AccessToken when using other SDKs, such as: The AccessToken unified cache management project implemented using OSS.Clients.Platform.WX.AccessToken implements the configuration AccessToken acquisition method under WXPlatConfigProvider in the project using OSS.Clients.Platform.WX.Basic
This project is currently mainly focused on WeChat SDK processing. The main framework of WeChat has been completed and the interface needs to be supplemented. It will be very simple to complete an interface based on the encapsulated framework. Taking the basic information of the user as an example, it is simply divided into the following two Part steps:
1. Declare the object entity
// 获取用户基本信息请求实体
public class WXPlatUserInfoReq
{
public string openid { get ; set ; }
public string lang { get ; set ; }
}
// 响应实体,继承WXBaseResp
public class WXPlatUserInfoResp : WXBaseResp
{
public string openid { get ; set ; }
public string nickname { get ; set ; }
//... 字段省略
public List < int > tagid_list { get ; set ; }
}
I wrote a js page to quickly generate entity Gist addresses through WeChat document field descriptions, open source Chinese addresses
2.Function implementation
public WXPlatUserInfoResp GetUserInfo ( WXPlatUserInfoReq userReq )
{
var req = new OssHttpRequest ( ) ;
req . HttpMethod = HttpMethod . Get ;
req . AddressUrl = string . Concat ( m_ApiUrl ,
$ "/cgi-bin/user/info?openid= { userReq . openid } &lang= { userReq . lang } " ) ;
// 请求地址中的AccessToken 底层会自动补充
return RestCommonOffcial < WXPlatUserInfoResp > ( req ) ;
}
3. Add unit tests (optional)
[ TestMethod ]
public void GetUserInfoTest ( )
{
var res = m_Api . GetUserInfo ( new WXPlatUserInfoReq ( )
{ openid = "o7gE1s6mygEKgopVWp7BBtEAqT-w" } ) ;
Assert . IsTrue ( res . IsSuccess ( ) ) ;
}
The rest can be submitted via git.
尽快完善
The current project relies on the open source projects I wrote before, OSS.Common (global error entity, and module injection) and OSS.Http (http request). They are relatively simple and compact, with only a few classes, so there is no need to worry about bloat.