Pārlūkot izejas kodu

自动回复调整测试OK

yuqian 8 gadi atpakaļ
vecāks
revīzija
2e4f54b56a

+ 115 - 32
ServiceWechat/ServiceWechat/Controllers/WechatApis/HomeController.cs

1
 using System;
1
 using System;
2
-using System.Collections.Generic;
3
-using System.Linq;
4
-using System.Threading.Tasks;
5
 using Microsoft.AspNetCore.Mvc;
2
 using Microsoft.AspNetCore.Mvc;
6
 using Senparc.Weixin.MP;
3
 using Senparc.Weixin.MP;
7
 using Microsoft.Extensions.Configuration;
4
 using Microsoft.Extensions.Configuration;
8
 using Senparc.Weixin.MP.Entities.Request;
5
 using Senparc.Weixin.MP.Entities.Request;
9
-using ServiceWechat.Api.Controllers.Base;
10
-using Microsoft.Extensions.Logging;
11
 using NLog;
6
 using NLog;
12
 using System.Text;
7
 using System.Text;
13
 using System.IO;
8
 using System.IO;
14
-using ServiceWechat.Api.CryptCode;
15
-using ServiceWechat.Utility;
16
-using System.Xml;
17
-using System.Xml.Linq;
18
 using ServiceWechat.Api.MessageHandlers;
9
 using ServiceWechat.Api.MessageHandlers;
19
 using Senparc.Weixin.MP.MvcExtension;
10
 using Senparc.Weixin.MP.MvcExtension;
20
 using Microsoft.AspNetCore.Hosting;
11
 using Microsoft.AspNetCore.Hosting;
63
         /// </summary>
54
         /// </summary>
64
         [HttpPost]
55
         [HttpPost]
65
         [Produces("text/xml")]
56
         [Produces("text/xml")]
66
-        public ActionResult Verify(PostModel postModel)
57
+        public IActionResult Verify(PostModel postModel)
67
         {
58
         {
68
             if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
59
             if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
69
             {
60
             {
81
             CustomMessageHandler messageHandler = new CustomMessageHandler(inputStream, postModel);//接收消息
72
             CustomMessageHandler messageHandler = new CustomMessageHandler(inputStream, postModel);//接收消息
82
             messageHandler.Execute();//执行微信处理过程
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
 }

+ 4 - 0
ServiceWechat/ServiceWechat/MessageHandlers/CustomMessageHandler.cs

138
             return responseMessage;
138
             return responseMessage;
139
         }
139
         }
140
 
140
 
141
+        public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage)
142
+        {
141
 
143
 
144
+            return base.OnEvent_ScanRequest(requestMessage);
145
+        }
142
 
146
 
143
         public override void OnExecuting()
147
         public override void OnExecuting()
144
         {
148
         {

+ 38 - 0
ServiceWechat/ServiceWechat/Models/Ticket.cs

1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Threading.Tasks;
5
+
6
+namespace ServiceWechat.Api.Models
7
+{
8
+    public class Ticket
9
+    {
10
+        public Ticket()
11
+        {
12
+            //  
13
+            //TODO: 在此处添加构造函数逻辑  
14
+            //  
15
+        }
16
+
17
+        string _ticket;
18
+        string _expire_seconds;
19
+
20
+        /// <summary>  
21
+        /// 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码。  
22
+        /// </summary>  
23
+        public string ticket
24
+        {
25
+            get { return _ticket; }
26
+            set { _ticket = value; }
27
+        }
28
+
29
+        /// <summary>  
30
+        /// 凭证有效时间,单位:秒  
31
+        /// </summary>  
32
+        public string expire_seconds
33
+        {
34
+            get { return _expire_seconds; }
35
+            set { _expire_seconds = value; }
36
+        }
37
+    }
38
+}

+ 2 - 0
ServiceWechat/ServiceWechat/ServiceWechat.Api.csproj

18
   <ItemGroup>
18
   <ItemGroup>
19
     <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
19
     <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
20
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
20
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
21
+    <PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
21
     <PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
22
     <PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
22
     <PackageReference Include="Senparc.Weixin" Version="4.20.2" />
23
     <PackageReference Include="Senparc.Weixin" Version="4.20.2" />
23
     <PackageReference Include="Senparc.Weixin.MP" Version="14.10.3" />
24
     <PackageReference Include="Senparc.Weixin.MP" Version="14.10.3" />
24
     <PackageReference Include="Senparc.Weixin.MP.MVC" Version="5.2.0" />
25
     <PackageReference Include="Senparc.Weixin.MP.MVC" Version="5.2.0" />
26
+    <PackageReference Include="Senparc.Weixin.Open" Version="2.9.1" />
25
   </ItemGroup>
27
   </ItemGroup>
26
 
28
 
27
   <ItemGroup>
29
   <ItemGroup>

+ 1 - 0
ServiceWechat/ServiceWechat/appsettings.json

20
     "MchId": "1493808312", //商户号
20
     "MchId": "1493808312", //商户号
21
     "TenPayV3Notify": "http://meiwang.800100.net/wechatapi/payment/callback/", //回调地址
21
     "TenPayV3Notify": "http://meiwang.800100.net/wechatapi/payment/callback/", //回调地址
22
     "Key": "u92kcvielwoc045kigdslwod937ckw9c" //api秘钥
22
     "Key": "u92kcvielwoc045kigdslwod937ckw9c" //api秘钥
23
+
23
   }
24
   }
24
 
25
 
25
 
26