Example #1
0
        /// <summary>
        /// 获取预授权码
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static RequestApiResult <WeixinThirdPartyGetPreAuthCodeResult> GetPreAuthCode(
            string accessToken, string appid)
        {
            RequestApiResult <WeixinThirdPartyGetPreAuthCodeResult> result =
                new RequestApiResult <WeixinThirdPartyGetPreAuthCodeResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={0}",
                accessToken);
            requestArgs.Content = JsonConvert.SerializeObject(new { component_appid = appid });

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Success)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinThirdPartyGetPreAuthCodeResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
        /// <summary>
        /// 根据分组进行群发【订阅号与服务号认证后均可用】
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinGroupMessageSendAllResult> SendAll(string access_token, WeixinGroupMessageSendAllArgs args)
        {
            /*
             * 此处返回的JSON又傻逼一样的和其它接口不同
             * {
             *     "errcode":0,
             *     "errmsg":"send job submission success",
             *     "msg_id":34182,
             *     "msg_data_id": 206227730
             *  }
             *
             */

            WeixinRequestApiResult <WeixinGroupMessageSendAllResult> result =
                new WeixinRequestApiResult <WeixinGroupMessageSendAllResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinGroupMessageSendAllResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// 使用授权码换取公众号的授权信息
        /// </summary>
        public static RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> GetAuthorizationInfo(
            string accessToken, WeixinThirdPartyGetAuthorizationInfoArgs args)
        {
            RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> result =
                new RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={0}",
                accessToken);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Success)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinThirdPartyGetAuthorizationInfoResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
        /// <summary>
        /// 小程序登录
        /// </summary>
        public static WeixinRequestApiResult <WeixinMiniProgramJsCodeToSessionResult> JsCodeToSession(
            WeixinMiniProgramJsCodeToSessionArgs args)
        {
            WeixinRequestApiResult <WeixinMiniProgramJsCodeToSessionResult> result =
                new WeixinRequestApiResult <WeixinMiniProgramJsCodeToSessionResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code",
                args.AppId, args.AppSecret, args.JsCode);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinMiniProgramJsCodeToSessionResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
        /// <summary>
        /// 获取永久图文消息素材列表
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinGetArticleMaterialListResult> GetArticleMaterialList(string access_token, WeixinGetMaterialListArgs args)
        {
            args.Type = MaterialType.News;

            WeixinRequestApiResult <WeixinGetArticleMaterialListResult> result =
                new WeixinRequestApiResult <WeixinGetArticleMaterialListResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinGetArticleMaterialListResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #6
0
        /// <summary>
        /// 获取授权方的账户信息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static RequestApiResult <WeixinThirdPartyGetAuthorizerAccountInfoResult> GetAuthorizerAccountInfo(
            string accessToken, WeixinThirdPartyGetAuthorizerAccountInfoArgs args)
        {
            /*
             * 注意,这个接口官方页面给出的返回JSON有错误
             * qrcode_url 是 authorizer_info 的属性,而不是根级别的属性
             */

            RequestApiResult <WeixinThirdPartyGetAuthorizerAccountInfoResult> result =
                new RequestApiResult <WeixinThirdPartyGetAuthorizerAccountInfoResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token={0}",
                accessToken);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Success)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinThirdPartyGetAuthorizerAccountInfoResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
        public static WeixinRequestApiResult <WeixinCreateShortUrlResult> GetShortUrl(string access_token, string longUrl)
        {
            WeixinCreateShortUrlArgs args = new WeixinCreateShortUrlArgs();

            args.LongUrl = longUrl;

            WeixinRequestApiResult <WeixinCreateShortUrlResult> result =
                new WeixinRequestApiResult <WeixinCreateShortUrlResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/shorturl?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinCreateShortUrlResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #8
0
        public static string Handle(string message)
        {
            XElement xml = XElement.Parse(message);

            string messageType = xml.XPathSelectElement("MsgType").Value;

            if (String.IsNullOrEmpty(messageType))
            {
                return(String.Empty);
            }

            _fromUserName = xml.XPathSelectElement("FromUserName").Value;

            //事件消息
            if (messageType == "event")
            {
                string eventType = xml.XPathSelectElement("Event").Value;
                if (String.IsNullOrEmpty(eventType))
                {
                    return(String.Empty);
                }

                ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage();
                textMessage.Content      = eventType + "from_callback";
                textMessage.ToUserName   = _fromUserName;
                textMessage.FromUserName = UserName;
                textMessage.CreateTime   = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now);

                return(XMLMessageHelper.XmlSerialize(textMessage));
            }
            //普通消息
            else
            {
                string content = xml.XPathSelectElement("Content").Value;
                if (content == "TESTCOMPONENT_MSG_TYPE_TEXT")
                {
                    ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage();
                    textMessage.Content      = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
                    textMessage.ToUserName   = _fromUserName;
                    textMessage.FromUserName = UserName;
                    textMessage.CreateTime   = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now);

                    return(XMLMessageHelper.XmlSerialize(textMessage));
                }
                else
                {
                    _query_auth_code = content.Remove(0, "QUERY_AUTH_CODE:".Length);

                    RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> getAuthorizationInfo =
                        ThirdPartyApiWrapper.GetAuthorizationInfo(_query_auth_code);

                    _accessToken = getAuthorizationInfo.ApiResult.AuthorizationInfo.AccessToken;

                    _timer = new System.Threading.Timer(SendKFMessage, null, 2 * 1000, 1 * 60 * 1000);

                    return(null);
                }
            }
        }
        protected override string Handle(ReceivingXMLMessage_TextMessage message, ClientDomainContext domainContext)
        {
            //存储消息记录
            MessageEntity messageEntity = new MessageEntity();

            messageEntity.Receive = true;
            messageEntity.ReceivingMessageType = EnumReceivingMessageType.Text;

            messageEntity.OfficalWeixinId = message.ToUserName;
            messageEntity.MemberOpenId    = message.FromUserName;
            messageEntity.CreateTime      = WeixinApiHelper.ConvertIntToDateTime(message.CreateTime);
            messageEntity.MsgId           = message.MsgId;

            messageEntity.Content = message.Content;
            _messageManager.AddMessage(domainContext.Domain.Id, domainContext.AppId, messageEntity);

            bool   handled            = false;
            string replyMessageString = null;

            //判断是否存在关键词自动回复
            if (domainContext.AutoReplyOnKeyWords != null && domainContext.AutoReplyOnKeyWords.RuleList != null &&
                domainContext.AutoReplyOnKeyWords.RuleList.Count > 0)
            {
                //这里无法直接返回一个string去响应微信服务发出的http请求
                //因为那样的话只能回复一条消息,要回复多条消息必须得调用客服接口去发
                List <KFMessageBase> replyMessageList = domainContext.AutoReplyOnKeyWords.GetReplyMessage(domainContext, message);
                if (replyMessageList != null && replyMessageList.Count > 0)
                {
                    foreach (var item in replyMessageList)
                    {
                        KFApiWrapper.Send(domainContext, item);
                    }
                    handled = true;
                }
            }

            //如果没有命中关键字回复,则看有没有消息自动回复
            if (handled == false && domainContext.AutoReplyOnMessage != null)
            {
                ResponsiveXMLMessageBase replyMessage =
                    AutoReplyHelper.GetXMLMessage(domainContext, message.FromUserName, domainContext.AutoReplyOnMessage);

                if (replyMessage != null)
                {
                    handled            = true;
                    replyMessageString = XMLMessageHelper.XmlSerialize(replyMessage);
                }
            }

            return(replyMessageString);
        }
Example #10
0
        protected string GetReplyTextMessage(DomainContext domainContext, string toUserName, string content)
        {
            ResponsiveXMLMessage_TextMessage replyMessage = new ResponsiveXMLMessage_TextMessage();

            replyMessage.Content = content;

            replyMessage.ToUserName = toUserName;
            //这几个字段还是要的,因为当直接以HTTP返回的形式返回XML格式的数据时
            //是要求这几个字段的
            replyMessage.FromUserName = domainContext.UserName;
            replyMessage.CreateTime   = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now);

            return(XMLMessageHelper.XmlSerialize(replyMessage));
        }
Example #11
0
        //TODO:这里应该用 ResponsiveXMLMessage
        /// <summary>
        /// 获取XML版的回复消息
        /// </summary>
        /// <param name="domainContext"></param>
        /// <param name="toUserName"></param>
        /// <param name="autoReply"></param>
        /// <returns></returns>
        public static ResponsiveXMLMessageBase GetXMLMessage(ClientDomainContext domainContext, string toUserName,
                                                             IAutoReply autoReply)
        {
            if (autoReply == null)
            {
                return(null);
            }

            ResponsiveXMLMessageBase replyMessage = null;

            switch (autoReply.Type)
            {
            case EnumAutoReplyType.Text:
                //不能返回空白 Content 的XML给微信 API
                //否则它会在客户端提示该公众号暂时无法提供服务
                if (String.IsNullOrEmpty(autoReply.Content) == false)
                {
                    ResponsiveXMLMessage_TextMessage textMessage = new ResponsiveXMLMessage_TextMessage();
                    textMessage.Content = autoReply.Content;
                    replyMessage        = textMessage;
                }
                break;

            case EnumAutoReplyType.Image:
                if (String.IsNullOrEmpty(autoReply.MediaId) == false)
                {
                    ResponsiveXMLMessage_ImageMessage imageMessage = new ResponsiveXMLMessage_ImageMessage();
                    imageMessage.Image.MediaId = autoReply.MediaId;
                    replyMessage = imageMessage;
                }
                break;

            default:
                Debug.Assert(false, "GetXMLMessage 不支持的 AutoReplyOnKeyWordsRule.Type:" + autoReply.Type.ToString());
                _log.Write("GetXMLMessage 不支持的 AutoReplyOnKeyWordsRule.Type:" + autoReply.Type.ToString(),
                           domainContext.AutoReplyOnSubscribe.Type.ToString(), TraceEventType.Error);
                break;
            }

            if (replyMessage != null)
            {
                replyMessage.ToUserName = toUserName;
                //这几个字段还是要的,因为当直接以HTTP返回的形式返回XML格式的数据时
                //是要求这几个字段的
                replyMessage.FromUserName = domainContext.UserName;
                replyMessage.CreateTime   = WeixinApiHelper.ConvertDateTimeToInt(DateTime.Now);
            }
            return(replyMessage);
        }
Example #12
0
        protected override string Handle(ReceivingXMLMessage_ShortVideoMessage message, ClientDomainContext domainContext)
        {
            //存储消息记录
            MessageEntity messageEntity = new MessageEntity();

            messageEntity.Receive = true;
            messageEntity.ReceivingMessageType = EnumReceivingMessageType.Shortvideo;

            messageEntity.OfficalWeixinId = message.ToUserName;
            messageEntity.MemberOpenId    = message.FromUserName;
            messageEntity.CreateTime      = WeixinApiHelper.ConvertIntToDateTime(message.CreateTime);
            messageEntity.MsgId           = message.MsgId;

            messageEntity.ThumbMediaId = message.ThumbMediaId;
            messageEntity.MediaId      = message.MediaId;

            _messageManager.AddMessage(domainContext.Domain.Id, domainContext.AppId, messageEntity);

            //向文件服务器发起文件异步下载请求
            FileDownloadQueueWithMediaIdArgs args = new FileDownloadQueueWithMediaIdArgs();

            args.Domain      = domainContext.Domain.Id;
            args.AppId       = domainContext.AppId;
            args.MediaId     = message.MediaId;
            args.CallbackUrl = _fileDownloadCallbackUrl;
            args.Tag         = messageEntity.Id.ToString();

            ApiResult apiResult = _fileService.DownloadQueueWithMediaId(args);

            if (apiResult.Success == false)
            {
                _log.Write("队列下载请求失败", apiResult.Message, TraceEventType.Warning);
            }

            //下载缩略图
            args.MediaId     = message.ThumbMediaId;
            args.CallbackUrl = _thumbFileDownloadCallbackUrl;

            apiResult = _fileService.DownloadQueueWithMediaId(args);
            if (apiResult.Success == false)
            {
                _log.Write("队列下载请求失败", apiResult.Message, TraceEventType.Warning);
            }

            return(null);
        }
        public ActionResult Deposit()
        {
            CashAccountDepositArgs args = RequestArgs <CashAccountDepositArgs>();

            if (args == null || args.Fee <= 0)
            {
                return(RespondResult(false, "参数无效。"));
            }

            args.Fee = args.Fee * 100;

            args.MemberId       = MemberContext.Member.Id;
            args.OpenId         = MemberContext.Member.OpenId;
            args.SpbillCreateIp = Request.UserHostAddress;

            NormalResult <CreatePayOrderResult> depositResult =
                _payManager.Deposit(args, DomainContext.AuthorizerPay);

            if (depositResult.Success)
            {
                WxPayData jsApiParam = new WxPayData(DomainContext.AuthorizerPay.Key);
                jsApiParam.SetValue("appId", DomainContext.AppId);
                jsApiParam.SetValue("timeStamp", WeixinApiHelper.GetTimesTamp().ToString());
                jsApiParam.SetValue("nonceStr", WeixinApiHelper.GetNonceStr());
                jsApiParam.SetValue("package", "prepay_id=" + depositResult.Data.PrepayId);
                jsApiParam.SetValue("signType", "MD5");
                jsApiParam.SetValue("paySign", jsApiParam.MakeSign());

                string parameters = jsApiParam.ToJson();

                //WeixinPayGetBrandWCPayRequestArgs result = new WeixinPayGetBrandWCPayRequestArgs();
                //result.AppId = this.DomainContext.AppId;
                //result.TimeStamp = WeixinApiHelper.GetTimesTamp().ToString();
                //result.NonceStr = WeixinApiHelper.GetNonceStr();
                //result.Package = "prepay_id=" + depositResult.Data.PrepayId;
                //result.SignType = "MD5";

                return(RespondDataResult(parameters));
            }
            else
            {
                return(RespondResult(false, depositResult.Message));
            }
        }
        /// <summary>
        /// 删除永久素材
        /// 可以删除公众号在公众平台官网素材管理模块中新建的图文消息、语音、视频等素材
        /// http://mp.weixin.qq.com/wiki/17/630699b9593f9a53725c26719d522101.html
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="mediaId"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult RemoveMaterial(string access_token, string mediaId)
        {
            WeixinRequestApiResult result      = new WeixinRequestApiResult();
            HttpRequestArgs        requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(new { media_id = mediaId });

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiError = WeixinApiHelper.Parse(result.HttpRequestResult.Content);
            }

            return(result);
        }
        /// <summary>
        /// 自定义菜单创建接口
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="accessToken"></param>
        public static WeixinRequestApiResult Create(ButtonWrapper menu, string accessToken)
        {
            WeixinRequestApiResult result = new WeixinRequestApiResult();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method  = "POST";
            requestArgs.Url     = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
            requestArgs.Content = JsonConvert.SerializeObject(menu);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiError = WeixinApiHelper.Parse(result.HttpRequestResult.Content);
            }

            return(result);
        }
        /// <summary>
        /// 修改永久图文素材
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult UpdateArticleMaterial(string access_token, WeixinUpdateArticleMaterialArgs args)
        {
            WeixinRequestApiResult result      = new WeixinRequestApiResult();
            HttpRequestArgs        requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiError = WeixinApiHelper.Parse(result.HttpRequestResult.Content);
            }

            return(result);
        }
Example #17
0
        protected override string Handle(ReceivingXMLMessage_LinkMessage message, ClientDomainContext domainContext)
        {
            //存储消息记录
            MessageEntity messageEntity = new MessageEntity();

            messageEntity.Receive = true;
            messageEntity.ReceivingMessageType = EnumReceivingMessageType.Link;

            messageEntity.OfficalWeixinId = message.ToUserName;
            messageEntity.MemberOpenId    = message.FromUserName;
            messageEntity.CreateTime      = WeixinApiHelper.ConvertIntToDateTime(message.CreateTime);
            messageEntity.MsgId           = message.MsgId;

            messageEntity.Link_Title       = message.Title;
            messageEntity.Link_Description = message.Description;
            messageEntity.Link_Url         = message.Url;

            _messageManager.AddMessage(domainContext.Domain.Id, domainContext.AppId, messageEntity);

            return(null);
        }
Example #18
0
        /// <summary>
        /// 批量移动用户分组
        /// 用户唯一标识符openid的列表(size不能超过50)
        /// 如果仅传一个不存在的OpenId,微信API会返回空串
        /// 如果用户列表中的OpenId,有存在的,有不存在的,则返回正常OK的json
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult SetUserListGroup(string access_token, WeixinSetUserListGroupArgs args)
        {
            WeixinRequestApiResult result = new WeixinRequestApiResult();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiError = WeixinApiHelper.Parse(result.HttpRequestResult.Content);
            }

            return(result);
        }
        /// <summary>
        /// 上传图文消息内的图片获取URL
        /// 请注意,在图文消息的具体内容中,将过滤外部的图片链接,开发者可以通过下述接口上传图片得到URL,放到图文内容中使用。
        /// http://mp.weixin.qq.com/wiki/10/10ea5a44870f53d79449290dfd43d006.html
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="file"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinUploadImgResult> UploadImg(string access_token, string file)
        {
            WeixinRequestApiResult <WeixinUploadImgResult> result = new WeixinRequestApiResult <WeixinUploadImgResult>();
            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}", access_token);
            requestArgs.File = file;

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinUploadImgResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #20
0
        /// <summary>
        /// 发消息
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult Send(string access_token, KFMessageBase message)
        {
            WeixinRequestApiResult result = new WeixinRequestApiResult();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(message);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiError = WeixinApiHelper.Parse(result.HttpRequestResult.Content);
            }

            return(result);
        }
        /// <summary>
        /// 获取前端 WeixinJSBridge.invoke 使用的支付参数的 json 字符串
        /// </summary>
        /// <returns></returns>
        public ActionResult GetBrandWCPayRequestArgs()
        {
            string strPayOrderId = Request.QueryString["payOrderId"];
            Guid   payOrderId    = Guid.Empty;

            if (String.IsNullOrEmpty(strPayOrderId) || Guid.TryParse(strPayOrderId, out payOrderId) == false)
            {
                return(RespondResult(false, "参数无效。"));
            }

            PayOrderEntity payOrder = _payManager.GetPayOrder(payOrderId);

            if (payOrder == null)
            {
                return(RespondResult(false, "指定的支付订单不存在。"));
            }

            if (payOrder.Member != MemberContext.Member.Id)
            {
                return(RespondResult(false, "订单不属于您。"));
            }

            if (payOrder.TradeState != EnumPayTradeState.NOTPAY)
            {
                return(RespondResult(false, "订单状态不是待支付状态。"));
            }

            WxPayData jsApiParam = new WxPayData(DomainContext.AuthorizerPay.Key);

            jsApiParam.SetValue("appId", DomainContext.AppId);
            jsApiParam.SetValue("timeStamp", WeixinApiHelper.GetTimesTamp().ToString());
            jsApiParam.SetValue("nonceStr", WeixinApiHelper.GetNonceStr());
            jsApiParam.SetValue("package", "prepay_id=" + payOrder.PrepayId);
            jsApiParam.SetValue("signType", "MD5");
            jsApiParam.SetValue("paySign", jsApiParam.MakeSign());

            string parameters = jsApiParam.ToJson();

            return(RespondDataResult(parameters));
        }
Example #22
0
        /// <summary>
        /// 独立运行时获取网页授权access token
        /// http://mp.weixin.qq.com/wiki/9/01f711493b5a02f24b04365ac5d8fd95.html
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinWebAccessTokenResult> GetWebAccessToken(string appid, string appSecret, string code)
        {
            WeixinRequestApiResult <WeixinWebAccessTokenResult> result =
                new WeixinRequestApiResult <WeixinWebAccessTokenResult>();

            HttpRequestArgs args = new HttpRequestArgs();

            args.Method = "GET";
            args.Url    = String.Format(
                "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code",
                appid, appSecret, code);

            result.HttpRequestResult = _httpService.Request(args);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinWebAccessTokenResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #23
0
        public static WeixinRequestApiResult <WeixinGetJsApiTicketResult> GetJsApiTicket(string accessToken)
        {
            WeixinRequestApiResult <WeixinGetJsApiTicketResult> result =
                new WeixinRequestApiResult <WeixinGetJsApiTicketResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "GET";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", accessToken);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinGetJsApiTicketResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
        /// <summary>
        /// 新增永久素材
        /// http://mp.weixin.qq.com/wiki/0/21cad57369e3652f6be542c1e3a531ec.html
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="file"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinAddMaterialResult> AddNormalMaterial(string access_token, string file, MaterialType type)
        {
            WeixinRequestApiResult <WeixinAddMaterialResult> result = new WeixinRequestApiResult <WeixinAddMaterialResult>();
            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type={1}",
                access_token, EnumHelper.GetEnumMemberValue(type));
            requestArgs.File = file;

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinAddMaterialResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #25
0
        /// <summary>
        /// 获取access token
        /// http://mp.weixin.qq.com/wiki/2/88b2bf1265a707c031e51f26ca5e6512.html
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinAccessTokenResult> GetAccessToken(string appid, string appSecret)
        {
            WeixinRequestApiResult <WeixinAccessTokenResult> result =
                new WeixinRequestApiResult <WeixinAccessTokenResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "GET";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}",
                appid, appSecret);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinAccessTokenResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #26
0
        /// <summary>
        /// 获取第三方平台access_token
        /// </summary>
        public static RequestApiResult <WeixinThirdPartyGetAccessTokenResult> GetAccessToken(
            WeixinThirdPartyGetAccessTokenArgs args)
        {
            RequestApiResult <WeixinThirdPartyGetAccessTokenResult> result =
                new RequestApiResult <WeixinThirdPartyGetAccessTokenResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method  = "POST";
            requestArgs.Url     = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Success)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinThirdPartyGetAccessTokenResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #27
0
        /// <summary>
        /// 查询用户所在分组
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinGetUserGroupIdResult> GetUserGroupId(string access_token, string openid)
        {
            WeixinRequestApiResult <WeixinGetUserGroupIdResult> result =
                new WeixinRequestApiResult <WeixinGetUserGroupIdResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(new { openid = openid });

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinGetUserGroupIdResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
        /// <summary>
        /// 根据OpenID列表群发【订阅号不可用,服务号认证后可用】
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinGroupMessageSendResult> Send(string access_token, WeixinGroupMessageSendArgs args)
        {
            WeixinRequestApiResult <WeixinGroupMessageSendResult> result =
                new WeixinRequestApiResult <WeixinGroupMessageSendResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "POST";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(args);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinGroupMessageSendResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
Example #29
0
        /// <summary>
        /// 获取用户列表
        /// 关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。
        /// 一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。
        /// http://mp.weixin.qq.com/wiki/11/434109e8de46b3968639217bbcb16c2f.html
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="next_openid">第一个拉取的OPENID,不填默认从头开始拉取</param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinGetUserListResult> GetUserList(string access_token, string next_openid)
        {
            WeixinRequestApiResult <WeixinGetUserListResult> result =
                new WeixinRequestApiResult <WeixinGetUserListResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "GET";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}",
                access_token, next_openid);

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinGetUserListResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }
        /// <summary>
        /// 获取标签下粉丝列表
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static WeixinRequestApiResult <WeixinGetTagUserListResult> GetTagUserList(
            string access_token, int tagId, string next_openid)
        {
            WeixinRequestApiResult <WeixinGetTagUserListResult> result =
                new WeixinRequestApiResult <WeixinGetTagUserListResult>();

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method = "GET";
            requestArgs.Url    = String.Format(
                "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token={0}", access_token);
            requestArgs.Content = JsonConvert.SerializeObject(new { tagid = tagId, next_openid = next_openid });

            result.HttpRequestArgs   = requestArgs;
            result.HttpRequestResult = _httpService.Request(requestArgs);

            if (result.HttpRequestResult.Successful)
            {
                result.ApiResult = WeixinApiHelper.Parse <WeixinGetTagUserListResult>(
                    result.HttpRequestResult.Content, ref result.ApiError);
            }

            return(result);
        }