1、微信用户数据表
2、controller文件
@RestController
@RequestMapping("/api/wechatuserinfo")
public class WechatController {
@Autowired
WechatService wechatService;
@Autowired
CustomerInfoService customerInfoService;
@Autowired
WechatUserInfoService wechatUserInfoService;
@Autowired
AddressInfoService addressInfoService;
@RequestMapping("wechat")
@Transactional
public R getOpenId(String code) {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +
WECHAT_APP_ID +
"&secret=" +
WECHAT_APP_SECRET +
"&code=" +
code +
"&grant_type=authorization_code";
String urlResult = HttpUtil.get(url);
JSONObject jsonUrl = JSONUtil.parseObj(urlResult);
String openid = MapUtil.getStr(jsonUrl, "openid");
String accessToken = MapUtil.getStr(jsonUrl, "access_token");
WechatUserInfoEntity userInfo =wechatService.getSNSUserInfo(accessToken, openid);
WechatUserInfoEntity wechatUserInfoEntity = wechatUserInfoService.queryByOpenId(userInfo.getOpenId());
if (wechatUserInfoEntity != null) {
return R.ok(wechatUserInfoEntity);
} else {
CustomerInfoEntity customerInfoEntity = new CustomerInfoEntity();
customerInfoEntity.setName(Base64.encode(userInfo.getNickname()));
customerInfoEntity.setPhoto(userInfo.getHeadImage());
customerInfoEntity.setCreateTime(new Date());
customerInfoEntity.setUpdateTime(new Date());
customerInfoService.save(customerInfoEntity);
userInfo.setNickname(Base64.encode(userInfo.getNickname()));
userInfo.setUserId(customerInfoEntity.getId());
userInfo.setCreateTime(new Date());
userInfo.setUpdateTime(new Date());
wechatUserInfoService.save(userInfo);
AddressInfoEntity addressInfoEntity = new AddressInfoEntity();
addressInfoEntity.setUserId(userInfo.getUserId());
addressInfoEntity.setArea("");
addressInfoService.save(addressInfoEntity);
customerInfoEntity.setAddressId(addressInfoEntity.getId());
customerInfoService.updateById(customerInfoEntity);
userInfo.setNickname(Base64.decodeStr(userInfo.getNickname()));
return R.ok(userInfo);
}
}
3、service代码
/**
* 通过网页授权获取用户信息
*/
@Service
public class WechatService {
@SuppressWarnings({"deprecation", "unchecked"})
public WechatUserInfoEntity getSNSUserInfo(String accessToken, String openId) {
WechatUserInfoEntity snsUserInfo = null;
// 拼接请求地址
String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
// 通过网页授权获取用户信息
String urlResult = HttpUtil.get(requestUrl);
JSONObject jsonUrl = JSONUtil.parseObj(urlResult);
if (null != jsonUrl) {
snsUserInfo = new WechatUserInfoEntity();
// 用户的标识
snsUserInfo.setOpenId(MapUtil.getStr(jsonUrl, "openid"));
// 昵称
snsUserInfo.setNickname(MapUtil.getStr(jsonUrl, "nickname"));
// 用户头像
snsUserInfo.setHeadImage(MapUtil.getStr(jsonUrl, "headimgurl"));
}
return snsUserInfo;
}
}
4、常量代码
public interface WechatConsts {
// String WECHAT_APP_ID="wx0afb876c6fc70e1e";
// String WECHAT_APP_SECRET="1e7db4c466a2846006836c0007991d49";
// String TEMPLATE_ID="cLrzp2ThnYNhKUySQADT8Yqizyd9Cdm7qUzKgOFSoBs";
String WECHAT_APP_ID="wx9648a4f1ddb1abfa";
String WECHAT_APP_SECRET="5db2c92690351b04f01a06913abdf186";
String TEMPLATE_ID="cLrzp2ThnYNhKUySQADT8Yqizyd9Cdm7qUzKgOFSoBs";
}
请问为什么我用测试号可以获取到头像,但是用了APPID就获取不到头像了?
楼主看看这个https://developers.weixin.qq.com/community/develop/doc/000840c3ef4258e3377908baa56000
公众号还有这样的接口?
小程序可以吗?
更换头像后 头像链接失效 怎么办呢。是否有上传到cdn上?通过openId和cdn链接绑定么?