Ejemplo n.º 1
0
        public ReturnValueModel GetWxUserInfo(WxManageInputDto dto)
        {
            ReturnValueModel rvm = new ReturnValueModel
            {
                Msg     = "success",
                Success = true
            };

            //string _host1 = ConfigurationManager.AppSettings["HostUrl"];
            //var authPath1 = $@"{_host1}/auth/token/Wx";
            //var postStr1 = $@"username=8e1731d9-ce48-4ef1-9522-087c9bd5076a&grant_type=password";
            //SysToken sysToken1 = HttpUtils.PostResponse<SysToken>(authPath1, postStr1, "application/x-www-form-urlencoded");
            //var user1 = _rep.FirstOrDefault<WxUserModel>(s => s.IsDeleted != 1 && s.Id == "8e1731d9-ce48-4ef1-9522-087c9bd5076a");
            //_wxRegisterService.CacheWxUser(user1);//必须添加到内存
            //rvm.Result = sysToken1;
            //return rvm;
            if (string.IsNullOrEmpty(dto.AppId))
            {
                rvm.Success = false;
                rvm.Msg     = "The parameter 'appId' is required.";
                return(rvm);
            }
            var configure = _rep.FirstOrDefault <BotSaleConfigure>(o => o.IsDeleted == 0 && o.AppId == dto.AppId);

            if (configure == null)
            {
                rvm.Msg     = "fail";
                rvm.Success = false;
                rvm.Result  = "获取BOT信息失败,请先配置BOT";
                return(rvm);
            }

            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();//监视代码运行时间
            var appId     = configure.AppId;
            var appSecret = configure.AppSecret;
            var url       = string.Format(WxUrls.UnionIdUrl, appId, appSecret, dto.code);
            var openModel = JsonConvert.DeserializeObject <OpenModel>(HttpUtils.HttpGet(url, ""));

            if (string.IsNullOrEmpty(openModel?.SessionKey))
            {
                rvm.Success = false;
                rvm.Msg     = "没有获取到SessionKey";
                rvm.Result  = null;
                LoggerHelper.WriteLogInfo("[GetUnionId]:错误------没有获取到SessionKey");
                return(rvm);
            }
            var encryptedData = dto.encryptedData;
            var iv            = dto.iv;
            var openid        = openModel.OpenId;
            var wxUserInfo    = dto.userInfo ?? new DecodedUserInfoModel();
            var userinfo      = new DecodedUserInfoModel()
            {
                openId    = openid,
                nickName  = wxUserInfo?.nickName,
                city      = wxUserInfo?.nickName,
                country   = wxUserInfo?.country,
                gender    = wxUserInfo.gender,
                avatarUrl = wxUserInfo?.avatarUrl,
                province  = wxUserInfo?.province,
            };

            if (!string.IsNullOrEmpty(encryptedData) && !string.IsNullOrEmpty(iv))
            {
                //var d= EncryptHelper.DecodeEncryptedData_1(openModel.SessionKey, dto.encryptedData, dto.iv);
                //userinfo = EncryptHelper.DecodeUserInfoBySessionKey(openModel.SessionKey, dto.encryptedData, dto.iv);
                //if (string.IsNullOrEmpty(userinfo?.unionId) || string.IsNullOrEmpty(userinfo?.openId))
                //{
                //    LoggerHelper.WriteLogInfo("[GetUnionId]:错误------unionId无效或openid无效");
                //    rvm.Success = false;
                //    rvm.Msg = "unionId无效或openid无效";
                //    rvm.Result = null;
                //    return rvm;
                //}
            }

            var user = _rep.FirstOrDefault <WxUserModel>(s => s.IsDeleted != 1 && s.OpenId == openid);

            if (user == null)
            {
                user = new WxUserModel
                {
                    Id       = Guid.NewGuid().ToString(),
                    UserName = "",
                    OpenId   = userinfo.openId,
                    //UnionId = openModel.UnionId,
                    //目前先使用OpenId,不知道为什么没获取到我的UnionId
                    UnionId    = userinfo.unionId,
                    WxCity     = userinfo.city,
                    WxName     = userinfo.nickName,
                    WxCountry  = userinfo.country,
                    WxGender   = userinfo.gender.ToString(),
                    WxPicture  = userinfo.avatarUrl,
                    WxProvince = userinfo.province,
                    //CreateTime = DateTime.Now,
                    //IsDeleted = 0,
                    //IsEnabled = 0,
                    IsVerify           = 0,
                    IsCompleteRegister = 1,//必须要设置为1
                    IsSalesPerson      = 1,
                    CreateTime         = DateTime.Now,
                    //SourceAppId = wxUserInfoRequestModel.SourceAppId,
                    // SourceType = "0",
                    WxSceneId = dto.WxSceneId
                };
                _rep.Insert(user);
                _rep.SaveChanges();
            }
            else
            {
                user.OpenId     = userinfo.openId ?? user.OpenId;
                user.UnionId    = userinfo.unionId ?? user.UnionId;
                user.WxCity     = userinfo.city ?? user.WxCity;
                user.WxName     = userinfo.nickName ?? user.WxName;
                user.WxCountry  = userinfo.country ?? user.WxCountry;
                user.WxGender   = userinfo.gender.ToString() ?? user.WxGender;
                user.WxPicture  = userinfo.avatarUrl ?? user.WxPicture;
                user.WxProvince = userinfo.province ?? user.WxProvince;
                user.UpdateTime = DateTime.Now;
                _rep.Update(user);
                _rep.SaveChanges();
            }
            _wxRegisterService.CacheWxUser(user);//必须添加到内存

            string   _host    = ConfigurationManager.AppSettings["HostUrl"];
            var      authPath = $@"{_host}/auth/token/Wx";
            var      postStr  = $@"username={user.Id}&grant_type=password";
            SysToken sysToken = HttpUtils.PostResponse <SysToken>(authPath, postStr, "application/x-www-form-urlencoded");

            //string hostUrl = $"{HttpContext.Current.Request.Url.Scheme}://{HttpContext.Current.Request.Url.Authority}";
            string OAuthServerUrl   = ConfigurationManager.AppSettings["OAuthServerUrl"];
            string OAuthAppId       = ConfigurationManager.AppSettings["OAuthAppId"];
            string OAuthServerState = ConfigurationManager.AppSettings["OAuthServerState"];
            string OAuthServerScope = ConfigurationManager.AppSettings["OAuthServerScope"];

            var _loginConfirmUrl = ConfigurationManager.AppSettings["loginConfirmUrl"];
            //验证地址
            var redirect_uri = $"{_loginConfirmUrl}/{user.Id}";
            //登录获取Code地址
            var authorizeurl = $"{OAuthServerUrl}/authorize?client_id={OAuthAppId}&scope={OAuthServerScope}&response_type=code&state={OAuthServerState}&redirect_uri={redirect_uri}";


            rvm.Success = true;
            rvm.Msg     = "success";
            rvm.Result  = new
            {
                openModel    = openModel,
                sysToken     = sysToken,
                user         = user,
                authorizeurl = authorizeurl,
                sysTokenUrl  = authPath,
                username     = user.Id,
                grant_type   = "password"
            };
            stopwatch.Stop();//结束
            rvm.ResponseTime = stopwatch.Elapsed.TotalMilliseconds;
            return(rvm);
        }
Ejemplo n.º 2
0
        public IHttpActionResult GetWxUserInfo([FromBody] WxManageInputDto dto)
        {
            var ret = _wxManageService.GetWxUserInfo(dto);

            return(Ok(ret));
        }