|
@@ -0,0 +1,123 @@
|
|
|
+package cn.flea.chexnetmaster.service.impl;
|
|
|
+
|
|
|
+import cn.flea.chexnetmaster.mapper.UserMapper;
|
|
|
+import cn.flea.chexnetmaster.pojo.Result;
|
|
|
+import cn.flea.chexnetmaster.pojo.User;
|
|
|
+import cn.flea.chexnetmaster.service.UserService;
|
|
|
+import cn.flea.chexnetmaster.util.JwtUtil;
|
|
|
+import cn.flea.chexnetmaster.util.WeiXinUtil;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UserServiceA implements UserService {
|
|
|
+ @Autowired
|
|
|
+ WeiXinUtil weiXinUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserMapper userMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result login(String code) {
|
|
|
+ String res = weiXinUtil.getOpenid(code);
|
|
|
+ log.info(res);
|
|
|
+ try {
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ // 将String解析为JsonNode
|
|
|
+ JsonNode jsonNode = objectMapper.readTree(res);
|
|
|
+
|
|
|
+ String openid = jsonNode.get("openid").asText();
|
|
|
+ String sessionKey = jsonNode.get("session_key").asText();
|
|
|
+ String unionid = jsonNode.get("unionid").asText();
|
|
|
+
|
|
|
+ User user = userMapper.getUserByOpenid(openid);
|
|
|
+
|
|
|
+ if (user == null) {
|
|
|
+ log.info("用户不存在,创建新用户");
|
|
|
+
|
|
|
+ user = new User();
|
|
|
+ user.setName("微信用户");
|
|
|
+ user.setSessionKey(sessionKey);
|
|
|
+ user.setWechatOpenid(openid);
|
|
|
+ user.setWechatUnionid(unionid);
|
|
|
+ Integer id = addUser(user);
|
|
|
+
|
|
|
+ HashMap<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put("id", id);
|
|
|
+ map.put("openid", openid);
|
|
|
+
|
|
|
+ HashMap<String, String> info = new HashMap<String, String>();
|
|
|
+ info.put("name", user.getName());
|
|
|
+ info.put("token",JwtUtil.getJwt(map));
|
|
|
+
|
|
|
+ return Result.success("new", info);
|
|
|
+ } else {
|
|
|
+ log.info("用户已存在,更新用户session_key");
|
|
|
+ user.setSessionKey(sessionKey);
|
|
|
+ Integer id = updateUserByOpenid(user);
|
|
|
+
|
|
|
+ HashMap<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put("id", id);
|
|
|
+ map.put("openid", openid);
|
|
|
+
|
|
|
+ HashMap<String, String> info = new HashMap<String, String>();
|
|
|
+ info.put("name", user.getName());
|
|
|
+ info.put("token",JwtUtil.getJwt(map));
|
|
|
+
|
|
|
+ return Result.success("old", info);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return Result.error("登录失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ public int addUser(User user) {
|
|
|
+ userMapper.addUser(user);
|
|
|
+ int id = user.getId();
|
|
|
+ log.info("用户添加成功:" + id);
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int deleteUser(String wechatOpenid) {
|
|
|
+ return userMapper.deleteUserByOpenid(wechatOpenid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int updateUserByOpenid(User user) {
|
|
|
+ userMapper.updateUserByOpenid(user);
|
|
|
+ int id = user.getId();
|
|
|
+ log.info("用户更新成功:" + id);
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int updateUserById(User user) {
|
|
|
+ return userMapper.updateUserById(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public User getUserByOpenid(String wechatOpenid) {
|
|
|
+ return userMapper.getUserByOpenid(wechatOpenid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String checkToken(HashMap<String, Object> map, Integer exp) {
|
|
|
+
|
|
|
+ if (exp < 24 * 60 * 60 * 1000) return JwtUtil.getJwt(map);
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public User getUserById(Integer id) {
|
|
|
+ return userMapper.getUserById(id);
|
|
|
+ }
|
|
|
+}
|