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
{
///
/// 获取消息
///
///
///
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 dic = new Dictionary();
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(resStr);
return response;
}
///
/// 获取微信客服acess_token
///
///
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(resStr);
return response;
}
///
/// 获取公众号acess_token
///
///
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(resStr);
return response;
}
///
/// 发送客服欢迎语
///
///
///
///
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 dic = new Dictionary();
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(resStr);
return response;
}
///
/// 发送消息
///
///
///
public static SendMsgOnEventResponse SendMsg(SendMsgRequest request, string accessToken)
{
string url = $"https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_token={accessToken}";
Dictionary dic = new Dictionary();
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(resStr);
return response;
}
///
/// 上传图片
///
///
///
///
///
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(response.Content);
return res;
}
catch (Exception e)
{
return null;
}
}
///
/// 创建公众号菜单
///
public static CreateMenuResponse CreateGzhMenu(CreateMenuRequest request, string accessToken)
{
string url = $"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={accessToken}";
Dictionary dic = new Dictionary();
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(resStr);
return response;
}
///
/// 发送客服消息
///
///
public static GzhCustomerSendResponse GzhCustomerSend(GzhCustomerSendRequest request, string accessToken)
{
string url = $"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={accessToken}";
Dictionary dic = new Dictionary();
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(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(response.Content);
return res;
}
catch (Exception e)
{
return null;
}
}
///
/// 通过code获取access_token,用户授权
///
///
///
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(resStr);
return response;
}
///
/// 通过accesstoken获取用户信息
///
///
///
///
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(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 dic = new Dictionary();
dic.Add("rid", rid);
string resStr = new WebUtil().DoPostWithJson(url, dic, null);
LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(dic)},返回:{resStr}", "发送客服消息日志");
return resStr;
}
///
/// 发送公众号模板消息
///
///
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 dic = new Dictionary();
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(resStr);
return res;
}
}
}