Exemplo n.º 1
0
        private static JSApiTicketModel GetJSApiTicketFromWeChat(string AppId, string access_token)
        {
            if (string.IsNullOrWhiteSpace(access_token))
            {
                throw new ArgumentNullException(nameof(access_token));
            }

            string errorInfo = (string)CacheHelper.GetCache(keyState(AppId));

            if (!string.IsNullOrWhiteSpace(errorInfo) && errorInfo.Contains(Common.error))
            {
                throw new AggregateException(errorInfo + " 如果确认故障已解除,请回收应用程序池后再试");
            }

            if (GlobalFlag.Instance.wxJSApiTicket_IsBusy)
            {
                throw new ApplicationException("已经有一个请求正在进行,请稍后再试");
            }

            //获取新票据
            string url = Common.ApiUrl + string.Format("ticket/getticket?access_token={0}&type=jsapi", access_token);

            try
            {
                GlobalFlag.Instance.wxJSApiTicket_IsBusy = true;
                string           result = HttpRequestHelper.Request(url);
                JSApiTicketModel model  = JsonHelper.DeSerialize <JSApiTicketModel>(result);
                if (null != model && !string.IsNullOrWhiteSpace(model.ticket) && model.expires_in > 0)
                {
                    GlobalFlag.Instance.wxJSApiTicket_IsBusy = false;
                    return(model);
                }
                else
                {
                    CacheHelper.SetCache(keyState(AppId), string.Concat("Request API Ticket ", Common.error));
                    throw new AggregateException("获取Ticket票据失败,url=" + url + ",result=" + result);
                }
            }
            catch (Exception Ex)
            {
                GlobalFlag.Instance.wxJSApiTicket_IsBusy = false;
                LogHelper.Save(Ex);
                CacheHelper.SetCache(keyState(AppId), string.Concat("Request API Ticket ", Common.error));
                throw Ex;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 查询所有分组
 /// </summary>
 /// <returns>GroupsModel</returns>
 public static GroupsModel GetGroup(string access_token)
 {
     try
     {
         string url    = Common.ApiUrl + string.Format("groups/get?access_token={0}", access_token);
         string result = HttpRequestHelper.Request(url, HttpRequestHelper.Method.GET);
         if (!string.IsNullOrEmpty(result))
         {
             return(JsonHelper.DeSerialize <GroupsModel>(result));
         }
     }
     catch (Exception Ex)
     {
         LogHelper.Save(Ex, "GetGroup", LogTime.day);
         throw Ex;
     }
     return(new GroupsModel());
 }
Exemplo n.º 3
0
 /// <summary>
 /// 获取关注者列表,返回JSON数据包,公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。
 /// </summary>
 /// <param name="next_openid">第一个拉取的OPENID,不填默认从头开始拉取</param>
 /// <returns>UserListModel</returns>
 public static UserListModel GetUsers(string next_openid, string access_token)
 {
     try
     {
         string url    = Common.ApiUrl + string.Format("user/get?access_token={0}&next_openid={1}", access_token, string.IsNullOrEmpty(next_openid) ? string.Empty : next_openid);
         string result = HttpRequestHelper.Request(url, HttpRequestHelper.Method.GET);
         if (!string.IsNullOrEmpty(result))
         {
             return(JsonHelper.DeSerialize <WeChatHelper4Net.Models.UserListModel>(result));
         }
     }
     catch (Exception Ex)
     {
         LogHelper.Save(Ex, "GetUsers", LogTime.day);
         throw Ex;
     }
     return(new UserListModel());
 }
Exemplo n.º 4
0
        /// <summary>
        /// 检验授权凭证(access_token)是否有效
        /// </summary>
        /// <param name="access_token">访问令牌</param>
        /// <param name="openid"></param>
        /// <returns></returns>
        public static bool CheckAccessToken(string access_token, string openid)
        {
            if (string.IsNullOrWhiteSpace(access_token) || string.IsNullOrWhiteSpace(openid))
            {
                return(false);
            }

            string url    = string.Format("https://api.weixin.qq.com/sns/auth?access_token={0}&openid={1}", access_token, openid);
            string result = HttpRequestHelper.Request(url);
            RequestResult_errmsg resultEntity = JsonHelper.DeSerialize <RequestResult_errmsg>(result);

            if (null != resultEntity && 0 == resultEntity.errcode)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// 查询用户所在分组
 /// </summary>
 /// <param name="openid">用户的OpenID</param>
 /// <returns>GroupModel</returns>
 public static GroupModel GetGroupID(string openid, string access_token)
 {
     if (string.IsNullOrEmpty(openid))
     {
         return(new GroupModel());
     }
     try
     {
         string url  = Common.ApiUrl + string.Format("groups/getid?access_token={0}", access_token);
         string data = "{\"openid\":\"OpenId\"}";
         data = data.Replace("OpenId", openid);
         string result = HttpRequestHelper.Request(url, data, HttpRequestHelper.Method.POST, System.Text.Encoding.UTF8);
         if (!string.IsNullOrEmpty(result))
         {
             return(JsonHelper.DeSerialize <WeChatHelper4Net.Models.GroupModel>(result));
         }
     }
     catch (Exception Ex)
     {
         LogHelper.Save(Ex, "GetGroupID", LogTime.day);
         throw Ex;
     }
     return(new GroupModel());
 }
Exemplo n.º 6
0
        /// <summary>
        /// 获取用户基本信息,返回JSON数据包
        /// </summary>
        /// <param name="openid">普通用户的标识,对当前公众号唯一</param>
        /// <returns>UserModel</returns>
        public static UserModel GetUserInfo(string openid, string access_token)
        {
            if (string.IsNullOrEmpty(openid))
            {
                return(new UserModel());
            }
            string result = string.Empty;

            try
            {
                string url = Common.ApiUrl + string.Format("user/info?access_token={0}&openid={1}&lang=zh_CN", access_token, openid);
                result = HttpRequestHelper.Request(url, HttpRequestHelper.Method.GET);
                if (!string.IsNullOrEmpty(result))
                {
                    return(JsonHelper.DeSerialize <UserModel>(StringHelper.RemoveSpecialChar(result)));
                }
            }
            catch (Exception Ex)
            {
                LogHelper.Save("【Message】:" + Ex.Message + "          【StackTrace】:" + Ex.StackTrace + "\n\r" + result, "GetUserInfo", LogType.Error, LogTime.day);
                throw Ex;
            }
            return(new UserModel());
        }
Exemplo n.º 7
0
 /// <summary>
 /// 创建分组
 /// </summary>
 /// <param name="name">分组名字(30个字符以内)</param>
 /// <returns>JSON数据包</returns>
 public static GroupSubModel CreateGroup(string name, string access_token)
 {
     if (string.IsNullOrEmpty(name))
     {
         return(new GroupSubModel());
     }
     try
     {
         string url  = Common.ApiUrl + string.Format("groups/create?access_token={0}", access_token);
         string data = "{\"group\":{\"name\":\"GroupName\"}}";
         data = data.Replace("GroupName", name);
         string result = HttpRequestHelper.Request(url, data, HttpRequestHelper.Method.POST, System.Text.Encoding.UTF8);
         if (!string.IsNullOrEmpty(result))
         {
             return(JsonHelper.DeSerialize <WeChatHelper4Net.Models.GroupSubModel>(result));
         }
     }
     catch (Exception Ex)
     {
         LogHelper.Save(Ex, "CreateGroup", LogTime.day);
         throw Ex;
     }
     return(new GroupSubModel());
 }
Exemplo n.º 8
0
 /// <summary>
 /// 获取永久素材(临时)
 /// </summary>
 /// <param name="ACCESS_TOKEN">访问令牌</param>
 /// <param name="MEDIA_ID"></param>
 /// <returns></returns>
 public static TempVideo GetTempVideo(string ACCESS_TOKEN, string MEDIA_ID)
 {
     if (string.IsNullOrEmpty(MEDIA_ID))
     {
         return(new TempVideo());
     }
     try
     {
         string url        = Common.ApiUrl + string.Format("media/get?access_token={0}&media_id={1}", ACCESS_TOKEN, MEDIA_ID);
         var    jsonString = "";
         string result     = HttpRequestHelper.Request(url, jsonString, HttpRequestHelper.Method.GET, System.Text.Encoding.UTF8);
         //LogHelper.Save("GetTempVideo > " + "ACCESS_TOKEN=" + ACCESS_TOKEN + ",MEDIA_ID=" + MEDIA_ID + ",result=" + result, nameof(Multimedia), LogType.Common, LogTime.day);
         if (!string.IsNullOrEmpty(result))
         {
             return(JsonHelper.DeSerialize <TempVideo>(StringHelper.RemoveSpecialChar(result)));
         }
     }
     catch (Exception Ex)
     {
         LogHelper.Save(Ex);
         throw Ex;
     }
     return(new TempVideo());
 }
Exemplo n.º 9
0
        /// <summary>
        /// 获取自定义菜单配置
        /// </summary>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public static string GetCurrentSelfMenu(string access_token)
        {
            string url = Common.ApiUrl + string.Format("get_current_selfmenu_info?access_token={0}", access_token);

            return(HttpRequestHelper.Request(url));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 删除当前使用的自定义菜单
        /// </summary>
        /// <returns></returns>
        public static string DeleteMenu(string access_token)
        {
            string url = Common.ApiUrl + string.Format("menu/delete?access_token={0}", access_token);

            return(HttpRequestHelper.Request(url));
        }
Exemplo n.º 11
0
        /// <summary>
        /// 微信支付统一下单接口
        /// </summary>
        /// <param name="unifiedorder">统一下单参数</param>
        /// <param name="time">当前时间</param>
        /// <returns></returns>
        public static unifiedorderResultModel unifiedorder(unifiedorderModel unifiedorder, DateTime time)
        {
            unifiedorder.out_trade_no = !string.IsNullOrWhiteSpace(unifiedorder.out_trade_no) ? unifiedorder.out_trade_no : (Privacy.PartnerID + time.ToString("yyyyMMddHHmmss") + RandomCode.createRandomCode(3, true));
            unifiedorder.fee_type     = !string.IsNullOrWhiteSpace(unifiedorder.fee_type) ? unifiedorder.fee_type : "CNY";
            unifiedorder.time_start   = !string.IsNullOrWhiteSpace(unifiedorder.time_start) ? unifiedorder.time_start : time.ToString("yyyyMMddHHmmss");
            unifiedorder.trade_type   = !string.IsNullOrWhiteSpace(unifiedorder.trade_type) ? unifiedorder.trade_type : "JSAPI";

            unifiedorder.appid     = Privacy.AppId;
            unifiedorder.mch_id    = Privacy.PartnerID;
            unifiedorder.nonce_str = RandomCode.createRandomCode(16, true);
            unifiedorder.sign      = "";

            Dictionary <string, string> unifiedorderParams = new Dictionary <string, string>();

            unifiedorderParams.Add("appid", unifiedorder.appid);
            unifiedorderParams.Add("mch_id", unifiedorder.mch_id);
            unifiedorderParams.Add("device_info", unifiedorder.device_info);
            unifiedorderParams.Add("nonce_str", unifiedorder.nonce_str);
            unifiedorderParams.Add("sign", unifiedorder.sign);
            unifiedorderParams.Add("body", unifiedorder.body);
            unifiedorderParams.Add("detail", unifiedorder.detail);
            unifiedorderParams.Add("attach", unifiedorder.attach);
            unifiedorderParams.Add("out_trade_no", unifiedorder.out_trade_no);
            unifiedorderParams.Add("fee_type", unifiedorder.fee_type);
            unifiedorderParams.Add("total_fee", unifiedorder.total_fee.ToString());
            unifiedorderParams.Add("spbill_create_ip", unifiedorder.spbill_create_ip);
            unifiedorderParams.Add("time_start", unifiedorder.time_start);
            unifiedorderParams.Add("time_expire", unifiedorder.time_expire);
            unifiedorderParams.Add("goods_tag", unifiedorder.goods_tag);
            unifiedorderParams.Add("notify_url", unifiedorder.notify_url);
            unifiedorderParams.Add("trade_type", unifiedorder.trade_type);
            unifiedorderParams.Add("product_id", unifiedorder.product_id);
            unifiedorderParams.Add("openid", unifiedorder.openid);

            string sign    = "";
            string package = GetPackage(unifiedorderParams, out sign);

            unifiedorder.sign          = sign;
            unifiedorderParams["sign"] = sign;

            /*
             * 注:参数值用XML转义即可,CDATA标签用于说明数据不被XML解析器解析。
             */
            string unifiedorderXml = unifiedorderParams.ToXml();

            unifiedorderXml = unifiedorderXml.Replace("<unifiedorderModel>", "<xml>").Replace("</unifiedorderModel>", "</xml>");
            string resultXml = string.Empty;

            try
            {
                /*
                 * 得到最终发送的数据:
                 */
                resultXml = HttpRequestHelper.Request("https://api.mch.weixin.qq.com/pay/unifiedorder", unifiedorderXml, HttpRequestHelper.Method.POST, System.Text.Encoding.UTF8);
                resultXml = resultXml.Replace("<xml>", "<unifiedorderResultModel>").Replace("</xml>", "</unifiedorderResultModel>");
                unifiedorderResultModel result = WeChatHelper4Net.XmlHelper.DeSerialize <unifiedorderResultModel>(resultXml);
                return(result);
            }
            catch (Exception Ex)
            {
                LogHelper.Save("Pay > unifiedorder:unifiedorderXml=" + unifiedorderXml + ",resultXml=" + resultXml, "Pay", LogType.Error, LogTime.day);
                throw Ex;
            }
        }