Ejemplo n.º 1
0
        public void WeChatGetAccessToken()
        {
            var client1 = new WeChatApiClient();

            var token1 = client1.AccessToken;

            var client2 = new WeChatApiClient();

            var token2 = client2.AccessToken;

            Assert.AreSame(token1, token2);
        }
Ejemplo n.º 2
0
        private static void SyncUserWeChatInfo()
        {
            IRepository repo = new Repository();

            // 7天来未更新的微信会员 或 微信昵称为空的会员
            var usersWeChat = repo.Query<UserWeChat>(x => x.LastAuthorizeDate < DateTime.Now.AddDays(-7));

            var users = repo.Query<User>(x => x.WeChatOpenID != "")
                            .FindAll(x => string.IsNullOrEmpty(x.WeChatNickName) || usersWeChat.Exists(y => y.ID == x.ID));

            if (users.Count > 0 && ConfigGlobal_Arsenal.WeChatActive)
            {
                // 根据users,生成输入参数
                //{"user_list": [
                //        { "openid": "otvxTs4dckWG7imySrJd6jSi0CWE", "lang": "zh-CN"},
                //        { "openid": "otvxTs_JZ6SEiP0imdhpi50fuSZg",  "lang": "zh-CN" }
                //    ]
                //}

                var mapper = UserWeChatRequestDto.ConfigMapper().CreateMapper();

                var userList = new { user_list = mapper.Map<IEnumerable<UserWeChatRequestDto>>(users.DistinctBy(x => x.WeChatOpenID)).ToList() };
                var openIds = JsonConvert.SerializeObject(userList);

                var client = new WeChatApiClient();
                var result = client.BatchGetUserInfo(openIds);

                var json = JToken.Parse(result);
                if (!string.IsNullOrEmpty(json["user_info_list"].ToString()))
                {
                    var list = JsonConvert.DeserializeObject<List<UserWeChatResponseDto>>(json["user_info_list"].ToString());

                    if (list.Count > 0)
                    {
                        foreach (var u in users)
                        {
                            var uResp = list.Find(x => x.openid == u.WeChatOpenID);
                            var uWeChat = repo.Single<UserWeChat>(u.ID);

                            if (uResp != null)
                            {
                                u.WeChatOpenID = uResp.openid;
                                u.WeChatNickName = uResp.nickname;

                                repo.Update(u);

                                if (uWeChat != null)
                                {
                                    uWeChat.Gender = uResp.sex;
                                    uWeChat.LastAuthorizeDate = DateTime.Now;
                                    uWeChat.City = uResp.city ?? string.Empty;
                                    uWeChat.Province = uResp.province ?? string.Empty;
                                    uWeChat.Country = uResp.country ?? string.Empty;
                                    uWeChat.HeadImgUrl = uResp.headimgurl ?? string.Empty;
                                    uWeChat.UnionID = uResp.unionid ?? string.Empty;

                                    repo.Update(uWeChat);
                                }
                                else
                                {
                                    var instance = new UserWeChat
                                    {
                                        ID = u.ID,
                                        UserName = u.UserName,
                                        LastAuthorizeDate = DateTime.Now,
                                        AccessToken = string.Empty,
                                        AccessTokenExpiredDate = DateTime.Now,
                                        RefreshToken = string.Empty,
                                        RefreshTokenExpiredDate = DateTime.Now,
                                        Gender = uResp.sex,
                                        Province = uResp.province ?? string.Empty,
                                        City = uResp.city ?? string.Empty,
                                        Country = uResp.country ?? string.Empty,
                                        HeadImgUrl = uResp.headimgurl ?? string.Empty,
                                        Privilege = string.Empty,
                                        UnionID = uResp.unionid ?? string.Empty
                                    };

                                    repo.Insert(instance);
                                }
                            }
                        }
                    }
                }
            }
        }