|
|
@@ -1,20 +1,11 @@
|
|
1
|
1
|
using System;
|
|
2
|
|
-using System.Collections.Generic;
|
|
3
|
|
-using System.Linq;
|
|
4
|
|
-using System.Threading.Tasks;
|
|
5
|
2
|
using Microsoft.AspNetCore.Mvc;
|
|
6
|
3
|
using Senparc.Weixin.MP;
|
|
7
|
4
|
using Microsoft.Extensions.Configuration;
|
|
8
|
5
|
using Senparc.Weixin.MP.Entities.Request;
|
|
9
|
|
-using ServiceWechat.Api.Controllers.Base;
|
|
10
|
|
-using Microsoft.Extensions.Logging;
|
|
11
|
6
|
using NLog;
|
|
12
|
7
|
using System.Text;
|
|
13
|
8
|
using System.IO;
|
|
14
|
|
-using ServiceWechat.Api.CryptCode;
|
|
15
|
|
-using ServiceWechat.Utility;
|
|
16
|
|
-using System.Xml;
|
|
17
|
|
-using System.Xml.Linq;
|
|
18
|
9
|
using ServiceWechat.Api.MessageHandlers;
|
|
19
|
10
|
using Senparc.Weixin.MP.MvcExtension;
|
|
20
|
11
|
using Microsoft.AspNetCore.Hosting;
|
|
|
@@ -63,7 +54,7 @@ namespace ServiceWechat.Api.Controllers.WechatApis
|
|
63
|
54
|
/// </summary>
|
|
64
|
55
|
[HttpPost]
|
|
65
|
56
|
[Produces("text/xml")]
|
|
66
|
|
- public ActionResult Verify(PostModel postModel)
|
|
|
57
|
+ public IActionResult Verify(PostModel postModel)
|
|
67
|
58
|
{
|
|
68
|
59
|
if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
|
|
69
|
60
|
{
|
|
|
@@ -81,31 +72,123 @@ namespace ServiceWechat.Api.Controllers.WechatApis
|
|
81
|
72
|
CustomMessageHandler messageHandler = new CustomMessageHandler(inputStream, postModel);//接收消息
|
|
82
|
73
|
messageHandler.Execute();//执行微信处理过程
|
|
83
|
74
|
|
|
84
|
|
- var responseDocumentFileName = Path.Combine(_host.ContentRootPath, string.Format("{0}_Response_{1}.txt", _getRandomFileName(), messageHandler.RequestMessage.FromUserName));
|
|
85
|
|
- var ecryptResponseDocumentFileName = Path.Combine(_host.ContentRootPath, string.Format("{0}_Response_Final_{1}.txt", _getRandomFileName(), messageHandler.RequestMessage.FromUserName));
|
|
|
75
|
+ #region 在本地记录解密前后xml:仅调试有用
|
|
|
76
|
+ //var responseDocumentFileName = Path.Combine(_host.ContentRootPath, string.Format("{0}_Response_{1}.txt", _getRandomFileName(), messageHandler.RequestMessage.FromUserName));
|
|
|
77
|
+ //var ecryptResponseDocumentFileName = Path.Combine(_host.ContentRootPath, string.Format("{0}_Response_Final_{1}.txt", _getRandomFileName(), messageHandler.RequestMessage.FromUserName));
|
|
86
|
78
|
|
|
87
|
|
- if (messageHandler.ResponseDocument != null)
|
|
88
|
|
- {
|
|
89
|
|
- using (FileStream fs = new FileStream(responseDocumentFileName, FileMode.CreateNew, FileAccess.ReadWrite))
|
|
90
|
|
- {
|
|
91
|
|
- messageHandler.ResponseDocument.Save(fs);
|
|
92
|
|
- }
|
|
93
|
|
- }
|
|
|
79
|
+ //if (messageHandler.ResponseDocument != null)
|
|
|
80
|
+ //{
|
|
|
81
|
+ // using (FileStream fs = new FileStream(responseDocumentFileName, FileMode.CreateNew, FileAccess.ReadWrite))
|
|
|
82
|
+ // {
|
|
|
83
|
+ // messageHandler.ResponseDocument.Save(fs);
|
|
|
84
|
+ // }
|
|
|
85
|
+ //}
|
|
94
|
86
|
|
|
95
|
|
- if (messageHandler.UsingEcryptMessage && messageHandler.FinalResponseDocument != null)
|
|
96
|
|
- {
|
|
97
|
|
- using (FileStream fs = new FileStream(ecryptResponseDocumentFileName, FileMode.CreateNew, FileAccess.ReadWrite))
|
|
98
|
|
- {
|
|
99
|
|
- //记录加密后的响应信息
|
|
100
|
|
- messageHandler.FinalResponseDocument.Save(fs);
|
|
101
|
|
- }
|
|
102
|
|
- }
|
|
103
|
|
- return new WeixinResult(messageHandler);//返回结果
|
|
104
|
|
- }
|
|
|
87
|
+ //if (messageHandler.UsingEcryptMessage && messageHandler.FinalResponseDocument != null)
|
|
|
88
|
+ //{
|
|
|
89
|
+ // using (FileStream fs = new FileStream(ecryptResponseDocumentFileName, FileMode.CreateNew, FileAccess.ReadWrite))
|
|
|
90
|
+ // {
|
|
|
91
|
+ // //记录加密后的响应信息
|
|
|
92
|
+ // messageHandler.FinalResponseDocument.Save(fs);
|
|
|
93
|
+ // }
|
|
|
94
|
+ //}
|
|
|
95
|
+ #endregion
|
|
105
|
96
|
|
|
106
|
|
- public string Test()
|
|
107
|
|
- {
|
|
108
|
|
- return "test";
|
|
|
97
|
+ //直接返回 xml 文件流,微信消息接口调试: 文本消息工具,返回success,有解析前后xml,但是公众号提示“该公众号提供的服务出现故障,请稍后再试”
|
|
|
98
|
+ //直接返回xml文档转为xml字符串,微信消息接口调试:解析错误
|
|
|
99
|
+
|
|
|
100
|
+ //最终返回结果是:【xml文档转为xml字符串并且去掉“换行”、“空格”后的字符串】
|
|
|
101
|
+ return new WeixinResult(messageHandler.FinalResponseDocument.ToString().Replace("\r\n", "").Replace(" ", ""));
|
|
109
|
102
|
}
|
|
|
103
|
+
|
|
|
104
|
+
|
|
|
105
|
+ #region 生成带参数的微信公众号二维码 :暂无用
|
|
|
106
|
+ //public async Task<string> GetTicket()
|
|
|
107
|
+ //{
|
|
|
108
|
+
|
|
|
109
|
+ // var accessToken = await AccessTokenContainer.GetAccessTokenAsync(AppId);
|
|
|
110
|
+
|
|
|
111
|
+ // var str = CreateTicket(accessToken);
|
|
|
112
|
+ // return str;
|
|
|
113
|
+ //}
|
|
|
114
|
+
|
|
|
115
|
+ //public string GetImage(string ticket)
|
|
|
116
|
+ //{
|
|
|
117
|
+ // string content = string.Empty;
|
|
|
118
|
+ // string strpath = string.Empty;
|
|
|
119
|
+ // string savepath = string.Empty;
|
|
|
120
|
+
|
|
|
121
|
+ // string stUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket;
|
|
|
122
|
+
|
|
|
123
|
+ // HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);
|
|
|
124
|
+
|
|
|
125
|
+ // req.Method = "GET";
|
|
|
126
|
+
|
|
|
127
|
+ // using (HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse())
|
|
|
128
|
+ // {
|
|
|
129
|
+
|
|
|
130
|
+ // strpath = myResponse.ResponseUri.ToString();
|
|
|
131
|
+
|
|
|
132
|
+ // WebClient mywebclient = new WebClient();
|
|
|
133
|
+ // var path = _host.ContentRootPath + "\\iamges\\";
|
|
|
134
|
+ // if (!Directory.Exists(path))
|
|
|
135
|
+ // {
|
|
|
136
|
+ // Directory.CreateDirectory(path);
|
|
|
137
|
+ // }
|
|
|
138
|
+
|
|
|
139
|
+ // savepath = Path.Combine(path, DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + "." + myResponse.ContentType.Split('/')[1].ToString());
|
|
|
140
|
+
|
|
|
141
|
+ // try
|
|
|
142
|
+ // {
|
|
|
143
|
+ // mywebclient.DownloadFile(strpath, savepath);
|
|
|
144
|
+ // }
|
|
|
145
|
+ // catch (Exception ex)
|
|
|
146
|
+ // {
|
|
|
147
|
+ // savepath = ex.ToString();
|
|
|
148
|
+ // }
|
|
|
149
|
+
|
|
|
150
|
+
|
|
|
151
|
+ // }
|
|
|
152
|
+ // return savepath;
|
|
|
153
|
+ //}
|
|
|
154
|
+
|
|
|
155
|
+ ///// <summary>
|
|
|
156
|
+ ///// 创建二维码ticket
|
|
|
157
|
+ ///// </summary>
|
|
|
158
|
+ ///// <returns></returns>
|
|
|
159
|
+ //public static string CreateTicket(string TOKEN)
|
|
|
160
|
+ //{
|
|
|
161
|
+
|
|
|
162
|
+ // string result = "";
|
|
|
163
|
+ // //string strJson = @"{""expire_seconds"":1800, ""action_name"": ""QR_SCENE"", ""action_info"": {""scene"": {""scene_id"":100000023}}}";
|
|
|
164
|
+ // string strJson = @"{""action_name"": ""QR_LIMIT_STR_SCENE"", ""action_info"": {""scene"": {""scene_str"": ""http://www.baidu.com""}}}";
|
|
|
165
|
+ // string wxurl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + TOKEN;
|
|
|
166
|
+
|
|
|
167
|
+ // WebClient myWebClient = new WebClient();
|
|
|
168
|
+ // myWebClient.Credentials = CredentialCache.DefaultCredentials;
|
|
|
169
|
+ // try
|
|
|
170
|
+ // {
|
|
|
171
|
+
|
|
|
172
|
+
|
|
|
173
|
+ // result = myWebClient.UploadString(wxurl, "POST", strJson);
|
|
|
174
|
+ // //WriteLog("上传result:" + result);
|
|
|
175
|
+ // Ticket _mode = JsonConvert.DeserializeObject<Ticket>(result);
|
|
|
176
|
+ // //UploadMM _mode = JsonHelper.ParseFromJson<UploadMM>(result);
|
|
|
177
|
+ // //result = _mode.ticket;
|
|
|
178
|
+ // result = _mode.ticket;
|
|
|
179
|
+ // }
|
|
|
180
|
+ // catch (Exception ex)
|
|
|
181
|
+ // {
|
|
|
182
|
+ // result = "Error:" + ex.Message;
|
|
|
183
|
+ // }
|
|
|
184
|
+ // //WriteLog("上传MediaId:" + result);
|
|
|
185
|
+
|
|
|
186
|
+ // return result;
|
|
|
187
|
+ //}
|
|
|
188
|
+ #endregion
|
|
|
189
|
+
|
|
|
190
|
+
|
|
|
191
|
+
|
|
|
192
|
+
|
|
110
|
193
|
}
|
|
111
|
194
|
}
|