예제 #1
0
        public void Add(UserInfo userInfo)
        {
            if (userInfo == null)
                return;

            _efRepository.Add(userInfo);
        }
예제 #2
0
        /// <summary>
        /// 获取微信用户信息
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo(int appId, string openId)
        {
            //判断appId是否合法
            var appInfo = _appInfoRepository.GetAppInfo(appId);
            if (appInfo == null)
                throw new Exception(string.Format("获取用户信息失败:通过参数{0}获取到的公众号信息为空", appId));

            var userInfoFromDb = _userRepository.GetUserInfo(appId, openId);
            //如果从数据库获取用户为空,则需要重新从微信更新
            string tokenErrorMessage;
            var accessToken = new AccessTokenService().GetAccessToken(appId, out tokenErrorMessage);

            if (accessToken == null || !string.IsNullOrEmpty(tokenErrorMessage))
                throw new Exception(string.Format("获取用户信息失败:", tokenErrorMessage));
            if (userInfoFromDb != null) return userInfoFromDb;

            var newUserInfoFromWeiXin = UserApi.Info(accessToken.Ticket, openId);
            if (newUserInfoFromWeiXin == null)
                throw new Exception("获取用户信息失败:从微信获取到的用户为空");
            var newUserInfo = new UserInfo
            {
                AppId = appId,
                Openid = newUserInfoFromWeiXin.openid,
                City = newUserInfoFromWeiXin.city,
                Country = newUserInfoFromWeiXin.country,
                Groupid = newUserInfoFromWeiXin.groupid,
                Headimgurl = newUserInfoFromWeiXin.headimgurl,
                Language = newUserInfoFromWeiXin.language,
                Nickname = newUserInfoFromWeiXin.nickname,
                Province = newUserInfoFromWeiXin.province,
                Remark = newUserInfoFromWeiXin.remark,
                Sex = newUserInfoFromWeiXin.sex,
                //Subscribe = newUserInfoFromWeiXin.subscribe,
                SubscribeTime = newUserInfoFromWeiXin.subscribe_time
            };

            //缓存到数据库
            _userRepository.Add(newUserInfo);

            return newUserInfo;
        }