master
ourEmpire 6 years ago
parent d7eaad1cbf
commit fe8565c2a2

@ -1,10 +1,11 @@
package config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="mail")
@ConfigurationProperties(prefix="spring.mail")
public class MailConfig {
private String encoding;
private String host;

@ -0,0 +1,38 @@
package controller;
import cache.DataSet;
import database.mssql.UsersEntity;
import message.Message;
import message.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ConcurrentHashMap;
@RestController
@RequestMapping("/recommend")
public class AutoRecommendCtl {
@Autowired
private DataSet dataSet;
@GetMapping
public Message getRecommend(@RequestParam String username){
Message message = new Message();
ConcurrentHashMap<String, UsersEntity> userSet = dataSet.getUserMap();
UsersEntity usersEntity = userSet.get(username);
if(usersEntity == null){
message.setMsg(username + "未找到")
.setCode(StateCode.NOT_FOUND)
.setData(null);
}
return message;
}
}

@ -2,41 +2,40 @@ package controller;
import config.HarmonyWordConfig;
import database.mongo.Comment;
import database.mongo.CommentRepo;
import database.mongo.Help;
import database.mongo.HelpRepo;
import message.Message;
import message.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/commits")
@RequestMapping("/help/commits")
@RestController
public class CommentCtl {
@Autowired
private HarmonyWordConfig words;
@Autowired
private CommentRepo commentRepo;
@GetMapping
public Message getAllComment(){
Message message = new Message();
return message.setCode(StateCode.OK)
.setMsg("option successfully")
.setData(commentRepo.findAll());
}
private HelpRepo helpRepo;
@PostMapping
public Message postComment(@RequestParam String id,
public Message postComment(@RequestParam String article,
@RequestParam String username,
@RequestParam String content){
@RequestParam String content,
@RequestParam(required = false) String email){
Message message = new Message();
Help help = helpRepo.findByArticle(article);
if(help == null){
return message.setMsg(article+"没有找到")
.setCode(StateCode.NOT_FOUND)
.setData(null);
}
try {
Long time = System.currentTimeMillis();
Comment comment = new Comment(id, username, harmony(content), time);
commentRepo.save(comment);
return message.setCode(StateCode.OK)
.setMsg("option successfully")
.setData(null);
Comment comment = new Comment(username, harmony(content), email, time);
help.getComments().add(comment);
return message.setCode(StateCode.CREATED)
.setMsg("操作成功")
.setData(help);
}catch (Exception e){
return message.setCode(StateCode.INTERNAL_SERVER_ERROR)
.setMsg(e.getMessage())
@ -44,6 +43,30 @@ public class CommentCtl {
}
}
@DeleteMapping
public Message deleteCommit(@RequestParam String article,
@RequestParam String username,
@RequestParam Long time){
Message message = new Message();
Help help = helpRepo.findByArticle(article);
if(help == null){
return message.setMsg(article+"没有找到")
.setCode(StateCode.NOT_FOUND)
.setData(null);
}else{
for(Comment comment: help.getComments()){
if(comment.getUsername().equals(username) && comment.getTime() == time){
help.getComments().remove(comment);
return message.setMsg(username + "的评论:\""+ comment.getContent()+ "\"已删除")
.setCode(StateCode.ACCEPTED)
.setData(null);
}
}
return message.setMsg(username+"的评论未找到")
.setCode(StateCode.NOT_FOUND)
.setData(null);
}
}
//将不良评论和谐掉
public String harmony(String content){

@ -1,5 +1,7 @@
package controller;
import cache.DataSet;
import database.mssql.UsersEntity;
import helper.MailService;
import message.Message;
import message.StateCode;
@ -10,35 +12,25 @@ import java.util.Random;
/**
*
*/
@RequestMapping("/mail")
@RequestMapping("/email")
@RestController
public class EmailCtl {
public EmailCtl(MailService mailService) {
private final MailService mailService;
private final DataSet dataSet;
public EmailCtl(MailService mailService,DataSet dataSet) {
this.mailService = mailService;
this.dataSet = dataSet;
}
private static HashMap<String,Repo> repos = new HashMap<>();
final MailService mailService;
//设置验证码的有效时间 单位 ms
private static final long MAX_LIVE = 1000 * 60 * 5L;
@GetMapping({"/create","/{id}/update"})
public Message sendEmail(@RequestParam String email){
Message message = new Message();
Repo repo;
//查看email用户是否已发送过邮件
if(repos.containsKey(email)){
repo = repos.get(email);
repo.setRepo();
repos.put(email,repo);
}else{
repo = new Repo();
repos.put(email,repo);
}
boolean success = mailService.sendSimpleMail(email,repo.verify);
@PostMapping("/user")
public Message create(@RequestParam String email){
Message message = new Message();
boolean success = mailService.sendVerify(email);
if(success){
return message.setCode(StateCode.OK)
.setMsg("验证码已成功发送")
@ -49,38 +41,23 @@ public class EmailCtl {
.setData(null);
}
//存储验证码,产生时间戳,并记录产生次数
private class Repo{
int verify;
long timeStamp;
int count = 0;
Repo(){
setRepo();
}
void setRepo(){
//随机产生一个六位数的验证码
this.verify = new Random().nextInt(1000000 - 100000) + 100000;
this.timeStamp = System.currentTimeMillis();
this.count++;
@PutMapping("/user")
public Message update(@RequestParam String username){
Message message = new Message();
UsersEntity user = dataSet.getUserMap().get(username);
if(user == null){
return message.setMsg(username + "不存在")
.setCode(StateCode.NOT_FOUND)
.setData(null);
}
@Override
public String toString() {
return "Repo{" +
"verify=" + verify +
", timeStamp=" + timeStamp +
", count=" + count +
'}';
String email = user.getEmail();
if(email == null){
return message.setMsg(username + "未设置邮箱")
.setCode(StateCode.NON_AUTHORITATIVE_INFORMATION)
.setData(null);
}
}
//检验验证码
public boolean check(String email,int verify){
Repo repo = repos.get(email);
long nowTime = System.currentTimeMillis();
if((repo.verify == verify) && (nowTime - repo.timeStamp < MAX_LIVE))
return true;
return false;
message = create(email);
return message;
}

@ -1,13 +1,15 @@
package controller;
import database.mongo.Comment;
import database.mongo.Help;
import database.mongo.HelpRepo;
import message.Message;
import message.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
@Configuration
@RequestMapping("/help")
@ -16,9 +18,62 @@ public class HelpCtl {
private HelpRepo helpRepo;
@GetMapping
public Message view(){
public Message view(@RequestParam(required = false) String article){
Message message = new Message();
if(article != null){
Help help = helpRepo.findByArticle(article);
if(help == null){
message.setMsg(article + "没有找到")
.setCode(StateCode.NOT_FOUND)
.setData(null);
}else{
message.setMsg("操作成功")
.setCode(StateCode.OK)
.setData(help);
}
}else {
ArrayList<Help> helps = helpRepo.findAll();
message.setMsg("操作成功")
.setCode(StateCode.OK)
.setData(helps);
}
return message;
}
@PostMapping
public Message create(@RequestParam String article,
@RequestParam String md,
@RequestParam String html){
Message message = new Message();
Help help = helpRepo.findByArticle(article);
if(help != null){
message.setMsg(article + "已存在")
.setCode(StateCode.FORBIDDEN)
.setData(null);
}else{
help = new Help(article,md,html);
helpRepo.save(help);
message.setMsg("操作成功")
.setCode(StateCode.OK)
.setData(null);
}
return message;
}
@DeleteMapping
public Message delete(@RequestParam String article){
Message message = new Message();
Help help = helpRepo.findByArticle(article);
if(help == null){
message.setMsg(article + "没有找到")
.setCode(StateCode.NOT_FOUND)
.setData(null);
}else{
message.setMsg("操作成功")
.setCode(StateCode.OK)
.setData(null);
}
return message;
}
}

@ -18,7 +18,6 @@ import java.lang.NullPointerException
import javax.servlet.http.HttpServletRequest
@RestController
@CrossOrigin(origins=["*"])
@RequestMapping("/pay")
class PayController {

@ -0,0 +1,16 @@
package server
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
@Controller
@RequestMapping("/pay")
class Pages {
@CrossOrigin(origins = ["*"])
@GetMapping("/alipay/result")
fun result (): String {
return "pay"
}
}

@ -1,6 +1,7 @@
package controller;
import com.alibaba.fastjson.JSON;
import database.mongo.Company;
import database.mongo.CompanyRepo;
import database.mongo.StudyWay;
import database.mongo.StudyWayRepo;
import message.Message;
@ -9,21 +10,55 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("/search")
@RestController
public class SearchCtl {
@Autowired
private StudyWayRepo wayRepo;
@Autowired
private CompanyRepo companyRepo;
@GetMapping
public Message view(){
public Message search(@RequestParam String name){
Message message = new Message();
final StudyWay way = wayRepo.findByName(name);
final ArrayList<Company> companies = companyRepo.findAllByJobName(name);
return message.setMsg("操作成功")
.setCode(StateCode.OK)
.setData(wayRepo.findAll());
.setCode(StateCode.OK)
.setData(new WayAndCompany(way,companies));
}
private class WayAndCompany{
private StudyWay studyWay;
private ArrayList<Company> companies;
public WayAndCompany() {
}
public WayAndCompany(StudyWay studyWay, ArrayList<Company> companies) {
this.studyWay = studyWay;
this.companies = companies;
}
public StudyWay getStudyWay() {
return studyWay;
}
public WayAndCompany setStudyWay(StudyWay studyWay) {
this.studyWay = studyWay;
return this;
}
public ArrayList<Company> getCompanies() {
return companies;
}
public WayAndCompany setCompanies(ArrayList<Company> companies) {
this.companies = companies;
return this;
}
}
}

@ -2,11 +2,14 @@ package controller;
import cache.DataSet;
import database.mssql.UsersEntity;
import helper.MailService;
import helper.UserService;
import message.Message;
import message.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
@ -14,9 +17,10 @@ import java.util.concurrent.ConcurrentHashMap;
@RestController
public class UserCtl {
private final DataSet ds;
@Autowired
public UserCtl(DataSet ds) {
private final UserService userService;
public UserCtl(DataSet ds, UserService userService) {
this.ds = ds;
this.userService = userService;
}
@ -39,13 +43,20 @@ public class UserCtl {
public Message create(@RequestParam String username,
@RequestParam String password,
@RequestParam String email,
@RequestParam(required = false) String phone) {
@RequestParam(required = false) String phone,
@RequestParam Integer verify) {
Message message = new Message();
if(ds.getUserMap().get(username) != null){
return message.setMsg("用户名已存在")
.setCode(StateCode.FORBIDDEN)
.setData(null);
}
boolean success = new MailService().check(email,verify);
if(!success){
return message.setMsg("验证码错误或超时")
.setCode(StateCode.FORBIDDEN)
.setData(null);
}
ds.getUserMap().put(username,new UsersEntity(username,password,email,phone));
return message.setMsg("用户"+username+"创建成功")
.setCode(StateCode.OK)
@ -79,7 +90,7 @@ public class UserCtl {
@RequestParam(required = false) String province,
@RequestParam(required = false) String country,
@RequestParam(required = false) String phone,
@RequestParam(required = false) String tags){
@RequestParam(required = false) ArrayList<String> tags){
Message message = new Message();
final UsersEntity usersEntity = ds.getUserMap().get(username);
if(usersEntity == null){
@ -95,7 +106,7 @@ public class UserCtl {
if(province != null) usersEntity.setProvince(province);
if(country != null) usersEntity.setCountry(country);
if(phone != null) usersEntity.setPhone(phone);
if(tags != null) usersEntity.setTags(tags);
if(tags != null) usersEntity.setTags(userService.joinTags(tags));
return message.setMsg(username+"信息已修改")
.setCode(StateCode.OK)
.setData(usersEntity);
@ -104,7 +115,8 @@ public class UserCtl {
@PutMapping("/password")
public Message updatePassword(@RequestParam String username,
@RequestParam String password){
@RequestParam String password,
@RequestParam Integer verify){
Message message = new Message();
final UsersEntity user = ds.getUserMap().get(username);
if(user == null){
@ -112,22 +124,22 @@ public class UserCtl {
.setCode(StateCode.NOT_FOUND)
.setData(null);
}
MailService mailService = new MailService();
boolean success = mailService.check(user.getEmail(),verify);
if(!success) {
return message.setMsg("验证码错误或超时")
.setCode(StateCode.FORBIDDEN)
.setData(null);
}
user.setPassword(password);
return message.setMsg(username+"密码修改成功")
return message.setMsg(username + "密码修改成功")
.setCode(StateCode.OK)
.setData(null);
}
@GetMapping("/all")
public Message all(){
final Message message = new Message();
ConcurrentHashMap<String,UsersEntity> users = ds.getUserMap();
return message.setMsg("operation successfully")
.setData(users.values())
.setCode(StateCode.OK);
}
}

@ -8,6 +8,7 @@ import message.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ -19,7 +20,7 @@ public class OrderCtl {
private OrderRepo orderRepo;
@GetMapping("/all")
@GetMapping
public Message getOrders(){
Message message = new Message();
List<OrdersEntity> orders = (List<OrdersEntity>) orderRepo.findAll();

@ -21,10 +21,10 @@ public class StudyWayCtl {
public Message get(@RequestParam(required = false) String name) {
final Message message = new Message();
if(name==null) {
List<StudyWay> nodes = wayRepo.findAll();
List<StudyWay> ways = wayRepo.findAll();
message.setCode(StateCode.OK)
.setMsg("option successfully")
.setData(nodes);
.setData(ways);
} else {
StudyWay node = wayRepo.findByName(name);
if(node != null) message.setCode(StateCode.OK)
@ -43,9 +43,6 @@ public class StudyWayCtl {
public Message create(@RequestParam String ways){
System.out.println(ways);
List<StudyWay> list = JSON.parseArray(ways,StudyWay.class);
for(StudyWay item : list) {
System.out.println(item);
}
wayRepo.saveAll(list);
Message message = new Message();
return message.setMsg("存入成功")
@ -67,11 +64,11 @@ public class StudyWayCtl {
final StudyWay way = wayRepo.findByName(name);
if(way != null) {
wayRepo.deleteById(name);
return message.setMsg("option successfully")
return message.setMsg("操作成功")
.setCode(StateCode.OK)
.setData(way);
}
message.setMsg(name + "is not find")
message.setMsg(name + "没有找到")
.setCode(StateCode.NOT_FOUND);
}
@ -80,6 +77,4 @@ public class StudyWayCtl {
}

@ -4,6 +4,7 @@ import cache.DataSet;
import database.mongo.Company;
import database.mongo.CompanyRepo;
import database.mssql.UsersEntity;
import helper.MailService;
import message.Message;
import message.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,6 +20,8 @@ public class UsersCtl {
private DataSet ds;
@Autowired
private CompanyRepo companyRepo;
@Autowired
private MailService mailService;
@GetMapping
@ -44,31 +47,42 @@ public class UsersCtl {
}
try{
return message.setCode(StateCode.OK)
.setMsg("option successfully")
.setData("");
return message.setCode(StateCode.CREATED)
.setMsg("操作成功")
.setData(null);
}catch (Exception e){
return message.setCode(StateCode.INTERNAL_SERVER_ERROR)
.setMsg("option failure")
.setMsg(e.getMessage())
.setData(null);
}
}
@PutMapping
public Message updateUserPassword(@RequestParam String username,
@RequestParam(name = "password") String newPwd){
public Message updateUserPassword(@RequestParam String username){
final Message message = new Message();
final ConcurrentHashMap<String,UsersEntity> users = ds.getUserMap();
final UsersEntity user = users.get(username);
if(user == null){
return message.setCode(StateCode.NOT_FOUND)
.setMsg("the user" + username + "not find")
.setMsg("用户" + username + "没有找到")
.setData(null);
}
String email = user.getEmail();
if(email == null){
return message.setMsg("用户"+username+"未设置邮箱,禁止此操作")
.setCode(StateCode.FORBIDDEN)
.setData(null);
}
Integer password = mailService.sendPassword(email);
if(password == -1){
return message.setMsg("密码邮件发送失败")
.setCode(StateCode.INTERNAL_SERVER_ERROR)
.setData(null);
}
user.setPassword(password.toString());
return message.setCode(StateCode.OK)
.setMsg("option successfully")
.setData("");
.setMsg("操作成功")
.setData(null);
}
@PostMapping("/tags")

@ -4,28 +4,21 @@ import javax.persistence.Id;
public class Comment {
@Id
private String id; //用户的id 或 email
private String username;
private String content;
private String email;
private Long time;//发送评论的时间戳
public Comment() {
}
public Comment(String id, String username, String content, Long time) {
this.id = id;
public Comment(String username, String content, String email,Long time) {
this.username = username;
this.content = content;
this.email = email;
this.time = time;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
@ -43,6 +36,15 @@ public class Comment {
this.content = content;
}
public String getEmail() {
return email;
}
public Comment setEmail(String email) {
this.email = email;
return this;
}
public Long getTime() {
return time;
}

@ -1,11 +0,0 @@
package database.mongo;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
@Repository
public interface CommentRepo extends MongoRepository<Comment,String> {
ArrayList<Comment> findAll();
ArrayList<Comment> findAllById(String id);
}

@ -11,6 +11,15 @@ public class Help {
private String html;
private ArrayList<Comment> comments;
public Help() {
}
public Help(String article, String md, String html) {
this.article = article;
this.md = md;
this.html = html;
}
public String getId() {
return id;
}

@ -6,4 +6,5 @@ import java.util.ArrayList;
public interface HelpRepo extends MongoRepository<Help,String> {
ArrayList<Help> findAll();
Help findByArticle(String article);
}

@ -2,12 +2,14 @@ package helper;
import config.MailConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Component;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -17,16 +19,42 @@ import java.util.Properties;
* subject
* content
*/
@RestController
@Component
public class MailService {
final
MailConfig mc;
@Autowired
private MailConfig mc;
//设置验证码的有效时间 单位 ms
private static final long MAX_LIVE = 1000 * 60 * 5L;
private static ConcurrentHashMap<String,Repo> repos = new ConcurrentHashMap<>();
public MailService(MailConfig mc) {
this.mc = mc;
public synchronized boolean sendVerify(String to){
Repo repo;
//查看email用户是否已发送过邮件
if(repos.containsKey(to)){
repo = repos.get(to);
repo.setRepo();
repos.put(to,repo);
}else{
repo = new Repo();
repos.put(to,repo);
}
boolean success = setMail(to,setVerify(repo.verify),"验证码");
return success;
}
public boolean sendSimpleMail(String to,int verify) {
public synchronized int sendPassword(String to){
int password = new Random().nextInt(100000000 - 10000000) + 10000000;
boolean success = setMail(to,password(password),"新密码");
if(success){
return password;
}
return -1;
}
//自定义信件
public synchronized boolean setMail(String to,String content,String subject) {
try {
Properties props = new Properties();
@ -36,8 +64,8 @@ public class MailService {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(mc.getUsername()));
msg.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(to));
msg.setSubject("",mc.getEncoding());
msg.setContent(setContent(verify),"auth");
msg.setSubject(subject,mc.getEncoding());
msg.setText(content,"UTF-8","html");
Transport transport = session.getTransport();
transport.connect(mc.getUsername(),mc.getPassword());
transport.sendMessage(msg,msg.getAllRecipients());
@ -50,11 +78,42 @@ public class MailService {
}
//设置发送内容
private String setContent(int verify){
private String setVerify(int verify){
return "<html><body>不要告诉任何人!验证码为<b>" + verify
+"</b>欢迎您注册本网站,任何人索取验证码都是诈骗。</body></html>";
+"</b>,欢迎您注册本网站,任何人索取验证码都是诈骗。</body></html>";
}
//管理员修改密码时非给用户的信息
private String password(int password){
//产生一个随机8位数的新密码
return "<html><body>您的新密码是<b>" + password + "</b>,为确保安全,请您在登陆后修改该密码。</body></html>";
}
//存储验证码,产生时间戳,并记录产生次数
private class Repo{
private int verify;
private long timeStamp;
private int count = 0;
public Repo(){
setRepo();
}
void setRepo(){
//随机产生一个六位数的验证码
this.verify = new Random().nextInt(1000000 - 100000) + 100000;
this.timeStamp = System.currentTimeMillis();
count++;
}
}
//检验验证码
public boolean check(String email,int verify){
Repo repo = repos.get(email);
long nowTime = System.currentTimeMillis();
if((repo.verify == verify) && (nowTime - repo.timeStamp < MAX_LIVE)){
repos.remove(email);
return true;
}
return false;
}
}

@ -0,0 +1,17 @@
package helper;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@Component
public class UserService {
public String joinTags(ArrayList<String> tags){
StringBuilder builder = new StringBuilder(tags.get(0));
for(int i = 1;i < tags.size();i++){
builder.append("-").append(tags.indexOf(i));
}
return builder.toString();
}
}

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save