feat: 添加用户登录授权和金币消耗功能
后端: - 创建UserInfo实体类和数据库表 - 创建UserInfoRepository和UserInfoService - 创建UserController处理用户登录、信息更新等API 前端: - 创建登录授权页面(auth.vue) - 创建金币检查工具函数(coinUtils.js) - 更新api.js添加用户API - 更新测算页面(bazi.vue, ziwei.vue)添加金币检查逻辑 - 更新我的账号页面显示用户信息 - 更新个人信息与权限使用记录页面 功能: - 首次使用需要授权登录 - 自动生成随机用户名和用户ID - 每次测算消耗1金币 - 金币不足时弹出提示 - 会员用户无需消耗金币
This commit is contained in:
1 parent
15ee012d76
commit
b05650860e
12 files changed
+1051
-633
No files matched your search
@@ -0,0 +1,165 @@
|
||||
package xuan.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
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;
|
||||
|
||||
@PostMapping("/login")
|
||||
public Map<String, Object> login(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
String openId = params.get("openId") != null ? params.get("openId").toString() : null;
|
||||
String nickname = params.get("nickname") != null ? params.get("nickname").toString() : "微信用户";
|
||||
String avatarUrl = params.get("avatarUrl") != null ? params.get("avatarUrl").toString() : "";
|
||||
|
||||
if (openId == null || openId.isEmpty()) {
|
||||
openId = "guest_" + System.currentTimeMillis();
|
||||
}
|
||||
|
||||
UserInfo user = userInfoService.loginOrRegister(openId, nickname, avatarUrl);
|
||||
|
||||
Map<String, Object> 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<String, Object> getUserInfo(@RequestParam String userId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
UserInfo user = userInfoService.getUserInfo(userId);
|
||||
if (user != null) {
|
||||
Map<String, Object> 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<String, Object> updateVipLevel(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> 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) {
|
||||
result.put("success", true);
|
||||
result.put("data", Map.of("vipLevel", user.getVipLevel()));
|
||||
} 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<String, Object> updateCoins(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> 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) {
|
||||
result.put("success", true);
|
||||
result.put("data", Map.of("coins", user.getCoins()));
|
||||
} 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<String, Object> updatePhone(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> 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) {
|
||||
result.put("success", true);
|
||||
result.put("data", Map.of("phone", user.getPhone()));
|
||||
} 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<String, Object> addPermissionLog(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package xuan.entity;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "user_info")
|
||||
public class UserInfo {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, unique = true, length = 64)
|
||||
private String openId;
|
||||
|
||||
@Column(length = 50)
|
||||
private String nickname;
|
||||
|
||||
@Column(length = 255)
|
||||
private String avatarUrl;
|
||||
|
||||
@Column(length = 50)
|
||||
private String username;
|
||||
|
||||
@Column(length = 20)
|
||||
private String userId;
|
||||
|
||||
@Column(length = 20)
|
||||
private String phone;
|
||||
|
||||
private Integer vipLevel;
|
||||
|
||||
private Integer coins;
|
||||
|
||||
private Integer streakDays;
|
||||
|
||||
@Column(nullable = false, updatable = false)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
createTime = LocalDateTime.now();
|
||||
updateTime = createTime;
|
||||
vipLevel = 0;
|
||||
coins = 10;
|
||||
streakDays = 0;
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
updateTime = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getOpenId() { return openId; }
|
||||
public void setOpenId(String openId) { this.openId = openId; }
|
||||
|
||||
public String getNickname() { return nickname; }
|
||||
public void setNickname(String nickname) { this.nickname = nickname; }
|
||||
|
||||
public String getAvatarUrl() { return avatarUrl; }
|
||||
public void setAvatarUrl(String avatarUrl) { this.avatarUrl = avatarUrl; }
|
||||
|
||||
public String getUsername() { return username; }
|
||||
public void setUsername(String username) { this.username = username; }
|
||||
|
||||
public String getUserId() { return userId; }
|
||||
public void setUserId(String userId) { this.userId = userId; }
|
||||
|
||||
public String getPhone() { return phone; }
|
||||
public void setPhone(String phone) { this.phone = phone; }
|
||||
|
||||
public Integer getVipLevel() { return vipLevel; }
|
||||
public void setVipLevel(Integer vipLevel) { this.vipLevel = vipLevel; }
|
||||
|
||||
public Integer getCoins() { return coins; }
|
||||
public void setCoins(Integer coins) { this.coins = coins; }
|
||||
|
||||
public Integer getStreakDays() { return streakDays; }
|
||||
public void setStreakDays(Integer streakDays) { this.streakDays = streakDays; }
|
||||
|
||||
public LocalDateTime getCreateTime() { return createTime; }
|
||||
public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }
|
||||
|
||||
public LocalDateTime getUpdateTime() { return updateTime; }
|
||||
public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package xuan.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import xuan.entity.UserInfo;
|
||||
|
||||
@Repository
|
||||
public interface UserInfoRepository extends JpaRepository<UserInfo, Long> {
|
||||
UserInfo findByOpenId(String openId);
|
||||
UserInfo findByUserId(String userId);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package xuan.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xuan.entity.UserInfo;
|
||||
import xuan.repository.UserInfoRepository;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class UserInfoService {
|
||||
|
||||
@Autowired
|
||||
private UserInfoRepository userInfoRepository;
|
||||
|
||||
public UserInfo loginOrRegister(String openId, String nickname, String avatarUrl) {
|
||||
UserInfo user = userInfoRepository.findByOpenId(openId);
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
|
||||
user = new UserInfo();
|
||||
user.setOpenId(openId);
|
||||
user.setNickname(nickname);
|
||||
user.setAvatarUrl(avatarUrl);
|
||||
user.setUsername(generateUsername());
|
||||
user.setUserId(generateUserId());
|
||||
user.setVipLevel(0);
|
||||
user.setCoins(10);
|
||||
user.setStreakDays(0);
|
||||
|
||||
return userInfoRepository.save(user);
|
||||
}
|
||||
|
||||
public UserInfo getUserInfo(String userId) {
|
||||
return userInfoRepository.findByUserId(userId);
|
||||
}
|
||||
|
||||
public UserInfo updateVipLevel(String userId, Integer vipLevel) {
|
||||
UserInfo user = userInfoRepository.findByUserId(userId);
|
||||
if (user != null) {
|
||||
user.setVipLevel(vipLevel);
|
||||
return userInfoRepository.save(user);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserInfo updateCoins(String userId, Integer coins) {
|
||||
UserInfo user = userInfoRepository.findByUserId(userId);
|
||||
if (user != null) {
|
||||
user.setCoins(coins);
|
||||
return userInfoRepository.save(user);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserInfo updatePhone(String userId, String phone) {
|
||||
UserInfo user = userInfoRepository.findByUserId(userId);
|
||||
if (user != null) {
|
||||
user.setPhone(phone);
|
||||
return userInfoRepository.save(user);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String generateUsername() {
|
||||
String prefix = "玄机用户";
|
||||
String suffix = generateRandomString(6);
|
||||
return prefix + suffix;
|
||||
}
|
||||
|
||||
private String generateUserId() {
|
||||
return String.valueOf(System.currentTimeMillis()).substring(5);
|
||||
}
|
||||
|
||||
private String generateRandomString(int length) {
|
||||
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < length; i++) {
|
||||
sb.append(chars.charAt(random.nextInt(chars.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user