Exemple #1
0
        public void BatchGetUserInfoTest()
        {
            List <BatchGetUserInfoData> data = new List <BatchGetUserInfoData>();

            //改成自己公众号的OpenId
            var openids = new[] { "oxRg0uLsnpHjb8o93uVnwMK_WAVw", "oxRg0uFnf66iXoS_ScybtgjUgK28", "oxRg0uKDWyD8yxgLEFuJFRsI_LQ0" };

            foreach (var item in openids)
            {
                data.Add(new BatchGetUserInfoData()
                {
                    openid = item,
                    lang   = Language.zh_CN.ToString()
                });
            }

            var accessToken = AccessTokenContainer.GetAccessToken(_appId);
            var result      = UserApi.BatchGetUserInfo(accessToken, data);

            Assert.AreEqual(result.errcode, ReturnCode.请求成功);

            var result2 = UserApi.BatchGetUserInfo(_appId, data);

            Assert.AreEqual(result2.errcode, ReturnCode.请求成功);
        }
Exemple #2
0
        public void BatchGetUserInfoTest()
        {
            List <BatchGetUserInfoData> data = new List <BatchGetUserInfoData>();

            //改成自己公众号的OpenId
            var openids = new[] { "oIskUw6gb6rokzMf_do7oFOkpfQ4", "oIskUw1tiJ5jr4l26dKyG7wmm4F8", "oIskUwyt33w_qlvNbDtdrqLovOoU" };

            foreach (var item in openids)
            {
                data.Add(new BatchGetUserInfoData()
                {
                    openid = item,
                    lang   = Language.zh_CN.ToString()
                });
            }

            var accessToken = AccessTokenContainer.GetAccessToken(_appId);
            var result      = UserApi.BatchGetUserInfo(accessToken, data);

            Assert.AreEqual(result.errcode, ReturnCode.请求成功);

            var result2 = UserApi.BatchGetUserInfo(_appId, data);

            Assert.AreEqual(result2.errcode, ReturnCode.请求成功);
        }
Exemple #3
0
        public void BatchGetUserInfo()
        {
            string            appId             = WxOperatorProvider.Provider.GetCurrent().AppId;
            string            appSecret         = WxOperatorProvider.Provider.GetCurrent().AppSecret;
            AccessTokenResult accessTokenResult = AccessTokenContainer.GetAccessTokenResult(appId);
            var specification = new Specification <WxUser>(p => p.AppId == appId && p.SubscribeStatus == 1 && p.SynchronisedTime == null);
            List <BatchGetUserInfoData> batchGetUserInfoDataList = new List <BatchGetUserInfoData>();
            List <WxUser> wxUserList = wxUserRepository.FindAll(specification).ToList();

            if (wxUserList.Count > 0)
            {
                foreach (WxUser wxuser in wxUserList)
                {
                    int index = wxUserList.IndexOf(wxuser);
                    if (index > 100)
                    {
                        break;
                    }
                    BatchGetUserInfoData batchGetUserInfoData = new BatchGetUserInfoData();
                    batchGetUserInfoData.openid   = wxuser.OpenId;
                    batchGetUserInfoData.lang     = "zh_CN";
                    batchGetUserInfoData.LangEnum = 0;
                    batchGetUserInfoDataList.Add(batchGetUserInfoData);
                }
                BatchGetUserInfoJsonResult batchGetUserInfoJsonResult = UserApi.BatchGetUserInfo(accessTokenResult.access_token, batchGetUserInfoDataList);
                if (batchGetUserInfoJsonResult.ErrorCodeValue == 0)
                {
                    if (batchGetUserInfoJsonResult.user_info_list.Count > 0)
                    {
                        foreach (UserInfoJson userInfoJson in batchGetUserInfoJsonResult.user_info_list)
                        {
                            var    specification2 = new Specification <WxUser>(p => p.AppId == appId && p.OpenId == userInfoJson.openid);
                            WxUser wxUser         = wxUserRepository.Find(specification2);
                            wxUser.Nickname         = userInfoJson.nickname;
                            wxUser.Sex              = userInfoJson.sex;
                            wxUser.HeadImgUrl       = userInfoJson.headimgurl;
                            wxUser.Country          = userInfoJson.country;
                            wxUser.Province         = userInfoJson.province;
                            wxUser.City             = userInfoJson.city;
                            wxUser.Language         = userInfoJson.language;
                            wxUser.UnionId          = userInfoJson.unionid;
                            wxUser.Remark           = userInfoJson.remark;
                            wxUser.GroupId          = userInfoJson.groupid;
                            wxUser.SynchronisedTime = System.DateTime.Now;
                            wxUserRepository.Update(wxUser);
                        }
                    }
                }
            }
        }
Exemple #4
0
        public void BatchGetUserInfoTest()
        {
            List <BatchGetUserInfoData> data = new List <BatchGetUserInfoData>();

            data.Add(new BatchGetUserInfoData()
            {
                openid = "",
                lang   = Language.zh_CN
            });

            // var accessToken = AccessTokenContainer.GetAccessToken(_appId);
            var result = UserApi.BatchGetUserInfo(_appId, _appSecret, data);

            Assert.AreEqual(result.errcode, ReturnCode.请求成功);
        }
        public ActionResult UpdateUserWechatInfo()
        {
            using (var db = new IdentityEntities())
            {
                var page     = 0;
                var pageSize = 100;

                //init
                AccessTokenContainer.Register(Configuration.GreenspotConfiguration.AccessAccounts["wechat"].Id,
                                              Configuration.GreenspotConfiguration.AccessAccounts["wechat"].Secret);

                //take all users
                var users = db.UserSnsInfos.Where(x => WeChatClaimTypes.OpenId.Equals(x.InfoKey)).ToList();
                if (users.Count == 0)
                {
                    return(Content("NG"));
                }

                var user100 = users.Skip(pageSize * page++).Take(pageSize).ToList();

                //take 100 user
                while (user100.Count > 0)
                {
                    //openId, userId
                    var openIdDic = new SortedList <string, string>();

                    var reqData = new List <Senparc.Weixin.MP.AdvancedAPIs.User.BatchGetUserInfoData>();

                    //build user info data
                    foreach (var u in users)
                    {
                        openIdDic.Add(u.InfoValue, u.UserId);
                        reqData.Add(new Senparc.Weixin.MP.AdvancedAPIs.User.BatchGetUserInfoData()
                        {
                            lang   = Senparc.Weixin.Language.zh_CN,
                            openid = u.InfoValue
                        });
                    }

                    //pull data
                    var result = UserApi.BatchGetUserInfo(Configuration.GreenspotConfiguration.AccessAccounts["wechat"].Id, reqData);
                    if (result.user_info_list != null && result.user_info_list.Count > 0)
                    {
                        foreach (var userInfo in result.user_info_list)
                        {
                            string userId = null;
                            if (string.IsNullOrEmpty(userInfo.openid) || !openIdDic.ContainsKey(userInfo.openid))
                            {
                                //no openid or exist openid, move next
                                continue;
                            }
                            else
                            {
                                userId = openIdDic[userInfo.openid];
                                UpdateUserSnsInfo(db, userId, userInfo);
                            }
                        }
                        db.SaveChanges();
                        user100 = users.Skip(pageSize * page++).Take(pageSize).ToList();
                    }
                }


                return(Content("OK"));
            }
        }