• 章节:26
  • 访问:782
  • 时长:00:04:10
  • 日期:2019-12-29 09:58

微信提供批量获取用户详情的接口,不过最多100个

public static string UserInfoBatch = "http://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={0}";

private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                UserOpenidInfo userInfos = UserHelper.GetUserInfos();
                List<UserDetail> userDetails = new List<UserDetail>();
                for (int i = 0; i < userInfos.data.openid.Count(); i++)
                {
                    UserDetail userDetail = UserHelper.GetUserDetail(userInfos.data.openid[i]);
                    userDetails.Add(userDetail);
                }
                this.userDetails.ItemsSource = userDetails;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
<Window x:Class="WeiXin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WeiXin"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="1">
                <Button Content="获取用户列表" Width="150" HorizontalAlignment="Left" Height="50" Click="Button_Click"/>
            </StackPanel>
        </Grid>
        <DataGrid Grid.Row="1" Name="userDetails" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding nickname}" Header="昵称"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding sex}" Header="性别"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding city}" Header="城市"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding headimgurl}" Header="头像"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding groupid}" Header="分组ID"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>