Commit 531f05c2 authored by 黄奎's avatar 黄奎

新增获取微信模板信息

parent 567511e9
......@@ -132,12 +132,21 @@ namespace Mall.Common.Plugin
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/octet-stream";
req.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
Stream myRequestStream = req.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
req.ContentType = "application/x-www-form-urlencoded";
//req.Headers.Add("Accept", "application/json");
//req.Headers.Add("Content-Type", "application/json");
byte[] data = Encoding.UTF8.GetBytes(postDataStr);
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
if (string.IsNullOrWhiteSpace(rsp.CharacterSet))
......
......@@ -10,6 +10,27 @@ namespace Mall.Common.Plugin
{
public class WeiXinHelper
{
static List<WeChatMiniAppMessage> weChatMiniAppMessages = new List<WeChatMiniAppMessage>();
static WeiXinHelper()
{
// //(拼团失败通知) 980(拼团成功通知)
weChatMiniAppMessages.Add(new WeChatMiniAppMessage()
{
Tid = "1953",
Title = "拼团失败通知",
KidList = new List<int>() {1,2,3 },
SceneDesc = "",
});
weChatMiniAppMessages.Add(new WeChatMiniAppMessage()
{
Tid = "980",
Title = "拼团成功通知",
KidList = new List<int>() {1,2,3},
SceneDesc = "",
});
}
/// <summary>
/// 获取微信用户OpenId
......@@ -40,9 +61,14 @@ namespace Mall.Common.Plugin
return openid;
}
public static string GetWeChatAccessToken(string AppId, string AppSecret)
/// <summary>
/// 获取微信订阅消息模板
/// </summary>
/// <param name="AppId"></param>
/// <param name="AppSecret"></param>
/// <returns></returns>
public static List<WeChatMiniAppMessage> GetWeChatTemplateMessage(string AppId, string AppSecret)
{
string returnStr = "";
string result = "";
string access_token = "";
try
......@@ -57,23 +83,35 @@ namespace Mall.Common.Plugin
}
if (!string.IsNullOrWhiteSpace(access_token))
{
result = "";
string templateUrl = string.Format("https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token={0}", access_token);
result = Common.Plugin.HttpHelper.HttpGet(templateUrl);
returnStr = result;
if (result != null && !string.IsNullOrWhiteSpace(result))
string addTemplate = string.Format("https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={0}",access_token);
foreach (var item in weChatMiniAppMessages)
{
JObject jObj = JObject.Parse(result);
result = "";
string newParameters = string.Format("tid={0}",item.Tid);
if (item.KidList != null && item.KidList.Count > 0)
{
int index = 0;
foreach (var subItem in item.KidList)
{
newParameters += string.Format("&kidList[{0}]={1}", index,subItem);
index++;
}
}
newParameters += string.Format("&sceneDesc={0}",item.Title);
result = Common.Plugin.HttpHelper.HttpPostStr(addTemplate, newParameters);
if (!string.IsNullOrWhiteSpace(result))
{
JObject jObj = JObject.Parse(result);
item.MessageTemplateId = jObj["priTmplId"].ToString();
}
}
//1953(拼团失败通知) 980(拼团成功通知)
}
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "GetWeChatAccessToken:result=" + result);
}
return returnStr;
return weChatMiniAppMessages;
}
}
......@@ -101,6 +139,11 @@ namespace Mall.Common.Plugin
/// 服务场景描述,15个字以内
/// </summary>
public string SceneDesc { get; set; }
/// <summary>
/// 模板消息Id
/// </summary>
public string MessageTemplateId { get; set; }
}
/// <summary>
......
......@@ -592,13 +592,13 @@ namespace Mall.WebApi.Controllers.MallBase
public ApiResult GetMessageTemplate()
{
JObject parms = JObject.Parse(RequestParm.msg.ToString());
string result = "";
var result = new object();
if (RequestParm.MiniAppId != null && !string.IsNullOrWhiteSpace(RequestParm.MiniAppId))
{
var miniProgram = programModule.GetMiniProgramModule(new Model.Extend.User.RB_MiniProgram_Extend() { MiniAppId = RequestParm.MiniAppId });
if (miniProgram != null && miniProgram.MallBaseId > 0)
{
result = Common.Plugin.WeiXinHelper.GetWeChatAccessToken(miniProgram.MiniAppId, miniProgram.MiniAppSecret);
result = Common.Plugin.WeiXinHelper.GetWeChatTemplateMessage(miniProgram.MiniAppId, miniProgram.MiniAppSecret);
}
}
return ApiResult.Success(data: result);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment