package xuan.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import xuan.entity.UserInfo; import xuan.service.UserInfoService; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.UUID; @RestController @RequestMapping("/api/user") @CrossOrigin(origins = "*") public class UserController { @Autowired private UserInfoService userInfoService; @Value("${wechat.appid:}") private String appId; @Value("${wechat.secret:}") private String appSecret; @PostMapping("/login") public Map login(@RequestBody Map params) { Map result = new HashMap<>(); try { String code = params.get("code") != null ? params.get("code").toString() : null; String username = params.get("nickname") != null ? params.get("nickname").toString() : "微信用户"; String avatarUrl = params.get("avatarUrl") != null ? params.get("avatarUrl").toString() : ""; System.out.println("=== 登录请求 ==="); System.out.println("code: " + code); System.out.println("appId: " + appId); System.out.println("appSecret: " + (appSecret != null ? "已配置" : "未配置")); String openId = null; if (code != null && !code.isEmpty() && appId != null && !appId.isEmpty() && appSecret != null && !appSecret.isEmpty()) { String url = String.format( "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, appSecret, code ); System.out.println("调用微信API: " + url); try { RestTemplate restTemplate = new RestTemplate(); @SuppressWarnings("unchecked") Map wxResult = restTemplate.getForObject(url, Map.class); System.out.println("微信API返回: " + wxResult); if (wxResult != null && wxResult.containsKey("openid")) { openId = (String) wxResult.get("openid"); System.out.println("获取到openId: " + openId); } else if (wxResult != null) { System.err.println("微信API返回错误: " + wxResult.get("errmsg") + ", errcode: " + wxResult.get("errcode")); } } catch (Exception e) { System.err.println("调用微信API失败: " + e.getMessage()); e.printStackTrace(); } } else { System.err.println("登录参数不完整: code=" + code + ", appId=" + appId + ", appSecret=" + (appSecret != null ? "有" : "无")); } if (openId == null || openId.isEmpty()) { result.put("success", false); result.put("error", "获取用户身份失败,请重试"); return result; } UserInfo user = userInfoService.loginOrRegister(openId, username, avatarUrl); System.out.println("登录成功: userId=" + user.getUserId() + ", openId=" + user.getOpenId()); Map data = new HashMap<>(); data.put("id", user.getId()); data.put("userId", user.getUserId()); data.put("openId", user.getOpenId()); data.put("avatarUrl", user.getAvatarUrl()); data.put("username", user.getUsername()); data.put("phone", user.getPhone()); data.put("vipLevel", user.getVipLevel()); data.put("coins", user.getCoins()); data.put("streakDays", user.getStreakDays()); data.put("createTime", user.getCreateTime()); result.put("success", true); result.put("data", data); } catch (Exception e) { System.err.println("登录异常: " + e.getMessage()); e.printStackTrace(); result.put("success", false); result.put("error", e.getMessage()); } return result; } @GetMapping("/info") public Map getUserInfo(@RequestParam String userId) { Map result = new HashMap<>(); try { UserInfo user = userInfoService.getUserInfo(userId); if (user != null) { Map data = new HashMap<>(); data.put("id", user.getId()); data.put("userId", user.getUserId()); data.put("openId", user.getOpenId()); data.put("avatarUrl", user.getAvatarUrl()); data.put("username", user.getUsername()); data.put("phone", user.getPhone()); data.put("vipLevel", user.getVipLevel()); data.put("coins", user.getCoins()); data.put("streakDays", user.getStreakDays()); data.put("createTime", user.getCreateTime()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/updateVip") public Map updateVipLevel(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); Integer vipLevel = Integer.parseInt(params.get("vipLevel").toString()); UserInfo user = userInfoService.updateVipLevel(userId, vipLevel); if (user != null) { Map data = new HashMap<>(); data.put("vipLevel", user.getVipLevel()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/updateCoins") public Map updateCoins(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); Integer coins = Integer.parseInt(params.get("coins").toString()); UserInfo user = userInfoService.updateCoins(userId, coins); if (user != null) { Map data = new HashMap<>(); data.put("coins", user.getCoins()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/updatePhone") public Map updatePhone(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); String phone = params.get("phone").toString(); UserInfo user = userInfoService.updatePhone(userId, phone); if (user != null) { Map data = new HashMap<>(); data.put("phone", user.getPhone()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/updateUsername") public Map updateUsername(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); String username = params.get("username").toString(); UserInfo user = userInfoService.updateUsername(userId, username); if (user != null) { Map data = new HashMap<>(); data.put("username", user.getUsername()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/updateAvatar") public Map updateAvatar(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); String avatarUrl = params.get("avatarUrl").toString(); UserInfo user = userInfoService.updateAvatarUrl(userId, avatarUrl); if (user != null) { Map data = new HashMap<>(); data.put("avatarUrl", user.getAvatarUrl()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/permissionLog") public Map addPermissionLog(@RequestBody Map params) { Map result = new HashMap<>(); try { result.put("success", true); result.put("message", "权限使用记录已保存"); } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/deleteAccount") public Map deleteAccount(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); boolean deleted = userInfoService.deleteAccount(userId); if (deleted) { result.put("success", true); result.put("message", "账号已注销"); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/consumeCoins") public Map consumeCoins(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); Integer amount = params.get("amount") != null ? Integer.parseInt(params.get("amount").toString()) : 1; String type = params.get("type") != null ? params.get("type").toString() : "unknown"; String description = params.get("description") != null ? params.get("description").toString() : "金币消耗"; String relatedId = params.get("relatedId") != null ? params.get("relatedId").toString() : null; UserInfo user = userInfoService.consumeCoins(userId, amount, type, description, relatedId); if (user != null) { Map data = new HashMap<>(); data.put("coins", user.getCoins()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在或金币不足"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/addCoins") public Map addCoins(@RequestBody Map params) { Map result = new HashMap<>(); try { String userId = params.get("userId").toString(); Integer amount = params.get("amount") != null ? Integer.parseInt(params.get("amount").toString()) : 1; String type = params.get("type") != null ? params.get("type").toString() : "unknown"; String description = params.get("description") != null ? params.get("description").toString() : "金币奖励"; UserInfo user = userInfoService.addCoins(userId, amount, type, description); if (user != null) { Map data = new HashMap<>(); data.put("coins", user.getCoins()); result.put("success", true); result.put("data", data); } else { result.put("success", false); result.put("error", "用户不存在"); } } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } @PostMapping("/uploadAvatar") public Map uploadAvatar( @RequestParam("file") MultipartFile file, @RequestParam(value = "userId", required = false) String userId, @RequestParam(value = "temp", required = false) String temp) { Map result = new HashMap<>(); try { if (file.isEmpty()) { result.put("success", false); result.put("error", "请选择文件"); return result; } String uploadDir = "uploads/avatars/"; File dir = new File(uploadDir); if (!dir.exists()) { dir.mkdirs(); } String originalFilename = file.getOriginalFilename(); String extension = ".jpg"; if (originalFilename != null && originalFilename.contains(".")) { extension = originalFilename.substring(originalFilename.lastIndexOf(".")); } String filename = UUID.randomUUID().toString() + extension; Path filePath = Paths.get(uploadDir + filename); Files.write(filePath, file.getBytes()); String avatarUrl = "/uploads/avatars/" + filename; if (userId != null && !userId.isEmpty() && !"true".equals(temp)) { UserInfo user = userInfoService.updateAvatarUrl(userId, avatarUrl); if (user == null) { result.put("success", false); result.put("error", "用户不存在"); return result; } } Map data = new HashMap<>(); data.put("avatarUrl", avatarUrl); result.put("success", true); result.put("data", data); } catch (IOException e) { result.put("success", false); result.put("error", "文件上传失败: " + e.getMessage()); } catch (Exception e) { result.put("success", false); result.put("error", e.getMessage()); } return result; } }