commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.core.aop; |
|
26 |
|
|
27 |
import cn.hutool.core.bean.BeanUtil; |
|
28 |
import cn.hutool.core.util.ArrayUtil; |
|
29 |
import cn.hutool.core.util.ObjectUtil; |
|
30 |
import cn.hutool.log.Log; |
|
31 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
32 |
import org.aspectj.lang.ProceedingJoinPoint; |
|
33 |
import org.aspectj.lang.annotation.Around; |
|
34 |
import org.aspectj.lang.annotation.Aspect; |
|
35 |
import org.aspectj.lang.annotation.Pointcut; |
|
36 |
import org.aspectj.lang.reflect.MethodSignature; |
|
37 |
import org.springframework.core.annotation.Order; |
|
38 |
import vip.xiaonuo.core.annotion.Wrapper; |
|
39 |
import vip.xiaonuo.core.consts.AopSortConstant; |
|
40 |
import vip.xiaonuo.core.exception.ServiceException; |
|
41 |
import vip.xiaonuo.core.exception.enums.WrapperExceptionEnum; |
|
42 |
import vip.xiaonuo.core.pojo.base.wrapper.BaseWrapper; |
|
43 |
import vip.xiaonuo.core.pojo.page.PageResult; |
|
44 |
import vip.xiaonuo.core.pojo.response.ResponseData; |
|
45 |
|
|
46 |
import java.lang.reflect.Array; |
|
47 |
import java.lang.reflect.Method; |
|
48 |
import java.util.ArrayList; |
|
49 |
import java.util.Collection; |
|
50 |
import java.util.Map; |
|
51 |
|
|
52 |
/** |
|
53 |
* controller结果包装的aop |
|
54 |
* |
|
55 |
* @author xuyuxiang |
|
56 |
* @date 2020/7/24 17:42 |
|
57 |
*/ |
|
58 |
@Aspect |
|
59 |
@Order(AopSortConstant.WRAPPER_AOP) |
|
60 |
public class WrapperAop { |
|
61 |
|
|
62 |
private static final Log log = Log.get(); |
|
63 |
|
|
64 |
/** |
|
65 |
* 切入点 |
|
66 |
* |
|
67 |
* @author xuyuxiang |
|
68 |
* @date 2020/7/24 17:42 |
|
69 |
*/ |
|
70 |
@Pointcut("@annotation(vip.xiaonuo.core.annotion.Wrapper)") |
|
71 |
private void wrapperPointcut() { |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* 执行具体的包装过程 |
|
76 |
* |
|
77 |
* @author xuyuxiang |
|
78 |
* @date 2020/7/24 17:44 |
|
79 |
*/ |
|
80 |
@Around("wrapperPointcut()") |
|
81 |
public Object doWrapper(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
|
82 |
|
|
83 |
// 直接执行原有业务逻辑 |
|
84 |
Object proceedResult = proceedingJoinPoint.proceed(); |
|
85 |
|
|
86 |
return processWrapping(proceedingJoinPoint, proceedResult); |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* 具体包装过程 |
|
91 |
* |
|
92 |
* @author xuyuxiang |
|
93 |
* @date 2020/7/24 17:53 |
|
94 |
*/ |
|
95 |
@SuppressWarnings("all") |
|
96 |
private Object processWrapping(ProceedingJoinPoint proceedingJoinPoint, Object originResult) throws IllegalAccessException, InstantiationException { |
|
97 |
|
|
98 |
// 获取@Wrapper注解 |
|
99 |
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); |
|
100 |
Method method = methodSignature.getMethod(); |
|
101 |
Wrapper wrapperAnnotation = method.getAnnotation(Wrapper.class); |
|
102 |
|
|
103 |
// 获取注解上的处理类 |
|
104 |
Class<? extends BaseWrapper<?>>[] baseWrapperClasses = wrapperAnnotation.value(); |
|
105 |
|
|
106 |
// 如果注解上的为空直接返回 |
|
107 |
if (ObjectUtil.isEmpty(baseWrapperClasses)) { |
|
108 |
return originResult; |
|
109 |
} |
|
110 |
|
|
111 |
// 获取原有返回结果,如果不是ResponseData则不进行处理(需要遵守这个约定) |
|
112 |
if (!(originResult instanceof ResponseData)) { |
|
113 |
log.warn(">>> 当前请求的返回结果不是ResponseData类型,直接返回原值!"); |
|
114 |
return originResult; |
|
115 |
} |
|
116 |
|
|
117 |
// 获取ResponseData中的值 |
|
118 |
ResponseData responseData = (ResponseData) originResult; |
|
119 |
Object beWrapped = responseData.getData(); |
|
120 |
|
|
121 |
// 如果是基本类型,不进行加工处理 |
|
122 |
if (ObjectUtil.isBasicType(beWrapped)) { |
|
123 |
throw new ServiceException(WrapperExceptionEnum.BASIC_TYPE_ERROR); |
|
124 |
} |
|
125 |
|
|
126 |
// 如果是Page类型 |
|
127 |
if (beWrapped instanceof Page) { |
|
128 |
|
|
129 |
// 获取Page原有对象 |
|
130 |
Page page = (Page) beWrapped; |
|
131 |
|
|
132 |
// 将page中所有records都包装一遍 |
|
133 |
ArrayList<Map<String, Object>> maps = new ArrayList<>(); |
|
134 |
for (Object wrappedItem : page.getRecords()) { |
|
135 |
maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses)); |
|
136 |
} |
|
137 |
|
|
138 |
page.setRecords(maps); |
|
139 |
responseData.setData(page); |
|
140 |
} |
|
141 |
|
|
142 |
// 如果是PageResult类型 |
|
143 |
else if (beWrapped instanceof PageResult) { |
|
144 |
|
|
145 |
// 获取PageResult原有对象 |
|
146 |
PageResult pageResult = (PageResult) beWrapped; |
|
147 |
|
|
148 |
// 将PageResult中所有rows都包装一遍 |
|
149 |
ArrayList<Map<String, Object>> maps = new ArrayList<>(); |
|
150 |
for (Object wrappedItem : pageResult.getRows()) { |
|
151 |
maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses)); |
|
152 |
} |
|
153 |
|
|
154 |
pageResult.setRows(maps); |
|
155 |
responseData.setData(pageResult); |
|
156 |
} |
|
157 |
|
|
158 |
// 如果是List类型 |
|
159 |
else if (beWrapped instanceof Collection) { |
|
160 |
|
|
161 |
// 获取原有的List |
|
162 |
Collection collection = (Collection) beWrapped; |
|
163 |
|
|
164 |
// 将page中所有records都包装一遍 |
|
165 |
ArrayList<Map<String, Object>> maps = new ArrayList<>(); |
|
166 |
for (Object wrappedItem : collection) { |
|
167 |
maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses)); |
|
168 |
} |
|
169 |
|
|
170 |
responseData.setData(maps); |
|
171 |
} |
|
172 |
|
|
173 |
// 如果是Array类型 |
|
174 |
else if (ArrayUtil.isArray(beWrapped)) { |
|
175 |
|
|
176 |
// 获取原有的Array |
|
177 |
Object[] objects = this.objToArray(beWrapped); |
|
178 |
|
|
179 |
// 将array中所有records都包装一遍 |
|
180 |
ArrayList<Map<String, Object>> maps = new ArrayList<>(); |
|
181 |
for (Object wrappedItem : objects) { |
|
182 |
maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses)); |
|
183 |
} |
|
184 |
|
|
185 |
responseData.setData(maps); |
|
186 |
} |
|
187 |
|
|
188 |
// 如果是Object类型 |
|
189 |
else { |
|
190 |
responseData.setData(this.wrapPureObject(beWrapped, baseWrapperClasses)); |
|
191 |
} |
|
192 |
|
|
193 |
|
|
194 |
return responseData; |
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* 原始对象包装成一个map的过程 |
|
199 |
* <p> |
|
200 |
* 期间多次根据BaseWrapper接口方法执行包装过程 |
|
201 |
* |
|
202 |
* @author xuyuxiang |
|
203 |
* @date 2020/7/24 21:40 |
|
204 |
*/ |
|
205 |
@SuppressWarnings("all") |
|
206 |
private Map<String, Object> wrapPureObject(Object originModel, Class<? extends BaseWrapper<?>>[] baseWrapperClasses) { |
|
207 |
|
|
208 |
// 首先将原始的对象转化为map |
|
209 |
Map<String, Object> originMap = BeanUtil.beanToMap(originModel); |
|
210 |
|
|
211 |
// 经过多个包装类填充属性 |
|
212 |
try { |
|
213 |
for (Class<? extends BaseWrapper<?>> baseWrapperClass : baseWrapperClasses) { |
|
214 |
BaseWrapper baseWrapper = baseWrapperClass.newInstance(); |
|
215 |
Map<String, Object> incrementFieldsMap = baseWrapper.doWrap(originModel); |
|
216 |
originMap.putAll(incrementFieldsMap); |
|
217 |
} |
|
218 |
} catch (Exception e) { |
|
219 |
log.error(">>> 原始对象包装过程,字段转化异常:{}", e.getMessage()); |
|
220 |
throw new ServiceException(WrapperExceptionEnum.TRANSFER_ERROR); |
|
221 |
} |
|
222 |
|
|
223 |
return originMap; |
|
224 |
} |
|
225 |
|
|
226 |
/** |
|
227 |
* Object转为一个array,确保object为数组类型 |
|
228 |
* |
|
229 |
* @author xuyuxiang |
|
230 |
* @date 2020/7/24 22:06 |
|
231 |
*/ |
|
232 |
private Object[] objToArray(Object object) { |
|
233 |
int length = Array.getLength(object); |
|
234 |
Object[] result = new Object[length]; |
|
235 |
for (int i = 0; i < result.length; i++) { |
|
236 |
result[i] = Array.get(object, i); |
|
237 |
} |
|
238 |
return result; |
|
239 |
} |
|
240 |
|
|
241 |
} |