用户类完善 商城功能
Change-Id: I2fa4cecee4203114998d7f9f24e956660a2da5ec
diff --git a/src/main/java/com/pt5/pthouduan/controller/ShopController.java b/src/main/java/com/pt5/pthouduan/controller/ShopController.java
new file mode 100644
index 0000000..dfb38b2
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/controller/ShopController.java
@@ -0,0 +1,28 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.User;
+import com.pt5.pthouduan.service.InviteService;
+import com.pt5.pthouduan.service.ShopService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
+
+@RestController
+@RequestMapping("/shop")
+public class ShopController {
+ @Autowired
+ private ShopService shopService;
+ @PostMapping("/soldDecoration")
+ public Map<String, Object> soldDecoration(@RequestParam String buyername,@RequestParam String decoration,@RequestParam int price) {
+ return shopService.buyDecoration(buyername,decoration,price);
+ }
+ @PostMapping("/soldUpload")
+ public Map<String, Object> soldUpload(@RequestParam String buyername,@RequestParam int price,@RequestParam int upload) {
+ return shopService.buyUpload(buyername,price,upload);
+ }
+ @PostMapping("/soldInvite")
+ public Map<String, Object> soldInvite(@RequestParam String buyername,@RequestParam int price) {
+ return shopService.setbuyername(buyername,price);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/pt5/pthouduan/controller/UserController.java b/src/main/java/com/pt5/pthouduan/controller/UserController.java
index 82ac9fd..9198708 100644
--- a/src/main/java/com/pt5/pthouduan/controller/UserController.java
+++ b/src/main/java/com/pt5/pthouduan/controller/UserController.java
@@ -23,8 +23,8 @@
private UserService userService;
@PostMapping("/register")
- public Map<String, Object> register(@RequestBody User user,@RequestParam String code) {
- return userService.register(user,code);
+ public Map<String, Object> register(@RequestBody User user,@RequestParam String code,@RequestParam String emailcode) {//code是邀请码,emailcode是验证码
+ return userService.register(user,code,emailcode);
}
@PostMapping("/login")
@@ -46,7 +46,7 @@
@PostMapping("/changeimage")
public Map<String, Object> changeimage(@RequestParam String username,
- @RequestParam String image) {
+ @RequestParam String image) {
return userService.changeImage(username,image);
}
@@ -57,8 +57,24 @@
return userService.changePassword(username,oldpassword,newpassword);
}
- @GetMapping("/info")
- public Map<String, Object> getUserInfo(@RequestParam(required = false) String username) {
- return userService.login(username, "");
+ @PostMapping("/sendCode")
+ public Map<String, Object> sendCode(@RequestParam String email) {
+ return userService.sendCode(email);
}
+
+ @GetMapping("/Info")
+ public Map<String, Object> getuser(@RequestParam String username) {
+ return userService.UserInfo(username);
+ }
+
+ @PostMapping("/creatUser")
+ public Map<String, Object> creatUser(@RequestBody User user) {
+ return userService.CreateUser(user);
+ }
+
+ @PostMapping("/DeleteUser")
+ public Map<String, Object> DeleteUser(@RequestParam String username) {
+ return userService.DeleteUser(username);
+ }
+
}
diff --git a/src/main/java/com/pt5/pthouduan/entity/Shop.java b/src/main/java/com/pt5/pthouduan/entity/Shop.java
new file mode 100644
index 0000000..3340ee6
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/entity/Shop.java
@@ -0,0 +1,64 @@
+package com.pt5.pthouduan.entity;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+
+import java.io.Serializable;
+//商城订单类 注意这个表没建哈 暂时没什么用 以后如果用到也可以再建
+@TableName("Shop")
+public class Shop implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ @TableId("ShoppingId")
+ private Long ShoppingId;//订单id
+
+ private String buyername;//购买人用户名
+
+ private int type;//订单类型 1表示外观类订单 2表示上传量类订单 3表示邀请码类订单
+
+ private int info;//表示的是订单信息 如果是外观订单它就是外观编号,如果是邀请码订单就是邀请码,如果是上传量订单就是上传量值
+
+
+ public Long getShoppingId() {
+ return ShoppingId;
+ }
+
+ public void setShoppingId(Long shoppingId) {
+ ShoppingId = shoppingId;
+ }
+
+ public String getBuyername() {
+ return buyername;
+ }
+
+ public void setBuyername(String buyername) {
+ this.buyername = buyername;
+ }
+
+ public int getType() {
+ return type;
+ }
+
+ public void setType(int type) {
+ this.type = type;
+ }
+
+ @Override
+ public String toString() {
+ return "Shop{" +
+ "ShoppingId=" + ShoppingId +
+ ", buyername='" + buyername + '\'' +
+ ", type=" + type +
+ ", info=" + info +
+ '}';
+ }
+
+ public int getInfo() {
+ return info;
+ }
+
+ public void setInfo(int info) {
+ this.info = info;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/pt5/pthouduan/entity/User.java b/src/main/java/com/pt5/pthouduan/entity/User.java
index b1128be..7fc13e7 100644
--- a/src/main/java/com/pt5/pthouduan/entity/User.java
+++ b/src/main/java/com/pt5/pthouduan/entity/User.java
@@ -8,7 +8,7 @@
/**
* <p>
- *
+ *
* </p>
*
* @author ljx
@@ -48,6 +48,10 @@
private String email;
+ private String decoration;//用户外观
+
+ private String permission;//用户权限 1表示普通用户 0表示管理员
+
// 构造函数
public User(Long id, String name) {
this.userid = id;
@@ -158,25 +162,6 @@
this.privacy = privacy;
}
- @Override
- public String toString() {
- return "User{" +
- "username = " + username +
- ", password = " + password +
- ", userUpload = " + userUpload +
- ", userDownload = " + userDownload +
- ", credit = " + credit +
- ", image = " + image +
- ", sex = " + sex +
- ", userid = " + userid +
- ", gradeId = " + gradeId +
- ", passkey = " + passkey +
- ", ratio = " + ratio +
- ", age = " + age +
- ", privacy = " + privacy + ", email = " + email +
- "}";
- }
-
public String getEmail() {
return email;
}
@@ -184,4 +169,42 @@
public void setEmail(String email) {
this.email = email;
}
+
+ public String getDecoration() {
+ return decoration;
+ }
+
+ public void setDecoration(String decoration) {
+ this.decoration = decoration;
+ }
+
+ public String getPermission() {
+ return permission;
+ }
+
+ public void setPermission(String permission) {
+ this.permission = permission;
+ }
+
+ @Override
+ public String toString() {
+ return "User{" +
+ "username='" + username + '\'' +
+ ", password='" + password + '\'' +
+ ", userUpload=" + userUpload +
+ ", userDownload=" + userDownload +
+ ", credit=" + credit +
+ ", image='" + image + '\'' +
+ ", sex='" + sex + '\'' +
+ ", userid=" + userid +
+ ", gradeId=" + gradeId +
+ ", passkey='" + passkey + '\'' +
+ ", ratio=" + ratio +
+ ", age=" + age +
+ ", privacy=" + privacy +
+ ", email='" + email + '\'' +
+ ", decoration='" + decoration + '\'' +
+ ", permission='" + permission + '\'' +
+ '}';
+ }
}
diff --git a/src/main/java/com/pt5/pthouduan/mapper/ShopMapper.java b/src/main/java/com/pt5/pthouduan/mapper/ShopMapper.java
new file mode 100644
index 0000000..dc09493
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/mapper/ShopMapper.java
@@ -0,0 +1,17 @@
+package com.pt5.pthouduan.mapper;
+
+import com.pt5.pthouduan.entity.Invites;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.pt5.pthouduan.entity.User;
+import org.apache.ibatis.annotations.*;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ShopMapper extends BaseMapper<Invites> {
+//
+// @Insert("INSERT INTO shop(username, info, type) VALUES(#{username}, #{decoration}, 1)")
+// int firstshop(
+// @Param("username") String username,
+// @Param("decoration") String decoration
+// );
+}
diff --git a/src/main/java/com/pt5/pthouduan/mapper/UserMapper.java b/src/main/java/com/pt5/pthouduan/mapper/UserMapper.java
index 2f96180..ebd150f 100644
--- a/src/main/java/com/pt5/pthouduan/mapper/UserMapper.java
+++ b/src/main/java/com/pt5/pthouduan/mapper/UserMapper.java
@@ -21,6 +21,9 @@
@Insert("INSERT INTO user(username, password, email, passkey) VALUES(#{username}, #{password}, #{email}, #{passkey})")
int insert(User user);
+ @Delete("DELETE FROM user_behavior WHERE user_id = (SELECT user_id FROM User WHERE username = #{username})")
+ int deleteByUsername(@Param("username") String username);
+
@Select("SELECT * FROM User WHERE username = #{username}")
User selectByUsername(String username);
@@ -57,4 +60,7 @@
String getUsernameByPasskey(String passkey);
List<PeerInfo> findSeedersByInfoHash(@Param("infoHash") String infoHash);
+
+ @Select("SELECT passkey FROM user WHERE userid = #{userid}")
+ String selectPasskeyByUserid(Long userid);
}
diff --git a/src/main/java/com/pt5/pthouduan/service/EmailService.java b/src/main/java/com/pt5/pthouduan/service/EmailService.java
new file mode 100644
index 0000000..492ade0
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/EmailService.java
@@ -0,0 +1,75 @@
+package com.pt5.pthouduan.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mail.SimpleMailMessage;
+import org.springframework.mail.javamail.JavaMailSender;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+@Service
+public class EmailService {
+
+ @Autowired
+ private JavaMailSender mailSender;
+
+ // 存储邮箱和验证码的映射(实际项目中应该用Redis等缓存)
+ private final Map<String, String> emailCodeMap = new HashMap<>();
+
+ /**
+ * 发送验证码到指定邮箱
+ * @param email 目标邮箱
+ * @return 是否发送成功
+ */
+ public boolean sendVerificationCode(String email) {
+ // 生成6位随机验证码
+ String code = generateRandomCode(6);
+
+ // 存储验证码(5分钟有效)
+ emailCodeMap.put(email, code);
+
+ try {
+ SimpleMailMessage message = new SimpleMailMessage();
+ message.setFrom("zhutai940@163.com"); // 发件人
+ message.setTo(email); // 收件人
+ message.setSubject("您的注册验证码"); // 主题
+ message.setText("您的验证码是: " + code + ",5分钟内有效。"); // 内容
+
+ mailSender.send(message);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ /**
+ * 验证邮箱和验证码是否匹配
+ * @param email 邮箱
+ * @param code 验证码
+ * @return 是否验证通过
+ */
+ public boolean verifyCode(String email, String code) {
+ String storedCode = emailCodeMap.get(email);
+ return storedCode != null && storedCode.equals(code);
+ }
+
+ /**
+ * 生成随机验证码
+ * @param length 验证码长度
+ * @return 验证码字符串
+ */
+ private String generateRandomCode(int length) {
+ String numbers = "0123456789";
+ Random random = new Random();
+ StringBuilder sb = new StringBuilder();
+
+ for (int i = 0; i < length; i++) {
+ sb.append(numbers.charAt(random.nextInt(numbers.length())));
+ }
+
+ return sb.toString();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/pt5/pthouduan/service/ShopService.java b/src/main/java/com/pt5/pthouduan/service/ShopService.java
new file mode 100644
index 0000000..a85b5ef
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/ShopService.java
@@ -0,0 +1,78 @@
+package com.pt5.pthouduan.service;
+
+import com.pt5.pthouduan.entity.Invites;
+import com.pt5.pthouduan.mapper.InvitesMapper;
+import com.pt5.pthouduan.mapper.ShopMapper;
+import com.pt5.pthouduan.mapper.UserMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class ShopService {
+ @Autowired
+ private InvitesMapper invitesMapper;
+ @Autowired
+ private UserMapper userMapper;
+ //外观类订单
+ public Map<String, Object> buyDecoration(String buyername,String decoration,int price){//decoration指的是外观编号或id
+ Map<String, Object> result = new HashMap<>();
+ //首先查询用户是否拥有足够的保种积分
+ int credit=userMapper.getcreditByUsername(buyername);
+ if(credit<price){
+ result.put("success", false);
+ result.put("message", "余额不足");
+ return result;
+ }
+ //更新用户保种积分
+ userMapper.deductCreditByUsername(buyername,price);
+ //更新用户数据库
+ userMapper.appendUserDecoration(buyername,decoration);
+ //更新订单数据库 To do...
+ result.put("success", true);
+ result.put("message", "外观购买成功");
+ return result;
+ }
+ //上传量类订单
+ public Map<String, Object> buyUpload(String buyername,int price,int upload){
+ Map<String, Object> result = new HashMap<>();
+ //首先查询用户是否拥有足够的保种积分
+ int credit=userMapper.getcreditByUsername(buyername);
+ if(credit<price){
+ result.put("success", false);
+ result.put("message", "余额不足");
+ return result;
+ }
+ //更新用户保种积分
+ userMapper.deductCreditByUsername(buyername,price);
+ //更新用户上传量
+ userMapper.increaseUploadByUsername(buyername,upload);
+ result.put("success", true);
+ result.put("message", "上传量购买成功");
+ return result;
+ }
+ //购买邀请码
+ public Map<String, Object> setbuyername(String buyername,int price){
+ Map<String, Object> result = new HashMap<>();
+ //首先查询用户是否拥有足够的保种积分
+ int credit=userMapper.getcreditByUsername(buyername);
+ if(credit<price){
+ result.put("success", false);
+ result.put("message", "余额不足");
+ return result;
+ }
+ //更新用户保种积分
+ userMapper.deductCreditByUsername(buyername,price);
+ Invites invite=invitesMapper.selectFirstEmptyBuyername();
+ invitesMapper.updatebuyer(invite.getCode(),buyername);
+ result.put("success", true);
+ result.put("invitecode", invite.getCode());
+ result.put("message", "邀请码购买成功");
+ return result;
+ }
+
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/pt5/pthouduan/service/UserService.java b/src/main/java/com/pt5/pthouduan/service/UserService.java
index 365580f..59865c2 100644
--- a/src/main/java/com/pt5/pthouduan/service/UserService.java
+++ b/src/main/java/com/pt5/pthouduan/service/UserService.java
@@ -19,8 +19,18 @@
private UserMapper userMapper;
@Autowired
private InvitesMapper invitesMapper;
+ @Autowired
+ private EmailService emailService;
+ //发送验证码
+ public Map<String, Object> sendCode(String email){
+ Map<String, Object> result = new HashMap<>();
+ emailService.sendVerificationCode(email);
+ result.put("success", true);
+ result.put("message", "验证码发送成功");
+ return result;
+ }
// 注册业务
- public Map<String, Object> register(User user,String code) {
+ public Map<String, Object> register(User user,String code,String emailcode) {
Map<String, Object> result = new HashMap<>();
// 参数校验
@@ -61,8 +71,12 @@
result.put("message","邀请码已被使用");
return result;
}
- //邮箱发送验证码验证
- //To do... ..
+ //邮箱验证码验证
+ if (!emailService.verifyCode(user.getEmail(), emailcode)) {
+ result.put("success", false);
+ result.put("message","验证码错误");
+ return result;
+ }
//生成passkey
SecureRandom random = new SecureRandom();
user.setPasskey(String.valueOf(10000000 + random.nextInt(90000000)));
@@ -73,6 +87,7 @@
result.put("message", "注册成功");
return result;
}
+ //
// 登录业务
public Map<String, Object> login(String username, String password) {
@@ -156,4 +171,30 @@
result.put("message", "密码重置成功");
return result;
}
+ //获取用户信息(前端)
+ public Map<String, Object> UserInfo(String username){
+ Map<String, Object> result = new HashMap<>();
+ User user = userMapper.selectByUsername(username);
+ result.put("success", true);
+ result.put("message", "用户信息获取成功");
+ result.put("user", user);
+ return result;
+ }
+ //直接创建用户
+ public Map<String, Object> CreateUser(User user){
+ Map<String, Object> result = new HashMap<>();
+ userMapper.insert(user);
+ result.put("success", true);
+ result.put("message", "用户创建成功");
+ return result;
+ }
+ //直接删除用户
+ public Map<String, Object> DeleteUser(String username){
+ Map<String, Object> result = new HashMap<>();
+ userMapper.deleteByUsername(username);
+ result.put("success", true);
+ result.put("message", "用户删除成功");
+ return result;
+ }
+
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 749faf7..cb8a7cc 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1,14 +1,8 @@
spring.application.name=PT-houduan
spring.datasource.url=jdbc:mysql://localhost:3306/pt?useSSL=false&serverTimezone=Asia/Shanghai
-#spring.datasource.url=jdbc:mysql://host.docker.internal:3306/pt?useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-# application.properties
-#spring.datasource.url=jdbc:mysql://202.205.102.121:3306/1group5?useSSL=false&serverTimezone=Asia/Shanghai
-#spring.datasource.username=team5
-#spring.datasource.password=Team5001#
-#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
hikari.maximum-pool-size = 20
hikari.minimum-idle = 5
hikari.idle-timeout = 30000
@@ -23,13 +17,20 @@
#logging.level.root=DEBUG
mybatis-plus.mapper-locations=classpath:mapper/xml/*.xml
mybatis-plus.type-aliases-package=com.pt5.pthouduan.entity
-
uploadDirectory= ./uploads/files # ????????
torrent-dir= ./uploads/torrents # ????????
# ??????
pt.storage.torrent-path=/var/pt/torrents
pt.storage.temp-path=/var/pt/temp
# application.properties ??
+# ????
+spring.mail.host=smtp.163.com # ?163????
+spring.mail.username=zhutai940@163.com
+spring.mail.password=Cmr2005#
+spring.mail.properties.mail.smtp.auth=true
+spring.mail.properties.mail.smtp.starttls.enable=true
+spring.mail.properties.mail.smtp.starttls.required=true
+
tracker.url=http://localhost:6969/announce