java微信端html_H5微信网页授权java后端SpringBoot实现

在这里我整理了一个最简便的实现方式,请大家参考

首先在pom.xml中引入java工具包:

org.weixin4j.spring.boot

weixin4j-spring-boot-starter

1.0.0

com.github.liyiorg

weixin-popular

2.8.5

weixin4j.config.appid=yourappid

weixin4j.config.secret=yoursecret

Controller层需要接受的参数需要以下两个:

code:前端页面授权同意获取的code

Service层方法如下(核心):

@Resource

private WeixinTemplate weixinTemplate; //weixin4j工具模板

@Transactional

public Map login(Map param) throws WeixinException {

SnsUser snsUser = weixinTemplate.sns().getSnsUserByCode((String) param.get("code")); //通过code获取access_token信息

String subscribe = weixinTemplate.user().info(snsUser.getOpenid()).getSubscribe();

Map map = new HashMap<>(); //返回值map

String randomStr = UUID.randomUUID().toString().substring(0, 18);

map.put("appId", weixinTemplate.getAppId()); //appid,前端若不需要可忽略

Date date = new Date();

String sign = SignUtil.getSignature(weixinTemplate.getJsApiTicket().getTicket(), randomStr, new Long(date.getTime() / 1000).toString(), (String) param.get("url"));

//-----以下代码根据具体业务处理-----

//拼装前端需要的返回结果

Map signature_dict = new HashMap<>();

signature_dict.put("nonceStr", randomStr);

signature_dict.put("signature", sign);

signature_dict.put("timestamp", new Long(date.getTime() / 1000));

map.put("signature_dict", signature_dict);

//根据openid查询用户id,以此决定更新还是新增用户信息

WxUsers wxUsers = wxUsersMapper.selectIdByOpenid(snsUser.getOpenid());

if(wxUsers == null){

wxUsers = new WxUsers();

}

wxUsers.setNickName(snsUser.getNickname());

wxUsers.setSex(snsUser.getSex());

wxUsers.setImg(snsUser.getHeadimgurl());

wxUsers.setOpenid(snsUser.getOpenid());

map.put("nickName",wxUsers.getNickName());

map.put("img",wxUsers.getImg());

if (wxUsers.getWxUsersId() == null) {

wxUsersMapper.insert(wxUsers);

map.put("wxUsersId",wxUsers.getWxUsersId());

} else {

map.put("wxUsersId",wxUsers.getWxUsersId());

wxUsersMapper.updateByPrimaryKey(wxUsers);

}

return map;

}

在这里我整理了一个最简便的实现方式,请大家参考 首先在pom.xml中引入java工具包: org.weixin4j.spring.boot weixin4j-spring-boot-starter 1.0.0 com.github.liyiorg weixin-popular 2.8.5 weixin4j.config.appid=yourappid weixin4j.config.secret=yoursecret Controller层需要接受的参数需要以下两个: code:前端页面授权同意获取的code Service层方法如下(核心): @Resource private WeixinTemplate weixinTemplate; //weixin4j工具模板 @Transactional public Map login(Map param) throws WeixinException { SnsUser snsUser = weixinTemplate.sns().getSnsUserByCode((String) param.get("code")); //通过code获取access_token信息 String subscribe = weixinTemplate.user().info(snsUser.getOpenid()).getSubscribe(); Map map = new HashMap<>(); //返回值map String randomStr = UUID.randomUUID().toString().substring(0, 18); map.put("appId", weixinTemplate.getAppId()); //appid,前端若不需要可忽略 Date date = new Date(); String sign = SignUtil.getSignature(weixinTemplate.getJsApiTicket().getTicket(), randomStr, new Long(date.getTime() / 1000).toString(), (String) param.get("url")); //-----以下代码根据具体业务处理----- //拼装前端需要的返回结果 Map signature_dict = new HashMap<>(); signature_dict.put("nonceStr", randomStr); signature_dict.put("signature", sign); signature_dict.put("timestamp", new Long(date.getTime() / 1000)); map.put("signature_dict", signature_dict); //根据openid查询用户id,以此决定更新还是新增用户信息 WxUsers wxUsers = wxUsersMapper.selectIdByOpenid(snsUser.getOpenid()); if(wxUsers == null){ wxUsers = new WxUsers(); } wxUsers.setNickName(snsUser.getNickname()); wxUsers.setSex(snsUser.getSex()); wxUsers.setImg(snsUser.getHeadimgurl()); wxUsers.setOpenid(snsUser.getOpenid()); map.put("nickName",wxUsers.getNickName()); map.put("img",wxUsers.getImg()); if (wxUsers.getWxUsersId() == null) { wxUsersMapper.insert(wxUsers); map.put("wxUsersId",wxUsers.getWxUsersId()); } else { map.put("wxUsersId",wxUsers.getWxUsersId()); wxUsersMapper.updateByPrimaryKey(wxUsers); } return map; }
经验分享 程序员 微信小程序 职场和发展