Exemple #1
0
        /// <summary>
        /// 发送客服文本消息
        /// </summary>
        /// <param name="wid"></param>
        /// <param name="openid"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task <ServiceResult <CommonResultStateOutput> > SendText(WxMP_KfMsgTextMsgInput input)
        {
            try
            {
                input.CheckNull(nameof(WxMP_KfMsgTextMsgInput));
                input.guid.CheckEmpty(nameof(input.guid));
                input.openid.CheckEmpty(nameof(input.openid));
                input.content.CheckEmpty(nameof(input.content));

                var token = _tokenService.GetAccessToken(new WxMP_AuthorizeAccessTokenInput {
                    guid = input.guid
                });
                if (token.code != 200)
                {
                    return(ServiceResult <CommonResultStateOutput> .Failed(StatusCodes.Status400BadRequest, token.msg));
                }
                var text = new WxMP_KfTextMessage();
                text.touser       = input.openid;
                text.text.content = input.content;
                var result = await WxMPContext.KfMessageAPI.SendKfMessage(token.data.access_token, text);

                return(ServiceResult <CommonResultStateOutput> .Success(new CommonResultStateOutput { result_state = result.errmsg }));
            }
            catch (Exception ex)
            {
                return(ServiceResult <CommonResultStateOutput> .Exception(ex.Message));
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取jssdk配置
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ServiceResult <WxMP_JssdkConfigOutput> GetConfig(WxMP_JssdkConfigInput input)
        {
            try
            {
                input.CheckNull(nameof(WxMP_JssdkConfigInput));
                input.guid.CheckEmpty(nameof(input.guid));
                input.url.CheckEmpty(nameof(input.url));

                var config = new WxMP_JssdkConfigOutput();
                //时间戳
                config.time_stamp = WxContext.Common.GenerateTimeStamp();
                //随机字符
                config.nonce_str = WxContext.Common.GenerateNonceStr();
                //获取微信配置
                var wechatConfig = _middleDB.WechatConfig.Select(s => new { s.Id, s.Guid, s.AppId }).FirstOrDefault(w => w.Guid == input.guid);
                config.app_id = wechatConfig.AppId;
                //token
                var token = _configService.GetAccessToken(new WxMP_AuthorizeAccessTokenInput {
                    wid = wechatConfig.Id
                });
                if (token.code != StatusCodes.Status200OK)
                {
                    return(ServiceResult <WxMP_JssdkConfigOutput> .Failed(token.code, token.msg));
                }
                //jsapi_ticket
                var ticket = GetJsapiTicket(new WxMP_JssdkJsapiTicketInput {
                    guid = wechatConfig.Guid, access_token = token.data.access_token
                });
                //signature
                config.signature = WxContext.Common.GenerateSignature(config.time_stamp, config.nonce_str, ticket.data.jsapi_ticket, input.url);
                return(ServiceResult <WxMP_JssdkConfigOutput> .Success(config));
            }
            catch (Exception ex)
            {
                return(ServiceResult <WxMP_JssdkConfigOutput> .Exception(ex.Message));
            }
        }
Exemple #3
0
        /// <summary>
        /// 生成二维码
        /// </summary>
        public async Task <ServiceResult <WxMP_QrcodeStreamOutput> > GenerateStream(WxMP_QrcodeStreamInput input)
        {
            try
            {
                input.CheckNull(nameof(WxMP_QrcodeStreamInput));
                input.wid.CheckZero(nameof(input.wid));
                input.business_id.CheckEmpty(nameof(input.business_id));
                input.module_type.CheckEmpty(nameof(input.module_type));

                var result = new ServiceResult <WxMP_QrcodeStreamOutput>();
                result.data = new WxMP_QrcodeStreamOutput();
                //获取临时二维码(业务ID)
                var scene = _middleDB.WechatQrscene.FirstOrDefault(w =>
                                                                   w.WechatId == input.wid && w.BusinessId == input.business_id && w.ModuleType == input.module_type);
                var ticket  = string.Empty;
                var sceneId = 0;
                //未创建
                if (scene == null)
                {
                    scene = new WechatQrscene
                    {
                        BusinessId = input.business_id,
                        WechatId   = input.wid,
                        ModuleType = input.module_type,
                        ActionName = WxMP_QRSceneActionType.QR_SCENE.ToString(),
                        ExpireTime = DateTime.Now.AddMinutes(-5),//5分钟之前
                        CreateTime = DateTime.Now
                    };
                    //添加数据
                    _middleDB.WechatQrscene.Add(scene);
                    _middleDB.SaveChanges();
                    sceneId = scene.SceneId;
                }
                else
                {
                    sceneId = scene.SceneId;
                    ticket  = scene.Ticket;
                }

                //临时二维码,网址访问,到期后自动延时
                if (scene.ExpireTime < DateTime.Now || input.is_new)
                {
                    var access_token = _configService.GetAccessToken(new WxMP_AuthorizeAccessTokenInput {
                        wid = input.wid
                    });
                    //创建临时二维码,30天
                    var createResult = await WxMPContext.AcountAPI.CreateTempQRSceneById(access_token.data.access_token, sceneId, 60 * 60 * 24 * 30);

                    ticket = createResult.ticket;
                    //自动延时 30天-5分钟
                    var expireTime = DateTime.Now.AddSeconds(createResult.expire_seconds - (60 * 5));
                    //更新二维码信息
                    scene.Ticket        = createResult.ticket;
                    scene.ExpireSeconds = createResult.expire_seconds;
                    scene.Url           = createResult.url;
                    scene.ExpireTime    = expireTime;
                    _middleDB.SaveChanges();
                }
                //获取图片
                var stream = await WxMPContext.AcountAPI.GetQRSceneImgStream(ticket);

                var image = new Bitmap(stream);
                var ms    = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                result.data.stream = ms;
                return(result);
            }
            catch (Exception ex)
            {
                return(ServiceResult <WxMP_QrcodeStreamOutput> .Exception(ex.Message));
            }
        }
Exemple #4
0
        public async Task <ServiceResult <CommonResultStateOutput> > Send(WxMP_TmplMsgInput input)
        {
            try
            {
                input.CheckNull(nameof(WxMP_KfMsgTextMsgInput));
                input.guid.CheckEmpty(nameof(input.guid));
                input.openid.CheckEmpty(nameof(input.openid));
                input.first.value.CheckEmpty(nameof(input.first));
                input.remark.value.CheckEmpty(nameof(input.remark));

                var token = _tokenService.GetAccessToken(new WxMP_AuthorizeAccessTokenInput {
                    guid = input.guid
                });
                if (token.code != 200)
                {
                    return(ServiceResult <CommonResultStateOutput> .Failed(StatusCodes.Status400BadRequest, token.msg));
                }
                var msg = new WxMP_TemplateMessage();
                msg.touser      = input.openid;
                msg.template_id = input.template_id;
                msg.url         = "http://www.baidu.com";
                if (input.miniprogram != null)
                {
                    msg.miniprogram = new WxMP_TemplateMessageMiniProgram
                    {
                        appid    = input.miniprogram.appid,
                        pagepath = input.miniprogram.pagepath
                    };
                }
                msg.data.first.value = input.first.value;
                msg.data.first.color = input.first.color;
                #region keyword1-6
                if (input.items.Count > 0)
                {
                    msg.data.keyword1 = new WxMP_TemplateMessageDataItem
                    {
                        value = input.items[0].value,
                        color = input.items[0].color
                    };
                }
                if (input.items.Count > 1)
                {
                    msg.data.keyword2 = new WxMP_TemplateMessageDataItem
                    {
                        value = input.items[1].value,
                        color = input.items[1].color
                    };
                }
                if (input.items.Count > 2)
                {
                    msg.data.keyword3 = new WxMP_TemplateMessageDataItem
                    {
                        value = input.items[2].value,
                        color = input.items[2].color
                    };
                }
                if (input.items.Count > 3)
                {
                    msg.data.keyword4 = new WxMP_TemplateMessageDataItem
                    {
                        value = input.items[3].value,
                        color = input.items[3].color
                    };
                }
                if (input.items.Count > 4)
                {
                    msg.data.keyword5 = new WxMP_TemplateMessageDataItem
                    {
                        value = input.items[4].value,
                        color = input.items[4].color
                    };
                }
                if (input.items.Count > 5)
                {
                    msg.data.keyword6 = new WxMP_TemplateMessageDataItem
                    {
                        value = input.items[5].value,
                        color = input.items[5].color
                    };
                }
                #endregion

                msg.data.remark.value = input.remark.value;
                msg.data.remark.color = input.remark.color;
                var result = await WxMPContext.MessageAPI.SendTmplMessage(token.data.access_token, msg);

                if (result.errcode != 0)
                {
                    return(ServiceResult <CommonResultStateOutput> .Failed(StatusCodes.Status400BadRequest, $"{result.errcode}:{result.errmsg}"));
                }
                return(ServiceResult <CommonResultStateOutput> .Success(new CommonResultStateOutput { result_state = result.errmsg }));
            }
            catch (Exception ex)
            {
                return(ServiceResult <CommonResultStateOutput> .Exception(ex.Message));
            }
        }