rows) {
30 | this.rows = rows;
31 | }
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/tensquare_common/src/main/java/entity/Result.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | public class Result {
4 | private boolean flag;
5 | private Integer code;
6 | private String message;
7 | private Object data;
8 |
9 | public Result() {
10 | }
11 |
12 | public Result(boolean flag, Integer code, String message) {
13 | this.flag = flag;
14 | this.code = code;
15 | this.message = message;
16 | }
17 |
18 | public Result(boolean flag, Integer code, String message, Object data) {
19 | this.flag = flag;
20 | this.code = code;
21 | this.message = message;
22 | this.data = data;
23 | }
24 |
25 | public boolean isFlag() {
26 | return flag;
27 | }
28 |
29 | public void setFlag(boolean flag) {
30 | this.flag = flag;
31 | }
32 |
33 | public Integer getCode() {
34 | return code;
35 | }
36 |
37 | public void setCode(Integer code) {
38 | this.code = code;
39 | }
40 |
41 | public String getMessage() {
42 | return message;
43 | }
44 |
45 | public void setMessage(String message) {
46 | this.message = message;
47 | }
48 |
49 | public Object getData() {
50 | return data;
51 | }
52 |
53 | public void setData(Object data) {
54 | this.data = data;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/tensquare_common/src/main/java/entity/StatusCode.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | public class StatusCode {
4 | public static final int OK=20000;//成功
5 | public static final int ERROR =20001;//失败
6 | public static final int LOGINERROR =20002;//用户名或密码错误
7 | public static final int ACCESSERROR =20003;//权限不足
8 | public static final int REMOTEERROR =20004;//远程调用失败
9 | public static final int REPERROR =20005;//重复操作
10 | }
11 |
--------------------------------------------------------------------------------
/tensquare_common/src/main/java/util/IdWorker.java:
--------------------------------------------------------------------------------
1 | package util;
2 |
3 | import java.lang.management.ManagementFactory;
4 | import java.net.InetAddress;
5 | import java.net.NetworkInterface;
6 |
7 | /**
8 | * 名称:IdWorker.java
9 | * 描述:分布式自增长ID
10 | *
11 | * Twitter的 Snowflake JAVA实现方案
12 | *
13 | * 核心代码为其IdWorker这个类实现,其原理结构如下,我分别用一个0表示一位,用—分割开部分的作用:
14 | * 1||0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000
15 | * 在上面的字符串中,第一位为未使用(实际上也可作为long的符号位),接下来的41位为毫秒级时间,
16 | * 然后5位datacenter标识位,5位机器ID(并不算标识符,实际是为线程标识),
17 | * 然后12位该毫秒内的当前毫秒内的计数,加起来刚好64位,为一个Long型。
18 | * 这样的好处是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由datacenter和机器ID作区分),
19 | * 并且效率较高,经测试,snowflake每秒能够产生26万ID左右,完全满足需要。
20 | *
21 | * 64位ID (42(毫秒)+5(机器ID)+5(业务编码)+12(重复累加))
22 | *
23 | * @author Polim
24 | */
25 | public class IdWorker {
26 | // 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
27 | private final static long twepoch = 1288834974657L;
28 | // 机器标识位数
29 | private final static long workerIdBits = 5L;
30 | // 数据中心标识位数
31 | private final static long datacenterIdBits = 5L;
32 | // 机器ID最大值
33 | private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
34 | // 数据中心ID最大值
35 | private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
36 | // 毫秒内自增位
37 | private final static long sequenceBits = 12L;
38 | // 机器ID偏左移12位
39 | private final static long workerIdShift = sequenceBits;
40 | // 数据中心ID左移17位
41 | private final static long datacenterIdShift = sequenceBits + workerIdBits;
42 | // 时间毫秒左移22位
43 | private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
44 |
45 | private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
46 | /* 上次生产id时间戳 */
47 | private static long lastTimestamp = -1L;
48 | // 0,并发控制
49 | private long sequence = 0L;
50 |
51 | private final long workerId;
52 | // 数据标识id部分
53 | private final long datacenterId;
54 |
55 | public IdWorker(){
56 | this.datacenterId = getDatacenterId(maxDatacenterId);
57 | this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
58 | }
59 | /**
60 | * @param workerId
61 | * 工作机器ID
62 | * @param datacenterId
63 | * 序列号
64 | */
65 | public IdWorker(long workerId, long datacenterId) {
66 | if (workerId > maxWorkerId || workerId < 0) {
67 | throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
68 | }
69 | if (datacenterId > maxDatacenterId || datacenterId < 0) {
70 | throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
71 | }
72 | this.workerId = workerId;
73 | this.datacenterId = datacenterId;
74 | }
75 | /**
76 | * 获取下一个ID
77 | *
78 | * @return
79 | */
80 | public synchronized long nextId() {
81 | long timestamp = timeGen();
82 | if (timestamp < lastTimestamp) {
83 | throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
84 | }
85 |
86 | if (lastTimestamp == timestamp) {
87 | // 当前毫秒内,则+1
88 | sequence = (sequence + 1) & sequenceMask;
89 | if (sequence == 0) {
90 | // 当前毫秒内计数满了,则等待下一秒
91 | timestamp = tilNextMillis(lastTimestamp);
92 | }
93 | } else {
94 | sequence = 0L;
95 | }
96 | lastTimestamp = timestamp;
97 | // ID偏移组合生成最终的ID,并返回ID
98 | long nextId = ((timestamp - twepoch) << timestampLeftShift)
99 | | (datacenterId << datacenterIdShift)
100 | | (workerId << workerIdShift) | sequence;
101 |
102 | return nextId;
103 | }
104 |
105 | private long tilNextMillis(final long lastTimestamp) {
106 | long timestamp = this.timeGen();
107 | while (timestamp <= lastTimestamp) {
108 | timestamp = this.timeGen();
109 | }
110 | return timestamp;
111 | }
112 |
113 | private long timeGen() {
114 | return System.currentTimeMillis();
115 | }
116 |
117 | /**
118 | *
119 | * 获取 maxWorkerId
120 | *
121 | */
122 | protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
123 | StringBuffer mpid = new StringBuffer();
124 | mpid.append(datacenterId);
125 | String name = ManagementFactory.getRuntimeMXBean().getName();
126 | if (!name.isEmpty()) {
127 | /*
128 | * GET jvmPid
129 | */
130 | mpid.append(name.split("@")[0]);
131 | }
132 | /*
133 | * MAC + PID 的 hashcode 获取16个低位
134 | */
135 | return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
136 | }
137 |
138 | /**
139 | *
140 | * 数据标识id部分
141 | *
142 | */
143 | protected static long getDatacenterId(long maxDatacenterId) {
144 | long id = 0L;
145 | try {
146 | InetAddress ip = InetAddress.getLocalHost();
147 | NetworkInterface network = NetworkInterface.getByInetAddress(ip);
148 | if (network == null) {
149 | id = 1L;
150 | } else {
151 | byte[] mac = network.getHardwareAddress();
152 | id = ((0x000000FF & (long) mac[mac.length - 1])
153 | | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
154 | id = id % (maxDatacenterId + 1);
155 | }
156 | } catch (Exception e) {
157 | System.out.println(" getDatacenterId: " + e.getMessage());
158 | }
159 | return id;
160 | }
161 |
162 |
163 | }
164 |
--------------------------------------------------------------------------------
/tensquare_common/src/main/java/util/JwtUtil.java:
--------------------------------------------------------------------------------
1 | package util;
2 |
3 | import io.jsonwebtoken.Claims;
4 | import io.jsonwebtoken.JwtBuilder;
5 | import io.jsonwebtoken.Jwts;
6 | import io.jsonwebtoken.SignatureAlgorithm;
7 | import org.springframework.boot.context.properties.ConfigurationProperties;
8 | import org.springframework.stereotype.Component;
9 |
10 | import java.util.Date;
11 |
12 | /**
13 | * Created by Administrator on 2018/4/11.
14 | */
15 | @ConfigurationProperties("jwt.config")
16 | public class JwtUtil {
17 |
18 | private String key ;
19 |
20 | private long ttl ;//一个小时
21 |
22 | public String getKey() {
23 | return key;
24 | }
25 |
26 | public void setKey(String key) {
27 | this.key = key;
28 | }
29 |
30 | public long getTtl() {
31 | return ttl;
32 | }
33 |
34 | public void setTtl(long ttl) {
35 | this.ttl = ttl;
36 | }
37 |
38 | /**
39 | * 生成JWT
40 | *
41 | * @param id
42 | * @param subject
43 | * @return
44 | */
45 | public String createJWT(String id, String subject, String roles) {
46 | long nowMillis = System.currentTimeMillis();
47 | Date now = new Date(nowMillis);
48 | JwtBuilder builder = Jwts.builder().setId(id)
49 | .setSubject(subject)
50 | .setIssuedAt(now)
51 | .signWith(SignatureAlgorithm.HS256, key).claim("roles", roles);
52 | if (ttl > 0) {
53 | builder.setExpiration( new Date( nowMillis + ttl));
54 | }
55 | return builder.compact();
56 | }
57 |
58 | /**
59 | * 解析JWT
60 | * @param jwtStr
61 | * @return
62 | */
63 | public Claims parseJWT(String jwtStr){
64 | return Jwts.parser()
65 | .setSigningKey(key)
66 | .parseClaimsJws(jwtStr)
67 | .getBody();
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/tensquare_common/src/test/java/com/tensquare/jwt/CreateJwt.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.jwt;
2 |
3 | import io.jsonwebtoken.JwtBuilder;
4 | import io.jsonwebtoken.Jwts;
5 | import io.jsonwebtoken.SignatureAlgorithm;
6 |
7 | import java.util.Date;
8 |
9 | public class CreateJwt {
10 | public static void main(String[] args) {
11 | JwtBuilder jwtBuilder = Jwts.builder()
12 | .setId("666")
13 | .setSubject("Jay")
14 | .setIssuedAt(new Date())
15 | .signWith(SignatureAlgorithm.HS256,"itcast")
16 | .setExpiration(new Date(new Date().getTime()+60000))
17 | .claim("role","admin");
18 | System.out.println(jwtBuilder.compact());
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/tensquare_common/src/test/java/com/tensquare/jwt/ParseJwtTest.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.jwt;
2 |
3 |
4 | import io.jsonwebtoken.Claims;
5 | import io.jsonwebtoken.Jwts;
6 |
7 | import java.text.SimpleDateFormat;
8 |
9 | public class ParseJwtTest {
10 | public static void main(String[] args) {
11 | Claims claims = Jwts.parser().setSigningKey("itcast")
12 | .parseClaimsJws("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NjYiLCJzdWIiOiJKYXkiLCJpYXQiOjE1ODc3MTE1MTEsImV4cCI6MTU4NzcxMTU3MSwicm9sZSI6ImFkbWluIn0.iJ_ccXoYBnJvLDeVfhfpIw3YzJl59gd6m6S0R8YduiA")
13 | .getBody();
14 | System.out.println("用户id:"+claims.getId());
15 | System.out.println("用户名:"+claims.getSubject());
16 | System.out.println("登录时间:"+new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(claims.getIssuedAt()));
17 | System.out.println("过期时间:"+new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(claims.getExpiration()));
18 | System.out.println("用户角色:"+claims.get("role"));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/tensquare_eureka/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_eureka
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter
18 | 2.1.1.RELEASE
19 |
20 |
21 | org.springframework.cloud
22 | spring-cloud-starter-netflix-eureka-server
23 | 2.1.1.RELEASE
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-test
28 | test
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/tensquare_eureka/src/main/java/com/tensquare/eureka/EurekaServer.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.eureka;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6 |
7 |
8 | @SpringBootApplication
9 | @EnableEurekaServer
10 | public class EurekaServer {
11 | public static void main(String[] args) {
12 | SpringApplication.run(EurekaServer.class,args);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/tensquare_eureka/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 6868
3 | eureka:
4 | client:
5 | register-with-eureka: false
6 | fetch-registry: false
7 | service-url:
8 | defaultZone: http://127.0.0.1:${server.port}/eureka/
9 |
--------------------------------------------------------------------------------
/tensquare_friend/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_friend
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-data-jpa
18 |
19 |
20 | com.tensquare
21 | tensquare_common
22 | 1.0-SNAPSHOT
23 |
24 |
25 | mysql
26 | mysql-connector-java
27 |
28 |
29 | org.springframework.cloud
30 | spring-cloud-starter-netflix-eureka-client
31 |
32 |
33 | org.springframework.cloud
34 | spring-cloud-starter-openfeign
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/FriendApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend;
2 |
3 |
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
8 | import org.springframework.cloud.openfeign.EnableFeignClients;
9 | import org.springframework.context.annotation.Bean;
10 | import util.JwtUtil;
11 |
12 | @SpringBootApplication
13 | @EnableEurekaClient
14 | @EnableDiscoveryClient
15 | @EnableFeignClients
16 | public class FriendApplication {
17 |
18 | public static void main(String[] args) {
19 | SpringApplication.run(FriendApplication.class,args);
20 | }
21 |
22 | @Bean
23 | public JwtUtil jwtUtil(){
24 | return new JwtUtil();
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/client/UserClient.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.client;
2 |
3 | import org.springframework.cloud.openfeign.FeignClient;
4 | import org.springframework.stereotype.Component;
5 | import org.springframework.web.bind.annotation.PathVariable;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RequestMethod;
8 |
9 | @Component
10 | @FeignClient("tensquare-user")
11 | public interface UserClient {
12 |
13 | @RequestMapping(value="/user/{userid}/{friendid}/{x}",method = RequestMethod.PUT)
14 | public void updatefanscountandfollowcount(@PathVariable("userid") String userid, @PathVariable("friendid") String friendid, @PathVariable("x") int x);
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/config/InterceptorConfig.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.config;
2 |
3 |
4 | import com.tensquare.friend.interceptor.JwtInterceptor;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
9 |
10 | @Configuration
11 | public class InterceptorConfig extends WebMvcConfigurationSupport {
12 |
13 | @Autowired
14 | private JwtInterceptor jwtInterceptor;
15 | protected void addInterceptors(InterceptorRegistry registry){
16 | //注册拦截器,要声明拦截器对象和要拦截的请求
17 | registry.addInterceptor(jwtInterceptor)
18 | .addPathPatterns("/**")
19 | .excludePathPatterns("/**/login/**");
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/controller/FriendController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.controller;
2 |
3 | import com.tensquare.friend.client.UserClient;
4 | import com.tensquare.friend.service.FriendService;
5 | import entity.Result;
6 | import entity.StatusCode;
7 | import io.jsonwebtoken.Claims;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.RequestMethod;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import javax.servlet.http.HttpServletRequest;
15 |
16 | @RestController
17 | @RequestMapping("/friend")
18 | public class FriendController {
19 |
20 | @Autowired
21 | private HttpServletRequest request;
22 |
23 | @Autowired
24 | private FriendService friendService;
25 |
26 | @Autowired
27 | private UserClient userClient;
28 |
29 | /**
30 | * 添加好友或者非好友
31 | * @return
32 | */
33 | @RequestMapping(value="/like/{friendid}/{type}",method = RequestMethod.PUT)
34 | public Result addFriend(@PathVariable String friendid,@PathVariable String type){
35 | //验证是否登录,并且拿到当前登录的用户id
36 | Claims claims =(Claims) request.getAttribute("claims_user");
37 | if(claims == null ){
38 | //说明当前用户没有user角色
39 | return new Result(false,StatusCode.LOGINERROR,"权限不足");
40 | }
41 |
42 | //得到当前登录的用户id
43 | String userid = claims.getId();
44 | //判断是添加好友还是非好友
45 | if(type!=null){
46 | if(type.equals("1")){
47 | //添加好友
48 | int flag = friendService.addFriend(userid,friendid);
49 | if(flag==0){
50 | return new Result(false, StatusCode.ERROR,"不能重复添加好友");
51 | }
52 | if(flag==1){
53 | userClient.updatefanscountandfollowcount(userid,friendid,1);
54 | return new Result(true,StatusCode.OK,"添加成功");
55 | }
56 |
57 | }else if(type.equals("2")) {
58 | //添加非好友
59 | int flag = friendService.addNoFriend(userid,friendid);
60 |
61 | if(flag==0){
62 | return new Result(false, StatusCode.ERROR,"不能重复添加非好友");
63 | }
64 | if(flag==1){
65 | return new Result(true,StatusCode.OK,"添加成功");
66 | }
67 |
68 | }
69 | return new Result(false, StatusCode.ERROR,"参数异常");
70 |
71 | }else{
72 | return new Result(false, StatusCode.ERROR,"参数异常");
73 | }
74 | }
75 |
76 | @RequestMapping(value="/{friendid}",method=RequestMethod.DELETE)
77 | public Result deleteFriend(@PathVariable String friendid){
78 | //验证是否登录,并且拿到当前登录的用户id
79 | Claims claims =(Claims) request.getAttribute("claims_user");
80 | if(claims == null ){
81 | //说明当前用户没有user角色
82 | return new Result(false,StatusCode.LOGINERROR,"权限不足");
83 | }
84 | //得到当前登录的用户id
85 | String userid = claims.getId();
86 | friendService.deleteFriend(userid,friendid);
87 | userClient.updatefanscountandfollowcount(userid,friendid,-1);
88 | return new Result(true,StatusCode.OK,"删除成功");
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/dao/FriendDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.dao;
2 |
3 | import com.tensquare.friend.pojo.Friend;
4 | import org.springframework.data.jpa.repository.JpaRepository;
5 | import org.springframework.data.jpa.repository.Modifying;
6 | import org.springframework.data.jpa.repository.Query;
7 |
8 | public interface FriendDao extends JpaRepository {
9 |
10 | public Friend findByUseridAndFriendid(String userid, String friendid);
11 |
12 | @Modifying
13 | @Query(value = "UPDATE tb_friend SET islike = ? WHERE userid = ? AND friendid = ?", nativeQuery = true)
14 | public void updateIslike(String islike, String userid, String friendid);
15 |
16 |
17 | @Modifying
18 | @Query(value="DELETE FROM tb_friend WHERE userid = ? AND friendid = ?",nativeQuery = true)
19 | public void deletefriend(String userid, String friendid);
20 | }
21 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/dao/NoFriendDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.dao;
2 |
3 |
4 | import com.tensquare.friend.pojo.NoFriend;
5 | import org.springframework.data.jpa.repository.JpaRepository;
6 |
7 |
8 |
9 | public interface NoFriendDao extends JpaRepository {
10 |
11 | public NoFriend findByUseridAndFriendid(String userid,String friendid);
12 |
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/interceptor/JwtInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.interceptor;
2 |
3 |
4 | import io.jsonwebtoken.Claims;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Component;
7 | import org.springframework.web.servlet.HandlerInterceptor;
8 | import util.JwtUtil;
9 |
10 | import javax.servlet.http.HttpServletRequest;
11 | import javax.servlet.http.HttpServletResponse;
12 |
13 | @Component
14 | public class JwtInterceptor implements HandlerInterceptor {
15 | @Autowired
16 | private JwtUtil jwtUtil;
17 |
18 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
19 | System.out.println("经过了拦截器");
20 | //无论如何都放行,具体能不能操作还是在具体的操作中去判断
21 | //拦截器只是负责把头请求中包含token的令牌进行一个解析验证
22 |
23 | String header = request.getHeader("Authorization");
24 |
25 | if(header!=null && !"".equals(header)){
26 | //如果有包含Authorization头信息,就对其进行解析
27 | if(header.startsWith("Bearer")){
28 | //得到token
29 | String token = header.substring(7);
30 | //验证令牌
31 | try{
32 | Claims claims = jwtUtil.parseJWT(token);
33 | String roles = (String) claims.get("roles");
34 | if(roles!=null && roles.equals("admin")){
35 | request.setAttribute("claims_admin",claims);
36 | }
37 | if(roles!=null && roles.equals("user")){
38 | request.setAttribute("claims_user",claims);
39 | }
40 | }catch(Exception e){
41 | throw new RuntimeException("令牌不正确!");
42 | }
43 | }
44 | }
45 |
46 | return true;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/pojo/Friend.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.pojo;
2 |
3 |
4 | import javax.persistence.Entity;
5 | import javax.persistence.Id;
6 | import javax.persistence.IdClass;
7 | import javax.persistence.Table;
8 | import java.io.Serializable;
9 |
10 | @Entity
11 | @Table(name="tb_friend")
12 | @IdClass(Friend.class)
13 | public class Friend implements Serializable {
14 |
15 | @Id
16 | private String userid;
17 | @Id
18 | private String friendid;
19 |
20 | private String islike;
21 |
22 | public String getUserid() {
23 | return userid;
24 | }
25 |
26 | public void setUserid(String userid) {
27 | this.userid = userid;
28 | }
29 |
30 | public String getFriendid() {
31 | return friendid;
32 | }
33 |
34 | public void setFriendid(String friendid) {
35 | this.friendid = friendid;
36 | }
37 |
38 | public String getIslike() {
39 | return islike;
40 | }
41 |
42 | public void setIslike(String islike) {
43 | this.islike = islike;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/pojo/NoFriend.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.pojo;
2 |
3 |
4 | import javax.persistence.Entity;
5 | import javax.persistence.Id;
6 | import javax.persistence.IdClass;
7 | import javax.persistence.Table;
8 | import java.io.Serializable;
9 |
10 | @Entity
11 | @Table(name="tb_nofriend")
12 | @IdClass(NoFriend.class)
13 | public class NoFriend implements Serializable {
14 |
15 | @Id
16 | private String userid;
17 | @Id
18 | private String friendid;
19 |
20 |
21 |
22 | public String getUserid() {
23 | return userid;
24 | }
25 |
26 | public void setUserid(String userid) {
27 | this.userid = userid;
28 | }
29 |
30 | public String getFriendid() {
31 | return friendid;
32 | }
33 |
34 | public void setFriendid(String friendid) {
35 | this.friendid = friendid;
36 | }
37 |
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/java/com/tensquare/friend/service/FriendService.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.friend.service;
2 |
3 | import com.tensquare.friend.dao.FriendDao;
4 | import com.tensquare.friend.dao.NoFriendDao;
5 | import com.tensquare.friend.pojo.Friend;
6 | import com.tensquare.friend.pojo.NoFriend;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Service;
9 | import org.springframework.transaction.annotation.Transactional;
10 |
11 | @Service
12 | @Transactional
13 | public class FriendService {
14 |
15 | @Autowired
16 | private FriendDao friendDao;
17 |
18 | @Autowired
19 | private NoFriendDao noFriendDao;
20 |
21 | public int addFriend(String userid,String friendid){
22 | //先判断userid到friendid是否有数据,有就是重复添加好友,返回0
23 | Friend friend = friendDao.findByUseridAndFriendid(userid, friendid);
24 | if(friend!=null){
25 | return 0;
26 | }
27 | //直接添加好友,让好友表中userid到friendid方向的type为0
28 | friend = new Friend();
29 | friend.setUserid(userid);
30 | friend.setFriendid(friendid);
31 | friend.setIslike("0");
32 | friendDao.save(friend);
33 | //判断从friendid到userid是否有数据,如果有,把双方的状态都改为1
34 | if(friendDao.findByUseridAndFriendid(friendid,userid)!=null){
35 | //把双方的islike都改成1
36 | friendDao.updateIslike("1",userid,friendid);
37 | friendDao.updateIslike("1",friendid,userid);
38 | }
39 | return 1;
40 | }
41 |
42 | public int addNoFriend(String userid,String friendid){
43 | //先判断是否已经是非好友
44 | NoFriend noFriend = noFriendDao.findByUseridAndFriendid(userid, friendid);
45 | if(noFriend!=null){
46 | return 0;
47 | }
48 | noFriend = new NoFriend();
49 | noFriend.setUserid(userid);
50 | noFriend.setFriendid(friendid);
51 | noFriendDao.save(noFriend);
52 | return 1;
53 | }
54 |
55 | public void deleteFriend(String userid,String friendid){
56 | //先删除好友表中userid到fri9endid这条数据
57 | friendDao.deletefriend(userid,friendid);
58 | //更新friendid到userid的islike为0
59 | friendDao.updateIslike("0",friendid,userid);
60 | //非好友表中添加数据
61 | NoFriend nofriend = new NoFriend();
62 | nofriend.setUserid(userid);
63 | nofriend.setFriendid(friendid);
64 | noFriendDao.save(nofriend);
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/tensquare_friend/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9010
3 | spring:
4 | application:
5 | name: tensquare-friend
6 | datasource:
7 | driver-class-name: com.mysql.jdbc.Driver
8 | url: jdbc:mysql://192.168.213.135:3306/tensquare_friend?characterEncoding=utf-8
9 | username: root
10 | password: root
11 | jpa:
12 | database: mysql
13 | show-sql: true
14 | jwt:
15 | config:
16 | key: itcast
17 | eureka:
18 | client:
19 | service-url:
20 | defaultZone: http://127.0.0.1:6868/eureka/
21 | instance:
22 | prefer-ip-address: true
23 |
24 |
25 |
--------------------------------------------------------------------------------
/tensquare_gathering/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | com.tensquare
5 | tensquare_parent52
6 | 1.0-SNAPSHOT
7 |
8 | tensquare_gathering
9 |
10 |
11 | org.springframework.boot
12 | spring-boot-starter-data-jpa
13 |
14 |
15 | mysql
16 | mysql-connector-java
17 |
18 |
19 | com.tensquare
20 | tensquare_common
21 | 1.0-SNAPSHOT
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/tensquare_gathering/src/main/java/com/tensquare/gathering/GatheringApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.gathering;
2 | import org.springframework.boot.SpringApplication;
3 | import org.springframework.boot.autoconfigure.SpringBootApplication;
4 | import org.springframework.cache.annotation.EnableCaching;
5 | import org.springframework.context.annotation.Bean;
6 | import util.IdWorker;
7 | @SpringBootApplication
8 | @EnableCaching
9 | public class GatheringApplication {
10 |
11 | public static void main(String[] args) {
12 | SpringApplication.run(GatheringApplication.class, args);
13 | }
14 |
15 | @Bean
16 | public IdWorker idWorkker(){
17 | return new IdWorker(1, 1);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/tensquare_gathering/src/main/java/com/tensquare/gathering/controller/BaseExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.gathering.controller;
2 | import entity.Result;
3 | import entity.StatusCode;
4 | import org.springframework.web.bind.annotation.ControllerAdvice;
5 | import org.springframework.web.bind.annotation.ExceptionHandler;
6 | import org.springframework.web.bind.annotation.ResponseBody;
7 | import javax.servlet.ServletException;
8 | import javax.servlet.http.HttpServletResponse;
9 | import java.io.IOException;
10 | /**
11 | * 统一异常处理类
12 | */
13 | @ControllerAdvice
14 | public class BaseExceptionHandler {
15 |
16 | @ExceptionHandler(value = Exception.class)
17 | @ResponseBody
18 | public Result error(Exception e){
19 | e.printStackTrace();
20 | return new Result(false, StatusCode.ERROR, "执行出错");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/tensquare_gathering/src/main/java/com/tensquare/gathering/controller/GatheringController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.gathering.controller;
2 | import java.util.List;
3 | import java.util.Map;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.web.bind.annotation.CrossOrigin;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RequestBody;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.RequestMethod;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import com.tensquare.gathering.pojo.Gathering;
15 | import com.tensquare.gathering.service.GatheringService;
16 |
17 | import entity.PageResult;
18 | import entity.Result;
19 | import entity.StatusCode;
20 | /**
21 | * 控制器层
22 | * @author Administrator
23 | *
24 | */
25 | @RestController
26 | @CrossOrigin
27 | @RequestMapping("/gathering")
28 | public class GatheringController {
29 |
30 | @Autowired
31 | private GatheringService gatheringService;
32 |
33 |
34 | /**
35 | * 查询全部数据
36 | * @return
37 | */
38 | @RequestMapping(method= RequestMethod.GET)
39 | public Result findAll(){
40 | return new Result(true,StatusCode.OK,"查询成功",gatheringService.findAll());
41 | }
42 |
43 | /**
44 | * 根据ID查询
45 | * @param id ID
46 | * @return
47 | */
48 | @RequestMapping(value="/{id}",method= RequestMethod.GET)
49 | public Result findById(@PathVariable String id){
50 | return new Result(true,StatusCode.OK,"查询成功",gatheringService.findById(id));
51 | }
52 |
53 |
54 | /**
55 | * 分页+多条件查询
56 | * @param searchMap 查询条件封装
57 | * @param page 页码
58 | * @param size 页大小
59 | * @return 分页结果
60 | */
61 | @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
62 | public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
63 | Page pageList = gatheringService.findSearch(searchMap, page, size);
64 | return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
65 | }
66 |
67 | /**
68 | * 根据条件查询
69 | * @param searchMap
70 | * @return
71 | */
72 | @RequestMapping(value="/search",method = RequestMethod.POST)
73 | public Result findSearch( @RequestBody Map searchMap){
74 | return new Result(true,StatusCode.OK,"查询成功",gatheringService.findSearch(searchMap));
75 | }
76 |
77 | /**
78 | * 增加
79 | * @param gathering
80 | */
81 | @RequestMapping(method=RequestMethod.POST)
82 | public Result add(@RequestBody Gathering gathering ){
83 | gatheringService.add(gathering);
84 | return new Result(true,StatusCode.OK,"增加成功");
85 | }
86 |
87 | /**
88 | * 修改
89 | * @param gathering
90 | */
91 | @RequestMapping(value="/{id}",method= RequestMethod.PUT)
92 | public Result update(@RequestBody Gathering gathering, @PathVariable String id ){
93 | gathering.setId(id);
94 | gatheringService.update(gathering);
95 | return new Result(true,StatusCode.OK,"修改成功");
96 | }
97 |
98 | /**
99 | * 删除
100 | * @param id
101 | */
102 | @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
103 | public Result delete(@PathVariable String id ){
104 | gatheringService.deleteById(id);
105 | return new Result(true,StatusCode.OK,"删除成功");
106 | }
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/tensquare_gathering/src/main/java/com/tensquare/gathering/dao/GatheringDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.gathering.dao;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5 |
6 | import com.tensquare.gathering.pojo.Gathering;
7 | /**
8 | * 数据访问接口
9 | * @author Administrator
10 | *
11 | */
12 | public interface GatheringDao extends JpaRepository,JpaSpecificationExecutor{
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/tensquare_gathering/src/main/java/com/tensquare/gathering/pojo/Gathering.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.gathering.pojo;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.Id;
5 | import javax.persistence.Table;
6 | import java.io.Serializable;
7 | /**
8 | * 实体类
9 | * @author Administrator
10 | *
11 | */
12 | @Entity
13 | @Table(name="tb_gathering")
14 | public class Gathering implements Serializable{
15 |
16 | @Id
17 | private String id;//编号
18 |
19 |
20 |
21 | private String name;//活动名称
22 | private String summary;//大会简介
23 | private String detail;//详细说明
24 | private String sponsor;//主办方
25 | private String image;//活动图片
26 | private java.util.Date starttime;//开始时间
27 | private java.util.Date endtime;//截止时间
28 | private String address;//举办地点
29 | private java.util.Date enrolltime;//报名截止
30 | private String state;//是否可见
31 | private String city;//城市
32 |
33 |
34 | public String getId() {
35 | return id;
36 | }
37 | public void setId(String id) {
38 | this.id = id;
39 | }
40 |
41 | public String getName() {
42 | return name;
43 | }
44 | public void setName(String name) {
45 | this.name = name;
46 | }
47 |
48 | public String getSummary() {
49 | return summary;
50 | }
51 | public void setSummary(String summary) {
52 | this.summary = summary;
53 | }
54 |
55 | public String getDetail() {
56 | return detail;
57 | }
58 | public void setDetail(String detail) {
59 | this.detail = detail;
60 | }
61 |
62 | public String getSponsor() {
63 | return sponsor;
64 | }
65 | public void setSponsor(String sponsor) {
66 | this.sponsor = sponsor;
67 | }
68 |
69 | public String getImage() {
70 | return image;
71 | }
72 | public void setImage(String image) {
73 | this.image = image;
74 | }
75 |
76 | public java.util.Date getStarttime() {
77 | return starttime;
78 | }
79 | public void setStarttime(java.util.Date starttime) {
80 | this.starttime = starttime;
81 | }
82 |
83 | public java.util.Date getEndtime() {
84 | return endtime;
85 | }
86 | public void setEndtime(java.util.Date endtime) {
87 | this.endtime = endtime;
88 | }
89 |
90 | public String getAddress() {
91 | return address;
92 | }
93 | public void setAddress(String address) {
94 | this.address = address;
95 | }
96 |
97 | public java.util.Date getEnrolltime() {
98 | return enrolltime;
99 | }
100 | public void setEnrolltime(java.util.Date enrolltime) {
101 | this.enrolltime = enrolltime;
102 | }
103 |
104 | public String getState() {
105 | return state;
106 | }
107 | public void setState(String state) {
108 | this.state = state;
109 | }
110 |
111 | public String getCity() {
112 | return city;
113 | }
114 | public void setCity(String city) {
115 | this.city = city;
116 | }
117 |
118 |
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/tensquare_gathering/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9005
3 | spring:
4 | application:
5 | name: tensquare-gathering #指定服务名
6 | datasource:
7 | driverClassName: com.mysql.jdbc.Driver
8 | url: jdbc:mysql://192.168.213.135:3306/tensquare_gathering?characterEncoding=UTF8
9 | username: root
10 | password: root
11 | jpa:
12 | database: MySQL
13 | show-sql: true
14 |
--------------------------------------------------------------------------------
/tensquare_manager/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_manager
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter-netflix-eureka-client
18 |
19 |
20 | org.springframework.cloud
21 | spring-cloud-starter-netflix-zuul
22 |
23 |
24 | com.tensquare
25 | tensquare_common
26 | 1.0-SNAPSHOT
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/tensquare_manager/src/main/java/com/tensquare/manager/ManagerApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.manager;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
7 | import org.springframework.context.annotation.Bean;
8 | import util.JwtUtil;
9 |
10 | @SpringBootApplication
11 | @EnableEurekaClient
12 | @EnableZuulProxy
13 | public class ManagerApplication {
14 | public static void main(String[] args) {
15 | SpringApplication.run(ManagerApplication.class,args);
16 | }
17 |
18 | @Bean
19 | public JwtUtil jwtUtil(){
20 | return new JwtUtil();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/tensquare_manager/src/main/java/com/tensquare/manager/filter/ManagerFilter.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.manager.filter;
2 |
3 | import com.netflix.zuul.ZuulFilter;
4 | import com.netflix.zuul.context.RequestContext;
5 | import com.netflix.zuul.exception.ZuulException;
6 | import com.sun.xml.internal.ws.client.ResponseContext;
7 | import io.jsonwebtoken.Claims;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.stereotype.Component;
10 | import util.JwtUtil;
11 |
12 | import javax.servlet.http.HttpServletRequest;
13 |
14 | @Component
15 | public class ManagerFilter extends ZuulFilter {
16 |
17 | @Autowired
18 | private JwtUtil jwtUtil;
19 | /**
20 | * 在请求前pre或者后post执行
21 | * @return
22 | */
23 | @Override
24 | public String filterType() {
25 | return "pre";
26 | }
27 |
28 | /**
29 | * 多个过滤器的执行顺序,数字越小越先执行
30 | * @return
31 | */
32 | @Override
33 | public int filterOrder() {
34 | return 0;
35 | }
36 |
37 | /**
38 | * 当前过滤器是否开启 true表示开启
39 | * @return
40 | */
41 |
42 | @Override
43 | public boolean shouldFilter() {
44 | return true;
45 | }
46 |
47 | /**
48 | * 过滤器内执行的操作 return任何object的值都表示继续执行
49 | * setsendzullResponse(false) 表示不再继续执行
50 | * @return
51 | * @throws ZuulException
52 | */
53 | @Override
54 | public Object run() throws ZuulException {
55 | System.out.println("已经经过后台过滤器!");
56 | RequestContext requestContext = RequestContext.getCurrentContext();
57 | //request域
58 | HttpServletRequest request = requestContext.getRequest();
59 |
60 | if(request.getMethod().equals("OPTIONS")){
61 | return null;
62 | }
63 |
64 | if(request.getRequestURI().indexOf("login")>0){
65 | return null;
66 | }
67 |
68 | //得到头信息
69 | String header = request.getHeader("Authorization");
70 | if(header!=null && !"".equals(header)){
71 | if(header.startsWith("Bearer")){
72 | String token = header.substring(7);
73 | try{
74 | Claims claims = jwtUtil.parseJWT(token);
75 | String roles = (String)claims.get("roles");
76 | if(roles.equals("admin")){
77 | //把头信息转发下去,并且放行
78 | requestContext.addZuulRequestHeader("Authorization",header);
79 | return null;
80 | }
81 | }catch (Exception e){
82 | e.printStackTrace();
83 | requestContext.setSendZuulResponse(false);//终止运行
84 | }
85 | }
86 |
87 | }
88 | requestContext.setSendZuulResponse(false);//终止运行
89 | requestContext.setResponseStatusCode(403);
90 | requestContext.setResponseBody("权限不足");
91 | requestContext.getResponse().setContentType("text/html;charset=utf-8");
92 | return null;
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/tensquare_manager/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9011
3 | spring:
4 | application:
5 | name: tensquare-manager
6 | eureka:
7 | client:
8 | service-url:
9 | defaultZone: http://127.0.0.1:6868/eureka/
10 | instance:
11 | prefer-ip-address: true
12 |
13 | jwt:
14 | config:
15 | key: itcast
16 |
17 | zuul:
18 | routes:
19 | tensquare-base:
20 | path: /base/**
21 | serviceId: tensquare-base
22 | tensquare-user:
23 | path: /user/**
24 | serviceId: tensquare-user
25 | tensquare-qa:
26 | path: /qa/**
27 | serviceId: tensquare-qa
--------------------------------------------------------------------------------
/tensquare_parent52.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/tensquare_qa/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | com.tensquare
5 | tensquare_parent52
6 | 1.0-SNAPSHOT
7 |
8 | tensquare_qa
9 |
10 |
11 | org.springframework.boot
12 | spring-boot-starter-data-jpa
13 |
14 |
15 | mysql
16 | mysql-connector-java
17 |
18 |
19 | com.tensquare
20 | tensquare_common
21 | 1.0-SNAPSHOT
22 |
23 |
24 | org.springframework.cloud
25 | spring-cloud-starter-netflix-eureka-client
26 |
27 |
28 |
29 | org.springframework.cloud
30 | spring-cloud-starter-openfeign
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/QaApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa;
2 | import org.springframework.boot.SpringApplication;
3 | import org.springframework.boot.autoconfigure.SpringBootApplication;
4 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6 | import org.springframework.cloud.openfeign.EnableFeignClients;
7 | import org.springframework.context.annotation.Bean;
8 | import util.IdWorker;
9 | import util.JwtUtil;
10 |
11 | @SpringBootApplication
12 | @EnableEurekaClient
13 |
14 | @EnableDiscoveryClient
15 | @EnableFeignClients
16 | public class QaApplication {
17 |
18 | public static void main(String[] args) {
19 | SpringApplication.run(QaApplication.class, args);
20 | }
21 |
22 | @Bean
23 | public IdWorker idWorkker(){
24 | return new IdWorker(1, 1);
25 | }
26 |
27 | @Bean
28 | public JwtUtil jwtUtil(){
29 | return new JwtUtil();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/client/BaseClient.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.client;
2 |
3 | import com.tensquare.qa.client.impl.BaseClientImpl;
4 | import entity.Result;
5 | import org.springframework.cloud.openfeign.FeignClient;
6 | import org.springframework.stereotype.Component;
7 | import org.springframework.web.bind.annotation.PathVariable;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RequestMethod;
10 |
11 |
12 | @Component
13 | @FeignClient(value="tensquare-base",fallback= BaseClientImpl.class)
14 | public interface BaseClient {
15 |
16 | @RequestMapping(value="/label/{labelId}",method= RequestMethod.GET)
17 | public Result findById(@PathVariable("labelId") String labelId);
18 | }
19 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/client/impl/BaseClientImpl.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.client.impl;
2 |
3 | import com.tensquare.qa.client.BaseClient;
4 | import entity.Result;
5 | import entity.StatusCode;
6 | import org.springframework.stereotype.Component;
7 |
8 | @Component
9 | public class BaseClientImpl implements BaseClient {
10 |
11 | @Override
12 | public Result findById(String labelId) {
13 | return new Result(false, StatusCode.ERROR,"熔断器触发了!");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/config/InterceptorConfig.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.config;
2 |
3 |
4 | import com.tensquare.qa.interceptor.JwtInterceptor;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
9 |
10 | @Configuration
11 | public class InterceptorConfig extends WebMvcConfigurationSupport {
12 |
13 | @Autowired
14 | private JwtInterceptor jwtInterceptor;
15 | protected void addInterceptors(InterceptorRegistry registry){
16 | //注册拦截器,要声明拦截器对象和要拦截的请求
17 | registry.addInterceptor(jwtInterceptor)
18 | .addPathPatterns("/**")
19 | .excludePathPatterns("/**/login/**");
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/controller/BaseExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.controller;
2 | import entity.Result;
3 | import entity.StatusCode;
4 | import org.springframework.web.bind.annotation.ControllerAdvice;
5 | import org.springframework.web.bind.annotation.ExceptionHandler;
6 | import org.springframework.web.bind.annotation.ResponseBody;
7 | import javax.servlet.ServletException;
8 | import javax.servlet.http.HttpServletResponse;
9 | import java.io.IOException;
10 | /**
11 | * 统一异常处理类
12 | */
13 | @ControllerAdvice
14 | public class BaseExceptionHandler {
15 |
16 | @ExceptionHandler(value = Exception.class)
17 | @ResponseBody
18 | public Result error(Exception e){
19 | e.printStackTrace();
20 | return new Result(false, StatusCode.ERROR, "执行出错");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/controller/ProblemController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.controller;
2 | import java.util.List;
3 | import java.util.Map;
4 |
5 | import com.tensquare.qa.client.BaseClient;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.data.domain.Page;
8 | import org.springframework.web.bind.annotation.CrossOrigin;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestBody;
11 | import org.springframework.web.bind.annotation.RequestMapping;
12 | import org.springframework.web.bind.annotation.RequestMethod;
13 | import org.springframework.web.bind.annotation.RestController;
14 |
15 | import com.tensquare.qa.pojo.Problem;
16 | import com.tensquare.qa.service.ProblemService;
17 |
18 | import entity.PageResult;
19 | import entity.Result;
20 | import entity.StatusCode;
21 |
22 | import javax.servlet.http.HttpServletRequest;
23 |
24 | /**
25 | * 控制器层
26 | * @author Administrator
27 | *
28 | */
29 | @RestController
30 | @CrossOrigin
31 | @RequestMapping("/problem")
32 | public class ProblemController {
33 |
34 | @Autowired
35 | private ProblemService problemService;
36 | @Autowired
37 | private HttpServletRequest request;
38 |
39 | @Autowired
40 | private BaseClient baseClient;
41 |
42 | @RequestMapping(value="/label/{labelId}",method=RequestMethod.GET)
43 | public Result findByLabelId(@PathVariable String labelId){
44 | Result result = baseClient.findById(labelId);
45 | return result;
46 | }
47 |
48 | @RequestMapping(value="/newlist/{labelid}/{page}/{size}",method=RequestMethod.GET)
49 | public Result newlist(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
50 | Page pageData = problemService.newlist(labelid,page,size);
51 | return new Result(true,StatusCode.OK,"查询成功",new PageResult(pageData.getTotalElements(),pageData.getContent()));
52 | }
53 |
54 | @RequestMapping(value="/hotlist/{labelid}/{page}/{size}",method=RequestMethod.GET)
55 | public Result hotlist(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
56 | Page pageData = problemService.hotlist(labelid,page,size);
57 | return new Result(true,StatusCode.OK,"查询成功",new PageResult(pageData.getTotalElements(),pageData.getContent()));
58 | }
59 |
60 | @RequestMapping(value="/waitlist/{labelid}/{page}/{size}",method=RequestMethod.GET)
61 | public Result waitlist(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
62 | Page pageData = problemService.waitlist(labelid,page,size);
63 | return new Result(true,StatusCode.OK,"查询成功",new PageResult(pageData.getTotalElements(),pageData.getContent()));
64 | }
65 |
66 |
67 | /**
68 | * 查询全部数据
69 | * @return
70 | */
71 | @RequestMapping(method= RequestMethod.GET)
72 | public Result findAll(){
73 | return new Result(true,StatusCode.OK,"查询成功",problemService.findAll());
74 | }
75 |
76 | /**
77 | * 根据ID查询
78 | * @param id ID
79 | * @return
80 | */
81 | @RequestMapping(value="/{id}",method= RequestMethod.GET)
82 | public Result findById(@PathVariable String id){
83 | return new Result(true,StatusCode.OK,"查询成功",problemService.findById(id));
84 | }
85 |
86 |
87 | /**
88 | * 分页+多条件查询
89 | * @param searchMap 查询条件封装
90 | * @param page 页码
91 | * @param size 页大小
92 | * @return 分页结果
93 | */
94 | @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
95 | public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
96 | Page pageList = problemService.findSearch(searchMap, page, size);
97 | return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
98 | }
99 |
100 | /**
101 | * 根据条件查询
102 | * @param searchMap
103 | * @return
104 | */
105 | @RequestMapping(value="/search",method = RequestMethod.POST)
106 | public Result findSearch( @RequestBody Map searchMap){
107 | return new Result(true,StatusCode.OK,"查询成功",problemService.findSearch(searchMap));
108 | }
109 |
110 | /**
111 | * 增加
112 | * @param problem
113 | */
114 | @RequestMapping(method=RequestMethod.POST)
115 | public Result add(@RequestBody Problem problem ){
116 | String token = (String) request.getAttribute("claims_user");
117 | if(token==null || !"".equals(token)){
118 | return new Result(false,StatusCode.ACCESSERROR,"权限不足");
119 | }
120 |
121 | problemService.add(problem);
122 | return new Result(true,StatusCode.OK,"增加成功");
123 | }
124 |
125 | /**
126 | * 修改
127 | * @param problem
128 | */
129 | @RequestMapping(value="/{id}",method= RequestMethod.PUT)
130 | public Result update(@RequestBody Problem problem, @PathVariable String id ){
131 | problem.setId(id);
132 | problemService.update(problem);
133 | return new Result(true,StatusCode.OK,"修改成功");
134 | }
135 |
136 | /**
137 | * 删除
138 | * @param id
139 | */
140 | @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
141 | public Result delete(@PathVariable String id ){
142 | problemService.deleteById(id);
143 | return new Result(true,StatusCode.OK,"删除成功");
144 | }
145 |
146 | }
147 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/controller/ReplyController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.controller;
2 | import java.util.List;
3 | import java.util.Map;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.web.bind.annotation.CrossOrigin;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RequestBody;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.RequestMethod;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import com.tensquare.qa.pojo.Reply;
15 | import com.tensquare.qa.service.ReplyService;
16 |
17 | import entity.PageResult;
18 | import entity.Result;
19 | import entity.StatusCode;
20 | /**
21 | * 控制器层
22 | * @author Administrator
23 | *
24 | */
25 | @RestController
26 | @CrossOrigin
27 | @RequestMapping("/reply")
28 | public class ReplyController {
29 |
30 | @Autowired
31 | private ReplyService replyService;
32 |
33 |
34 | /**
35 | * 查询全部数据
36 | * @return
37 | */
38 | @RequestMapping(method= RequestMethod.GET)
39 | public Result findAll(){
40 | return new Result(true,StatusCode.OK,"查询成功",replyService.findAll());
41 | }
42 |
43 | /**
44 | * 根据ID查询
45 | * @param id ID
46 | * @return
47 | */
48 | @RequestMapping(value="/{id}",method= RequestMethod.GET)
49 | public Result findById(@PathVariable String id){
50 | return new Result(true,StatusCode.OK,"查询成功",replyService.findById(id));
51 | }
52 |
53 |
54 | /**
55 | * 分页+多条件查询
56 | * @param searchMap 查询条件封装
57 | * @param page 页码
58 | * @param size 页大小
59 | * @return 分页结果
60 | */
61 | @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
62 | public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
63 | Page pageList = replyService.findSearch(searchMap, page, size);
64 | return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
65 | }
66 |
67 | /**
68 | * 根据条件查询
69 | * @param searchMap
70 | * @return
71 | */
72 | @RequestMapping(value="/search",method = RequestMethod.POST)
73 | public Result findSearch( @RequestBody Map searchMap){
74 | return new Result(true,StatusCode.OK,"查询成功",replyService.findSearch(searchMap));
75 | }
76 |
77 | /**
78 | * 增加
79 | * @param reply
80 | */
81 | @RequestMapping(method=RequestMethod.POST)
82 | public Result add(@RequestBody Reply reply ){
83 | replyService.add(reply);
84 | return new Result(true,StatusCode.OK,"增加成功");
85 | }
86 |
87 | /**
88 | * 修改
89 | * @param reply
90 | */
91 | @RequestMapping(value="/{id}",method= RequestMethod.PUT)
92 | public Result update(@RequestBody Reply reply, @PathVariable String id ){
93 | reply.setId(id);
94 | replyService.update(reply);
95 | return new Result(true,StatusCode.OK,"修改成功");
96 | }
97 |
98 | /**
99 | * 删除
100 | * @param id
101 | */
102 | @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
103 | public Result delete(@PathVariable String id ){
104 | replyService.deleteById(id);
105 | return new Result(true,StatusCode.OK,"删除成功");
106 | }
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/dao/ProblemDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.dao;
2 |
3 | import org.springframework.data.domain.Page;
4 | import org.springframework.data.domain.Pageable;
5 | import org.springframework.data.jpa.repository.JpaRepository;
6 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
7 |
8 | import com.tensquare.qa.pojo.Problem;
9 | import org.springframework.data.jpa.repository.Query;
10 |
11 | import java.util.List;
12 |
13 | /**
14 | * 数据访问接口
15 | * @author Administrator
16 | *
17 | */
18 | public interface ProblemDao extends JpaRepository,JpaSpecificationExecutor{
19 |
20 | @Query(value = "SELECT * FROM tb_problem, tb_pl WHERE id = problemid AND labelid = ? ORDER BY replytime DESC", nativeQuery = true)
21 | public Page newlist(String labelid, Pageable pageable);
22 | @Query(value = "SELECT * FROM tb_problem, tb_pl WHERE id = problemid AND labelid = ? ORDER BY reply DESC", nativeQuery = true)
23 | public Page hotlist(String labelid, Pageable pageable);
24 | @Query(value = "SELECT * FROM tb_problem, tb_pl WHERE id = problemid AND labelid = ? AND reply = 0 ORDER BY createtime DESC", nativeQuery = true)
25 | public Page waitlist(String labelid, Pageable pageable);
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/dao/ReplyDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.dao;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5 |
6 | import com.tensquare.qa.pojo.Reply;
7 | /**
8 | * 数据访问接口
9 | * @author Administrator
10 | *
11 | */
12 | public interface ReplyDao extends JpaRepository,JpaSpecificationExecutor{
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/interceptor/JwtInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.interceptor;
2 |
3 |
4 | import io.jsonwebtoken.Claims;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Component;
7 | import org.springframework.web.servlet.HandlerInterceptor;
8 | import util.JwtUtil;
9 |
10 | import javax.servlet.http.HttpServletRequest;
11 | import javax.servlet.http.HttpServletResponse;
12 |
13 | @Component
14 | public class JwtInterceptor implements HandlerInterceptor {
15 | @Autowired
16 | private JwtUtil jwtUtil;
17 |
18 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
19 | System.out.println("经过了拦截器");
20 | //无论如何都放行,具体能不能操作还是在具体的操作中去判断
21 | //拦截器只是负责把头请求中包含token的令牌进行一个解析验证
22 |
23 | String header = request.getHeader("Authorization");
24 |
25 | if(header!=null && !"".equals(header)){
26 | //如果有包含Authorization头信息,就对其进行解析
27 | if(header.startsWith("Bearer")){
28 | //得到token
29 | String token = header.substring(7);
30 | //验证令牌
31 | try{
32 | Claims claims = jwtUtil.parseJWT(token);
33 | String roles = (String) claims.get("roles");
34 | if(roles!=null && roles.equals("admin")){
35 | request.setAttribute("claims_admin",token);
36 | }
37 | if(roles!=null && roles.equals("user")){
38 | request.setAttribute("claims_user",token);
39 | }
40 | }catch(Exception e){
41 | throw new RuntimeException("令牌不正确!");
42 | }
43 | }
44 | }
45 |
46 | return true;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/pojo/Problem.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.pojo;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.Id;
5 | import javax.persistence.Table;
6 | import java.io.Serializable;
7 | /**
8 | * 实体类
9 | * @author Administrator
10 | *
11 | */
12 | @Entity
13 | @Table(name="tb_problem")
14 | public class Problem implements Serializable{
15 |
16 | @Id
17 | private String id;//ID
18 |
19 |
20 |
21 | private String title;//标题
22 | private String content;//内容
23 | private java.util.Date createtime;//创建日期
24 | private java.util.Date updatetime;//修改日期
25 | private String userid;//用户ID
26 | private String nickname;//昵称
27 | private Long visits;//浏览量
28 | private Long thumbup;//点赞数
29 | private Long reply;//回复数
30 | private String solve;//是否解决
31 | private String replyname;//回复人昵称
32 | private java.util.Date replytime;//回复日期
33 |
34 |
35 | public String getId() {
36 | return id;
37 | }
38 | public void setId(String id) {
39 | this.id = id;
40 | }
41 |
42 | public String getTitle() {
43 | return title;
44 | }
45 | public void setTitle(String title) {
46 | this.title = title;
47 | }
48 |
49 | public String getContent() {
50 | return content;
51 | }
52 | public void setContent(String content) {
53 | this.content = content;
54 | }
55 |
56 | public java.util.Date getCreatetime() {
57 | return createtime;
58 | }
59 | public void setCreatetime(java.util.Date createtime) {
60 | this.createtime = createtime;
61 | }
62 |
63 | public java.util.Date getUpdatetime() {
64 | return updatetime;
65 | }
66 | public void setUpdatetime(java.util.Date updatetime) {
67 | this.updatetime = updatetime;
68 | }
69 |
70 | public String getUserid() {
71 | return userid;
72 | }
73 | public void setUserid(String userid) {
74 | this.userid = userid;
75 | }
76 |
77 | public String getNickname() {
78 | return nickname;
79 | }
80 | public void setNickname(String nickname) {
81 | this.nickname = nickname;
82 | }
83 |
84 | public Long getVisits() {
85 | return visits;
86 | }
87 | public void setVisits(Long visits) {
88 | this.visits = visits;
89 | }
90 |
91 | public Long getThumbup() {
92 | return thumbup;
93 | }
94 | public void setThumbup(Long thumbup) {
95 | this.thumbup = thumbup;
96 | }
97 |
98 | public Long getReply() {
99 | return reply;
100 | }
101 | public void setReply(Long reply) {
102 | this.reply = reply;
103 | }
104 |
105 | public String getSolve() {
106 | return solve;
107 | }
108 | public void setSolve(String solve) {
109 | this.solve = solve;
110 | }
111 |
112 | public String getReplyname() {
113 | return replyname;
114 | }
115 | public void setReplyname(String replyname) {
116 | this.replyname = replyname;
117 | }
118 |
119 | public java.util.Date getReplytime() {
120 | return replytime;
121 | }
122 | public void setReplytime(java.util.Date replytime) {
123 | this.replytime = replytime;
124 | }
125 |
126 |
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/pojo/Reply.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.pojo;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.Id;
5 | import javax.persistence.Table;
6 | import java.io.Serializable;
7 | /**
8 | * 实体类
9 | * @author Administrator
10 | *
11 | */
12 | @Entity
13 | @Table(name="tb_reply")
14 | public class Reply implements Serializable{
15 |
16 | @Id
17 | private String id;//编号
18 |
19 |
20 |
21 | private String problemid;//问题ID
22 | private String content;//回答内容
23 | private java.util.Date createtime;//创建日期
24 | private java.util.Date updatetime;//更新日期
25 | private String userid;//回答人ID
26 | private String nickname;//回答人昵称
27 |
28 |
29 | public String getId() {
30 | return id;
31 | }
32 | public void setId(String id) {
33 | this.id = id;
34 | }
35 |
36 | public String getProblemid() {
37 | return problemid;
38 | }
39 | public void setProblemid(String problemid) {
40 | this.problemid = problemid;
41 | }
42 |
43 | public String getContent() {
44 | return content;
45 | }
46 | public void setContent(String content) {
47 | this.content = content;
48 | }
49 |
50 | public java.util.Date getCreatetime() {
51 | return createtime;
52 | }
53 | public void setCreatetime(java.util.Date createtime) {
54 | this.createtime = createtime;
55 | }
56 |
57 | public java.util.Date getUpdatetime() {
58 | return updatetime;
59 | }
60 | public void setUpdatetime(java.util.Date updatetime) {
61 | this.updatetime = updatetime;
62 | }
63 |
64 | public String getUserid() {
65 | return userid;
66 | }
67 | public void setUserid(String userid) {
68 | this.userid = userid;
69 | }
70 |
71 | public String getNickname() {
72 | return nickname;
73 | }
74 | public void setNickname(String nickname) {
75 | this.nickname = nickname;
76 | }
77 |
78 |
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/service/ProblemService.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collection;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import javax.persistence.criteria.CriteriaBuilder;
9 | import javax.persistence.criteria.CriteriaQuery;
10 | import javax.persistence.criteria.Expression;
11 | import javax.persistence.criteria.Predicate;
12 | import javax.persistence.criteria.Root;
13 | import javax.persistence.criteria.Selection;
14 |
15 | import org.springframework.beans.factory.annotation.Autowired;
16 | import org.springframework.data.domain.Page;
17 | import org.springframework.data.domain.PageRequest;
18 | import org.springframework.data.domain.Pageable;
19 | import org.springframework.data.domain.Sort;
20 | import org.springframework.data.jpa.domain.Specification;
21 | import org.springframework.stereotype.Service;
22 |
23 | import util.IdWorker;
24 |
25 | import com.tensquare.qa.dao.ProblemDao;
26 | import com.tensquare.qa.pojo.Problem;
27 |
28 | /**
29 | * 服务层
30 | *
31 | * @author Administrator
32 | *
33 | */
34 | @Service
35 | public class ProblemService {
36 |
37 | @Autowired
38 | private ProblemDao problemDao;
39 |
40 | @Autowired
41 | private IdWorker idWorker;
42 |
43 | public Page newlist(String labelid,int page,int rows){
44 | Pageable pageable = PageRequest.of(page-1,rows);
45 | return problemDao.newlist(labelid, pageable);
46 | }
47 |
48 | public Page hotlist(String labelid,int page,int rows){
49 | Pageable pageable = PageRequest.of(page-1,rows);
50 | return problemDao.hotlist(labelid, pageable);
51 | }
52 |
53 | public Page waitlist(String labelid,int page,int rows){
54 | Pageable pageable = PageRequest.of(page-1,rows);
55 | return problemDao.waitlist(labelid, pageable);
56 | }
57 |
58 |
59 | /**
60 | * 查询全部列表
61 | * @return
62 | */
63 | public List findAll() {
64 | return problemDao.findAll();
65 | }
66 |
67 |
68 | /**
69 | * 条件查询+分页
70 | * @param whereMap
71 | * @param page
72 | * @param size
73 | * @return
74 | */
75 | public Page findSearch(Map whereMap, int page, int size) {
76 | Specification specification = createSpecification(whereMap);
77 | PageRequest pageRequest = PageRequest.of(page-1, size);
78 | return problemDao.findAll(specification, pageRequest);
79 | }
80 |
81 |
82 | /**
83 | * 条件查询
84 | * @param whereMap
85 | * @return
86 | */
87 | public List findSearch(Map whereMap) {
88 | Specification specification = createSpecification(whereMap);
89 | return problemDao.findAll(specification);
90 | }
91 |
92 | /**
93 | * 根据ID查询实体
94 | * @param id
95 | * @return
96 | */
97 | public Problem findById(String id) {
98 | return problemDao.findById(id).get();
99 | }
100 |
101 | /**
102 | * 增加
103 | * @param problem
104 | */
105 | public void add(Problem problem) {
106 | problem.setId( idWorker.nextId()+"" );
107 | problemDao.save(problem);
108 | }
109 |
110 | /**
111 | * 修改
112 | * @param problem
113 | */
114 | public void update(Problem problem) {
115 | problemDao.save(problem);
116 | }
117 |
118 | /**
119 | * 删除
120 | * @param id
121 | */
122 | public void deleteById(String id) {
123 | problemDao.deleteById(id);
124 | }
125 |
126 | /**
127 | * 动态条件构建
128 | * @param searchMap
129 | * @return
130 | */
131 | private Specification createSpecification(Map searchMap) {
132 |
133 | return new Specification() {
134 |
135 | @Override
136 | public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
137 | List predicateList = new ArrayList();
138 | // ID
139 | if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
140 | predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
141 | }
142 | // 标题
143 | if (searchMap.get("title")!=null && !"".equals(searchMap.get("title"))) {
144 | predicateList.add(cb.like(root.get("title").as(String.class), "%"+(String)searchMap.get("title")+"%"));
145 | }
146 | // 内容
147 | if (searchMap.get("content")!=null && !"".equals(searchMap.get("content"))) {
148 | predicateList.add(cb.like(root.get("content").as(String.class), "%"+(String)searchMap.get("content")+"%"));
149 | }
150 | // 用户ID
151 | if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
152 | predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
153 | }
154 | // 昵称
155 | if (searchMap.get("nickname")!=null && !"".equals(searchMap.get("nickname"))) {
156 | predicateList.add(cb.like(root.get("nickname").as(String.class), "%"+(String)searchMap.get("nickname")+"%"));
157 | }
158 | // 是否解决
159 | if (searchMap.get("solve")!=null && !"".equals(searchMap.get("solve"))) {
160 | predicateList.add(cb.like(root.get("solve").as(String.class), "%"+(String)searchMap.get("solve")+"%"));
161 | }
162 | // 回复人昵称
163 | if (searchMap.get("replyname")!=null && !"".equals(searchMap.get("replyname"))) {
164 | predicateList.add(cb.like(root.get("replyname").as(String.class), "%"+(String)searchMap.get("replyname")+"%"));
165 | }
166 |
167 | return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
168 |
169 | }
170 | };
171 |
172 | }
173 |
174 | }
175 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/java/com/tensquare/qa/service/ReplyService.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.qa.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collection;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import javax.persistence.criteria.CriteriaBuilder;
9 | import javax.persistence.criteria.CriteriaQuery;
10 | import javax.persistence.criteria.Expression;
11 | import javax.persistence.criteria.Predicate;
12 | import javax.persistence.criteria.Root;
13 | import javax.persistence.criteria.Selection;
14 |
15 | import org.springframework.beans.factory.annotation.Autowired;
16 | import org.springframework.data.domain.Page;
17 | import org.springframework.data.domain.PageRequest;
18 | import org.springframework.data.domain.Sort;
19 | import org.springframework.data.jpa.domain.Specification;
20 | import org.springframework.stereotype.Service;
21 |
22 | import util.IdWorker;
23 |
24 | import com.tensquare.qa.dao.ReplyDao;
25 | import com.tensquare.qa.pojo.Reply;
26 |
27 | /**
28 | * 服务层
29 | *
30 | * @author Administrator
31 | *
32 | */
33 | @Service
34 | public class ReplyService {
35 |
36 | @Autowired
37 | private ReplyDao replyDao;
38 |
39 | @Autowired
40 | private IdWorker idWorker;
41 |
42 | /**
43 | * 查询全部列表
44 | * @return
45 | */
46 | public List findAll() {
47 | return replyDao.findAll();
48 | }
49 |
50 |
51 | /**
52 | * 条件查询+分页
53 | * @param whereMap
54 | * @param page
55 | * @param size
56 | * @return
57 | */
58 | public Page findSearch(Map whereMap, int page, int size) {
59 | Specification specification = createSpecification(whereMap);
60 | PageRequest pageRequest = PageRequest.of(page-1, size);
61 | return replyDao.findAll(specification, pageRequest);
62 | }
63 |
64 |
65 | /**
66 | * 条件查询
67 | * @param whereMap
68 | * @return
69 | */
70 | public List findSearch(Map whereMap) {
71 | Specification specification = createSpecification(whereMap);
72 | return replyDao.findAll(specification);
73 | }
74 |
75 | /**
76 | * 根据ID查询实体
77 | * @param id
78 | * @return
79 | */
80 | public Reply findById(String id) {
81 | return replyDao.findById(id).get();
82 | }
83 |
84 | /**
85 | * 增加
86 | * @param reply
87 | */
88 | public void add(Reply reply) {
89 | reply.setId( idWorker.nextId()+"" );
90 | replyDao.save(reply);
91 | }
92 |
93 | /**
94 | * 修改
95 | * @param reply
96 | */
97 | public void update(Reply reply) {
98 | replyDao.save(reply);
99 | }
100 |
101 | /**
102 | * 删除
103 | * @param id
104 | */
105 | public void deleteById(String id) {
106 | replyDao.deleteById(id);
107 | }
108 |
109 | /**
110 | * 动态条件构建
111 | * @param searchMap
112 | * @return
113 | */
114 | private Specification createSpecification(Map searchMap) {
115 |
116 | return new Specification() {
117 |
118 | @Override
119 | public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
120 | List predicateList = new ArrayList();
121 | // 编号
122 | if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
123 | predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
124 | }
125 | // 问题ID
126 | if (searchMap.get("problemid")!=null && !"".equals(searchMap.get("problemid"))) {
127 | predicateList.add(cb.like(root.get("problemid").as(String.class), "%"+(String)searchMap.get("problemid")+"%"));
128 | }
129 | // 回答内容
130 | if (searchMap.get("content")!=null && !"".equals(searchMap.get("content"))) {
131 | predicateList.add(cb.like(root.get("content").as(String.class), "%"+(String)searchMap.get("content")+"%"));
132 | }
133 | // 回答人ID
134 | if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
135 | predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
136 | }
137 | // 回答人昵称
138 | if (searchMap.get("nickname")!=null && !"".equals(searchMap.get("nickname"))) {
139 | predicateList.add(cb.like(root.get("nickname").as(String.class), "%"+(String)searchMap.get("nickname")+"%"));
140 | }
141 |
142 | return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
143 |
144 | }
145 | };
146 |
147 | }
148 |
149 | }
150 |
--------------------------------------------------------------------------------
/tensquare_qa/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9002
3 | spring:
4 | application:
5 | name: tensquare-qa #指定服务名
6 | datasource:
7 | driverClassName: com.mysql.jdbc.Driver
8 | url: jdbc:mysql://192.168.213.135:3306/tensquare_qa?characterEncoding=UTF8
9 | username: root
10 | password: root
11 | jpa:
12 | database: MySQL
13 | show-sql: true
14 | jwt:
15 | config:
16 | key: itcast
17 |
18 | eureka:
19 | client:
20 | service-url:
21 | defaultZone: http://127.0.0.1:6868/eureka/
22 | instance:
23 | prefer-ip-address: true
24 |
25 | feign:
26 | hystrix:
27 | enabled: true
28 |
--------------------------------------------------------------------------------
/tensquare_rabbitmqtest/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_rabbitmqtest
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-amqp
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tensquare_rabbitmqtest/src/main/java/com/tensquare/rabbit/RabbitApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.rabbit;
2 |
3 |
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 |
8 | @SpringBootApplication
9 | public class RabbitApplication {
10 | public static void main(String[] args) {
11 | SpringApplication.run(RabbitApplication.class);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/tensquare_rabbitmqtest/src/main/java/com/tensquare/rabbit/customer/Customer1.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.rabbit.customer;
2 |
3 |
4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler;
5 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
6 | import org.springframework.stereotype.Component;
7 |
8 |
9 | @Component
10 | @RabbitListener(queues = "itcast")
11 | public class Customer1 {
12 |
13 | @RabbitHandler
14 | public void getMsg(String msg){
15 | System.out.println("itcast: " +msg);
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tensquare_rabbitmqtest/src/main/java/com/tensquare/rabbit/customer/Customer2.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.rabbit.customer;
2 |
3 |
4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler;
5 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
6 | import org.springframework.stereotype.Component;
7 |
8 |
9 | @Component
10 | @RabbitListener(queues = "hanjay")
11 | public class Customer2 {
12 |
13 | @RabbitHandler
14 | public void getMsg(String msg){
15 | System.out.println("hanjay: " +msg);
16 |
17 | }
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/tensquare_rabbitmqtest/src/main/java/com/tensquare/rabbit/customer/Customer3.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.rabbit.customer;
2 |
3 |
4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler;
5 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
6 | import org.springframework.stereotype.Component;
7 |
8 |
9 | @Component
10 | @RabbitListener(queues = "jay")
11 | public class Customer3 {
12 |
13 | @RabbitHandler
14 | public void getMsg(String msg){
15 | System.out.println("jay: " +msg);
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tensquare_rabbitmqtest/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8001
3 | spring:
4 | rabbitmq:
5 | host: 192.168.213.135
--------------------------------------------------------------------------------
/tensquare_rabbitmqtest/src/test/java/com/tensquare/test/ProductTest.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.test;
2 |
3 |
4 | import com.tensquare.rabbit.RabbitApplication;
5 | import org.junit.Test;
6 | import org.junit.runner.RunWith;
7 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.boot.test.context.SpringBootTest;
10 | import org.springframework.test.context.junit4.SpringRunner;
11 |
12 | @RunWith(SpringRunner.class)
13 | @SpringBootTest(classes = RabbitApplication.class)
14 | public class ProductTest {
15 |
16 | @Autowired
17 | private RabbitTemplate rabbitTemplate;
18 |
19 | @Test
20 | public void sendMsg1(){
21 | rabbitTemplate.convertAndSend("itcast","直接模式");
22 | }
23 |
24 | /**
25 | 分裂模式
26 | */
27 | @Test
28 | public void sendMsg2(){
29 | rabbitTemplate.convertAndSend("hanjay","","分裂模式");
30 | }
31 |
32 | @Test
33 | public void sendMsg3(){
34 | rabbitTemplate.convertAndSend("topic84","","主题模式");
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/tensquare_recruit/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | com.tensquare
5 | tensquare_parent52
6 | 1.0-SNAPSHOT
7 |
8 | tensquare_recruit
9 |
10 |
11 | org.springframework.boot
12 | spring-boot-starter-data-jpa
13 |
14 |
15 | mysql
16 | mysql-connector-java
17 |
18 |
19 | com.tensquare
20 | tensquare_common
21 | 1.0-SNAPSHOT
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/RecruitApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit;
2 | import org.springframework.boot.SpringApplication;
3 | import org.springframework.boot.autoconfigure.SpringBootApplication;
4 | import org.springframework.context.annotation.Bean;
5 | import util.IdWorker;
6 | @SpringBootApplication
7 | public class RecruitApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(RecruitApplication.class, args);
11 | }
12 |
13 | @Bean
14 | public IdWorker idWorkker(){
15 | return new IdWorker(1, 1);
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/controller/BaseExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.controller;
2 | import entity.Result;
3 | import entity.StatusCode;
4 | import org.springframework.web.bind.annotation.ControllerAdvice;
5 | import org.springframework.web.bind.annotation.ExceptionHandler;
6 | import org.springframework.web.bind.annotation.ResponseBody;
7 | import javax.servlet.ServletException;
8 | import javax.servlet.http.HttpServletResponse;
9 | import java.io.IOException;
10 | /**
11 | * 统一异常处理类
12 | */
13 | @ControllerAdvice
14 | public class BaseExceptionHandler {
15 |
16 | @ExceptionHandler(value = Exception.class)
17 | @ResponseBody
18 | public Result error(Exception e){
19 | e.printStackTrace();
20 | return new Result(false, StatusCode.ERROR, "执行出错");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/controller/EnterpriseController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.controller;
2 | import java.util.List;
3 | import java.util.Map;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.web.bind.annotation.CrossOrigin;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RequestBody;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.RequestMethod;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import com.tensquare.recruit.pojo.Enterprise;
15 | import com.tensquare.recruit.service.EnterpriseService;
16 |
17 | import entity.PageResult;
18 | import entity.Result;
19 | import entity.StatusCode;
20 | /**
21 | * 控制器层
22 | * @author Administrator
23 | *
24 | */
25 | @RestController
26 | @CrossOrigin
27 | @RequestMapping("/enterprise")
28 | public class EnterpriseController {
29 |
30 | @Autowired
31 | private EnterpriseService enterpriseService;
32 |
33 | @RequestMapping(value="/search/hotlist",method=RequestMethod.GET)
34 | public Result hotlist(){
35 | List list = enterpriseService.hotList("1");
36 | return new Result(true,StatusCode.OK,"查询成功",list);
37 | }
38 |
39 |
40 | /**
41 | * 查询全部数据
42 | * @return
43 | */
44 | @RequestMapping(method= RequestMethod.GET)
45 | public Result findAll(){
46 | return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findAll());
47 | }
48 |
49 | /**
50 | * 根据ID查询
51 | * @param id ID
52 | * @return
53 | */
54 | @RequestMapping(value="/{id}",method= RequestMethod.GET)
55 | public Result findById(@PathVariable String id){
56 | return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findById(id));
57 | }
58 |
59 |
60 | /**
61 | * 分页+多条件查询
62 | * @param searchMap 查询条件封装
63 | * @param page 页码
64 | * @param size 页大小
65 | * @return 分页结果
66 | */
67 | @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
68 | public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
69 | Page pageList = enterpriseService.findSearch(searchMap, page, size);
70 | return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
71 | }
72 |
73 | /**
74 | * 根据条件查询
75 | * @param searchMap
76 | * @return
77 | */
78 | @RequestMapping(value="/search",method = RequestMethod.POST)
79 | public Result findSearch( @RequestBody Map searchMap){
80 | return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findSearch(searchMap));
81 | }
82 |
83 | /**
84 | * 增加
85 | * @param enterprise
86 | */
87 | @RequestMapping(method=RequestMethod.POST)
88 | public Result add(@RequestBody Enterprise enterprise ){
89 | enterpriseService.add(enterprise);
90 | return new Result(true,StatusCode.OK,"增加成功");
91 | }
92 |
93 | /**
94 | * 修改
95 | * @param enterprise
96 | */
97 | @RequestMapping(value="/{id}",method= RequestMethod.PUT)
98 | public Result update(@RequestBody Enterprise enterprise, @PathVariable String id ){
99 | enterprise.setId(id);
100 | enterpriseService.update(enterprise);
101 | return new Result(true,StatusCode.OK,"修改成功");
102 | }
103 |
104 | /**
105 | * 删除
106 | * @param id
107 | */
108 | @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
109 | public Result delete(@PathVariable String id ){
110 | enterpriseService.deleteById(id);
111 | return new Result(true,StatusCode.OK,"删除成功");
112 | }
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/controller/RecruitController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.controller;
2 | import java.util.List;
3 | import java.util.Map;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.web.bind.annotation.CrossOrigin;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RequestBody;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.RequestMethod;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import com.tensquare.recruit.pojo.Recruit;
15 | import com.tensquare.recruit.service.RecruitService;
16 |
17 | import entity.PageResult;
18 | import entity.Result;
19 | import entity.StatusCode;
20 | /**
21 | * 控制器层
22 | * @author Administrator
23 | *
24 | */
25 | @RestController
26 | @CrossOrigin
27 | @RequestMapping("/recruit")
28 | public class RecruitController {
29 |
30 | @Autowired
31 | private RecruitService recruitService;
32 |
33 | @RequestMapping(value="/search/recommend",method= RequestMethod.GET)
34 | public Result recommend(){
35 | return new Result(true,StatusCode.OK,"查询成功",recruitService.recommend());
36 | }
37 |
38 | @RequestMapping(value="/search/newlist",method= RequestMethod.GET)
39 | public Result newlist(){
40 | return new Result(true,StatusCode.OK,"查询成功",recruitService.newlist());
41 | }
42 |
43 |
44 |
45 |
46 | public RecruitService getRecruitService(){
47 | return recruitService;
48 | }
49 |
50 |
51 | /**
52 | * 查询全部数据
53 | * @return
54 | */
55 | @RequestMapping(method= RequestMethod.GET)
56 | public Result findAll(){
57 | return new Result(true,StatusCode.OK,"查询成功",recruitService.findAll());
58 | }
59 |
60 | /**
61 | * 根据ID查询
62 | * @param id ID
63 | * @return
64 | */
65 | @RequestMapping(value="/{id}",method= RequestMethod.GET)
66 | public Result findById(@PathVariable String id){
67 | return new Result(true,StatusCode.OK,"查询成功",recruitService.findById(id));
68 | }
69 |
70 |
71 | /**
72 | * 分页+多条件查询
73 | * @param searchMap 查询条件封装
74 | * @param page 页码
75 | * @param size 页大小
76 | * @return 分页结果
77 | */
78 | @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
79 | public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
80 | Page pageList = recruitService.findSearch(searchMap, page, size);
81 | return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
82 | }
83 |
84 | /**
85 | * 根据条件查询
86 | * @param searchMap
87 | * @return
88 | */
89 | @RequestMapping(value="/search",method = RequestMethod.POST)
90 | public Result findSearch( @RequestBody Map searchMap){
91 | return new Result(true,StatusCode.OK,"查询成功",recruitService.findSearch(searchMap));
92 | }
93 |
94 | /**
95 | * 增加
96 | * @param recruit
97 | */
98 | @RequestMapping(method=RequestMethod.POST)
99 | public Result add(@RequestBody Recruit recruit ){
100 | recruitService.add(recruit);
101 | return new Result(true,StatusCode.OK,"增加成功");
102 | }
103 |
104 | /**
105 | * 修改
106 | * @param recruit
107 | */
108 | @RequestMapping(value="/{id}",method= RequestMethod.PUT)
109 | public Result update(@RequestBody Recruit recruit, @PathVariable String id ){
110 | recruit.setId(id);
111 | recruitService.update(recruit);
112 | return new Result(true,StatusCode.OK,"修改成功");
113 | }
114 |
115 | /**
116 | * 删除
117 | * @param id
118 | */
119 | @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
120 | public Result delete(@PathVariable String id ){
121 | recruitService.deleteById(id);
122 | return new Result(true,StatusCode.OK,"删除成功");
123 | }
124 |
125 | }
126 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/dao/EnterpriseDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.dao;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5 |
6 | import com.tensquare.recruit.pojo.Enterprise;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * 数据访问接口
12 | * @author Administrator
13 | *
14 | */
15 | public interface EnterpriseDao extends JpaRepository,JpaSpecificationExecutor{
16 |
17 | public List findByIshot(String ishot); //where ishot=?
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/dao/RecruitDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.dao;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5 |
6 | import com.tensquare.recruit.pojo.Recruit;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * 数据访问接口
12 | * @author Administrator
13 | *
14 | */
15 |
16 | public interface RecruitDao extends JpaRepository,JpaSpecificationExecutor{
17 |
18 |
19 | public List findTop6ByStateOrderByCreatetimeDesc(String state); //where state=? order by createime
20 |
21 | public List findTop6ByStateNotOrderByCreatetimeDesc(String state); //where state!=? order by createime
22 | }
23 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/pojo/Enterprise.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.pojo;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.Id;
5 | import javax.persistence.Table;
6 | import java.io.Serializable;
7 | /**
8 | * 实体类
9 | * @author Administrator
10 | *
11 | */
12 | @Entity
13 | @Table(name="tb_enterprise")
14 | public class Enterprise implements Serializable{
15 |
16 | @Id
17 | private String id;//ID
18 |
19 |
20 |
21 | private String name;//企业名称
22 | private String summary;//企业简介
23 | private String address;//企业地址
24 | private String labels;//标签列表
25 | private String coordinate;//坐标
26 | private String ishot;//是否热门
27 | private String logo;//LOGO
28 | private Integer jobcount;//职位数
29 | private String url;//URL
30 |
31 |
32 | public String getId() {
33 | return id;
34 | }
35 | public void setId(String id) {
36 | this.id = id;
37 | }
38 |
39 | public String getName() {
40 | return name;
41 | }
42 | public void setName(String name) {
43 | this.name = name;
44 | }
45 |
46 | public String getSummary() {
47 | return summary;
48 | }
49 | public void setSummary(String summary) {
50 | this.summary = summary;
51 | }
52 |
53 | public String getAddress() {
54 | return address;
55 | }
56 | public void setAddress(String address) {
57 | this.address = address;
58 | }
59 |
60 | public String getLabels() {
61 | return labels;
62 | }
63 | public void setLabels(String labels) {
64 | this.labels = labels;
65 | }
66 |
67 | public String getCoordinate() {
68 | return coordinate;
69 | }
70 | public void setCoordinate(String coordinate) {
71 | this.coordinate = coordinate;
72 | }
73 |
74 | public String getIshot() {
75 | return ishot;
76 | }
77 | public void setIshot(String ishot) {
78 | this.ishot = ishot;
79 | }
80 |
81 | public String getLogo() {
82 | return logo;
83 | }
84 | public void setLogo(String logo) {
85 | this.logo = logo;
86 | }
87 |
88 | public Integer getJobcount() {
89 | return jobcount;
90 | }
91 | public void setJobcount(Integer jobcount) {
92 | this.jobcount = jobcount;
93 | }
94 |
95 | public String getUrl() {
96 | return url;
97 | }
98 | public void setUrl(String url) {
99 | this.url = url;
100 | }
101 |
102 |
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/pojo/Recruit.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.pojo;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.Id;
5 | import javax.persistence.Table;
6 | import java.io.Serializable;
7 | /**
8 | * 实体类
9 | * @author Administrator
10 | *
11 | */
12 | @Entity
13 | @Table(name="tb_recruit")
14 | public class Recruit implements Serializable{
15 |
16 | @Id
17 | private String id;//ID
18 |
19 |
20 |
21 | private String jobname;//职位名称
22 | private String salary;//薪资范围
23 | private String condition;//经验要求
24 | private String education;//学历要求
25 | private String type;//任职方式
26 | private String address;//办公地址
27 | private String eid;//企业ID
28 | private java.util.Date createtime;//创建日期
29 | private String state;//状态
30 | private String url;//网址
31 | private String label;//标签
32 | private String content1;//职位描述
33 | private String content2;//职位要求
34 |
35 |
36 | public String getId() {
37 | return id;
38 | }
39 | public void setId(String id) {
40 | this.id = id;
41 | }
42 |
43 | public String getJobname() {
44 | return jobname;
45 | }
46 | public void setJobname(String jobname) {
47 | this.jobname = jobname;
48 | }
49 |
50 | public String getSalary() {
51 | return salary;
52 | }
53 | public void setSalary(String salary) {
54 | this.salary = salary;
55 | }
56 |
57 | public String getCondition() {
58 | return condition;
59 | }
60 | public void setCondition(String condition) {
61 | this.condition = condition;
62 | }
63 |
64 | public String getEducation() {
65 | return education;
66 | }
67 | public void setEducation(String education) {
68 | this.education = education;
69 | }
70 |
71 | public String getType() {
72 | return type;
73 | }
74 | public void setType(String type) {
75 | this.type = type;
76 | }
77 |
78 | public String getAddress() {
79 | return address;
80 | }
81 | public void setAddress(String address) {
82 | this.address = address;
83 | }
84 |
85 | public String getEid() {
86 | return eid;
87 | }
88 | public void setEid(String eid) {
89 | this.eid = eid;
90 | }
91 |
92 | public java.util.Date getCreatetime() {
93 | return createtime;
94 | }
95 | public void setCreatetime(java.util.Date createtime) {
96 | this.createtime = createtime;
97 | }
98 |
99 | public String getState() {
100 | return state;
101 | }
102 | public void setState(String state) {
103 | this.state = state;
104 | }
105 |
106 | public String getUrl() {
107 | return url;
108 | }
109 | public void setUrl(String url) {
110 | this.url = url;
111 | }
112 |
113 | public String getLabel() {
114 | return label;
115 | }
116 | public void setLabel(String label) {
117 | this.label = label;
118 | }
119 |
120 | public String getContent1() {
121 | return content1;
122 | }
123 | public void setContent1(String content1) {
124 | this.content1 = content1;
125 | }
126 |
127 | public String getContent2() {
128 | return content2;
129 | }
130 | public void setContent2(String content2) {
131 | this.content2 = content2;
132 | }
133 |
134 |
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/java/com/tensquare/recruit/service/EnterpriseService.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.recruit.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collection;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import javax.persistence.criteria.CriteriaBuilder;
9 | import javax.persistence.criteria.CriteriaQuery;
10 | import javax.persistence.criteria.Expression;
11 | import javax.persistence.criteria.Predicate;
12 | import javax.persistence.criteria.Root;
13 | import javax.persistence.criteria.Selection;
14 |
15 | import org.springframework.beans.factory.annotation.Autowired;
16 | import org.springframework.data.domain.Page;
17 | import org.springframework.data.domain.PageRequest;
18 | import org.springframework.data.domain.Sort;
19 | import org.springframework.data.jpa.domain.Specification;
20 | import org.springframework.stereotype.Service;
21 |
22 | import util.IdWorker;
23 |
24 | import com.tensquare.recruit.dao.EnterpriseDao;
25 | import com.tensquare.recruit.pojo.Enterprise;
26 |
27 | /**
28 | * 服务层
29 | *
30 | * @author Administrator
31 | *
32 | */
33 | @Service
34 | public class EnterpriseService {
35 |
36 | @Autowired
37 | private EnterpriseDao enterpriseDao;
38 |
39 | @Autowired
40 | private IdWorker idWorker;
41 |
42 | public List hotList(String ishot){
43 | return enterpriseDao.findByIshot(ishot);
44 | }
45 |
46 | /**
47 | * 查询全部列表
48 | * @return
49 | */
50 | public List findAll() {
51 | return enterpriseDao.findAll();
52 | }
53 |
54 |
55 | /**
56 | * 条件查询+分页
57 | * @param whereMap
58 | * @param page
59 | * @param size
60 | * @return
61 | */
62 | public Page findSearch(Map whereMap, int page, int size) {
63 | Specification specification = createSpecification(whereMap);
64 | PageRequest pageRequest = PageRequest.of(page-1, size);
65 | return enterpriseDao.findAll(specification, pageRequest);
66 | }
67 |
68 |
69 | /**
70 | * 条件查询
71 | * @param whereMap
72 | * @return
73 | */
74 | public List findSearch(Map whereMap) {
75 | Specification specification = createSpecification(whereMap);
76 | return enterpriseDao.findAll(specification);
77 | }
78 |
79 | /**
80 | * 根据ID查询实体
81 | * @param id
82 | * @return
83 | */
84 | public Enterprise findById(String id) {
85 | return enterpriseDao.findById(id).get();
86 | }
87 |
88 | /**
89 | * 增加
90 | * @param enterprise
91 | */
92 | public void add(Enterprise enterprise) {
93 | enterprise.setId( idWorker.nextId()+"" );
94 | enterpriseDao.save(enterprise);
95 | }
96 |
97 | /**
98 | * 修改
99 | * @param enterprise
100 | */
101 | public void update(Enterprise enterprise) {
102 | enterpriseDao.save(enterprise);
103 | }
104 |
105 | /**
106 | * 删除
107 | * @param id
108 | */
109 | public void deleteById(String id) {
110 | enterpriseDao.deleteById(id);
111 | }
112 |
113 | /**
114 | * 动态条件构建
115 | * @param searchMap
116 | * @return
117 | */
118 | private Specification createSpecification(Map searchMap) {
119 |
120 | return new Specification() {
121 |
122 | @Override
123 | public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
124 | List predicateList = new ArrayList();
125 | // ID
126 | if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
127 | predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
128 | }
129 | // 企业名称
130 | if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
131 | predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
132 | }
133 | // 企业简介
134 | if (searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))) {
135 | predicateList.add(cb.like(root.get("summary").as(String.class), "%"+(String)searchMap.get("summary")+"%"));
136 | }
137 | // 企业地址
138 | if (searchMap.get("address")!=null && !"".equals(searchMap.get("address"))) {
139 | predicateList.add(cb.like(root.get("address").as(String.class), "%"+(String)searchMap.get("address")+"%"));
140 | }
141 | // 标签列表
142 | if (searchMap.get("labels")!=null && !"".equals(searchMap.get("labels"))) {
143 | predicateList.add(cb.like(root.get("labels").as(String.class), "%"+(String)searchMap.get("labels")+"%"));
144 | }
145 | // 坐标
146 | if (searchMap.get("coordinate")!=null && !"".equals(searchMap.get("coordinate"))) {
147 | predicateList.add(cb.like(root.get("coordinate").as(String.class), "%"+(String)searchMap.get("coordinate")+"%"));
148 | }
149 | // 是否热门
150 | if (searchMap.get("ishot")!=null && !"".equals(searchMap.get("ishot"))) {
151 | predicateList.add(cb.like(root.get("ishot").as(String.class), "%"+(String)searchMap.get("ishot")+"%"));
152 | }
153 | // LOGO
154 | if (searchMap.get("logo")!=null && !"".equals(searchMap.get("logo"))) {
155 | predicateList.add(cb.like(root.get("logo").as(String.class), "%"+(String)searchMap.get("logo")+"%"));
156 | }
157 | // URL
158 | if (searchMap.get("url")!=null && !"".equals(searchMap.get("url"))) {
159 | predicateList.add(cb.like(root.get("url").as(String.class), "%"+(String)searchMap.get("url")+"%"));
160 | }
161 |
162 | return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
163 |
164 | }
165 | };
166 |
167 | }
168 |
169 |
170 | }
171 |
--------------------------------------------------------------------------------
/tensquare_recruit/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9002
3 | spring:
4 | application:
5 | name: tensquare-recruit #指定服务名
6 | datasource:
7 | driverClassName: com.mysql.jdbc.Driver
8 | url: jdbc:mysql://192.168.213.135:3306/tensquare_recruit?characterEncoding=UTF8
9 | username: root
10 | password: root
11 | jpa:
12 | database: MySQL
13 | show-sql: true
14 |
--------------------------------------------------------------------------------
/tensquare_search/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_search
13 |
14 |
15 |
16 | com.tensquare
17 | tensquare_common
18 | 1.0-SNAPSHOT
19 |
20 |
21 | org.springframework.data
22 | spring-data-elasticsearch
23 | 3.1.9.RELEASE
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/tensquare_search/src/main/java/com/tensquare/search/SearchApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.search;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.context.annotation.Bean;
6 | import util.IdWorker;
7 |
8 | @SpringBootApplication
9 | public class SearchApplication {
10 | public static void main(String[] args) {
11 | SpringApplication.run(SearchApplication.class,args);
12 | }
13 |
14 | @Bean
15 | public IdWorker idWorker(){
16 | return new IdWorker(1,1);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tensquare_search/src/main/java/com/tensquare/search/controller/ArticleController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.search.controller;
2 |
3 |
4 | import com.tensquare.search.pojo.Article;
5 | import com.tensquare.search.service.ArticleService;
6 | import entity.PageResult;
7 | import entity.Result;
8 | import entity.StatusCode;
9 | import org.elasticsearch.action.index.IndexRequest;
10 | import org.elasticsearch.action.index.IndexResponse;
11 | import org.elasticsearch.client.transport.TransportClient;
12 | import org.elasticsearch.common.settings.Settings;
13 | import org.elasticsearch.common.transport.TransportAddress;
14 | import org.elasticsearch.transport.client.PreBuiltTransportClient;
15 | import org.springframework.beans.factory.annotation.Autowired;
16 | import org.springframework.context.annotation.Bean;
17 | import org.springframework.context.annotation.Configuration;
18 | import org.springframework.data.domain.Page;
19 | import org.springframework.web.bind.annotation.*;
20 |
21 | import java.net.InetAddress;
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | @RestController
30 | @RequestMapping("/article")
31 | @CrossOrigin
32 | public class ArticleController {
33 | @Autowired
34 | private ArticleService articleService;
35 |
36 | @RequestMapping(method= RequestMethod.POST)
37 | public Result save(@RequestBody Article article){
38 | articleService.save(article);
39 | return new Result(true, StatusCode.OK,"添加成功");
40 | }
41 |
42 | @RequestMapping(value="/{key}/{page}/{size}",method=RequestMethod.GET)
43 | public Result findByKey(@PathVariable String Key,@PathVariable int page,@PathVariable int size){
44 | Page pageData = articleService.findByKey(key,page,size);
45 | return new Result(true,StatusCode.OK,"查询成功",new PageResult(pageData.getTotalElements(),pageData.getContent()));
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/tensquare_search/src/main/java/com/tensquare/search/dao/ArticleDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.search.dao;
2 |
3 | import com.tensquare.search.pojo.Article;
4 |
5 | import org.springframework.data.domain.Page;
6 | import org.springframework.data.domain.Pageable;
7 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
8 |
9 | public interface ArticleDao extends ElasticsearchRepository {
10 |
11 | public Page findByTitleOrContentLike(String title, String content, Pageable pageable);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/tensquare_search/src/main/java/com/tensquare/search/pojo/Article.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.search.pojo;
2 |
3 |
4 | import com.sun.javafx.beans.IDProperty;
5 | import org.springframework.data.annotation.Id;
6 | import org.springframework.data.elasticsearch.annotations.Document;
7 | import org.springframework.data.elasticsearch.annotations.Field;
8 |
9 | import java.io.Serializable;
10 |
11 |
12 |
13 | @Document(indexName="tensquare_article",type="article")
14 | public class Article implements Serializable {
15 |
16 | @Id
17 | private String id;
18 |
19 | //是否索引,就是看该域是否能被搜索
20 | //是否分词,就表示搜索的时候是整体匹配还是单词匹配
21 | //是否存储,就是是否在页面上显示
22 | @Field(index=true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
23 | private String title;
24 |
25 | @Field(index=true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
26 | private String content;
27 |
28 | private String state; //审核状态
29 |
30 | public String getId() {
31 | return id;
32 | }
33 |
34 | public void setId(String id) {
35 | this.id = id;
36 | }
37 |
38 | public String getTitle() {
39 | return title;
40 | }
41 |
42 | public void setTitle(String title) {
43 | this.title = title;
44 | }
45 |
46 | public String getContent() {
47 | return content;
48 | }
49 |
50 | public void setContent(String content) {
51 | this.content = content;
52 | }
53 |
54 | public String getState() {
55 | return state;
56 | }
57 |
58 | public void setState(String state) {
59 | this.state = state;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/tensquare_search/src/main/java/com/tensquare/search/service/ArticleService.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.search.service;
2 |
3 | import com.tensquare.search.dao.ArticleDao;
4 | import com.tensquare.search.pojo.Article;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.data.domain.PageRequest;
8 | import org.springframework.data.domain.Pageable;
9 | import org.springframework.stereotype.Service;
10 | import util.IdWorker;
11 |
12 | @Service
13 | public class ArticleService {
14 |
15 | @Autowired
16 | private ArticleDao articleDao;
17 |
18 | // @Autowired
19 | // private IdWorker idWorker;
20 | //
21 | public void save(Article article){
22 | // article.setId(idWorker.nextId()+"");
23 | articleDao.save(article);
24 | }
25 |
26 | public Page findByKey(String key,int page,int size){
27 | Pageable pageable= PageRequest.of(page-1,size);
28 | articleDao.findByTitleOrContentLike(key,key,pageable);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/tensquare_search/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9007
3 | spring:
4 | application:
5 | name: tensquare-search
6 | data:
7 | elasticsearch:
8 | cluster-nodes: 192.168.213.135:9300
--------------------------------------------------------------------------------
/tensquare_sms/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_sms
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-amqp
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tensquare_sms/src/main/java/com/tensquare/sms/SmsApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.sms;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SmsApplication {
8 | public static void main(String[] args) {
9 | SpringApplication.run(SmsApplication.class,args);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/tensquare_sms/src/main/java/com/tensquare/sms/listener/SmsListener.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.sms.listener;
2 |
3 | import org.springframework.amqp.rabbit.annotation.RabbitHandler;
4 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
5 | import org.springframework.stereotype.Component;
6 |
7 | import java.util.Map;
8 |
9 | @Component
10 | @RabbitListener(queues="sms")
11 | public class SmsListener {
12 |
13 | @RabbitHandler
14 | public void executeSms(Map map){
15 | System.out.println("手机号:"+ map.get("mobile"));
16 | System.out.println("验证码:"+ map.get("checkcode"));
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/tensquare_sms/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9009
3 | spring:
4 | application:
5 | name: tensquare-sms
6 | rabbitmq:
7 | host: 192.168.213.135
--------------------------------------------------------------------------------
/tensquare_spit/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_spit
13 |
14 |
15 |
16 | com.tensquare
17 | tensquare_common
18 | 1.0-SNAPSHOT
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-data-mongodb
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-data-redis
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/tensquare_spit/src/main/java/com/tensquare/spit/SpitApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.spit;
2 |
3 |
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.context.annotation.Bean;
7 | import util.IdWorker;
8 |
9 | @SpringBootApplication
10 | public class SpitApplication {
11 | public static void main(String[] args) {
12 | SpringApplication.run(SpitApplication.class,args);
13 | }
14 |
15 | @Bean
16 | public IdWorker idWorker(){
17 | return new IdWorker();
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/tensquare_spit/src/main/java/com/tensquare/spit/controller/SpitController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.spit.controller;
2 |
3 |
4 | import com.tensquare.spit.pojo.Spit;
5 | import com.tensquare.spit.service.SpitService;
6 | import entity.PageResult;
7 | import entity.Result;
8 | import entity.StatusCode;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.data.domain.Page;
11 | import org.springframework.data.redis.core.RedisTemplate;
12 | import org.springframework.web.bind.annotation.*;
13 |
14 | import javax.annotation.Resource;
15 |
16 | @RestController
17 | @CrossOrigin
18 | @RequestMapping("/spit")
19 | public class SpitController {
20 |
21 | @Autowired
22 | private SpitService spitService;
23 |
24 | @Autowired
25 | private RedisTemplate redisTemplate;
26 |
27 |
28 | @RequestMapping(method= RequestMethod.GET)
29 | public Result findAll(){
30 | return new Result(true, StatusCode.OK,"查询成功",spitService.findAll());
31 | }
32 |
33 | @RequestMapping(value="/{spitId}",method=RequestMethod.GET)
34 | public Result findById(@PathVariable String spitId){
35 | return new Result(true,StatusCode.OK,"查询成功",spitService.findById(spitId));
36 | }
37 |
38 | @RequestMapping(method=RequestMethod.POST)
39 | public Result save(@RequestBody Spit spit){
40 | spitService.save(spit);
41 | return new Result(true,StatusCode.OK,"保存成功");
42 | }
43 |
44 | @RequestMapping(value="/{spitId}",method=RequestMethod.PUT)
45 | public Result update(@PathVariable String spitId,@RequestBody Spit spit){
46 | spit.set_id(spitId);
47 | spitService.update(spit);
48 | return new Result(true,StatusCode.OK,"修改成功");
49 | }
50 |
51 | @RequestMapping(value="/{spitId}",method=RequestMethod.DELETE)
52 | public Result delete(@PathVariable String spitId){
53 | spitService.deleteById(spitId);
54 | return new Result(true,StatusCode.OK,"删除成功");
55 | }
56 |
57 |
58 | @RequestMapping(value="/comment/{parentid}/{page}/{size}",method=RequestMethod.GET)
59 | public Result findByParentid(@PathVariable String parentid,@PathVariable int page, @PathVariable int size){
60 | Page pageData = spitService.findByParentid(parentid,page,size);
61 | return new Result(true,StatusCode.OK,"查询成功",new PageResult(pageData.getTotalElements(),pageData.getContent()));
62 | }
63 |
64 |
65 |
66 |
67 |
68 |
69 | @RequestMapping(value="/thumbup/{spitId}",method=RequestMethod.PUT)
70 | public Result thumbup(@PathVariable String spitId){
71 |
72 | //判断当前用户是否已经点赞,但是现在没有做认证,所以暂时把userid写死
73 | String userid = "111";
74 | //判断当前用户是否已经点赞
75 | if (redisTemplate.opsForValue().get("thumbup_"+userid)!=null){
76 | return new Result(false,StatusCode.REPERROR,"不能重复点赞");
77 | }
78 | spitService.thumbup(spitId);
79 | redisTemplate.opsForValue().set("thumbup_"+userid,1);
80 | return new Result(true,StatusCode.OK,"点赞成功");
81 | }
82 |
83 |
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/tensquare_spit/src/main/java/com/tensquare/spit/dao/SpitDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.spit.dao;
2 |
3 | import com.tensquare.spit.pojo.Spit;
4 | import org.springframework.data.domain.Page;
5 | import org.springframework.data.domain.Pageable;
6 | import org.springframework.data.mongodb.repository.MongoRepository;
7 |
8 | public interface SpitDao extends MongoRepository {
9 |
10 | public Page findByParentid(String parentid, Pageable pageable);
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/tensquare_spit/src/main/java/com/tensquare/spit/pojo/Spit.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.spit.pojo;
2 |
3 |
4 | import org.springframework.data.annotation.Id;
5 |
6 | import java.util.Date;
7 |
8 | public class Spit {
9 | @Id
10 | private String _id;
11 | private String content;
12 | private Date publishtime;
13 | private String userid;
14 | private String nickname;
15 | private Integer visits;
16 | private Integer thumbup;
17 | private Integer share;
18 | private Integer comment;
19 | private String state;
20 | private String parentid;
21 |
22 | public String get_id() {
23 | return _id;
24 | }
25 |
26 | public void set_id(String _id) {
27 | this._id = _id;
28 | }
29 |
30 | public String getContent() {
31 | return content;
32 | }
33 |
34 | public void setContent(String content) {
35 | this.content = content;
36 | }
37 |
38 | public Date getPublishtime() {
39 | return publishtime;
40 | }
41 |
42 | public void setPublishtime(Date publishtime) {
43 | this.publishtime = publishtime;
44 | }
45 |
46 | public String getUserid() {
47 | return userid;
48 | }
49 |
50 | public void setUserid(String userid) {
51 | this.userid = userid;
52 | }
53 |
54 | public String getNickname() {
55 | return nickname;
56 | }
57 |
58 | public void setNickname(String nickname) {
59 | this.nickname = nickname;
60 | }
61 |
62 | public Integer getVisits() {
63 | return visits;
64 | }
65 |
66 | public void setVisits(Integer visits) {
67 | this.visits = visits;
68 | }
69 |
70 | public Integer getThumbup() {
71 | return thumbup;
72 | }
73 |
74 | public void setThumbup(Integer thumbup) {
75 | this.thumbup = thumbup;
76 | }
77 |
78 | public Integer getShare() {
79 | return share;
80 | }
81 |
82 | public void setShare(Integer share) {
83 | this.share = share;
84 | }
85 |
86 | public Integer getComment() {
87 | return comment;
88 | }
89 |
90 | public void setComment(Integer comment) {
91 | this.comment = comment;
92 | }
93 |
94 | public String getState() {
95 | return state;
96 | }
97 |
98 | public void setState(String state) {
99 | this.state = state;
100 | }
101 |
102 | public String getParentid() {
103 | return parentid;
104 | }
105 |
106 | public void setParentid(String parentid) {
107 | this.parentid = parentid;
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/tensquare_spit/src/main/java/com/tensquare/spit/service/SpitService.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.spit.service;
2 |
3 |
4 | import com.tensquare.spit.dao.SpitDao;
5 | import com.tensquare.spit.pojo.Spit;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.data.domain.Page;
8 | import org.springframework.data.domain.PageRequest;
9 | import org.springframework.data.domain.Pageable;
10 | import org.springframework.data.mongodb.core.MongoTemplate;
11 | import org.springframework.data.mongodb.core.query.Criteria;
12 | import org.springframework.data.mongodb.core.query.Query;
13 | import org.springframework.data.mongodb.core.query.Update;
14 | import org.springframework.stereotype.Service;
15 | import org.springframework.transaction.annotation.Transactional;
16 | import util.IdWorker;
17 |
18 | import java.util.Date;
19 | import java.util.List;
20 | import java.util.Optional;
21 |
22 | @Service
23 | @Transactional
24 | public class SpitService {
25 |
26 | @Autowired
27 | private SpitDao spitDao;
28 |
29 | @Autowired
30 | private IdWorker idWorker;
31 |
32 | @Autowired
33 | private MongoTemplate mongoTemplate;
34 |
35 |
36 | public List findAll(){
37 | return spitDao.findAll();
38 | }
39 | public Spit findById(String id){
40 | return spitDao.findById(id).get();
41 | }
42 | public void save(Spit spit){
43 | spit.set_id(idWorker.nextId()+"");
44 | spit.setPublishtime(new Date()); // 发布日期
45 | spit.setVisits(0); // 浏览量
46 | spit.setShare(0); // 分享数
47 | spit.setThumbup(0); // 点赞数
48 | spit.setComment(0); // 回复数
49 | spit.setState("1"); // 状态
50 |
51 | // 如果当前的吐槽有父节点,那么父节点加一
52 | if (spit.getParentid() != null && !"".equals(spit.getParentid())) {
53 | Query query = new Query();
54 | query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
55 | Update update = new Update();
56 | update.inc("comment", 1);
57 | mongoTemplate.updateFirst(query, update, "spit");
58 | }
59 | spitDao.save(spit);
60 | }
61 | public void update(Spit spit){
62 | spitDao.save(spit);
63 | }
64 | public void deleteById(String id){
65 | spitDao.deleteById(id);
66 | }
67 |
68 | public Page findByParentid(String parentid,int page,int size){
69 | Pageable pageable = PageRequest.of(page-1,size);
70 | return spitDao.findByParentid(parentid, pageable);
71 | }
72 |
73 |
74 |
75 | public void thumbup(String spitId){
76 | // //方式一:效率有问题
77 | // Spit spit = spitDao.findById(spitId).get();
78 | // spit.setThumbup((spit.getThumbup()==null ?0 : spit.getThumbup()) + 1);
79 | // spitDao.save(spit);
80 | //方式二,使用原生mongo命令实现自增 db.spit.update({"_id":"1"}.{$inc:{thumbup:NumberInt(1)}})
81 | Query query = new Query();
82 | query.addCriteria(Criteria.where("_id").is("1"));
83 | Update update = new Update();
84 | update.inc("thumbup",1);
85 | mongoTemplate.updateFirst(query,update,"spit");
86 |
87 |
88 | }
89 |
90 |
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/tensquare_spit/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9006
3 | spring:
4 | application:
5 | name: tensquare-spit
6 | data:
7 | mongodb:
8 | host: 192.168.213.135
9 | database: spitdb
10 | redis:
11 | host: 192.168.213.135
--------------------------------------------------------------------------------
/tensquare_user/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | com.tensquare
5 | tensquare_parent52
6 | 1.0-SNAPSHOT
7 |
8 | tensquare_user
9 |
10 |
11 | org.springframework.boot
12 | spring-boot-starter-data-jpa
13 |
14 |
15 | mysql
16 | mysql-connector-java
17 |
18 |
19 | com.tensquare
20 | tensquare_common
21 | 1.0-SNAPSHOT
22 |
23 |
24 | org.apache.commons
25 | commons-lang3
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-data-redis
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-amqp
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-security
38 |
39 |
40 | org.springframework.cloud
41 | spring-cloud-starter-netflix-eureka-client
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/UserApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user;
2 | import org.springframework.boot.SpringApplication;
3 | import org.springframework.boot.autoconfigure.SpringBootApplication;
4 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
7 | import util.IdWorker;
8 | import util.JwtUtil;
9 |
10 | @SpringBootApplication
11 | @EnableEurekaClient
12 | public class UserApplication {
13 | public static void main(String[] args) {
14 | SpringApplication.run(UserApplication.class, args);
15 | }
16 |
17 | @Bean
18 | public IdWorker idWorkker(){
19 | return new IdWorker(1, 1);
20 | }
21 |
22 | @Bean
23 | public BCryptPasswordEncoder encoder(){
24 | return new BCryptPasswordEncoder();
25 | }
26 |
27 | @Bean
28 | public JwtUtil jwtUtil(){
29 | return new JwtUtil();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/config/InterceptorConfig.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.config;
2 |
3 | import com.tensquare.user.interceptor.JwtInterceptor;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
8 |
9 | @Configuration
10 | public class InterceptorConfig extends WebMvcConfigurationSupport {
11 |
12 | @Autowired
13 | private JwtInterceptor jwtInterceptor;
14 | protected void addInterceptors(InterceptorRegistry registry){
15 | //注册拦截器,要声明拦截器对象和要拦截的请求
16 | registry.addInterceptor(jwtInterceptor)
17 | .addPathPatterns("/**")
18 | .excludePathPatterns("/**/login/**");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/config/WebSecurityConfig.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.config;
2 |
3 | import org.springframework.context.annotation.Configuration;
4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7 |
8 | /**
9 | * 安全配置类
10 | */
11 | @Configuration
12 | @EnableWebSecurity
13 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
14 | @Override
15 | protected void configure(HttpSecurity http) throws Exception {
16 | //authorizeRequests所有security全注解配置实现的开端,表示开始说明需要的权限
17 | //需要的权限分两部分,第一部分是拦截的路径,第二部分是访问该路径需要的权限
18 | //antMatchers表示拦截什么路径,permitAll任何权限都可以访问
19 | //anyRequest()任何的请求,authenticated认证后才能访问
20 | //.and().csrf().disable(),固定写法 表示使csrf拦截失效
21 | http
22 | .authorizeRequests()
23 | .antMatchers("/**").permitAll()
24 | .anyRequest().authenticated()
25 | .and().csrf().disable();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/controller/AdminController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.controller;
2 | import java.util.HashMap;
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.data.domain.Page;
8 | import org.springframework.web.bind.annotation.CrossOrigin;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestBody;
11 | import org.springframework.web.bind.annotation.RequestMapping;
12 | import org.springframework.web.bind.annotation.RequestMethod;
13 | import org.springframework.web.bind.annotation.RestController;
14 |
15 | import com.tensquare.user.pojo.Admin;
16 | import com.tensquare.user.service.AdminService;
17 |
18 | import entity.PageResult;
19 | import entity.Result;
20 | import entity.StatusCode;
21 | import util.JwtUtil;
22 |
23 | /**
24 | * 控制器层
25 | * @author Administrator
26 | *
27 | */
28 | @RestController
29 | @CrossOrigin
30 | @RequestMapping("/admin")
31 | public class AdminController {
32 |
33 | @Autowired
34 | private AdminService adminService;
35 |
36 | @Autowired
37 | private JwtUtil jwtUtil;
38 |
39 | @RequestMapping(value="/login",method=RequestMethod.POST)
40 | public Result login(@RequestBody Admin admin){
41 | Admin adminLogin = adminService.login(admin);
42 | if(adminLogin==null){
43 | return new Result(false,StatusCode.LOGINERROR,"登录失败");
44 | }
45 | //使得前后端可以通话的操作,采用JWT来实现
46 | //生成令牌
47 | String token = jwtUtil.createJWT(adminLogin.getId(),adminLogin.getLoginname(),"admin");
48 | Map map = new HashMap<>();
49 | map.put("token",token);
50 | map.put("role","admin");
51 | return new Result(true,StatusCode.OK,"登录成功",map);
52 | }
53 |
54 |
55 | /**
56 | * 查询全部数据
57 | * @return
58 | */
59 | @RequestMapping(method= RequestMethod.GET)
60 | public Result findAll(){
61 | return new Result(true,StatusCode.OK,"查询成功",adminService.findAll());
62 | }
63 |
64 | /**
65 | * 根据ID查询
66 | * @param id ID
67 | * @return
68 | */
69 | @RequestMapping(value="/{id}",method= RequestMethod.GET)
70 | public Result findById(@PathVariable String id){
71 | return new Result(true,StatusCode.OK,"查询成功",adminService.findById(id));
72 | }
73 |
74 |
75 | /**
76 | * 分页+多条件查询
77 | * @param searchMap 查询条件封装
78 | * @param page 页码
79 | * @param size 页大小
80 | * @return 分页结果
81 | */
82 | @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
83 | public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
84 | Page pageList = adminService.findSearch(searchMap, page, size);
85 | return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
86 | }
87 |
88 | /**
89 | * 根据条件查询
90 | * @param searchMap
91 | * @return
92 | */
93 | @RequestMapping(value="/search",method = RequestMethod.POST)
94 | public Result findSearch( @RequestBody Map searchMap){
95 | return new Result(true,StatusCode.OK,"查询成功",adminService.findSearch(searchMap));
96 | }
97 |
98 | /**
99 | * 增加
100 | * @param admin
101 | */
102 | @RequestMapping(method=RequestMethod.POST)
103 | public Result add(@RequestBody Admin admin ){
104 | adminService.add(admin);
105 | return new Result(true,StatusCode.OK,"增加成功");
106 | }
107 |
108 | /**
109 | * 修改
110 | * @param admin
111 | */
112 | @RequestMapping(value="/{id}",method= RequestMethod.PUT)
113 | public Result update(@RequestBody Admin admin, @PathVariable String id ){
114 | admin.setId(id);
115 | adminService.update(admin);
116 | return new Result(true,StatusCode.OK,"修改成功");
117 | }
118 |
119 | /**
120 | * 删除
121 | * @param id
122 | */
123 | @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
124 | public Result delete(@PathVariable String id ){
125 | adminService.deleteById(id);
126 | return new Result(true,StatusCode.OK,"删除成功");
127 | }
128 |
129 | }
130 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/controller/BaseExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.controller;
2 | import entity.Result;
3 | import entity.StatusCode;
4 | import org.springframework.web.bind.annotation.ControllerAdvice;
5 | import org.springframework.web.bind.annotation.ExceptionHandler;
6 | import org.springframework.web.bind.annotation.ResponseBody;
7 | import javax.servlet.ServletException;
8 | import javax.servlet.http.HttpServletResponse;
9 | import java.io.IOException;
10 | /**
11 | * 统一异常处理类
12 | */
13 | @ControllerAdvice
14 | public class BaseExceptionHandler {
15 |
16 | @ExceptionHandler(value = Exception.class)
17 | @ResponseBody
18 | public Result error(Exception e){
19 | e.printStackTrace();
20 | return new Result(false, StatusCode.ERROR,e.getMessage());
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/controller/UserController.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.controller;
2 | import java.util.HashMap;
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.data.domain.Page;
8 | import org.springframework.data.redis.core.RedisTemplate;
9 | import org.springframework.web.bind.annotation.CrossOrigin;
10 | import org.springframework.web.bind.annotation.PathVariable;
11 | import org.springframework.web.bind.annotation.RequestBody;
12 | import org.springframework.web.bind.annotation.RequestMapping;
13 | import org.springframework.web.bind.annotation.RequestMethod;
14 | import org.springframework.web.bind.annotation.RestController;
15 |
16 | import com.tensquare.user.pojo.User;
17 | import com.tensquare.user.service.UserService;
18 |
19 | import entity.PageResult;
20 | import entity.Result;
21 | import entity.StatusCode;
22 | import util.JwtUtil;
23 |
24 | /**
25 | * 控制器层
26 | * @author Administrator
27 | *
28 | */
29 | @RestController
30 | @CrossOrigin
31 | @RequestMapping("/user")
32 | public class UserController {
33 |
34 | @Autowired
35 | private UserService userService;
36 |
37 | @Autowired
38 | private RedisTemplate redisTemplate;
39 |
40 | @Autowired
41 | private JwtUtil jwtUtil;
42 |
43 |
44 |
45 | /**
46 | * 更新好友粉丝数和用户关注数
47 | * @param user
48 | * @return
49 | */
50 | @RequestMapping(value="/{userid}/{friendid}/{x}",method = RequestMethod.PUT)
51 | public void updatefanscountandfollowcount(@PathVariable String userid,@PathVariable String friendid,@PathVariable int x){
52 | userService.updatefanscountandfollowcount(x,userid,friendid);
53 | }
54 |
55 |
56 |
57 |
58 | @RequestMapping(value="/login",method = RequestMethod.POST)
59 | public Result login(@RequestBody User user){
60 | user = userService.login(user.getMobile(),user.getPassword());
61 | if(user==null){
62 | return new Result(false,StatusCode.LOGINERROR,"登录失败");
63 | }
64 | String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user");
65 | Map map = new HashMap<>();
66 | map.put("token",token);
67 | map.put("roles","user");
68 | return new Result(true,StatusCode.OK,"登录成功",map);
69 | }
70 |
71 |
72 | /**
73 | * 发送短信验证码
74 | */
75 | @RequestMapping(value="/sendsms/{mobile}",method = RequestMethod.POST)
76 | public Result sendSms(@PathVariable String mobile){
77 | userService.sendSms(mobile);
78 | return new Result(true,StatusCode.OK,"发送成功");
79 | }
80 |
81 | /**
82 | * 注册
83 | * @return
84 | */
85 | @RequestMapping(value="/register/{code}",method = RequestMethod.POST)
86 | public Result regist(@PathVariable String code,@RequestBody User user){
87 | //得到缓存中的验证码
88 | String checkcodeRedis = (String)redisTemplate.opsForValue().get("checkcode_"+ user.getMobile());
89 | if(!checkcodeRedis.isEmpty()){
90 | return new Result(false,StatusCode.OK,"请先获取手机验证码");
91 | }
92 | if(checkcodeRedis.equals(code)){
93 | return new Result(false,StatusCode.OK,"请输入正确验证码");
94 | }
95 | userService.add(user);
96 | return new Result(true,StatusCode.OK,"注册成功");
97 | }
98 |
99 |
100 | /**
101 | * 查询全部数据
102 | * @return
103 | */
104 | @RequestMapping(method= RequestMethod.GET)
105 | public Result findAll(){
106 | return new Result(true,StatusCode.OK,"查询成功",userService.findAll());
107 | }
108 |
109 | /**
110 | * 根据ID查询
111 | * @param id ID
112 | * @return
113 | */
114 | @RequestMapping(value="/{id}",method= RequestMethod.GET)
115 | public Result findById(@PathVariable String id){
116 | return new Result(true,StatusCode.OK,"查询成功",userService.findById(id));
117 | }
118 |
119 |
120 | /**
121 | * 分页+多条件查询
122 | * @param searchMap 查询条件封装
123 | * @param page 页码
124 | * @param size 页大小
125 | * @return 分页结果
126 | */
127 | @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
128 | public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
129 | Page pageList = userService.findSearch(searchMap, page, size);
130 | return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
131 | }
132 |
133 | /**
134 | * 根据条件查询
135 | * @param searchMap
136 | * @return
137 | */
138 | @RequestMapping(value="/search",method = RequestMethod.POST)
139 | public Result findSearch( @RequestBody Map searchMap){
140 | return new Result(true,StatusCode.OK,"查询成功",userService.findSearch(searchMap));
141 | }
142 |
143 | /**
144 | * 增加
145 | * @param user
146 | */
147 | @RequestMapping(method=RequestMethod.POST)
148 | public Result add(@RequestBody User user ){
149 | userService.add(user);
150 | return new Result(true,StatusCode.OK,"增加成功");
151 | }
152 |
153 | /**
154 | * 修改
155 | * @param user
156 | */
157 | @RequestMapping(value="/{id}",method= RequestMethod.PUT)
158 | public Result update(@RequestBody User user, @PathVariable String id ){
159 | user.setId(id);
160 | userService.update(user);
161 | return new Result(true,StatusCode.OK,"修改成功");
162 | }
163 |
164 | /**
165 | * 删除 必须有admin角色才能删除
166 | * @param id
167 | */
168 | @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
169 | public Result delete(@PathVariable String id ){
170 | userService.deleteById(id);
171 | return new Result(true,StatusCode.OK,"删除成功");
172 | }
173 |
174 | }
175 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/dao/AdminDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.dao;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5 |
6 | import com.tensquare.user.pojo.Admin;
7 | /**
8 | * 数据访问接口
9 | * @author Administrator
10 | *
11 | */
12 | public interface AdminDao extends JpaRepository,JpaSpecificationExecutor{
13 | public Admin findByLoginname(String loginname);
14 | }
15 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/dao/UserDao.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.dao;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5 |
6 | import com.tensquare.user.pojo.User;
7 | import org.springframework.data.jpa.repository.Modifying;
8 | import org.springframework.data.jpa.repository.Query;
9 |
10 | /**
11 | * 数据访问接口
12 | * @author Administrator
13 | *
14 | */
15 | public interface UserDao extends JpaRepository,JpaSpecificationExecutor{
16 | public User findByMobile(String mobile);
17 |
18 | @Modifying
19 | @Query(value="update tb_user set fanscount=fanscount+? where id=?",nativeQuery = true)
20 | public void updatefanscount(int x,String friendid);
21 |
22 | @Modifying
23 | @Query(value="update tb_user set followcount=followcount+? where id=?",nativeQuery = true)
24 | public void updatefollowcount(int x,String userid);
25 | }
26 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/interceptor/JwtInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.interceptor;
2 |
3 |
4 | import io.jsonwebtoken.Claims;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Component;
7 | import org.springframework.web.servlet.HandlerInterceptor;
8 | import util.JwtUtil;
9 |
10 | import javax.persistence.metamodel.SetAttribute;
11 | import javax.servlet.http.HttpServletRequest;
12 | import javax.servlet.http.HttpServletResponse;
13 |
14 | @Component
15 | public class JwtInterceptor implements HandlerInterceptor {
16 | @Autowired
17 | private JwtUtil jwtUtil;
18 |
19 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
20 | System.out.println("经过了拦截器");
21 | //无论如何都放行,具体能不能操作还是在具体的操作中去判断
22 | //拦截器只是负责把头请求中包含token的令牌进行一个解析验证
23 |
24 | String header = request.getHeader("Authorization");
25 |
26 | if(header!=null && !"".equals(header)){
27 | //如果有包含Authorization头信息,就对其进行解析
28 | if(header.startsWith("Bearer")){
29 | //得到token
30 | String token = header.substring(7);
31 | //验证令牌
32 | try{
33 | Claims claims = jwtUtil.parseJWT(token);
34 | String roles = (String) claims.get("roles");
35 | if(roles!=null && roles.equals("admin")){
36 | request.setAttribute("claims_admin",token);
37 | }
38 | if(roles!=null && roles.equals("user")){
39 | request.setAttribute("claims_user",token);
40 | }
41 | }catch(Exception e){
42 | throw new RuntimeException("令牌不正确!");
43 | }
44 | }
45 | }
46 |
47 | return true;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/pojo/Admin.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.pojo;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.Id;
5 | import javax.persistence.Table;
6 | import java.io.Serializable;
7 | /**
8 | * 实体类
9 | * @author Administrator
10 | *
11 | */
12 | @Entity
13 | @Table(name="tb_admin")
14 | public class Admin implements Serializable{
15 |
16 | @Id
17 | private String id;//ID
18 |
19 |
20 |
21 | private String loginname;//登陆名称
22 | private String password;//密码
23 | private String state;//状态
24 |
25 |
26 | public String getId() {
27 | return id;
28 | }
29 | public void setId(String id) {
30 | this.id = id;
31 | }
32 |
33 | public String getLoginname() {
34 | return loginname;
35 | }
36 | public void setLoginname(String loginname) {
37 | this.loginname = loginname;
38 | }
39 |
40 | public String getPassword() {
41 | return password;
42 | }
43 | public void setPassword(String password) {
44 | this.password = password;
45 | }
46 |
47 | public String getState() {
48 | return state;
49 | }
50 | public void setState(String state) {
51 | this.state = state;
52 | }
53 |
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/pojo/User.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.pojo;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.Id;
5 | import javax.persistence.Table;
6 | import java.io.Serializable;
7 | /**
8 | * 实体类
9 | * @author Administrator
10 | *
11 | */
12 | @Entity
13 | @Table(name="tb_user")
14 | public class User implements Serializable{
15 |
16 | @Id
17 | private String id;//ID
18 |
19 |
20 |
21 | private String mobile;//手机号码
22 | private String password;//密码
23 | private String nickname;//昵称
24 | private String sex;//性别
25 | private java.util.Date birthday;//出生年月日
26 | private String avatar;//头像
27 | private String email;//E-Mail
28 | private java.util.Date regdate;//注册日期
29 | private java.util.Date updatedate;//修改日期
30 | private java.util.Date lastdate;//最后登陆日期
31 | private Long online;//在线时长(分钟)
32 | private String interest;//兴趣
33 | private String personality;//个性
34 | private Integer fanscount;//粉丝数
35 | private Integer followcount;//关注数
36 |
37 |
38 | public String getId() {
39 | return id;
40 | }
41 | public void setId(String id) {
42 | this.id = id;
43 | }
44 |
45 | public String getMobile() {
46 | return mobile;
47 | }
48 | public void setMobile(String mobile) {
49 | this.mobile = mobile;
50 | }
51 |
52 | public String getPassword() {
53 | return password;
54 | }
55 | public void setPassword(String password) {
56 | this.password = password;
57 | }
58 |
59 | public String getNickname() {
60 | return nickname;
61 | }
62 | public void setNickname(String nickname) {
63 | this.nickname = nickname;
64 | }
65 |
66 | public String getSex() {
67 | return sex;
68 | }
69 | public void setSex(String sex) {
70 | this.sex = sex;
71 | }
72 |
73 | public java.util.Date getBirthday() {
74 | return birthday;
75 | }
76 | public void setBirthday(java.util.Date birthday) {
77 | this.birthday = birthday;
78 | }
79 |
80 | public String getAvatar() {
81 | return avatar;
82 | }
83 | public void setAvatar(String avatar) {
84 | this.avatar = avatar;
85 | }
86 |
87 | public String getEmail() {
88 | return email;
89 | }
90 | public void setEmail(String email) {
91 | this.email = email;
92 | }
93 |
94 | public java.util.Date getRegdate() {
95 | return regdate;
96 | }
97 | public void setRegdate(java.util.Date regdate) {
98 | this.regdate = regdate;
99 | }
100 |
101 | public java.util.Date getUpdatedate() {
102 | return updatedate;
103 | }
104 | public void setUpdatedate(java.util.Date updatedate) {
105 | this.updatedate = updatedate;
106 | }
107 |
108 | public java.util.Date getLastdate() {
109 | return lastdate;
110 | }
111 | public void setLastdate(java.util.Date lastdate) {
112 | this.lastdate = lastdate;
113 | }
114 |
115 | public Long getOnline() {
116 | return online;
117 | }
118 | public void setOnline(Long online) {
119 | this.online = online;
120 | }
121 |
122 | public String getInterest() {
123 | return interest;
124 | }
125 | public void setInterest(String interest) {
126 | this.interest = interest;
127 | }
128 |
129 | public String getPersonality() {
130 | return personality;
131 | }
132 | public void setPersonality(String personality) {
133 | this.personality = personality;
134 | }
135 |
136 | public Integer getFanscount() {
137 | return fanscount;
138 | }
139 | public void setFanscount(Integer fanscount) {
140 | this.fanscount = fanscount;
141 | }
142 |
143 | public Integer getFollowcount() {
144 | return followcount;
145 | }
146 | public void setFollowcount(Integer followcount) {
147 | this.followcount = followcount;
148 | }
149 |
150 |
151 |
152 | }
153 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/java/com/tensquare/user/service/AdminService.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.user.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collection;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import javax.persistence.criteria.CriteriaBuilder;
9 | import javax.persistence.criteria.CriteriaQuery;
10 | import javax.persistence.criteria.Expression;
11 | import javax.persistence.criteria.Predicate;
12 | import javax.persistence.criteria.Root;
13 | import javax.persistence.criteria.Selection;
14 |
15 | import org.springframework.beans.factory.annotation.Autowired;
16 | import org.springframework.data.domain.Page;
17 | import org.springframework.data.domain.PageRequest;
18 | import org.springframework.data.domain.Sort;
19 | import org.springframework.data.jpa.domain.Specification;
20 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
21 | import org.springframework.stereotype.Service;
22 |
23 | import util.IdWorker;
24 |
25 | import com.tensquare.user.dao.AdminDao;
26 | import com.tensquare.user.pojo.Admin;
27 |
28 | /**
29 | * 服务层
30 | *
31 | * @author Administrator
32 | *
33 | */
34 | @Service
35 | public class AdminService {
36 |
37 | @Autowired
38 | private AdminDao adminDao;
39 |
40 | @Autowired
41 | private IdWorker idWorker;
42 |
43 | @Autowired
44 | private BCryptPasswordEncoder encoder;
45 |
46 |
47 | public Admin login(Admin admin){
48 | //先根据用户名查询对象
49 | Admin adminLogin = adminDao.findByLoginname(admin.getLoginname());
50 | //然后拿数据库中的密码和用户输入的密码匹配
51 | if(adminLogin!=null && encoder.matches(admin.getPassword(),adminLogin.getPassword())){
52 | //密码匹配后,登录成功
53 | return adminLogin;
54 | }
55 | //登录失败
56 | return null;
57 | }
58 |
59 | /**
60 | * 查询全部列表
61 | * @return
62 | */
63 | public List findAll() {
64 | return adminDao.findAll();
65 | }
66 |
67 |
68 | /**
69 | * 条件查询+分页
70 | * @param whereMap
71 | * @param page
72 | * @param size
73 | * @return
74 | */
75 | public Page findSearch(Map whereMap, int page, int size) {
76 | Specification specification = createSpecification(whereMap);
77 | PageRequest pageRequest = PageRequest.of(page-1, size);
78 | return adminDao.findAll(specification, pageRequest);
79 | }
80 |
81 |
82 | /**
83 | * 条件查询
84 | * @param whereMap
85 | * @return
86 | */
87 | public List findSearch(Map whereMap) {
88 | Specification specification = createSpecification(whereMap);
89 | return adminDao.findAll(specification);
90 | }
91 |
92 | /**
93 | * 根据ID查询实体
94 | * @param id
95 | * @return
96 | */
97 | public Admin findById(String id) {
98 | return adminDao.findById(id).get();
99 | }
100 |
101 | /**
102 | * 增加
103 | * @param admin
104 | */
105 | public void add(Admin admin) {
106 | admin.setId( idWorker.nextId()+"" );
107 | //密码加密
108 | admin.setPassword(encoder.encode(admin.getPassword()));
109 | adminDao.save(admin);
110 | }
111 |
112 | /**
113 | * 修改
114 | * @param admin
115 | */
116 | public void update(Admin admin) {
117 | adminDao.save(admin);
118 | }
119 |
120 | /**
121 | * 删除
122 | * @param id
123 | */
124 | public void deleteById(String id) {
125 | adminDao.deleteById(id);
126 | }
127 |
128 | /**
129 | * 动态条件构建
130 | * @param searchMap
131 | * @return
132 | */
133 | private Specification createSpecification(Map searchMap) {
134 |
135 | return new Specification() {
136 |
137 | @Override
138 | public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
139 | List predicateList = new ArrayList();
140 | // ID
141 | if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
142 | predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
143 | }
144 | // 登陆名称
145 | if (searchMap.get("loginname")!=null && !"".equals(searchMap.get("loginname"))) {
146 | predicateList.add(cb.like(root.get("loginname").as(String.class), "%"+(String)searchMap.get("loginname")+"%"));
147 | }
148 | // 密码
149 | if (searchMap.get("password")!=null && !"".equals(searchMap.get("password"))) {
150 | predicateList.add(cb.like(root.get("password").as(String.class), "%"+(String)searchMap.get("password")+"%"));
151 | }
152 | // 状态
153 | if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
154 | predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
155 | }
156 |
157 | return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
158 |
159 | }
160 | };
161 |
162 | }
163 |
164 | }
165 |
--------------------------------------------------------------------------------
/tensquare_user/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9008
3 | spring:
4 | application:
5 | name: tensquare-user #指定服务名
6 | datasource:
7 | driverClassName: com.mysql.jdbc.Driver
8 | url: jdbc:mysql://192.168.213.135:3306/tensquare_user?characterEncoding=UTF8
9 | username: root
10 | password: root
11 | jpa:
12 | database: MySQL
13 | show-sql: true
14 | redis:
15 | host: 192.168.213.135
16 | rabbitmq:
17 | host: 192.168.213.135
18 |
19 | jwt:
20 | config:
21 | key: itcast
22 | ttl: 3600000
23 |
24 | eureka:
25 | client:
26 | service-url:
27 | defaultZone: http://127.0.0.1:6868/eureka/
28 | instance:
29 | prefer-ip-address: true
30 |
--------------------------------------------------------------------------------
/tensquare_web/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | tensquare_parent52
7 | com.tensquare
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tensquare_web
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter-netflix-eureka-client
18 |
19 |
20 | org.springframework.cloud
21 | spring-cloud-starter-netflix-zuul
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/tensquare_web/src/main/java/com/tensquare/web/WebApplication.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.web;
2 |
3 |
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
7 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
8 |
9 | @SpringBootApplication
10 | @EnableEurekaClient
11 | @EnableZuulProxy
12 | public class WebApplication {
13 | public static void main(String[] args) {
14 | SpringApplication.run(WebApplication.class,args);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/tensquare_web/src/main/java/com/tensquare/web/filter/WebFilter.java:
--------------------------------------------------------------------------------
1 | package com.tensquare.web.filter;
2 |
3 | import com.netflix.zuul.ZuulFilter;
4 | import com.netflix.zuul.context.RequestContext;
5 | import com.netflix.zuul.exception.ZuulException;
6 | import org.springframework.stereotype.Component;
7 |
8 | import javax.servlet.http.HttpServletRequest;
9 |
10 | @Component
11 | public class WebFilter extends ZuulFilter {
12 | @Override
13 | public String filterType() {
14 | return "pre";
15 | }
16 |
17 | @Override
18 | public int filterOrder() {
19 | return 0;
20 | }
21 |
22 | @Override
23 | public boolean shouldFilter() {
24 | return true;
25 | }
26 |
27 | @Override
28 | public Object run() throws ZuulException {
29 | //得到request上下文
30 | RequestContext currentContext = RequestContext.getCurrentContext();
31 | //得到request域
32 | HttpServletRequest request = currentContext.getRequest();
33 | //得到头信息
34 | String header = request.getHeader("Authorization");
35 | //判断是否有头信息
36 | if(header!=null && !"".equals(header)){
37 | //把头信息继续往下传
38 | currentContext.addZuulRequestHeader("Authorization",header);
39 | }
40 | return null;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/tensquare_web/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 9012
3 | spring:
4 | application:
5 | name: tensquare-web
6 | eureka:
7 | client:
8 | service-url:
9 | defaultZone: http://127.0.0.1:6868/eureka/
10 | instance:
11 | prefer-ip-address: true
12 |
13 | zuul:
14 | routes:
15 | tensquare-base:
16 | path: /base/**
17 | serviceId: tensquare-base
18 | tensquare-user:
19 | path: /user/**
20 | serviceId: tensquare-user
21 | tensquare-qa:
22 | path: /qa/**
23 | serviceId: tensquare-qa
--------------------------------------------------------------------------------