5.1 创建个人中心页面(上)

master
barney 2 years ago
parent 1c0b0fec16
commit 5223e513d6
  1. 32
      backend/src/main/java/com/kob/backend/controller/pojo/Bot.java
  2. 2
      backend/src/main/java/com/kob/backend/controller/service/impl/user/account/RegisterServiceImpl.java
  3. 78
      backend/src/main/java/com/kob/backend/controller/service/impl/user/bot/AddServiceImpl.java
  4. 38
      backend/src/main/java/com/kob/backend/controller/service/impl/user/bot/GetListServiceImpl.java
  5. 54
      backend/src/main/java/com/kob/backend/controller/service/impl/user/bot/RemoveServiceImpl.java
  6. 95
      backend/src/main/java/com/kob/backend/controller/service/impl/user/bot/UpdateServiceImpl.java
  7. 10
      backend/src/main/java/com/kob/backend/controller/service/user/bot/AddService.java
  8. 12
      backend/src/main/java/com/kob/backend/controller/service/user/bot/GetListService.java
  9. 10
      backend/src/main/java/com/kob/backend/controller/service/user/bot/RemoveService.java
  10. 10
      backend/src/main/java/com/kob/backend/controller/service/user/bot/UpdateService.java
  11. 24
      backend/src/main/java/com/kob/backend/controller/user/bot/AddController.java
  12. 24
      backend/src/main/java/com/kob/backend/controller/user/bot/GetListController.java
  13. 24
      backend/src/main/java/com/kob/backend/controller/user/bot/RemoveController.java
  14. 23
      backend/src/main/java/com/kob/backend/controller/user/bot/UpdateController.java
  15. 12
      backend/src/main/java/com/kob/backend/mapper/BotMapper.java
  16. 81
      web/src/views/user/bot/UserBotIndexView.vue

@ -0,0 +1,32 @@
package com.kob.backend.controller.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @author zfp
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bot {
// pojo中对应数据库表中的变量要使用驼峰命名法,不能照抄
@TableId(type = IdType.AUTO)
private Integer id;
private Integer userId;
private String title;
private String description;
private String content;
private Integer rating;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date modifyTime;
}

@ -24,7 +24,7 @@ public class RegisterServiceImpl implements RegisterService {
private PasswordEncoder passwordEncoder;
@Override
public Map<String, String> register(String username, String password, String confirmedPassword) {
final int MAX_USERNAME_LEN = 100;
int MAX_USERNAME_LEN = 100;
Map<String, String> map = new HashMap<>();
if (username == null) {
map.put("error_msg","用户名不能为空!");

@ -0,0 +1,78 @@
package com.kob.backend.controller.service.impl.user.bot;
import com.kob.backend.controller.pojo.Bot;
import com.kob.backend.controller.pojo.User;
import com.kob.backend.controller.service.impl.utils.UserDetailsImpl;
import com.kob.backend.controller.service.user.bot.AddService;
import com.kob.backend.mapper.BotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author zfp
*/
@Service
public class AddServiceImpl implements AddService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> add(Map<String, String> data) {
// 获取当前登录的是哪个用户(三条语句)
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken)
SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
String title = data.get("title");
String description = data.get("description");
String content = data.get("content");
Map<String, String> map = new HashMap<>();
if (title == null || title.length() == 0) {
map.put("error_msg", "标题不能为空!");
return map;
}
int TITLE_MAX_LEN = 100;
if (title.length() > TITLE_MAX_LEN) {
map.put("error_msg", "标题长度不能大于100!");
return map;
}
if (description == null || description.length() == 0) {
description = "这个用户很懒,什么也没留下~";
}
int DESCRIPTION_MAX_LEN = 300;
if (description.length() > DESCRIPTION_MAX_LEN) {
map.put("error_msg", "bot的描述不能大于300!");
return map;
}
if (content == null || content.length() == 0) {
map.put("error_msg", "代码不能为空!");
return map;
}
int CONTENT_MAX_LEN = 10000;
if (content.length() > CONTENT_MAX_LEN) {
map.put("error_msg", "代码长度不能大于10000!");
return map;
}
Date now = new Date();
Bot bot = new Bot(null, user.getId(), title, description, content, 1500, now, now);
botMapper.insert(bot);
map.put("error_msg", "success");
return map;
}
}

@ -0,0 +1,38 @@
package com.kob.backend.controller.service.impl.user.bot;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.controller.pojo.Bot;
import com.kob.backend.controller.pojo.User;
import com.kob.backend.controller.service.impl.utils.UserDetailsImpl;
import com.kob.backend.controller.service.user.bot.GetListService;
import com.kob.backend.mapper.BotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @author zfp
*/
@Service
public class GetListServiceImpl implements GetListService {
@Autowired
private BotMapper botMapper;
@Override
public List<Bot> getList() {
// 每个用户只能查自己的bot,所以先要获取登录的用户信息
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken)
SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
QueryWrapper<Bot> qw = new QueryWrapper<>();
qw.eq("user_id", user.getId()); // 第一个参数要和数据库的列名相同
return botMapper.selectList(qw);
}
}

@ -0,0 +1,54 @@
package com.kob.backend.controller.service.impl.user.bot;
import com.kob.backend.controller.pojo.Bot;
import com.kob.backend.controller.pojo.User;
import com.kob.backend.controller.service.impl.utils.UserDetailsImpl;
import com.kob.backend.controller.service.user.bot.RemoveService;
import com.kob.backend.mapper.BotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @author zfp
*/
@Service
public class RemoveServiceImpl implements RemoveService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> remove(Map<String, String> data) {
// 获取当前登录的用户
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken)
SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
int bot_id = Integer.parseInt(data.get("bot_id"));
Bot bot = botMapper.selectById(bot_id);
Map<String,String> map = new HashMap<>();
if (bot == null) {
map.put("error_msg","bot不存在或已被删除");
return map;
}
if (!bot.getUserId().equals(user.getId())) {
map.put("error_msg","您没有权限删除该bot");
return map;
}
// 删除成功
botMapper.deleteById(bot_id);
map.put("error_msg","success");
return map;
}
}

@ -0,0 +1,95 @@
package com.kob.backend.controller.service.impl.user.bot;
import com.kob.backend.controller.pojo.Bot;
import com.kob.backend.controller.pojo.User;
import com.kob.backend.controller.service.impl.utils.UserDetailsImpl;
import com.kob.backend.controller.service.user.bot.UpdateService;
import com.kob.backend.mapper.BotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author zfp
*/
@Service
public class UpdateServiceImpl implements UpdateService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> update(Map<String, String> data) {
// 获得登录的用户信息
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken)
SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
// 需要知道更新哪个bot,所以要传入bot_id
int bot_id = Integer.parseInt(data.get("bot_id"));
Map<String,String> map = new HashMap<>();
String title = data.get("title");
String description = data.get("description");
String content = data.get("content");
if (title == null || title.length() == 0) {
map.put("error_msg", "标题不能为空!");
return map;
}
int TITLE_MAX_LEN = 100;
if (title.length() > TITLE_MAX_LEN) {
map.put("error_msg", "标题长度不能大于100!");
return map;
}
if (description == null || description.length() == 0) {
description = "这个用户很懒,什么也没留下~";
}
int DESCRIPTION_MAX_LEN = 300;
if (description.length() > DESCRIPTION_MAX_LEN) {
map.put("error_msg", "bot的描述不能大于300!");
return map;
}
if (content == null || content.length() == 0) {
map.put("error_msg", "代码不能为空!");
return map;
}
int CONTENT_MAX_LEN = 10000;
if (content.length() > CONTENT_MAX_LEN) {
map.put("error_msg", "代码长度不能大于10000!");
return map;
}
// 根据用户传入的bot_id查询数据库中是否存在该bot
Bot bot = botMapper.selectById(bot_id);
if (bot == null) {
map.put("error_msg","bot不存在或已被删除");
return map;
}
// 如果该bot不属于发出更新请求的用户,提示无权限
if (!bot.getUserId().equals(user.getId())){
map.put("error_msg","您没有权限更新该bot的信息");
return map;
}
// 如果可以修改该bot
Bot newBot = new Bot(bot.getId(), user.getId(), title,description,content,bot.getRating(),bot.getCreateTime(),new Date());
botMapper.updateById(newBot);
map.put("error_msg","success");
return map;
}
}

@ -0,0 +1,10 @@
package com.kob.backend.controller.service.user.bot;
import java.util.Map;
/**
* @author zfp
*/
public interface AddService {
Map<String,String> add(Map<String,String> data);
}

@ -0,0 +1,12 @@
package com.kob.backend.controller.service.user.bot;
import com.kob.backend.controller.pojo.Bot;
import java.util.List;
/**
* @author zfp
*/
public interface GetListService {
List<Bot> getList();
}

@ -0,0 +1,10 @@
package com.kob.backend.controller.service.user.bot;
import java.util.Map;
/**
* @author zfp
*/
public interface RemoveService {
Map<String,String> remove(Map<String,String> data);
}

@ -0,0 +1,10 @@
package com.kob.backend.controller.service.user.bot;
import java.util.Map;
/**
* @author zfp
*/
public interface UpdateService {
Map<String ,String > update(Map<String,String> data);
}

@ -0,0 +1,24 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.controller.service.user.bot.AddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author zfp
*/
@RestController
public class AddController {
@Autowired
private AddService addService;
@PostMapping("/user/bot/add/")
public Map<String, String> add(@RequestParam Map<String,String> data){
return addService.add(data);
}
}

@ -0,0 +1,24 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.controller.pojo.Bot;
import com.kob.backend.controller.service.user.bot.GetListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author zfp
*/
@RestController
public class GetListController {
@Autowired
private GetListService getListService;
@GetMapping("/user/bot/getlist")
public List<Bot> getList() {
return getListService.getList();
}
}

@ -0,0 +1,24 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.controller.service.user.bot.RemoveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author zfp
*/
@RestController
public class RemoveController {
@Autowired
private RemoveService removeService;
@PostMapping("/user/bot/remove/")
public Map<String, String> remove(@RequestParam Map<String, String> data) {
return removeService.remove(data);
}
}

@ -0,0 +1,23 @@
package com.kob.backend.controller.user.bot;
import com.kob.backend.controller.service.user.bot.UpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author zfp
*/
@RestController
public class UpdateController {
@Autowired
private UpdateService updateService;
@PostMapping("/user/bot/update/")
public Map<String, String> update(@RequestParam Map<String, String> data) {
return updateService.update(data);
}
}

@ -0,0 +1,12 @@
package com.kob.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kob.backend.controller.pojo.Bot;
import org.apache.ibatis.annotations.Mapper;
/**
* @author zfp
*/
@Mapper
public interface BotMapper extends BaseMapper<Bot> {
}

@ -6,11 +6,90 @@
<script>
import ContentField from '../../../components/ContentField.vue'
import $ from "jquery";
import { useStore } from "vuex";
export default {
components: {
ContentField
},
setup() {
const store = useStore();
// bot
// $.ajax({
// url: "http://localhost:3000/user/bot/add/",
// type: "POST",
// data: {
// title: "bot",
// description: "bot",
// content: "bot",
// },
// headers:{
// Authorization: "Bearer " + store.state.user.token,
// },
// success(resp) {
// console.log(resp);
// },
// error(resp) {
// console.log(resp);
// }
// })
// bot
// $.ajax({
// url: "http://localhost:3000/user/bot/remove/",
// type: "POST",
// data: {
// bot_id: 10,
// },
// headers:{
// Authorization: "Bearer " + store.state.user.token,
// },
// success(resp) {
// console.log(resp);
// },
// error(resp) {
// console.log(resp);
// }
// })
// bot
// $.ajax({
// url: "http://localhost:3000/user/bot/update/",
// type: "POST",
// data: {
// bot_id: 2,
// title: "bot()",
// description: "bot()",
// content: "bot()",
// },
// headers:{
// Authorization: "Bearer " + store.state.user.token,
// },
// success(resp) {
// console.log(resp);
// },
// error(resp) {
// console.log(resp);
// }
// })
// bot
$.ajax({
url: "http://localhost:3000/user/bot/getlist/",
type: "GET",
headers:{
Authorization: "Bearer " + store.state.user.token,
},
success(resp) {
console.log(resp);
},
error(resp) {
console.log(resp);
}
})
}
}
</script>

Loading…
Cancel
Save