请写出带注释的Java的SpringCloud微信登录的插入和查询后端代码分为controller层,service接口,service实现类,mapper接口,mapper配置文件,调用微信API类
时间: 2024-06-09 12:10:25 浏览: 154
1. controller层代码:
```java
@RestController
@RequestMapping("/api/wechat")
public class WechatController {
@Autowired
private WechatService wechatService;
@PostMapping("/login")
public String login(@RequestParam String code) {
return wechatService.login(code);
}
@GetMapping("/user")
public WechatUser getUser(@RequestParam String openid) {
return wechatService.getUser(openid);
}
}
```
2. service接口代码:
```java
public interface WechatService {
String login(String code);
WechatUser getUser(String openid);
}
```
3. service实现类代码:
```java
@Service
public class WechatServiceImpl implements WechatService {
@Autowired
private WechatMapper wechatMapper;
@Autowired
private WechatApi wechatApi;
@Override
public String login(String code) {
String openid = wechatApi.getOpenid(code);
WechatUser user = wechatMapper.selectUserByOpenid(openid);
if (user == null) {
user = wechatApi.getUserInfo(openid);
wechatMapper.insertUser(user);
}
return openid;
}
@Override
public WechatUser getUser(String openid) {
return wechatMapper.selectUserByOpenid(openid);
}
}
```
4. mapper接口代码:
```java
@Mapper
public interface WechatMapper {
void insertUser(WechatUser user);
WechatUser selectUserByOpenid(String openid);
}
```
5. mapper配置文件:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.WechatMapper">
<insert id="insertUser" parameterType="com.example.demo.entity.WechatUser">
INSERT INTO wechat_user(openid, nickname, avatar_url)
VALUES(#{openid}, #{nickname}, #{avatarUrl})
</insert>
<select id="selectUserByOpenid" parameterType="String" resultType="com.example.demo.entity.WechatUser">
SELECT openid, nickname, avatar_url
FROM wechat_user
WHERE openid = #{openid}
</select>
</mapper>
```
6. 调用微信API类代码:
```java
@Component
public class WechatApi {
@Value("${wechat.appid}")
private String appid;
@Value("${wechat.secret}")
private String secret;
public String getOpenid(String code) {
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid
+ "&secret=" + secret
+ "&js_code=" + code
+ "&grant_type=authorization_code";
String json = restTemplate.getForObject(url, String.class);
return JsonPath.read(json, "$.openid");
}
public WechatUser getUserInfo(String openid) {
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + getAccessToken() + "&openid=" + openid;
String json = restTemplate.getForObject(url, String.class);
String nickname = JsonPath.read(json, "$.nickname");
String avatarUrl = JsonPath.read(json, "$.headimgurl");
return new WechatUser(openid, nickname, avatarUrl);
}
private String getAccessToken() {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
String json = restTemplate.getForObject(url, String.class);
return JsonPath.read(json, "$.access_token");
}
}
```
注:在以上代码中,使用了RestTemplate和JsonPath,需要在pom.xml中添加相应的依赖。
阅读全文