Explorar el Código

Merge branch 'master' of http://192.168.1.222:3000/yuqian/Service-Wechat

clq1010 %!s(int64=8) %!d(string=hace) años
padre
commit
7ab926803c

+ 136 - 30
ServiceWechat/ServiceWechat/Controllers/WechatApis/HomeController.cs

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

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

@@ -138,7 +138,11 @@ namespace ServiceWechat.Api.MessageHandlers
138 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 147
         public override void OnExecuting()
144 148
         {

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

@@ -0,0 +1,38 @@
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,10 +18,12 @@
18 18
   <ItemGroup>
19 19
     <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
20 20
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
21
+    <PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
21 22
     <PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
22 23
     <PackageReference Include="Senparc.Weixin" Version="4.20.2" />
23 24
     <PackageReference Include="Senparc.Weixin.MP" Version="14.10.3" />
24 25
     <PackageReference Include="Senparc.Weixin.MP.MVC" Version="5.2.0" />
26
+    <PackageReference Include="Senparc.Weixin.Open" Version="2.9.1" />
25 27
   </ItemGroup>
26 28
 
27 29
   <ItemGroup>

+ 1 - 0
ServiceWechat/ServiceWechat/appsettings.json

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