微信公众平台--获取用户基本信息

目录

 

一.前言:

二. 微信公众平台接口:

三.获取用户列表

步骤1:

步骤2:

步骤3: 


一.前言:

希望肺炎早早过去,最近在家远程办公,想着在家咋也不能闲着是不,在家继续搬砖,刚好这段时间我们公司将微信公众平台的开发交给了me,在自我学习过程中,我将我在微信公众平台这块的学习写一个专栏,(以功能点为基础的专栏,看上去可能比较乱,不过大家有什么问题的话可以在底下评论,我看到一定会第一时间回复大家滴。)希望能够帮助到刚涉及为微信公众平台这块的小伙伴们,让我们一起进步。专栏地址:https://blog.csdn.net/tangthh123/category_8265239.html

二. 微信公众平台接口:

微信公众平台的提供了很多接口,接下来我们就看一下获取用户列表的信息。

 

三.获取用户列表

步骤1:

      开发文档地址

步骤2:

   获取微信用户列表的步骤:

   1.首先AppId 和AppSecret获取AccessToken , 

   2.根据Access_token和 next_openid 获取所有的微信公众平台的open_id  开发文档

   3.根据open_id 和AccessToken获取 用户基本信息。

步骤3: 

  •  控制层
  /**
     * 定时发送消息模板
     */
    @GetMapping(value = "/sendCondition")
    public void sendCondition(){
        weiUserInfoService.insertData();

    }

 

  • 业务逻辑层
/**
     * 处理微信公众平台的数据
     * @param
     * @return
     */
    @Override
    public void insertData() {
        try {
        

            //-----------------------------获取微信公众平台用户信息------------------------
            String accessToken = WeiXinParamesUtil.getAccessToken2("weixin");
            //next_openid    第一个拉取的OPENID,不填默认从头开始拉取
            List<String> openIdList = getWeiXinOpenIdList(accessToken, "");
            //获取所有微信公众平台用户列表
            List<WeiUserInfoModel> allUserList = getWeiXinInfo(accessToken, openIdList);

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error("保存用户信息失败 errcode:{} errmsg:{}",e);
        }
    }

 

  • 封装的一些方法 
/**
 * 获取微信公众平台的access_token
 * @param type
 * @return
 */
public static String getAccessToken2(String type) {
    String url = "";
    if ("weixin".equals(type)) {
        url = getWeiAccessToken.replace("APPID", WeiXinParamesUtil.APPID).replace("APPSECRET", WeiXinParamesUtil.SECRET);
    }
    JSONObject departmentJson = SendRequest.sendGet2(url);
    return departmentJson.getString("access_token");
}

 

/**
 * 获取微信公众平台用户列表
 * 参考:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140840
 *
 * @param
 * @return
 */
@Override
public List<String> getWeiXinOpenIdList(String access_token, String next_openid) {
    List<String> openidList = null;
    try {
        openidList = new ArrayList<String>();
        String requestUrl = WeiXinParamesUtil.getUserList.replace("ACCESS_TOKEN", access_token).replace("NEXT_OPENID", next_openid);
        // 发起GET请求获取凭证
        JSONObject jsonObject = SendRequest.sendGet2(requestUrl);
        String total = jsonObject.getString("total");
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray arr = data.getJSONArray("openid");
        for (int i = 0; i < arr.size(); i++) {
            openidList.add(arr.get(i).toString());
        }
    } catch (Exception e) {
        logger.error("获取微信公众平台openid列表失败",e);
    }

    return openidList;
}

 

 @Override
    public List getWeiXinInfo(String access_token, List open_id) {
        List<WeiUserInfoModel> list = null;
        try {
            list = new ArrayList<>();
            for (int i = 0; i < open_id.size(); i++) {
                //获取微信公众平台中用户基本信息
                String requestUrl = WeiXinParamesUtil.getUserInfoList.replace("ACCESS_TOKEN", access_token).replace("OPENID", open_id.get(i).toString());
                // 发起GET请求获取凭证
                JSONObject jsonObject = SendRequest.sendGet2(requestUrl);
                WeiUserInfoModel userInfoModel = null;
                if (null != jsonObject) {
                    userInfoModel = new WeiUserInfoModel();
                    // 用户的标识
                    userInfoModel.setOpenid(jsonObject.getString("openid"));
                    // 关注状态(1是关注,0是未关注),未关注时获取不到其余信息
                    userInfoModel.setSubscribe(jsonObject.getString("subscribe"));
                    // 用户关注时间
                    userInfoModel.setSubscribeTime(Long.parseLong(jsonObject.getString("subscribe_time"))*1000L);
                    // 昵称
                    userInfoModel.setNickname(jsonObject.getString("nickname"));
                    // 用户的性别(1是男性,2是女性,0是未知)
                    if ("1".equals(String.valueOf(jsonObject.getString("sex")))) {
                        userInfoModel.setSex("男");
                    } else if ("2".equals(String.valueOf(jsonObject.getString("sex")))) {
                        userInfoModel.setSex("女");
                    } else {
                        userInfoModel.setSex("未知");
                    }
                    // 用户所在国家
                    userInfoModel.setCountry(jsonObject.getString("country"));
                    // 用户所在省份
                    userInfoModel.setProvince(jsonObject.getString("province"));
                    // 用户所在城市
                    userInfoModel.setCity(jsonObject.getString("city"));
                    // 用户的语言,简体中文为zh_CN
                    userInfoModel.setLanguage(jsonObject.getString("language"));
                    // 用户头像
                    userInfoModel.setHeadimgurl(jsonObject.getString("headimgurl"));
                    //备注
                    userInfoModel.setRemark(jsonObject.getString("remark"));
                    userInfoModel.setSource("微信公众号");
                    list.add(userInfoModel);
                }
            }
        } catch (Exception e) {
            logger.error("获取微信公众平台--weiuserinfo中失败",e);
        }

        return list;
    }

 

  •  微信用户列表
package com.bos.data.model;

import io.swagger.annotations.ApiModelProperty;

import javax.persistence.*;
import java.util.Objects;

/**
 * @Author: tanghh
 *微信公众平台用户信息 和企业微信外部联系人表
 */
@Entity
@Table(name = "wei_user_info", schema = "test", catalog = "")
public class WeiUserInfoModel {
    private Integer id;
    @ApiModelProperty(value = "微信用户的标识")
    private String openid;
    @ApiModelProperty(value = "关注状态(1是关注,0是未关注)")
    private String subscribe;
    @ApiModelProperty(value = "关注时间")
    private Long subscribeTime;
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "用户的性别(1是男性,2是女性,0是未知)")
    private String sex;
    @ApiModelProperty(value = "用户所在的国家")
    private String country;
    @ApiModelProperty(value = "用户所在的省份")
    private String province;
    @ApiModelProperty(value = "用户所在的城市")
    private String city;
    @ApiModelProperty(value = "用户的语言")
    private String language;
    @ApiModelProperty(value = "用户的头像")
    private String headimgurl;
    @ApiModelProperty(value = "与微信开发平台共通的id")
    private String unionid;
    @ApiModelProperty(value = "公众号运营者对粉丝的备注,公众号运营者可在微信公众平台用户管理界面对粉丝添加备注")
    private String remark;
    @ApiModelProperty(value = "用户所在的分组id")
    private Integer groupid;
    @ApiModelProperty(value = "错误码")
    private String errcode;
    @ApiModelProperty(value = "错误信息")
    private String errmsg;
    @ApiModelProperty(value = "关注状态(1是关注,0是未关注)")
    private String source;
    private String description;
    private String remarkCropName;
    private String relatedName;
    private String phone1;
    private String phone2;
    private String isDel="0";


    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Basic
    @Column(name = "openid")
    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    @Basic
    @Column(name = "subscribe")
    public String getSubscribe() {
        return subscribe;
    }

    public void setSubscribe(String subscribe) {
        this.subscribe = subscribe;
    }

    @Basic
    @Column(name = "subscribe_time")
    public Long getSubscribeTime() {
        return subscribeTime;
    }

    public void setSubscribeTime(Long subscribeTime) {
        this.subscribeTime = subscribeTime;
    }

    @Basic
    @Column(name = "nickname")
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    @Basic
    @Column(name = "sex")
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Basic
    @Column(name = "country")
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Basic
    @Column(name = "province")
    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    @Basic
    @Column(name = "city")
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Basic
    @Column(name = "language")
    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    @Basic
    @Column(name = "headimgurl")
    public String getHeadimgurl() {
        return headimgurl;
    }

    public void setHeadimgurl(String headimgurl) {
        this.headimgurl = headimgurl;
    }

    @Basic
    @Column(name = "unionid")
    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    @Basic
    @Column(name = "remark")
    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Basic
    @Column(name = "groupid")
    public Integer getGroupid() {
        return groupid;
    }

    public void setGroupid(Integer groupid) {
        this.groupid = groupid;
    }

    @Basic
    @Column(name = "errcode")
    public String getErrcode() {
        return errcode;
    }

    public void setErrcode(String errcode) {
        this.errcode = errcode;
    }

    @Basic
    @Column(name = "errmsg")
    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    @Basic
    @Column(name = "source")
    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }
    @Basic
    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    @Basic
    @Column(name = "remark_crop_name")
    public String getRemarkCropName() {
        return remarkCropName;
    }

    public void setRemarkCropName(String remarkCropName) {
        this.remarkCropName = remarkCropName;
    }

    @Basic
    @Column(name = "related_name")
    public String getRelatedName() {
        return relatedName;
    }

    public void setRelatedName(String relatedName) {
        this.relatedName = relatedName;
    }
    @Basic
    @Column(name = "is_del")
    public String getIsDel() {
        return isDel;
    }

    public void setIsDel(String isDel) {
        this.isDel = isDel;
    }

    @Basic
    @Column(name = "phone1")
    public String getPhone1() {
        return phone1;
    }

    public void setPhone1(String phone1) {
        this.phone1 = phone1;
    }
    @Basic
    @Column(name = "phone2")
    public String getPhone2() {
        return phone2;
    }

    public void setPhone2(String phone2) {
        this.phone2 = phone2;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        WeiUserInfoModel that = (WeiUserInfoModel) o;
        return id == that.id &&
                Objects.equals(openid, that.openid) &&
                Objects.equals(subscribe, that.subscribe) &&
                Objects.equals(subscribeTime, that.subscribeTime) &&
                Objects.equals(nickname, that.nickname) &&
                Objects.equals(sex, that.sex) &&
                Objects.equals(country, that.country) &&
                Objects.equals(province, that.province) &&
                Objects.equals(city, that.city) &&
                Objects.equals(language, that.language) &&
                Objects.equals(headimgurl, that.headimgurl) &&
                Objects.equals(unionid, that.unionid) &&
                Objects.equals(remark, that.remark) &&
                Objects.equals(groupid, that.groupid) &&
                Objects.equals(errcode, that.errcode) &&
                Objects.equals(errmsg, that.errmsg);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, openid, subscribe, subscribeTime, nickname, sex, country, province, city, language, headimgurl, unionid, remark, groupid, errcode, errmsg);
    }
}

  • 存在表里面的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值