|
|
@@ -0,0 +1,122 @@
|
|
|
+package com.zhentao.controller;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.zhentao.mapper.UserProfilesMapper;
|
|
|
+import com.zhentao.mapper.UsersMapper;
|
|
|
+import com.zhentao.minio.FileStorageServiceImpl;
|
|
|
+import com.zhentao.pojo.UserProfiles;
|
|
|
+import com.zhentao.pojo.Users;
|
|
|
+import com.zhnetao.AliOssUtil;
|
|
|
+import com.zhnetao.AppJwtUtil;
|
|
|
+import com.zhnetao.GreenTextScan;
|
|
|
+import com.zhnetao.Result;
|
|
|
+import freemarker.template.Configuration;
|
|
|
+import freemarker.template.Template;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.StringWriter;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/user")
|
|
|
+public class UserController {
|
|
|
+ @Autowired
|
|
|
+ private UsersMapper usersMapper;
|
|
|
+ @Autowired
|
|
|
+ private UserProfilesMapper userProfilesMapper;
|
|
|
+
|
|
|
+ @PostMapping("/register")
|
|
|
+ public Result register(@RequestBody Users users){
|
|
|
+ if (users.getUsername()==null || users.getUsername().equals("")|| users.getPassword()==null || users.getPassword().equals("")){
|
|
|
+ return Result.error("用户名或密码不能为空");
|
|
|
+ }
|
|
|
+ Users user = usersMapper.selectOne(new QueryWrapper<Users>().eq("username", users.getUsername()));
|
|
|
+ if (user!=null){
|
|
|
+ return Result.error("用户已存在");
|
|
|
+ }
|
|
|
+ //密码盐加MD5加密
|
|
|
+ String substring = UUID.randomUUID().toString().substring(0, 5);
|
|
|
+ users.setSalt(substring);
|
|
|
+ //通过盐加md5的方式对密码记性加密保护
|
|
|
+ String password = DigestUtils.md5DigestAsHex((users.getPassword()+substring).getBytes());
|
|
|
+ users.setPassword(password);
|
|
|
+ users.setStaticUrl("");
|
|
|
+ usersMapper.insert(users);
|
|
|
+ return Result.success("注册成功");
|
|
|
+ }
|
|
|
+ @PostMapping("/login")
|
|
|
+ public Result login(@RequestBody Users users){
|
|
|
+ if (users.getUsername()==null || users.getUsername().equals("")|| users.getPassword()==null || users.getPassword().equals("")){
|
|
|
+ return Result.error("用户名或密码不能为空");
|
|
|
+ }
|
|
|
+ Users user = usersMapper.selectOne(new QueryWrapper<Users>().eq("username", users.getUsername()));
|
|
|
+ if (user==null){
|
|
|
+ return Result.error("用户不存在");
|
|
|
+ }
|
|
|
+ if (!user.getPassword().equals(DigestUtils.md5DigestAsHex((users.getPassword()+user.getSalt()).getBytes()))){
|
|
|
+ return Result.error("密码错误");
|
|
|
+ }
|
|
|
+ //通过jwt根据用户id生成token
|
|
|
+ String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
|
|
|
+ return Result.success(token);
|
|
|
+ }
|
|
|
+
|
|
|
+ //上传头像
|
|
|
+ @PostMapping("/upload")
|
|
|
+ public Result upload(MultipartFile file) throws IOException {
|
|
|
+ String s = AliOssUtil.uploadFile(UUID.randomUUID().toString() + ".png", file.getInputStream());
|
|
|
+ return Result.success(s);
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加用户信息
|
|
|
+ @PostMapping("/addUserInfo")
|
|
|
+ public Result addUserInfo(@RequestBody UserProfiles userProfiles) throws Exception {
|
|
|
+ String s = JSON.toJSONString(userProfiles);
|
|
|
+ GreenTextScan greenTextScan = new GreenTextScan();
|
|
|
+ Map<String, String> map = greenTextScan.textModeration(s);
|
|
|
+ if ("block".equals(map.get("suggestion"))|| "review".equals(map.get("suggestion"))){
|
|
|
+ userProfiles.setStatus(0);
|
|
|
+ userProfilesMapper.insert(userProfiles);
|
|
|
+ return Result.error("用户基本信息内容违规");
|
|
|
+ }else {
|
|
|
+ userProfiles.setStatus(1);
|
|
|
+ userProfilesMapper.insert(userProfiles);
|
|
|
+ buildArticleToMinIO(userProfiles.getUserId(),s);
|
|
|
+ return Result.success("用户基本信息内容正常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private Configuration configuration;
|
|
|
+ public void buildArticleToMinIO(Integer userId, String content) throws Exception {
|
|
|
+ StringWriter out = new StringWriter();
|
|
|
+ Template template = configuration.getTemplate("user.ftl");
|
|
|
+ Map<String,Object> contentDataModel = new HashMap<>();
|
|
|
+ contentDataModel.put("content", content);
|
|
|
+ //数据模型
|
|
|
+ template.process(contentDataModel,out);
|
|
|
+ //4.3 把html文件上传到minio中
|
|
|
+ InputStream in = new ByteArrayInputStream(out.toString().getBytes());
|
|
|
+ FileStorageServiceImpl fileStorageService = new FileStorageServiceImpl();
|
|
|
+ String path = fileStorageService.uploadHtmlFile("", userId + ".html", in);
|
|
|
+ //保存路径到user表
|
|
|
+ Users users = usersMapper.selectById(userId);
|
|
|
+ users.setStaticUrl(path);
|
|
|
+ usersMapper.updateById(users);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|