This repository has been archived by the owner on Sep 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WXManage.cs
312 lines (304 loc) · 10.4 KB
/
WXManage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using Ocrosoft;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Xml.Serialization;
namespace WXShare
{
/// <summary>
/// 微信接口管理
/// </summary>
public class WXManage
{
// 公众号微信号
private static string gh = "gh_7b4c81da71a3";
// 公众号appID
public static string appID = "wxe47cb091fd410b46";
// 公众号appsecret
private static string appsecret = "0a27cc9b2348b761de483e14dccd7cef";
// access_token
private static string access_token = string.Empty;
// jsapi_ticket,一般与access_token同时获取和刷新
private static string jsapi_ticket = string.Empty;
// access_token下次要刷新的时间
private static long timeStamp;
// 公众号设置的token
public static string token = "chenyanhong";
/// <summary>
/// 刷新access_token
/// </summary>
/// <returns></returns>
public static string RefreshAccessToken()
{
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
var jsonObject = ORequest.RequestGet(url, new System.Collections.Generic.Dictionary<string, string>
{
{ "appid",appID },
{"secret",appsecret }
});
// 获取access_token,计算过期时间
access_token = jsonObject["access_token"].ToString();
timeStamp = OSecurity.DateTimeToTimeStamp(DateTime.Now) +
Convert.ToInt64(jsonObject["expires_in"].ToString());
RefreshJsapiTicket();
return access_token;
}
/// <summary>
/// 只能由RefreshAccessToken()调用
/// </summary>
/// <returns></returns>
private static string RefreshJsapiTicket()
{
string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
var jsonObject = ORequest.RequestGet(url, new System.Collections.Generic.Dictionary<string, string>
{
{ "access_token",GetAccessToken() },
{"type","jsapi" }
});
// 获取ticket
jsapi_ticket = jsonObject["ticket"].ToString();
return jsapi_ticket;
}
/// <summary>
/// 获取access_token,每次调用接口都调用此函数,不要记录
/// </summary>
/// <returns></returns>
public static String GetAccessToken()
{
if (access_token == String.Empty ||
OSecurity.DateTimeToTimeStamp(DateTime.Now) > timeStamp)
{
return RefreshAccessToken();
}
return access_token;
}
/// <summary>
/// 后去jsapi_ticket,每次调用接口都调用此函数,不要记录
/// </summary>
/// <returns></returns>
public static string GetJsapiTicket()
{
if (access_token == String.Empty ||
OSecurity.DateTimeToTimeStamp(DateTime.Now) > timeStamp)
{
RefreshAccessToken();
}
return jsapi_ticket;
}
/// <summary>
/// 创建微信公众号的菜单
/// </summary>
/// <returns></returns>
public static bool CreateMenu()
{
var menuJson = "{\"button\":[{\"type\":\"view\",\"name\":\"我的\"," +
"\"url\":\"http://debug.ocrosoft.com/UserLogin.aspx\"}," +
"{\"type\":\"view\",\"name\":\"当前活动\",\"url\":\"http://debug.ocrosoft.com/Activity.aspx\"}]}";
var errcode = ORequest.RequestPost(
"https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + GetAccessToken(),
menuJson,
"errcode");
if (errcode == "0")
{
return true;
}
return false;
}
/// <summary>
/// 获取二维码地址
/// </summary>
/// <param name="content">编码二维码的内容,网址必须http(s)开头</param>
/// <returns></returns>
public static string QRCode(string content)
{
String api = "http://qr.liantu.com/api.php?text=";
api += HttpUtility.UrlEncode(content);
return api;
}
/// <summary>
/// 添加客服
/// </summary>
/// <param name="account">客服账号名</param>
/// <param name="nick">客服昵称</param>
/// <returns></returns>
public static bool AddKF(string account, string nick, string pass)
{
string postData = "{\"kf_account\":\"" + account + "@" + gh + "\",\"nickname\":\"" + nick + "\",\"password\":\"" + pass + "\"}";
var errcode = ORequest.RequestPost("https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" + GetAccessToken(),
postData,
"errcode");
if (errcode == "0")
{
return true;
}
return false;
}
/// <summary>
/// XML反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml"></param>
/// <returns></returns>
public static T FromXML<T>(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
StringReader sr = new StringReader(xml);
T obj = (T)xs.Deserialize(sr);
sr.Close();
sr.Dispose();
return obj;
}
/// <summary>
/// XML序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static String ToXML<T>(T t)
{
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add(string.Empty, string.Empty);
XmlSerializer xs = new XmlSerializer(typeof(T));
StringWriter sw = new StringWriter();
xs.Serialize(sw, t, xsn);
string str = sw.ToString();
sw.Close();
sw.Dispose();
return str;
}
/// <summary>
/// 发送客服消息
/// </summary>
/// <param name="OPENID"></param>
/// <param name="content"></param>
/// <returns></returns>
public static bool SendMessage(string OPENID, string content)
{
var jsonStr = "{\"touser\":\"" + OPENID + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + content + "\"}}";
var errcode = ORequest.RequestPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + GetAccessToken(),
jsonStr,
"errcode");
if (errcode == "0")
{
return true;
}
return false;
}
/// <summary>
/// 判断是否微信内置浏览器
/// </summary>
/// <param name="Request"></param>
/// <returns></returns>
public static bool IsWXBrowser(HttpRequest Request)
{
if (Request.UserAgent.IndexOf("MicroMessenger") == -1)
{
return false;
}
return true;
}
public static string WXJSSign(string nonce, Int64 timestamp, string url)
{
string str = "jsapi_ticket=" + GetJsapiTicket() +
"&noncestr=" + nonce +
"×tamp=" + timestamp.ToString() +
"&url=" + url;
str = OSecurity.SHA1(str);
return str;
}
}
/// <summary>
/// 消息基类
/// </summary>
[XmlRoot(ElementName = "xml")]
public class XMLObject
{
public XMLObject()
{
CreateTime = OSecurity.DateTimeToTimeStamp(DateTime.Now);
}
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName { get; set; }
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public Int64 CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public string MsgType { get; set; }
/// <summary>
/// 转换为XML,子类必须重写(抄一遍实现,不然调用此方法报错,typeof会取到基类)
/// </summary>
/// <returns></returns>
public virtual string ToXML()
{
CreateTime = OSecurity.DateTimeToTimeStamp(DateTime.Now);
return WXManage.ToXML(this);
}
}
/// <summary>
/// 文本消息,MsgType=text
/// </summary>
[XmlRoot(ElementName = "xml")]
public class TextMessageXMLObject : XMLObject
{
/// <summary>
/// 文本消息内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 消息id
/// </summary>
public string MsgId { get; set; }
public override string ToXML()
{
CreateTime = OSecurity.DateTimeToTimeStamp(DateTime.Now);
return WXManage.ToXML(this);
}
}
/// <summary>
/// 订阅事件,MsgType=event
/// </summary>
[XmlRoot(ElementName = "xml")]
public class SubscribeXMLObject : XMLObject
{
/// <summary>
/// 事件类型,subscribe、unsubscribe
/// </summary>
public string Event { get; set; }
public override string ToXML()
{
CreateTime = OSecurity.DateTimeToTimeStamp(DateTime.Now);
return WXManage.ToXML(this);
}
}
/// <summary>
/// 图文消息(外链消息),MsgType=news
/// </summary>
[XmlRoot(ElementName = "xml")]
public class ImageTextXMLObject : XMLObject
{
public int ArticleCount { get; set; }
public List<item> Articles { get; set; }
public override string ToXML()
{
CreateTime = OSecurity.DateTimeToTimeStamp(DateTime.Now);
return WXManage.ToXML(this);
}
public class item
{
public string Title { get; set; }
public string Description { get; set; }
public string PicUrl { get; set; }
public string Url { get; set; }
}
}
}