| | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import jdk.nashorn.internal.ir.IfNode; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.converter.StringHttpMessageConverter; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.nio.charset.Charset; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author wzy |
| | |
| | | private final DataDictionaryCustomMapper dataDictionaryCustomMapper; |
| | | private final MallMemberMapper memberMapper; |
| | | private final MallMemberWalletMapper mallMemberWalletMapper; |
| | | private final RestTemplate restTemplate; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | return; |
| | | } |
| | | String requrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN"; |
| | | String reslutData = null; |
| | | try { |
| | | reslutData = HttpCurlUtil.sendGetHttp(requrl, null); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return; |
| | | } |
| | | net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(reslutData); |
| | | // 创建一个StringHttpMessageConverter,并设置字符集为UTF-8 |
| | | StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8")); |
| | | stringConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN)); |
| | | // 将StringHttpMessageConverter添加到RestTemplate的消息转换器列表中 |
| | | restTemplate.getMessageConverters().add(0, stringConverter); |
| | | // 创建HttpHeaders对象,设置Accept头部的值为"text/plain;charset=UTF-8" |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setAccept(Collections.singletonList(MediaType.TEXT_PLAIN)); |
| | | headers.set(HttpHeaders.ACCEPT_CHARSET, "UTF-8"); |
| | | |
| | | String responseStr = restTemplate.getForObject(requrl, String.class); |
| | | net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(responseStr); |
| | | log.info("微信获取用户信息={}", json); |
| | | |
| | | if (json.containsKey("errcode")) { |
| | |
| | | mallMember.setAvatar(headImgUrl); |
| | | memberMapper.updateById(mallMember); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String accessToken = "70_6yOz-tXaDx6qMswGgmfl6j3LfgG-ur3JE-top_QvX5Oea9ryFxzEBwgVoW53IpMrzcS7mpPJf7tI3czT_kA2l5Y_Xu1WS7d7xDNbMs97ARk"; |
| | | String openId = "oXL7Y6LW5KGQTbmmP5W3JljjApSo"; |
| | | String requrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN"; |
| | | // 创建一个StringHttpMessageConverter,并设置字符集为UTF-8 |
| | | StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8")); |
| | | stringConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN)); |
| | | // 将StringHttpMessageConverter添加到RestTemplate的消息转换器列表中 |
| | | restTemplate.getMessageConverters().add(0, stringConverter); |
| | | // 创建HttpHeaders对象,设置Accept头部的值为"text/plain;charset=UTF-8" |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setAccept(Collections.singletonList(MediaType.TEXT_PLAIN)); |
| | | headers.set(HttpHeaders.ACCEPT_CHARSET, "UTF-8"); |
| | | |
| | | String responseStr = restTemplate.getForObject(requrl, String.class); |
| | | net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(responseStr); |
| | | log.info("微信获取用户信息={}", json); |
| | | String nickname = json.getString("nickname"); |
| | | String headImgUrl = json.getString("headimgurl"); |
| | | System.out.println(nickname); |
| | | System.out.println(headImgUrl); |
| | | } |
| | | } |