Пример #1
0
        /// <summary>
        /// 获取企业微信应用配置
        /// </summary>
        public ServiceResult <WxEE_AuthorizeAccessTokenOutput> GetAccessToken(WxEE_AuthorizeAccessTokenInput input)
        {
            input.CheckNull(nameof(WxEE_AuthorizeAccessTokenInput));

            var result = new ServiceResult <WxEE_AuthorizeAccessTokenOutput>();

            result.data = new WxEE_AuthorizeAccessTokenOutput();
            WechatEeappConfig wechatEEAppConfig = null;

            //微信配置
            if (input.aid == 0)
            {
                input.guid.CheckEmpty(nameof(input.guid));
                wechatEEAppConfig = _middleDB.WechatEeappConfig.FirstOrDefault(w => w.Guid == input.guid);
            }
            else
            {
                wechatEEAppConfig = _middleDB.WechatEeappConfig.FirstOrDefault(w => w.Id == input.aid);
            }
            if (wechatEEAppConfig == null)
            {
                return(ServiceResult <WxEE_AuthorizeAccessTokenOutput> .Failed(StatusCodes.Status404NotFound, "企业微信应用配置获取失败,请联系系统客服"));
            }
            //过期时间
            if (!string.IsNullOrEmpty(wechatEEAppConfig.AccessToken) &&
                DateTime.Now < (wechatEEAppConfig.TokenExpireTime ?? DateTime.Now.AddMinutes(-1)))
            {
                result.data.access_token = wechatEEAppConfig.AccessToken;
                result.data.expire_stamp = new DateTimeOffset(wechatEEAppConfig.TokenExpireTime.Value).ToUnixTimeMilliseconds();
            }
            else
            {
                //双重验证
                if (string.IsNullOrEmpty(result.data.access_token))
                {
                    lock (lock_token)
                    {
                        if (string.IsNullOrEmpty(result.data.access_token))
                        {
                            //从企业微信获取
                            var token = WxEEContext.BaseAPI.GetAccessToken(wechatEEAppConfig.CorpId, wechatEEAppConfig.Secret);
                            if (token.errcode != 0)
                            {
                                result.data.access_token = token.errmsg;
                                return(result);
                            }
                            //修改
                            wechatEEAppConfig.AccessToken     = token.access_token;
                            wechatEEAppConfig.TokenExpireTime = DateTime.Now.AddMinutes(90);
                            _middleDB.SaveChanges();

                            result.data.access_token = token.access_token;
                            result.data.expire_stamp = new DateTimeOffset(wechatEEAppConfig.TokenExpireTime.Value).ToUnixTimeMilliseconds();
                        }
                    }
                }
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 获取jssdk票据
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public ServiceResult <WxMP_JssdkJsapiTicketOutput> GetJsapiTicket(WxMP_JssdkJsapiTicketInput input)
        {
            try
            {
                input.CheckNull(nameof(WxMP_JssdkJsapiTicketInput));
                input.guid.CheckEmpty(nameof(input.guid));
                input.access_token.CheckEmpty(nameof(input.access_token));

                var result = new ServiceResult <WxMP_JssdkJsapiTicketOutput>();
                result.data = new WxMP_JssdkJsapiTicketOutput();
                //NOLOCK
                var wechatConfig = ApplicationContext.EF.WithNolock <WechatConfig>(() =>
                {
                    //微信配置
                    return(_middleDB.WechatConfig.FirstOrDefault(w => w.Guid == input.guid));
                });
                if (wechatConfig == null)
                {
                    return(ServiceResult <WxMP_JssdkJsapiTicketOutput> .Failed(StatusCodes.Status404NotFound, "微信配置获取失败,请联系系统客服"));
                }
                //数据库中
                if (!string.IsNullOrEmpty(wechatConfig.JssdkTicket) &&
                    DateTime.Now < (wechatConfig.JssdkTicketUpdateTime ?? DateTime.Now.AddMinutes(-1)))
                {
                    result.data.jsapi_ticket = wechatConfig.JssdkTicket;
                }
                else
                {
                    //双重验证
                    if (string.IsNullOrEmpty(result.data.jsapi_ticket))
                    {
                        lock (lock_ticket)
                        {
                            if (string.IsNullOrEmpty(result.data.jsapi_ticket))
                            {
                                //从微信获取
                                var ticket = WxMPContext.JsAPI.GetTicket(input.access_token);
                                //修改
                                wechatConfig.JssdkTicket           = ticket.ticket;
                                wechatConfig.JssdkTicketUpdateTime = DateTime.Now.AddMinutes(60);
                                _middleDB.SaveChanges();

                                result.data.jsapi_ticket = ticket.ticket;
                            }
                        }
                    }
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(ServiceResult <WxMP_JssdkJsapiTicketOutput> .Exception(ex.Message));
            }
        }
Пример #3
0
        /// <summary>
        /// 获取微信配置
        /// </summary>
        /// <param name="mp_id"></param>
        /// <returns></returns>
        public ServiceResult <WxMP_AuthorizeAccessTokenOutput> GetAccessToken(WxMP_AuthorizeAccessTokenInput input)
        {
            try
            {
                input.CheckNull(nameof(WxMP_AuthorizeAccessTokenInput));

                var result = new ServiceResult <WxMP_AuthorizeAccessTokenOutput>();
                result.data = new WxMP_AuthorizeAccessTokenOutput();
                WechatConfig wechatConfig = null;
                //微信配置
                if (input.wid == 0)
                {
                    input.guid.CheckEmpty(nameof(input.guid));
                    wechatConfig = _middleDB.WechatConfig.FirstOrDefault(w => w.Guid == input.guid);
                }
                else
                {
                    wechatConfig = _middleDB.WechatConfig.FirstOrDefault(w => w.Id == input.wid);
                }
                if (wechatConfig == null)
                {
                    return(ServiceResult <WxMP_AuthorizeAccessTokenOutput> .Failed(StatusCodes.Status404NotFound, "微信配置获取失败,请联系系统客服"));
                }
                //数据库中
                if (!string.IsNullOrEmpty(wechatConfig.AccessToken) &&
                    DateTime.Now < (wechatConfig.TokenUpdateTime ?? DateTime.Now.AddMinutes(-1)))
                {
                    result.data.access_token = wechatConfig.AccessToken;
                }
                else
                {
                    //双重验证
                    if (string.IsNullOrEmpty(result.data.access_token))
                    {
                        lock (lock_token)
                        {
                            if (string.IsNullOrEmpty(result.data.access_token))
                            {
                                //从微信获取
                                var token = WxMPContext.BaseAPI.GetAccessToken(wechatConfig.AppId, wechatConfig.AppSecret);
                                if (token.errcode != 0)
                                {
                                    result.data.access_token = token.errmsg;
                                    return(result);
                                }
                                //修改
                                wechatConfig.Token           = token.access_token;
                                wechatConfig.TokenUpdateTime = DateTime.Now.AddMinutes(60);
                                _middleDB.SaveChanges();

                                result.data.access_token = token.access_token;
                            }
                        }
                    }
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(ServiceResult <WxMP_AuthorizeAccessTokenOutput> .Exception(ex.Message));
            }
        }
Пример #4
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));
            }
        }