Beispiel #1
0
        public async Task <string> GetOpenId(string code, string organizationId = null)
        {
            WeChatOpenIdResponseDTO result = null;

            BaseGateway gateway = null;

            if (string.IsNullOrEmpty(organizationId))
            {
                gateway = _gateways.Get <WechatpayGateway>();
            }
            else
            {
                WeChatAppConfiguration appConfig = _wechatAppConfigurationRepository.GetFiltered(o => o.OrganizationId == organizationId).FirstOrDefault();
                if (appConfig != null)
                {
                    var wechatpayMerchant = new PaySharp.Wechatpay.Merchant
                    {
                        AppId           = appConfig.AppId,
                        MchId           = appConfig.MchId,
                        Key             = appConfig.Key,
                        AppSecret       = appConfig.AppSecret,
                        SslCertPath     = appConfig.SslCertPath,
                        SslCertPassword = appConfig.SslCertPassword,
                        NotifyUrl       = appConfig.NotifyUrl
                    };

                    gateway = new WechatpayGateway(wechatpayMerchant);
                }
                else
                {
                    gateway = _gateways.Get <WechatpayGateway>();
                }
            }

            var reqModel = new WeChatOpenIdRequestDTO {
                AppId = gateway.Merchant.AppId, Code = code, Secret = _configuration.WeChatAppSecret
            };

            result = await _wechatApiProxy.CheckAuthCode(reqModel);

            //TODO:验签

            return(result?.OpenId ?? string.Empty);
        }
Beispiel #2
0
        public async Task <WeChatOpenIdResponseDTO> CheckAuthCode(WeChatOpenIdRequestDTO model)
        {
            WeChatOpenIdResponseDTO result = null;

            var api = $"https://api.weixin.qq.com/sns/jscode2session?appid={model.AppId}&secret={model.Secret}&js_code={model.Code}&grant_type={model.GrantType}";

            using (var client = new HttpClient())
            {
                var res = await client.GetAsync(api);

                if (res.IsSuccessStatusCode)
                {
                    var resultStr = await res.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <WeChatOpenIdResponseDTO>(resultStr);
                }
            }

            //TODO:验签

            return(result);
        }
Beispiel #3
0
        public async Task <OpenIDSessionKeyDTO> GetSessionKey(string code, string organizationId = null, string applicationId = null)
        {
            WeChatOpenIdResponseDTO result = null;

            BaseGateway gateway = null;

            if (string.IsNullOrEmpty(organizationId))
            {
                gateway = _gateways.Get <WechatpayGateway>();
            }
            else
            {
                WeChatAppConfiguration appConfig = _wechatAppConfigurationRepository.GetFiltered(o => o.OrganizationId == organizationId).FirstOrDefault();
                if (appConfig != null)
                {
                    var wechatpayMerchant = new PaySharp.Wechatpay.Merchant
                    {
                        AppId           = appConfig.AppId,
                        MchId           = appConfig.MchId,
                        Key             = appConfig.Key,
                        AppSecret       = appConfig.AppSecret,
                        SslCertPath     = appConfig.SslCertPath,
                        SslCertPassword = appConfig.SslCertPassword,
                        NotifyUrl       = appConfig.NotifyUrl
                    };

                    gateway = new WechatpayGateway(wechatpayMerchant);
                }
                else
                {
                    gateway = _gateways.Get <WechatpayGateway>();
                }
            }

            var reqModel = new WeChatOpenIdRequestDTO {
                AppId = gateway.Merchant.AppId, Code = code, Secret = _configuration.WeChatAppSecret
            };

            result = await _wechatApiProxy.CheckAuthCode(reqModel);

            //TODO:验签

            AppletUserSessionDTO dto = null;

            if (result != null)
            {
                await _authServiceProxy.AddOrUpdateAppletUser(new AppletUserDTO
                {
                    ApplicationId  = applicationId,
                    Channel        = UserChannel.WeChat,
                    NickName       = "",
                    OpenId         = result.OpenId,
                    OrganizationId = organizationId,
                    UserId         = "",
                    UserPortrait   = ""
                });

                dto = await _authServiceProxy.AddOrUpdateAppletUserSession(new AppletUserSessionDTO
                {
                    UserId     = "",
                    OpenId     = result.OpenId,
                    SessionKey = result.SessionKey
                });
            }

            return(new OpenIDSessionKeyDTO {
                GooiosSessionKey = dto.GooiosSessionKey, OpenId = dto.OpenId
            });
        }