/// <summary> /// 主动发送信息 /// </summary> /// <param name="openid">openid</param> /// <param name="content">内容</param> public void SendMessage(string openid, string content) { try { UAP.JSON json = wc.POST("/cgi-bin/singlesend?t=ajax-response&f=json&token=" + token + "&lang=zh_CN", new { token = token, lang = "zh_CN", f = "json", ajax = 1, random = new Random().Next(), type = 1, content = content, tofakeid = openid, imgcode = "" }).ToJSON(); if (json.Json("base_resp").Int("ret") != 0) { Console.WriteLine(json.Json("base_resp").String("err_msg")); return; } } catch { } Console.WriteLine("发送成功"); }
/// <summary> /// 公众号机器人 /// </summary> public Robot(string user = null, string pass = null) { wc = new UAP.Net.WebClient { webSite = "https://mp.weixin.qq.com" }; wc.setLogin(delegate { wc.GET("/"); UAP.JSON json = wc.POST("/cgi-bin/login", new { username = user ?? Config.Acc_User, pwd = UAP.Function.String.MD5(pass ?? Config.Acc_Pass).ToLower(), imgcode = "", f = "json" }).ToJSON(); if (json.Json("base_resp").Int("ret") != 0) { throw new Exception(json.Json("base_resp").String("err_msg")); } string login_url = json.String("redirect_url"); if (login_url.Contains("validate_wx_tmpl")) { string admin = Regex.Match(login_url, "bindalias\\=([^\\&]+)").Result("$1"); throw new Exception("此公众号需要管理员 " + admin + " 授权验证通过。"); } token = Regex.Match(login_url, "token\\=(\\d+)").Result("$1"); Console.WriteLine("login token:" + token); }); wc.doLogin(); }
/// <summary> /// 统一下单 /// </summary> /// <param name="body">商品描述</param> /// <param name="detail">商品详情</param> /// <param name="orderNO">订单号</param> /// <param name="amt">金额(分)</param> /// <param name="openid">openid</param> /// <param name="limit_pay">是否使用信用卡支付</param> /// <param name="notify_url">支付成功回调地址</param> /// <param name="trade_type">NATIVE/JSAPI/APP</param> /// <returns>下单结果</returns> public static UAP.JSON CreateOrder(string body, string detail, string orderNO, int amt, string openid = null, bool limit_pay = true, string notify_url = null, string trade_type = null) { body = body.Replace(" ", "").Replace("\n", "").Replace("\t", ""); detail = detail.Replace(" ", "").Replace("\n", "").Replace("\t", ""); string[] parameters = { "appid=" + Config.Pay_AppID, "mch_id=" + Config.Pay_Memberid, "nonce_str=" + Guid.NewGuid().ToString("N"), "body=" + body, "detail=" + detail, "out_trade_no=" + orderNO, //"notify_url="+Config.Pay_NotifyURL, "total_fee=" + amt, "spbill_create_ip=220.115.186.146", //+Current.IP, }; List <string> paraList = parameters.ToList <string>(); if (!string.IsNullOrEmpty(trade_type)) { paraList.Add("trade_type=NATIVE"); } else { paraList.Add("trade_type=JSAPI"); paraList.Add("openid=" + openid); } if (limit_pay == false) { paraList.Add("limit_pay=no_credit"); } paraList.Add("notify_url=" + (notify_url == null ? Config.Pay_NotifyURL : notify_url)); string xmlstring = MakeXML(paraList.ToArray()); string result = UAP.Function.Http.getPost("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlstring.Replace(" ", "").Replace("\n", "").Replace("\t", ""), System.Text.Encoding.UTF8); XmlDocument xml = new XmlDocument(); xml.LoadXml(result); if (xml.SelectSingleNode("/xml/return_code").InnerText.Equals("SUCCESS")) { if (xml.SelectSingleNode("/xml/result_code").InnerText.Equals("SUCCESS")) { string prepay_id = xml.SelectSingleNode("/xml/prepay_id").InnerText; List <string> list = new List <string>(); list.Add("appId=" + Config.Pay_AppID); list.Add("timeStamp=" + UAP.Function.Datetime.GetStamp()); list.Add("nonceStr=" + Guid.NewGuid().ToString("N")); list.Add("package=prepay_id=" + prepay_id); list.Add("signType=MD5"); list.Add("paySign=" + Sign(list)); UAP.JSON ret = new UAP.JSON() { { "orderNo", orderNO } }; UAP.JSON dict = new UAP.JSON(); list.All(s => { int pos = s.IndexOf('='); dict.Add(s.Substring(0, pos), s.Substring(pos + 1)); return(true); }); XmlNode qr = xml.SelectSingleNode("/xml/code_url"); if (qr != null) { ret.Add("qrcode", WeiXin.Common.QRcode(qr.InnerText)); } ret.Add("data", dict); return(ret); } } throw new Exception(xml.SelectSingleNode("/xml/return_msg").InnerText); }