using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using CommonUtil.Web;
|
using CommonUtil;
|
using WeiXinKfSDK.Response;
|
using RestSharp;
|
using WeiXinKfSDK.Request;
|
using Newtonsoft.Json;
|
using System.Reflection;
|
|
namespace WeiXinKfSDK
|
{
|
public class WeiXinKfClient
|
{
|
/// <summary>
|
/// 获取消息
|
/// </summary>
|
/// <param name="token"></param>
|
/// <param name="accessToken"></param>
|
public static SyncMsgResponse SyncMsg(string token, string cursor, string accessToken)
|
{
|
string url = $"https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token={accessToken}";
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
dic.Add("token", token);
|
if (!cursor.IsNullOrEmpty())
|
{
|
dic.Add("cursor", cursor);
|
}
|
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
|
LogUtil.Info($"[获取消息]token:{token},返回:{resStr}", "接口请求日志");
|
if (resStr.IsNullOrEmpty())
|
{
|
return null;
|
}
|
SyncMsgResponse response = JSONUtil.JsonToObject<SyncMsgResponse>(resStr);
|
return response;
|
}
|
/// <summary>
|
/// 获取微信客服acess_token
|
/// </summary>
|
/// <returns></returns>
|
public static GetAccessTokenResponse GetAccessToken()
|
{
|
string getUrl = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={WeiXinKfSetting._comId}&corpsecret={WeiXinKfSetting._secret}";
|
string resStr = new WebUtil().DoGet(getUrl, null);
|
LogUtil.Info($"[获取access_token]返回:{resStr}", "接口请求日志");
|
if (resStr.IsNullOrEmpty())
|
{
|
return null;
|
}
|
GetAccessTokenResponse response = JSONUtil.JsonToObject<GetAccessTokenResponse>(resStr);
|
return response;
|
}
|
|
/// <summary>
|
/// 获取公众号acess_token
|
/// </summary>
|
/// <returns></returns>
|
public static GetAccessTokenResponse GetGzhAccessToken()
|
{
|
string getUrl = $" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={WeiXinKfSetting._gzhAppid}&secret={WeiXinKfSetting._gzhSecret}";
|
string resStr = new WebUtil().DoGet(getUrl, null);
|
LogUtil.Info($"[获取access_token]返回:{resStr}", "接口请求日志");
|
if (resStr.IsNullOrEmpty())
|
{
|
return null;
|
}
|
GetAccessTokenResponse response = JSONUtil.JsonToObject<GetAccessTokenResponse>(resStr);
|
return response;
|
}
|
|
/// <summary>
|
/// 发送客服欢迎语
|
/// </summary>
|
/// <param name="request"></param>
|
/// <param name="accessToken"></param>
|
/// <returns></returns>
|
public static SendMsgOnEventResponse SendMsgOnEvent(SendMsgOnEventRequest request, string accessToken)
|
{
|
string url = $"https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token={accessToken}";
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
dic.Add("code", request.code);
|
dic.Add("msgid", request.msgid);
|
dic.Add("msgtype", request.msgtype);
|
if (!request.text.IsNull())
|
{
|
dic.Add("text", request.text);
|
}
|
if (!request.msgmenu.IsNull())
|
{
|
dic.Add("msgmenu", request.msgmenu);
|
}
|
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
|
LogUtil.Info($"[发送客服欢迎语]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "接口请求日志");
|
if (resStr.IsNullOrEmpty())
|
{
|
return null;
|
}
|
SendMsgOnEventResponse response = JSONUtil.JsonToObject<SendMsgOnEventResponse>(resStr);
|
return response;
|
}
|
/// <summary>
|
/// 发送消息
|
/// </summary>
|
/// <param name="accessToken"></param>
|
/// <returns></returns>
|
public static SendMsgOnEventResponse SendMsg(SendMsgRequest request, string accessToken)
|
{
|
string url = $"https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_token={accessToken}";
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
dic.Add("touser", request.touser);
|
dic.Add("open_kfid", request.open_kfid);
|
if (!request.msgid.IsNullOrEmpty())
|
{
|
dic.Add("msgid", request.msgid);
|
}
|
dic.Add("msgtype", request.msgtype);
|
if (!request.text.IsNull())
|
{
|
dic.Add("text", request.text);
|
}
|
if (!request.image.IsNull())
|
{
|
dic.Add("image", request.image);
|
}
|
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
|
// LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "接口请求日志");
|
if (resStr.IsNullOrEmpty())
|
{
|
return null;
|
}
|
SendMsgOnEventResponse response = JSONUtil.JsonToObject<SendMsgOnEventResponse>(resStr);
|
return response;
|
}
|
/// <summary>
|
/// 上传图片
|
/// </summary>
|
/// <param name="imgPath"></param>
|
/// <param name="imgName"></param>
|
/// <param name="accessToken"></param>
|
/// <returns></returns>
|
public static MediaUploadResponse ImgMediaUpload(string imgPath, string imgName, string accessToken)
|
{
|
try
|
{
|
var client = new RestClient($"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={accessToken}&type=image");
|
client.Timeout = -1;
|
var request = new RestRequest(Method.POST);
|
request.AddParameter("filename", imgName);
|
request.AddParameter("Content-Type", "application/octet-stream");
|
request.AddFile("filelength", $"{imgPath}");
|
IRestResponse response = client.Execute(request);
|
MediaUploadResponse res = JSONUtil.JsonToObject<MediaUploadResponse>(response.Content);
|
return res;
|
}
|
catch (Exception e)
|
{
|
return null;
|
}
|
}
|
|
/// <summary>
|
/// 创建公众号菜单
|
/// </summary>
|
public static CreateMenuResponse CreateGzhMenu(CreateMenuRequest request, string accessToken)
|
{
|
string url = $"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={accessToken}";
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
dic.Add("button", request.button);
|
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
|
LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "创建公众号菜单日志");
|
if (resStr.IsNullOrEmpty())
|
{
|
return null;
|
}
|
CreateMenuResponse response = JSONUtil.JsonToObject<CreateMenuResponse>(resStr);
|
return response;
|
}
|
|
/// <summary>
|
/// 发送客服消息
|
/// </summary>
|
/// <returns></returns>
|
public static GzhCustomerSendResponse GzhCustomerSend(GzhCustomerSendRequest request, string accessToken)
|
{
|
string url = $"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={accessToken}";
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
dic.Add("touser", request.touser);
|
dic.Add("msgtype", request.msgtype);
|
switch (request.msgtype)
|
{
|
case "text":
|
dic.Add("text", request.text);
|
break;
|
case "image":
|
dic.Add("image", request.image);
|
break;
|
default: break;
|
}
|
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
|
LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "发送客服消息日志");
|
if (resStr.IsNullOrEmpty())
|
{
|
return null;
|
}
|
GzhCustomerSendResponse response = JSONUtil.JsonToObject<GzhCustomerSendResponse>(resStr);
|
return response;
|
}
|
//100000052
|
public static GzhBaseResponse UploadWxMedia(string imgPath, string imgName, string accessToken)
|
{
|
try
|
{
|
var client = new RestClient($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={accessToken}&type=image");
|
client.Timeout = -1;
|
var request = new RestRequest(Method.POST);
|
request.AddParameter("filename", imgName);
|
request.AddParameter("type", "image");
|
request.AddParameter("Content-Type", "application/octet-stream");
|
request.AddFile("filelength", $"{imgPath}");
|
IRestResponse response = client.Execute(request);
|
GzhBaseResponse res = JSONUtil.JsonToObject<GzhBaseResponse>(response.Content);
|
return res;
|
}
|
catch (Exception e)
|
{
|
return null;
|
}
|
}
|
|
/// <summary>
|
/// 通过code获取access_token,用户授权
|
/// </summary>
|
/// <param name="code"></param>
|
/// <returns></returns>
|
public static GetOauth2TokenResponse GetOauth2Token(string code)
|
{
|
string getUrl = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={WeiXinKfSetting._gzhAppid}&secret={WeiXinKfSetting._gzhSecret}&code={code}&grant_type=authorization_code";
|
string resStr = new WebUtil().DoGet(getUrl, null);
|
LogUtil.Info($"[gzh获取access_token]返回:{resStr}", "接口请求日志");
|
GetOauth2TokenResponse response = JSONUtil.JsonToObject<GetOauth2TokenResponse>(resStr);
|
return response;
|
}
|
|
|
/// <summary>
|
/// 通过accesstoken获取用户信息
|
/// </summary>
|
/// <param name="accessToken"></param>
|
/// <param name="openId"></param>
|
/// <returns></returns>
|
public static GetGzhUserInfoResonse GetGzhUserInfo(string accessToken, string openId)
|
{
|
string getUrl = $"https://api.weixin.qq.com/sns/userinfo?access_token={accessToken}&openid={openId}&lang=zh_CN";
|
string resStr = new WebUtil().DoGet(getUrl, null);
|
LogUtil.Info($"[获取用户信息]返回:{resStr}", "接口请求日志");
|
GetGzhUserInfoResonse response = JSONUtil.JsonToObject<GetGzhUserInfoResonse>(resStr);
|
return response;
|
}
|
|
public static string GetGzhRid(string rid, string accessToken)
|
{
|
string url = $"https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token={accessToken}";
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
dic.Add("rid", rid);
|
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
|
LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(dic)},返回:{resStr}", "发送客服消息日志");
|
return resStr;
|
}
|
/// <summary>
|
/// 发送公众号模板消息
|
/// </summary>
|
/// <param name="request"></param>
|
public static SendTemplateMsgResponse SendTemplateMsg(SendTemplateMsgRequest request, string access_token)
|
{
|
string url = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={access_token}";
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
dic.Add("touser", request.touser);
|
dic.Add("template_id", request.template_id);
|
dic.Add("data", request.data);
|
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
|
LogUtil.Info($"[发模板消息]请求:{JSONUtil.ObjectToJson(dic)},返回:{resStr}", "发送模板消息日志");
|
SendTemplateMsgResponse res = JSONUtil.JsonToObject<SendTemplateMsgResponse>(resStr);
|
return res;
|
}
|
}
|
}
|