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 xuan.entity.UserInfo; import xuan.service.UserInfoService; import java.util.HashMap; import java.util.Map; @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 nickname = params.get("nickname") != null ? params.get("nickname").toString() : "微信用户"; String avatarUrl = params.get("avatarUrl") != null ? params.get("avatarUrl").toString() : ""; 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 ); try { RestTemplate restTemplate = new RestTemplate(); @SuppressWarnings("unchecked") Map wxResult = restTemplate.getForObject(url, Map.class); if (wxResult != null && wxResult.containsKey("openid")) { openId = (String) wxResult.get("openid"); } } catch (Exception e) { System.err.println("调用微信API失败: " + e.getMessage()); } } if (openId == null || openId.isEmpty()) { openId = "guest_" + System.currentTimeMillis(); } UserInfo user = userInfoService.loginOrRegister(openId, nickname, avatarUrl); Map data = new HashMap<>(); data.put("id", user.getId()); data.put("userId", user.getUserId()); data.put("openId", user.getOpenId()); data.put("nickname", user.getNickname()); 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) { 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("nickname", user.getNickname()); 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; } }