BaseProjectController.java
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jflyfox.component.base;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jflyfox.api.constant.ApiConstant;
import com.jflyfox.api.form.ApiForm;
import com.jflyfox.api.form.ApiResp;
import com.jflyfox.api.service.ApiCache;
import com.jflyfox.component.util.JFlyFoxUtils;
import com.jflyfox.jfinal.base.BaseController;
import com.jflyfox.jfinal.base.SessionUser;
import com.jflyfox.jfinal.component.util.Attr;
import com.jflyfox.modules.admin.article.ArticleConstant;
import com.jflyfox.modules.admin.folder.FolderService;
import com.jflyfox.modules.admin.site.SessionSite;
import com.jflyfox.modules.admin.site.SiteConstant;
import com.jflyfox.modules.admin.site.SiteService;
import com.jflyfox.modules.admin.site.TbSite;
import com.jflyfox.system.file.model.FileUploadBean;
import com.jflyfox.system.file.service.FileUploadService;
import com.jflyfox.system.file.util.FileUploadUtils;
import com.jflyfox.system.log.SysLog;
import com.jflyfox.system.menu.SysMenu;
import com.jflyfox.system.user.SysUser;
import com.jflyfox.system.user.UserSvc;
import com.jflyfox.util.NumberUtils;
import com.jflyfox.util.StrUtils;
import com.jflyfox.util.cache.Cache;
import com.jflyfox.util.cache.CacheManager;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.Map;
/**
* 项目BaseControler
*
* @author flyfox
* @date 2015-08-02
*/
public abstract class BaseProjectController extends BaseController {
Cache cache = CacheManager.get("JFLYFOX_SESSION");
@Override
public void renderAuto(String view) {
String path = getAutoPath(view);
super.render(path);
}
@Override
public void redirectAuto(String view) {
String path = getAutoPath(view);
super.redirect(path);
}
@Override
protected String getAutoPath(String view) {
String path = view;
if (!view.startsWith("/")) {
path = "/" + path;
}
// path = (isMoblie() ? Attr.PATH_MOBILE : Attr.PATH_PC) + path;
TbSite site = new SiteService().getSite(getSessionSite().getSiteId());
path = SiteConstant.getTemplatePath() + (isMoblie() ? site.getTemplateMobile() : site.getTemplate()) + path;
if (view.startsWith("/")) {
path = "/" + path;
}
path = path.replace("//", "/");
return path;
}
public SysUser currUser(String key) {
return ApiCache.getCache(key);
}
/**
* 获取当前用户 并鉴权
*
* @param form
* @return
*/
public SysUser getSysUser(ApiForm form) {
SysUser user = currUser(form.getCheckSum());
if (user == null) {
renderJson(new ApiResp(form).setCode(ApiConstant.CODE_LOGIN_VALID_ERROR).setMsg(ApiConstant.MSG_LOGIN_VALID_ERROR));
return null;
}
return user;
}
/**
* 方法重写
* <p>
* 2015年8月2日 下午3:17:29 flyfox 369191470@qq.com
*
* @return
*/
@Override
@SuppressWarnings("rawtypes")
public SessionUser getSessionUser() {
SysUser sysUser = getSessionAttr(Attr.SESSION_NAME);
try {
// 如果session没有,cookie有~那么就设置到Session
if (sysUser == null) {
String cookieContent = getCookie(Attr.SESSION_NAME);
if (cookieContent != null) {
String key = JFlyFoxUtils.cookieDecrypt(cookieContent);
if (StrUtils.isNotEmpty(key) && key.split(",").length == 2) {
int userid = NumberUtils.parseInt(key.split(",")[0]);
String password = key.split(",")[1];
sysUser = SysUser.dao.findFirstByWhere(" where userid = ? and password = ? ", userid, password);
if (sysUser != null)
setSessionUser(sysUser);
}
}
}
} catch (Exception e) {
// 异常cookie重新登陆
removeSessionAttr(Attr.SESSION_NAME);
removeCookie(Attr.SESSION_NAME);
log.error("cooke user异常:", e);
return null;
}
return sysUser;
}
/**
* 方法重写
* <p>
* 2015年8月2日 下午3:17:29 flyfox 369191470@qq.com
*
* @return
*/
@Override
@SuppressWarnings("rawtypes")
public SessionUser setSessionUser(SessionUser user) {
// 单站点设置
if (!SiteConstant.isMultiSite()) {
TbSite defaultSite = new SiteService().getDefaultSite();
user.set("back_site_id", defaultSite.getId());
}
setSessionAttr(Attr.SESSION_NAME, user);
// 设置cookie,用id+password作为
SysUser sysUser = (SysUser) user;
String key = sysUser.getUserid() + "," + user.getStr("password");
HttpSession session = getSession();
session.setMaxInactiveInterval(2 * 60 * 60);
String cookieContent = JFlyFoxUtils.cookieEncrypt(key);
setCookie(Attr.SESSION_NAME, cookieContent, 2 * 60 * 60);
// 如果是管理员 设置菜单权限
if (user.getInt("usertype") == 1 || user.getInt("usertype") == 2) {
Map<Integer, List<SysMenu>> map = new UserSvc().getAuthMap(sysUser);
// 注入菜单
setSessionAttr("menu", map);
// 不能访问的菜单
setSessionAttr("nomenu", new UserSvc().getNoAuthMap(map));
}
return user;
}
/**
* 方法重写
* <p>
* 2015年8月2日 下午3:17:29 flyfox 369191470@qq.com
*
* @return
*/
@Override
public void removeSessionUser() {
removeSessionAttr(Attr.SESSION_NAME);
// 删除cookie
removeCookie(Attr.SESSION_NAME);
}
/**
* 用户登录,登出记录
* <p>
* 2015年10月16日 下午2:36:39 flyfox 369191470@qq.com
*
* @param user
* @param operType
*/
protected void saveLog(SysUser user, String operType) {
try {
String tableName = user.getTable().getName();
Integer updateId = user.getInt("update_id");
String updateTime = user.getStr("update_time");
String sql = "INSERT INTO `sys_log` ( `log_type`, `oper_object`, `oper_table`," //
+ " `oper_id`, `oper_type`, `oper_remark`, `create_time`, `create_id`) " //
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
Db.update(sql, SysLog.TYPE_SYSTEM, SysLog.getTableRemark(tableName), tableName, //
updateId, operType, "", updateTime, updateId);
} catch (Exception e) {
log.error("添加日志失败", e);
}
}
// ///////////////////栏目处理////////////
// 获取用户设置的SITE对象,设置默认
public SessionSite getSessionSite() {
SessionSite sessionSite = getSessionAttr(SiteConstant.getSessionSite());
// 获取用户设置的SITE对象,设置默认
if (sessionSite == null) {
sessionSite = new SessionSite();
TbSite defaultSite = new SiteService().getDefaultSite();
sessionSite.setModel(defaultSite);
sessionSite.setSiteId(defaultSite.getId());
setSessionAttr(SiteConstant.getSessionSite(), sessionSite);
}
return sessionSite;
}
public SessionSite setSessionSite(SessionSite sessionSite) {
setSessionAttr(SiteConstant.getSessionSite(), sessionSite);
return sessionSite;
}
@SuppressWarnings("rawtypes")
public TbSite getBackSite() {
SessionUser user = getSessionUser();
if (user == null) {
return null;
}
TbSite site = new SiteService().getSite(user.getBackSiteId());
return site;
}
public String selectFolder(Integer selected) {
return new FolderService().selectFolder(selected, getSessionUser().getBackSiteId());
}
public String selectFolderExcludeMe(Integer selected, Integer selfId) {
return new FolderService().selectFolder(selected, selfId, getSessionUser().getBackSiteId());
}
/**
* 公共文章查询sql
* <p>
* 2016年3月19日 下午7:03:11 flyfox 369191470@qq.com
*
* @return
*/
public String getPublicWhere() {
return " t.status = " + JFlyFoxUtils.STATUS_SHOW //
+ " and t.approve_status = " + ArticleConstant.APPROVE_STATUS_PASS // 审核通过
+ " and t.type in (11,12) " // 查询状态为显示,类型是预览和正常的文章
;
}
public Controller setSessionAttrCache(String key, Object value) {
String id = getSession().getId();
cache.add(key + "_" + id, value);
return this;
}
public <T> T getSessionAttrCache(String key) {
String id = getSession().getId();
return cache.get(key + "_" + id);
}
public Controller removeSessionAttrCache(String key) {
String id = getSession().getId();
cache.remove(key + "_" + id);
return this;
}
/**
* 是否是管理员
* <p>
* 2017年1月21日 下午11:55:16 flyfox 369191470@qq.com
*
* @param user
* @return
*/
@SuppressWarnings("rawtypes")
public boolean isAdmin(SessionUser user) {
return user.getInt("usertype") == 1;
}
/**
* 文件上传处理
* <p>
* 2017年4月5日 上午4:36:20 flyfox 369191470@qq.com
*
* @param site
* @param uploadFile
* @param appendPath
* @return
*/
public String uploadHandler(TbSite site, File uploadFile, String appendPath) {
String fileUrl = "";
String projectStorePath = FileUploadUtils.getUploadPath(site, appendPath);
FileUploadBean uploadBean = new FileUploadService().uploadHandle(projectStorePath, uploadFile, getSessionUser()
.getUserid());
if (uploadBean != null) {
fileUrl = projectStorePath + File.separator + uploadBean.getName();
}
return FileUploadUtils.rebuild(fileUrl);
}
}