Feat: [后台]:修改登录登录注册接口细节

This commit is contained in:
fanxb 2019-07-10 17:10:40 +08:00
parent 27dd0e80fa
commit e2d80cb2cf
11 changed files with 231 additions and 28 deletions

View File

@ -2,6 +2,7 @@ package com.fanxb.bookmark.business.bookmark.controller;
import com.fanxb.bookmark.business.bookmark.service.BookmarkService; import com.fanxb.bookmark.business.bookmark.service.BookmarkService;
import com.fanxb.bookmark.common.entity.Result; import com.fanxb.bookmark.common.entity.Result;
import com.fanxb.bookmark.common.util.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -28,8 +29,9 @@ public class BookmarkController {
* @date 2019/7/9 14:20 * @date 2019/7/9 14:20
*/ */
@GetMapping("/currentUser") @GetMapping("/currentUser")
public Result getUserBookmarkTree() { public Result getCurrentUserBookmarkTree() {
return null; int userId = UserContextHolder.get().getUserId();
return Result.success(bookmarkService.getOneBookmarkTree(userId));
} }
/** /**
@ -43,7 +45,7 @@ public class BookmarkController {
*/ */
@PutMapping("/uploadBookmarkFile") @PutMapping("/uploadBookmarkFile")
public Result uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) throws Exception { public Result uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) throws Exception {
bookmarkService.parseBookmarkFile(file.getInputStream(), path); bookmarkService.parseBookmarkFile(UserContextHolder.get().getUserId(), file.getInputStream(), path);
return Result.success(null); return Result.success(null);
} }

View File

@ -4,6 +4,8 @@ import com.fanxb.bookmark.common.entity.Bookmark;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
/** /**
* 类功能简述 * 类功能简述
* 类功能详述 * 类功能详述
@ -46,4 +48,14 @@ public interface BookmarkDao {
* @date 2019/7/8 17:35 * @date 2019/7/8 17:35
*/ */
Integer selectMaxSort(@Param("userId") int userId, @Param("path") String path); Integer selectMaxSort(@Param("userId") int userId, @Param("path") String path);
/**
* Description: 根据用户id获取其所有数据
*
* @param userId userid
* @return java.util.List<com.fanxb.bookmark.common.entity.Bookmark>
* @author fanxb
* @date 2019/7/9 18:55
*/
List<Bookmark> getListByUserId(int userId);
} }

View File

@ -11,6 +11,10 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* 类功能简述 * 类功能简述
@ -30,8 +34,16 @@ public class BookmarkService {
@Autowired @Autowired
private BookmarkDao bookmarkDao; private BookmarkDao bookmarkDao;
/**
* Description: 解析书签文件
*
* @param stream 输入流
* @param path 存放路径
* @author fanxb
* @date 2019/7/9 18:44
*/
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void parseBookmarkFile(InputStream stream, String path) throws Exception { public void parseBookmarkFile(int userId, InputStream stream, String path) throws Exception {
Document doc = Jsoup.parse(stream, "utf-8", ""); Document doc = Jsoup.parse(stream, "utf-8", "");
Elements elements = doc.select("html>body>dl>dt"); Elements elements = doc.select("html>body>dl>dt");
//获取当前层sort最大值 //获取当前层sort最大值
@ -45,14 +57,43 @@ public class BookmarkService {
Elements firstChildren = elements.get(0).child(1).children(); Elements firstChildren = elements.get(0).child(1).children();
count = firstChildren.size(); count = firstChildren.size();
for (int j = 0; j < count; j++) { for (int j = 0; j < count; j++) {
dealBookmark(firstChildren.get(j), path, sortBase + j); dealBookmark(userId, firstChildren.get(j), path, sortBase + j);
} }
} else { } else {
dealBookmark(elements.get(i), path, sortBase + count + i - 1); dealBookmark(userId, elements.get(i), path, sortBase + count + i - 1);
} }
} }
} }
/**
* Description: 获取某个用户的书签树
*
* @param userId 用户id
* @return void
* @author fanxb
* @date 2019/7/9 18:45
*/
public List<Bookmark> getOneBookmarkTree(int userId) {
List<Bookmark> list = bookmarkDao.getListByUserId(userId);
Map<String, List<Bookmark>> map = new HashMap<>(50);
list.forEach(item -> {
map.computeIfAbsent(item.getPath(), k -> new ArrayList<>());
map.get(item.getPath()).add(item);
});
List<Bookmark> res = map.get("");
res.forEach(item -> insertToBookmarkTree(item, map));
return res;
}
private void insertToBookmarkTree(Bookmark node, Map<String, List<Bookmark>> map) {
String path = node.getPath();
String key = path + (path.length() == 0 ? "" : ".") + node.getBookmarkId().toString();
if (map.containsKey(key)) {
node.setChildren(map.get(key));
node.getChildren().forEach(item -> insertToBookmarkTree(item, map));
}
}
/** /**
* Description: 处理html节点解析出文件夹和书签 * Description: 处理html节点解析出文件夹和书签
* *
@ -62,20 +103,20 @@ public class BookmarkService {
* @author fanxb * @author fanxb
* @date 2019/7/8 14:49 * @date 2019/7/8 14:49
*/ */
private void dealBookmark(Element ele, String path, int sort) { private void dealBookmark(int userId, Element ele, String path, int sort) {
if (!DT.equalsIgnoreCase(ele.tagName())) { if (!DT.equalsIgnoreCase(ele.tagName())) {
return; return;
} }
Element first = ele.child(0); Element first = ele.child(0);
if (A.equalsIgnoreCase(first.tagName())) { if (A.equalsIgnoreCase(first.tagName())) {
//说明为链接 //说明为链接
Bookmark node = new Bookmark(1, path, first.ownText(), first.attr("href"), first.attr("icon") Bookmark node = new Bookmark(userId, path, first.ownText(), first.attr("href"), first.attr("icon")
, Long.valueOf(first.attr("add_date")) * 1000, sort); , Long.valueOf(first.attr("add_date")) * 1000, sort);
//存入数据库 //存入数据库
insertOne(node); insertOne(node);
} else { } else {
//说明为文件夹 //说明为文件夹
Bookmark node = new Bookmark(1, path, first.ownText(), Long.valueOf(first.attr("add_date")) * 1000, sort); Bookmark node = new Bookmark(userId, path, first.ownText(), Long.valueOf(first.attr("add_date")) * 1000, sort);
Integer sortBase = 0; Integer sortBase = 0;
if (insertOne(node)) { if (insertOne(node)) {
sortBase = bookmarkDao.selectMaxSort(node.getUserId(), path); sortBase = bookmarkDao.selectMaxSort(node.getUserId(), path);
@ -86,13 +127,13 @@ public class BookmarkService {
String childPath = path.length() == 0 ? node.getBookmarkId().toString() : path + "." + node.getBookmarkId(); String childPath = path.length() == 0 ? node.getBookmarkId().toString() : path + "." + node.getBookmarkId();
Elements children = ele.child(1).children(); Elements children = ele.child(1).children();
for (int i = 0, size = children.size(); i < size; i++) { for (int i = 0, size = children.size(); i < size; i++) {
dealBookmark(children.get(i), childPath, sortBase + i + 1); dealBookmark(userId, children.get(i), childPath, sortBase + i + 1);
} }
} }
} }
/** /**
* Description: * Description: 插入一条书签如果已经存在同名书签将跳过
* *
* @param node node * @param node node
* @return boolean 如果已经存在返回true否则false * @return boolean 如果已经存在返回true否则false

View File

@ -28,5 +28,19 @@
where userId = #{userId} and path = #{path} where userId = #{userId} and path = #{path}
</select> </select>
<select id="getListByUserId" resultType="com.fanxb.bookmark.common.entity.Bookmark">
select
bookmarkId,
path,
type,
name,
url,
icon,
sort
from bookmark
where userId = #{userId}
order by path, sort
</select>
</mapper> </mapper>

View File

@ -22,6 +22,20 @@ public class UserController {
@Autowired @Autowired
private UserService userService; private UserService userService;
/**
* Description: 获取验证码
*
* @param email 邮箱
* @return com.fanxb.bookmark.common.entity.Result
* @author fanxb
* @date 2019/7/5 17:37
*/
@GetMapping("/authCode")
public Result getAuthCode(@Param("email") String email) {
userService.sendAuthCode(email);
return Result.success(null);
}
/** /**
* Description: 注册用户 * Description: 注册用户
* *
@ -50,16 +64,18 @@ public class UserController {
} }
/** /**
* Description: 获取验证 * Description: 重置密
* *
* @param email 邮箱 * @param body 重置密码表单
* @return com.fanxb.bookmark.common.entity.Result * @return com.fanxb.bookmark.common.entity.Result
* @author fanxb * @author fanxb
* @date 2019/7/5 17:37 * @date 2019/7/9 19:57
*/ */
@GetMapping("/authCode") @PostMapping("/resetPassword")
public Result getAuthCode(@Param("email") String email) { public Result resetPassword(@RequestBody RegisterBody body) {
userService.sendAuthCode(email); userService.resetPassword(body);
return Result.success(null); return Result.success(null);
} }
} }

View File

@ -26,13 +26,13 @@ public interface UserDao {
/** /**
* Description: 通过用户名或者email获取用户信息 * Description: 通过用户名或者email获取用户信息
* *
* @param name username * @param name username
* @param email email * @param email email
* @return com.fanxb.bookmark.common.entity.User * @return com.fanxb.bookmark.common.entity.User
* @author fanxb * @author fanxb
* @date 2019/7/6 16:45 * @date 2019/7/6 16:45
*/ */
User selectByUsernameOrEmail(@Param("name") String name,@Param("email") String email); User selectByUsernameOrEmail(@Param("name") String name, @Param("email") String email);
/** /**
* Description: 更新用户上次登录时间 * Description: 更新用户上次登录时间
@ -43,4 +43,14 @@ public interface UserDao {
* @date 2019/7/6 16:46 * @date 2019/7/6 16:46
*/ */
void updateLastLoginTime(@Param("time") long time, @Param("userId") int userId); void updateLastLoginTime(@Param("time") long time, @Param("userId") int userId);
/**
* Description: 更新一个参数
*
* @param password 新密码
* @param email 邮箱
* @author fanxb
* @date 2019/7/9 20:03
*/
void resetPassword(@Param("password") String password,@Param("email") String email);
} }

View File

@ -34,7 +34,7 @@ public class UserService {
/** /**
* 长期jwt失效时间 * 长期jwt失效时间
*/ */
private static final long LONG_EXPIRE_TIME = 30 * TimeUtil.DAY_MS; private static final long LONG_EXPIRE_TIME = 30L * TimeUtil.DAY_MS;
@Autowired @Autowired
private UserDao userDao; private UserDao userDao;
@ -122,4 +122,26 @@ public class UserService {
userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId()); userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId());
return res; return res;
} }
/**
* Description: 重置密码
*
* @param body 重置密码 由于参数和注册差不多所以用同一个表单
* @author fanxb
* @date 2019/7/9 19:59
*/
public void resetPassword(RegisterBody body) {
User user = userDao.selectByUsernameOrEmail(body.getEmail(), body.getEmail());
if (user == null) {
throw new FormDataException("用户不存在");
}
String codeKey = Constant.authCodeKey(body.getEmail());
String realCode = RedisUtil.get(codeKey, String.class);
if (StringUtil.isEmpty(realCode) || (!realCode.equals(body.getAuthCode()))) {
throw new FormDataException("验证码错误");
}
RedisUtil.delete(codeKey);
String newPassword = HashUtil.sha1(HashUtil.md5(body.getPassword()));
userDao.resetPassword(newPassword, body.getEmail());
}
} }

View File

@ -19,12 +19,19 @@
createTime createTime
from user from user
where username = #{name} or email = #{email} where username = #{name} or email = #{email}
limit 1
</select> </select>
<update id="updateLastLoginTime"> <update id="updateLastLoginTime">
update user update user
set lastLoginTime = #{time} set lastLoginTime = #{time}
where userId= #{userId} where userId = #{userId}
</update>
<update id="resetPassword">
update user
set password = #{password}
where email = #{email}
</update> </update>

View File

@ -2,7 +2,7 @@ package com.fanxb.bookmark.common.entity;
import lombok.Data; import lombok.Data;
import java.util.ArrayList; import java.util.List;
/** /**
* 类功能简述书签树 * 类功能简述书签树
@ -20,16 +20,16 @@ public class Bookmark {
/** /**
* 类型0文件夹1具体的书签 * 类型0文件夹1具体的书签
*/ */
private int type; private Integer type;
private int userId; private Integer userId;
private String path; private String path;
private String name; private String name;
private String url; private String url;
private String icon; private String icon;
private int sort; private Integer sort;
private long addTime; private Long addTime;
private long createTime; private Long createTime;
private ArrayList<Bookmark> children; private List<Bookmark> children;
public Bookmark() { public Bookmark() {
} }

View File

@ -6,6 +6,8 @@ server:
# 不要在最后加/ # 不要在最后加/
context-path: /bookmark/api context-path: /bookmark/api
spring: spring:
jackson:
default-property-inclusion: non_null
servlet: servlet:
# 表单配置 # 表单配置
multipart: multipart:

View File

@ -0,0 +1,77 @@
/*
Navicat Premium Data Transfer
Source Server : 10.82.17.91_3307
Source Server Type : MySQL
Source Server Version : 80016
Source Host : 10.82.17.91:3307
Source Schema : bookmark
Target Server Type : MySQL
Target Server Version : 80016
File Encoding : 65001
Date: 10/07/2019 17:10:57
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for bookmark
-- ----------------------------
DROP TABLE IF EXISTS `bookmark`;
CREATE TABLE `bookmark` (
`bookmarkId` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`userId` int(10) UNSIGNED NOT NULL COMMENT '所属用户id',
`path` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '节点路径,不包含自身',
`type` tinyint(4) NOT NULL COMMENT '节点类别',
`name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`url` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`icon` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`sort` int(10) UNSIGNED NOT NULL,
`addTime` bigint(20) NOT NULL COMMENT '添加书签时间',
`createTime` bigint(20) UNSIGNED NOT NULL,
PRIMARY KEY (`bookmarkId`) USING BTREE,
INDEX `userId_path_index`(`userId`, `path`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 325 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for url
-- ----------------------------
DROP TABLE IF EXISTS `url`;
CREATE TABLE `url` (
`urlId` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`method` enum('GET','POST','PUT','DELETE') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`url` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`type` tinyint(4) NOT NULL DEFAULT 1 COMMENT '0:公共接口1需登陆鉴权',
PRIMARY KEY (`urlId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'url表用于记录url' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of url
-- ----------------------------
INSERT INTO `url` VALUES (1, 'GET', '/bookmark/currentUser', 1);
INSERT INTO `url` VALUES (2, 'PUT', '/bookmark/uploadBookmarkFile', 1);
INSERT INTO `url` VALUES (3, 'PUT', '/user', 0);
INSERT INTO `url` VALUES (4, 'POST', '/user/login', 0);
INSERT INTO `url` VALUES (5, 'GET', '/user/authCode', 0);
INSERT INTO `url` VALUES (6, 'POST', '/user/resetPassword', 0);
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userId` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名20位以内数字字母组合',
`email` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`icon` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`password` char(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '明文6-20位数字密码组合+userId 进行sha1签名',
`createTime` bigint(20) NOT NULL DEFAULT 0,
`lastLoginTime` bigint(20) NOT NULL DEFAULT 0,
PRIMARY KEY (`userId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;