1. Wechat user data table

2. Controller file

@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()));
            returnR.ok(userInfo); }}Copy the code

3. Service code

/** * Obtain user information through web page authorization */
 @Service
public class WechatService {
    @SuppressWarnings({"deprecation"."unchecked"})
    public  WechatUserInfoEntity getSNSUserInfo(String accessToken, String openId) {
        WechatUserInfoEntity snsUserInfo = null;
        // Splice the request address
        String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
        // Obtain user information through web page authorization
        String urlResult = HttpUtil.get(requestUrl);
        JSONObject jsonUrl = JSONUtil.parseObj(urlResult);
        if (null! = jsonUrl) { snsUserInfo =new WechatUserInfoEntity();
            // The user's identity
            snsUserInfo.setOpenId(MapUtil.getStr(jsonUrl, "openid"));
            / / nickname
            snsUserInfo.setNickname(MapUtil.getStr(jsonUrl, "nickname"));
            // User profile picture
            snsUserInfo.setHeadImage(MapUtil.getStr(jsonUrl, "headimgurl"));
        }
        returnsnsUserInfo; }}`
Copy the code

4. Constant code

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";
}
Copy the code

Full code link: github.crmeb.net/u/LXT