ArticleService.java 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. package space.apps.moudules.website.services;
  2. import java.io.File;
  3. import java.net.URLEncoder;
  4. import java.sql.Timestamp;
  5. import java.text.ParseException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.LinkedHashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Set;
  13. import com.google.gson.JsonObject;
  14. import com.google.gson.JsonParser;
  15. import org.apache.logging.log4j.LogManager;
  16. import org.apache.logging.log4j.Logger;
  17. import org.jsoup.Jsoup;
  18. import org.jsoup.nodes.Document;
  19. import org.jsoup.nodes.Element;
  20. import org.jsoup.select.Elements;
  21. import apps.pojo.Article;
  22. import apps.pojo.ArticleCopy;
  23. import apps.pojo.ArticleJumpTypeCode;
  24. import apps.pojo.Website;
  25. import apps.pojo.WebsiteNavigation;
  26. import space.apps.common.ConstVars.dataStatusCode;
  27. import space.apps.moudules.sys.utils.ChangeLogUtil;
  28. import space.apps.moudules.website.common.ConstVars.articleJumpTypeCode;
  29. import space.apps.moudules.website.common.ConstVars.articleStatusCode;
  30. import space.apps.moudules.website.template.util.WebsiteTemplateArticleUtil;
  31. import space.apps.moudules.website.template.util.WebsiteTemplateNavUtil;
  32. import space.apps.moudules.website.template.util.WebsiteUtil;
  33. import space.apps.moudules.website.util.AppWebsiteManageUtil;
  34. import space.core.apps.common.BaseTransaction;
  35. import space.core.apps.common.ConstVars;
  36. import space.core.apps.common.ScheduleImpl;
  37. import space.core.apps.common.bean.AppUser;
  38. import space.core.apps.common.bean.NextService;
  39. import space.core.apps.common.bean.ResponsePackage;
  40. import space.core.apps.util.GdbaQueryUtil;
  41. import space.core.apps.util.NextServiceUtil;
  42. import space.core.apps.util.WebEditorUtil;
  43. import space.core.common.utils.DateTimeUtil;
  44. import space.core.common.utils.FileUtil;
  45. import space.core.common.utils.GsonUtil;
  46. import space.core.common.utils.PojoUtil;
  47. import space.core.common.utils.StringUtil;
  48. import space.core.db.jdbc.GeneralDBAccess;
  49. import space.core.db.jdbc.SqlCommandImpl;
  50. /**
  51. * 文章服务类
  52. *
  53. * @author SPACE
  54. * @date 2020年02月08日
  55. */
  56. public class ArticleService extends BaseTransaction {
  57. static Logger log = LogManager.getLogger(ArticleService.class);
  58. private static final long serialVersionUID = 1L;
  59. private static final String ARTICLE_PREVIEW_SESSION_PREFIX = "articlePreviewPath_";
  60. private static final Long NAVIGATION_STYLE_GRAPHIC_LIST = 10010L;
  61. private static final String COLLEGE_RED_TEMPLATE_ID_CODE = "ffkjcollegered1";
  62. private static final String ARTICLE_PUBLISHED_HISTORY_ROOT = "/web/upload/article-history/";
  63. private static final String PUBLISHED_CONTENT_SNAPSHOT_FIELD = "publishedContentSnapshot";
  64. private static final String PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD = "publishedEditContentSnapshot";
  65. /**
  66. * 转换文章内容 html 里的链接
  67. *
  68. * @param path
  69. * @param domain
  70. * @return
  71. * @throws Exception
  72. * @author 陈锋
  73. * @log 2020年10月16日
  74. */
  75. private String converArticleContentHtmlLink(String path, String domain) throws Exception {
  76. File input = new File(path);
  77. Document doc = Jsoup.parse(input, "UTF-8", "");
  78. replaceHtmlLink(doc, domain);
  79. imgSrcConverLazyloadSrc(doc);
  80. String html = doc.select("body").eq(0).html();
  81. String articleTemplate = FileUtil.readFile(
  82. getWebRootPath() + space.apps.moudules.website.common.ConstVars.ARTICLE_RUN_FILE_TEMPLATE_PATH);
  83. articleTemplate = articleTemplate
  84. .replace(space.apps.moudules.website.common.ConstVars.ARTICLE_RUN_FILE_TEMPLATE_CONTENT_KEY, html);
  85. return articleTemplate;
  86. }
  87. /**
  88. * 替换 html 的链接
  89. *
  90. * @param doc
  91. * @param domain
  92. * @author 陈锋
  93. * @log 2020年10月16日
  94. */
  95. private void replaceHtmlLink(Document doc, String domain) {
  96. Elements srcEles = doc.select("[src]");
  97. Elements hrefEles = doc.select("a[href]");
  98. replaceAttrPrefix(srcEles, "src", domain);
  99. replaceAttrPrefix(hrefEles, "href", domain);
  100. }
  101. /**
  102. * 替换属性的前缀
  103. *
  104. * @param attrEles 属性标签集合
  105. * @param attr 属性名
  106. * @param prefix 前缀
  107. * @author 陈锋
  108. * @log 2020年10月16日
  109. */
  110. private void replaceAttrPrefix(Elements attrEles, String attr, String prefix) {
  111. for (Element ele : attrEles) {
  112. String attrValue = ele.attr(attr);
  113. if (StringUtil.isNull(attrValue)) {
  114. continue;
  115. }
  116. if (shouldKeepOriginalAttrValue(attrValue)) {
  117. continue;
  118. }
  119. if (prefix.endsWith("/") && attrValue.startsWith("/")) {
  120. attrValue = prefix + attrValue.substring(1);
  121. } else {
  122. attrValue = prefix + attrValue;
  123. }
  124. ele.attr(attr, attrValue);
  125. }
  126. }
  127. private boolean shouldKeepOriginalAttrValue(String attrValue) {
  128. if (StringUtil.isNull(attrValue)) {
  129. return true;
  130. }
  131. String value = attrValue.trim().toLowerCase();
  132. return value.startsWith("http://") || value.startsWith("https://") || value.startsWith("//")
  133. || value.startsWith("data:") || value.startsWith("blob:") || value.startsWith("mailto:")
  134. || value.startsWith("tel:") || value.startsWith("javascript:") || value.startsWith("#");
  135. }
  136. /**
  137. * img 标签的 src 转为 jquery.lazyload.js 的懒加载的 data-original 属性
  138. *
  139. * @param doc
  140. * @author 陈锋
  141. * @log 2020年10月16日
  142. */
  143. private void imgSrcConverLazyloadSrc(Document doc) {
  144. Elements srcEles = doc.select("img[src]");
  145. int i = 0;
  146. for (Element ele : srcEles) {
  147. i++;
  148. if (i <= 3) {
  149. continue;
  150. }
  151. String srcValue = ele.attr("src");
  152. ele.removeAttr("src");
  153. ele.attr("data-original", srcValue);
  154. }
  155. }
  156. /**
  157. * 保留手填摘要;未填写摘要时允许为空,不再从正文自动提取。
  158. *
  159. * @param article 文章
  160. * @param htmlContent html 正文内容
  161. * @author 陈锋
  162. * @log 2020年10月19日
  163. */
  164. private void saveArticleDesc(Article article, String htmlContent) {
  165. }
  166. /**
  167. * 文章录入
  168. *
  169. * @return
  170. * @throws Exception
  171. * @author SPACE
  172. * @date 2020年02月09日
  173. */
  174. public String add() throws Exception {
  175. AppUser appUser = getAppUser();
  176. GeneralDBAccess gdba = getGdbaInstance();
  177. Timestamp nowTimestamp = DateTimeUtil.getNowTimestamp();
  178. String top = getParameter("top");
  179. String timeSwitch = getParameter("timeSwitch");
  180. String content = getParameterNotXSS("body");
  181. Article article = (Article) PojoUtil.formatRequest(getRequest(), Article.class);
  182. String navIdList = getParameter("navIdList");
  183. WebsiteNavigation wn = getArticleTargetNavigation(gdba, article.getNavigationid(), "发布栏目");
  184. if (wn == null) {
  185. return DEFAULT_FORWARD;
  186. }
  187. if (!verifyGraphicListThumbnailRequired(gdba, wn, article, "发布栏目")) {
  188. return DEFAULT_FORWARD;
  189. }
  190. if (!verifyArticleCopyNavigationList(gdba, navIdList, article)) {
  191. return DEFAULT_FORWARD;
  192. }
  193. String editPath = WebEditorUtil.createHtmlFile(content);
  194. String runContent = converArticleContentHtmlLink(getWebRootPath() + editPath,
  195. space.apps.moudules.website.common.ConstVars.domain);
  196. String runPath = WebEditorUtil.createHtmlFile(runContent);
  197. article.setContent(runPath);// 运行文件
  198. article.setEditContent(editPath);// 编辑文件
  199. article.setUnitid(appUser.getUnitId());
  200. article.setDcode(dataStatusCode.INSERT.getCode());
  201. article.setCreatePid(appUser.getUserId());
  202. article.setLastChangePid(appUser.getUserId());
  203. article.setCreateTime(nowTimestamp);
  204. // article.setReleaseTime(nowTimestamp);
  205. article.setLastChangeTime(nowTimestamp);
  206. article.setSticktop("yes".equals(top) ? 1L : 0L);
  207. article.setAscode(articleStatusCode.STAY_RELEASE.getCode());// 必填的
  208. article.setWebsiteid(wn.getWebsiteid());
  209. article.setRelease("yes".equals(timeSwitch) ? 1L : 0L);
  210. if (!article.getAjtcode().equals(10020L)) {
  211. article.setUrl(WebsiteTemplateArticleUtil.getArticleURL(wn));
  212. }
  213. // if (StringUtil.isNull(article.getReleaseTime())) {
  214. // article.setReleaseTime(nowTimestamp);
  215. // }
  216. saveArticleDesc(article, content);
  217. gdba.insertByBean(article);
  218. saveArticleCopy(gdba, article, appUser, navIdList);
  219. initArticleSticktopSchedule(gdba, article, appUser);
  220. initArticleReleaseTaskSchedule(gdba, article, appUser);
  221. gdba.updateById(article);
  222. setArticleApplyService(article.getArticleid());
  223. gdba.commit();
  224. setResponsePackage(getResponseSuccessMsgPackage());
  225. return DEFAULT_FORWARD;
  226. }
  227. /**
  228. * 设置文章申请服务
  229. *
  230. * @param articleid 文章 id
  231. * @author 陈锋
  232. * @log 2020年12月8日 下午8:28:18
  233. */
  234. private void setArticleApplyService(Long articleid) {
  235. // 设置下一个服务对象
  236. if (isNextServiceFlag()) {
  237. Map<String, Object> paramsMap = NextServiceUtil.newHashMap();
  238. paramsMap.put("articleid", articleid);
  239. NextService nextService = NextServiceUtil.getLayer("website.article_start", "sys.common_audit.apply", paramsMap);
  240. nextService.setRefresh(true); // 需要刷新
  241. setNextService(nextService);
  242. }
  243. }
  244. /**
  245. * 文章修改
  246. *
  247. * @return
  248. * @throws Exception
  249. * @author 陈锋
  250. * @log 2020年9月18日
  251. */
  252. public String modify() throws Exception {
  253. GeneralDBAccess gdba = getGdbaInstance();
  254. AppUser appUser = getAppUser();
  255. Article a2 = new Article();
  256. a2.setArticleid(getParameterToLong("articleid"));
  257. gdba.selectById(a2);
  258. if (!Long.valueOf(dataStatusCode.INSERT.getCode()).equals(a2.getDcode())) {
  259. // setResponsePackage(getResponseErrorMsgPackage());
  260. StringBuilder msgBuilder = new StringBuilder();
  261. msgBuilder.append("[").append(DateTimeUtil.getNowDate()).append("] - 文章修改,ip为:")
  262. .append(getRequest().getLocalAddr()).append(" 篡改的文章 id 为:").append(a2.getArticleid())
  263. .append(",文章标题为:").append(a2.getTitle());
  264. log.error(msgBuilder.toString());
  265. return ERROR_FORWARD;
  266. }
  267. Timestamp nowTimestamp = DateTimeUtil.getNowTimestamp();
  268. String top = getParameter("top");
  269. String timeSwitch = getParameter("timeSwitch");
  270. String content = getParameterNotXSS("body");
  271. String navIdList = getParameter("navIdList");
  272. Article article = new Article();
  273. article.setArticleid(a2.getArticleid());
  274. gdba.selectById(article);
  275. PojoUtil.formatRequest(getRequest(), article);
  276. WebsiteNavigation wn = getArticleTargetNavigation(gdba, article.getNavigationid(), "发布栏目");
  277. if (wn == null) {
  278. return DEFAULT_FORWARD;
  279. }
  280. if (!verifyGraphicListThumbnailRequired(gdba, wn, article, "发布栏目")) {
  281. return DEFAULT_FORWARD;
  282. }
  283. if (!verifyArticleCopyNavigationList(gdba, navIdList, article)) {
  284. return DEFAULT_FORWARD;
  285. }
  286. article.setSticktop("yes".equals(top) ? 1L : 0L);
  287. ChangeLogUtil.addChangeLog(gdba, appUser, "Article", a2.getArticleid(), GsonUtil.toJson(a2));
  288. FileUtil.writeFile(content, getWebRootPath() + a2.getEditContent());// 编辑文件,获取页面上的正文内容
  289. String runContent = converArticleContentHtmlLink(getWebRootPath() + a2.getEditContent(),
  290. space.apps.moudules.website.common.ConstVars.domain);
  291. FileUtil.writeFile(runContent, getWebRootPath() + a2.getContent());// 运行文件,从编辑文件里转换为运行文件
  292. article.setLastChangePid(appUser.getUserId());
  293. article.setLastChangeTime(nowTimestamp);
  294. article.setWebsiteid(wn.getWebsiteid());
  295. article.setRelease("yes".equals(timeSwitch) ? 1L : 0L);
  296. // 旧的文章是定时发布的,新的文章不是定时发布的,则需要把发布时间设置为空
  297. if (a2.getRelease().equals(1L) && !"yes".equals(timeSwitch)) {
  298. article.setReleaseTime(null);
  299. }
  300. verifyArticleUrl(article, a2, wn);
  301. saveArticleDesc(article, content);
  302. gdba.updateById(article);
  303. saveArticleCopy(gdba, article, appUser, navIdList);
  304. initArticleSticktopSchedule(gdba, article, appUser);
  305. initArticleReleaseTaskSchedule(gdba, article, appUser);
  306. gdba.updateById(article);
  307. setArticleApplyService(article.getArticleid());
  308. gdba.commit();
  309. setResponsePackage(getResponseSuccessMsgPackage());
  310. return DEFAULT_FORWARD;
  311. }
  312. /**
  313. * 根据栏目 id 获取栏目对象
  314. *
  315. * @param gdba
  316. * @param navigationId 栏目 id
  317. * @return
  318. * @throws Exception
  319. * @author 陈锋
  320. * @log 2020年9月26日
  321. */
  322. private WebsiteNavigation getWebsiteNavigationById(GeneralDBAccess gdba, Long navigationId) throws Exception {
  323. WebsiteNavigation wn = new WebsiteNavigation();
  324. wn.setNavigationid(navigationId);
  325. gdba.selectById(wn);
  326. return wn;
  327. }
  328. private WebsiteNavigation getArticleTargetNavigation(GeneralDBAccess gdba, Long navigationId, String targetName)
  329. throws Exception {
  330. if (navigationId == null || navigationId <= 0) {
  331. setResponsePackage(getResponseWarningMsgPackage("请选择" + targetName));
  332. return null;
  333. }
  334. WebsiteNavigation wn = getWebsiteNavigationById(gdba, navigationId);
  335. if (!isArticleTargetNavigation(wn)) {
  336. setResponsePackage(getResponseWarningMsgPackage(targetName + "必须选择可发布文章的栏目"));
  337. return null;
  338. }
  339. if (hasEnabledChildNavigation(gdba, navigationId)) {
  340. setResponsePackage(getResponseWarningMsgPackage(targetName + "必须选择最下级栏目,请继续向右选择最后一级栏目"));
  341. return null;
  342. }
  343. return wn;
  344. }
  345. private boolean verifyArticleCopyNavigationList(GeneralDBAccess gdba, String navIdList, Article article)
  346. throws Exception {
  347. if (StringUtil.isNull(navIdList)) {
  348. return true;
  349. }
  350. Set<String> navIdSet = new HashSet<String>();
  351. String[] navIdAry = navIdList.split(",");
  352. for (String navId : navIdAry) {
  353. if (StringUtil.isNull(navId) || navIdSet.contains(navId)) {
  354. continue;
  355. }
  356. navIdSet.add(navId);
  357. WebsiteNavigation wn = getArticleTargetNavigation(gdba, Long.valueOf(navId), "抄送栏目");
  358. if (wn == null) {
  359. return false;
  360. }
  361. if (!verifyGraphicListThumbnailRequired(gdba, wn, article, "抄送栏目")) {
  362. return false;
  363. }
  364. }
  365. return true;
  366. }
  367. private boolean verifyGraphicListThumbnailRequired(GeneralDBAccess gdba, WebsiteNavigation wn, Article article,
  368. String targetName) throws Exception {
  369. if (wn == null || !NAVIGATION_STYLE_GRAPHIC_LIST.equals(wn.getWnlscode())) {
  370. return true;
  371. }
  372. if (article != null && StringUtil.notNull(article.getThumbnail())) {
  373. return true;
  374. }
  375. setResponsePackage(getResponseWarningMsgPackage(targetName + "为图文列表时必须上传缩略图"));
  376. return false;
  377. }
  378. private boolean isCollegeRedGraphicListNavigation(GeneralDBAccess gdba, WebsiteNavigation wn) throws Exception {
  379. if (wn == null || !NAVIGATION_STYLE_GRAPHIC_LIST.equals(wn.getWnlscode())) {
  380. return false;
  381. }
  382. return isCollegeRedTemplateNavigation(gdba, wn);
  383. }
  384. private boolean isCollegeRedTemplateNavigation(GeneralDBAccess gdba, WebsiteNavigation wn) throws Exception {
  385. Long wtid = wn == null ? null : wn.getWtid();
  386. if (wtid == null && wn != null && wn.getWebsiteid() != null) {
  387. Website website = WebsiteUtil.getWebsiteByID(gdba, wn.getWebsiteid());
  388. wtid = website == null ? null : website.getWtid();
  389. }
  390. if (wtid == null) {
  391. return false;
  392. }
  393. return isCollegeRedTemplateIdCode(gdba, wtid);
  394. }
  395. private boolean isCollegeRedTemplateIdCode(GeneralDBAccess gdba, Long wtid) throws Exception {
  396. String sql = " select id_code from website_template where wtid = :wtid ";
  397. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  398. sqlCmd.setString("wtid", wtid);
  399. Map<String, Object> templateMap = gdba.selectAsMap(sqlCmd);
  400. Object idCode = templateMap == null ? null : templateMap.get("id_code");
  401. return idCode != null && idCode.toString().indexOf(COLLEGE_RED_TEMPLATE_ID_CODE) >= 0;
  402. }
  403. private boolean isArticleTargetNavigation(WebsiteNavigation wn) {
  404. if (wn == null || wn.getWntcode() == null) {
  405. return false;
  406. }
  407. return isArticleTargetWntcode(wn.getWntcode());
  408. }
  409. private boolean isArticleTargetWntcode(Object wntcode) {
  410. if (wntcode == null) {
  411. return false;
  412. }
  413. Long wntcodeLong = Long.valueOf(wntcode.toString());
  414. return Long.valueOf(10010L).equals(wntcodeLong) || Long.valueOf(10060L).equals(wntcodeLong);
  415. }
  416. private boolean hasEnabledChildNavigation(GeneralDBAccess gdba, Long navigationId) throws Exception {
  417. String sql = " select count(*) as c from website_navigation where dcode = 61 and parent_nid = :navigationid ";
  418. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  419. sqlCmd.setString("navigationid", navigationId);
  420. Map<String, Object> resultMap = gdba.selectAsMap(sqlCmd);
  421. return Integer.parseInt(resultMap.get("c").toString()) > 0;
  422. }
  423. /**
  424. * 验证文章的 url 是否需要生成新的、或使用旧的、或使用页面上的
  425. *
  426. * @param newArticle 新文章对象(也就是更新的)
  427. * @param oldArticle 旧文章对象
  428. * @param newArticleNavigation 新文章的的栏目对象
  429. * @throws Exception
  430. * @author 陈锋
  431. * @log 2020年10月11日
  432. */
  433. private void verifyArticleUrl(Article newArticle, Article oldArticle, WebsiteNavigation newArticleNavigation)
  434. throws Exception {
  435. // 新、旧都是一样
  436. if (oldArticle.getAjtcode().equals(newArticle.getAjtcode())) {
  437. // 都是正文、无动作,则使用旧的 url
  438. if (!oldArticle.getAjtcode().equals(10020L)) {// 不是链接
  439. newArticle.setUrl(oldArticle.getUrl());// 使用旧的 url
  440. }
  441. // 都是链接时,则使用页面的,使用页面的不用重新设置 url
  442. }
  443. // 旧的是链接,新的不是链接时,则需要生成 url
  444. else if (oldArticle.getAjtcode().equals(10020L) && !newArticle.getAjtcode().equals(10020L)) {
  445. newArticle.setUrl(WebsiteTemplateArticleUtil.getArticleURL(newArticleNavigation));// 生成新的 url
  446. }
  447. // 新的为无动作,则使用旧的
  448. else if (newArticle.getAjtcode().equals(10030L)) {
  449. newArticle.setUrl(oldArticle.getUrl());// 使用旧的 url
  450. }
  451. // url 为空时,则用旧的
  452. else if (newArticle.getUrl() == null) {
  453. newArticle.setUrl(oldArticle.getUrl());// 使用旧的 url
  454. }
  455. }
  456. /**
  457. * 设置文章变动申请服务
  458. *
  459. * @param articleid 文章 id
  460. * @author 陈锋
  461. * @log 2020年12月8日 下午8:28:18
  462. */
  463. private void setArticleChangeApplyService(Long articleid) {
  464. // 设置下一个服务对象
  465. if (isNextServiceFlag()) {
  466. Map<String, Object> paramsMap = NextServiceUtil.newHashMap();
  467. paramsMap.put("articleid", articleid);
  468. NextService nextService = NextServiceUtil.getLayer("website.article_start_change", "sys.common_audit.change_apply", paramsMap);
  469. nextService.setRefresh(true); // 需要刷新
  470. setNextService(nextService);
  471. }
  472. }
  473. /**
  474. * 文章变动
  475. *
  476. * @return
  477. * @throws Exception
  478. * @author SPACE
  479. * @date 2020年02月09日
  480. */
  481. public String change() throws Exception {
  482. AppUser appUser = getAppUser();
  483. GeneralDBAccess gdba = getGdbaInstance();
  484. Timestamp nowTimestamp = DateTimeUtil.getNowTimestamp();
  485. String top = getParameter("top");
  486. String timeSwitch = getParameter("timeSwitch");
  487. String content = getParameterNotXSS("body");
  488. String navIdList = getParameter("navIdList");
  489. Article article = new Article();
  490. article.setArticleid(getParameterToLong("articleid"));
  491. gdba.selectById(article);
  492. PojoUtil.formatRequest(getRequest(), article);
  493. WebsiteNavigation wn = getArticleTargetNavigation(gdba, article.getNavigationid(), "发布栏目");
  494. if (wn == null) {
  495. return TIPS_FORWARD;
  496. }
  497. if (!verifyGraphicListThumbnailRequired(gdba, wn, article, "发布栏目")) {
  498. return TIPS_FORWARD;
  499. }
  500. if (!verifyArticleCopyNavigationList(gdba, navIdList, article)) {
  501. return TIPS_FORWARD;
  502. }
  503. article.setSticktop("yes".equals(top) ? 1L : 0L);
  504. Article a2 = new Article();
  505. a2.setArticleid(article.getArticleid());
  506. gdba.selectById(a2);
  507. ChangeLogUtil.addChangeLog(gdba, appUser, "Article", a2.getArticleid(),
  508. buildArticleChangeLogContent(a2, nowTimestamp));
  509. verifyArticleUrl(article, a2, wn);
  510. FileUtil.writeFile(content, getWebRootPath() + a2.getEditContent());// 编辑文件,获取页面上的正文内容
  511. String runContent = converArticleContentHtmlLink(getWebRootPath() + a2.getEditContent(),
  512. space.apps.moudules.website.common.ConstVars.domain);
  513. FileUtil.writeFile(runContent, getWebRootPath() + a2.getContent());// 运行文件,从编辑文件里转换为运行文件
  514. article.setDcode(dataStatusCode.STAY_SUBMIT.getCode());
  515. article.setLastChangePid(appUser.getUserId());
  516. article.setLastChangeTime(nowTimestamp);
  517. article.setWebsiteid(wn.getWebsiteid());
  518. article.setRelease("yes".equals(timeSwitch) ? 1L : 0L);
  519. // 旧的文章设置了定时发布、发布时间,为到指定的发布时间时
  520. if (articleStatusCode.STAY_RELEASE.getCode().equals(a2.getAscode()) && a2.getRelease().equals(1L)
  521. && StringUtil.notNull(a2.getReleaseTaskSchedulesid())) {
  522. // 新的文章没有设置定时发布
  523. if (article.getRelease().equals(0L)) {
  524. article.setReleaseTime(null);
  525. }
  526. }
  527. saveArticleDesc(article, content);
  528. saveArticleCopy(gdba, article, appUser, navIdList);
  529. initArticleSticktopSchedule(gdba, article, appUser);
  530. initArticleReleaseTaskSchedule(gdba, article, appUser);
  531. gdba.updateById(article);
  532. setArticleChangeApplyService(article.getArticleid());
  533. gdba.commit();
  534. setResponsePackage(getResponseSuccessMsgPackage());
  535. return TIPS_FORWARD;
  536. }
  537. private String buildArticleChangeLogContent(Article article, Timestamp snapshotTime) throws Exception {
  538. JsonObject logContent = new JsonParser().parse(GsonUtil.toJson(article)).getAsJsonObject();
  539. if (!isPublishedArticle(article)) {
  540. return logContent.toString();
  541. }
  542. String contentSnapshot = copyPublishedArticleFile(article.getArticleid(), article.getContent(), "content",
  543. snapshotTime);
  544. if (StringUtil.notNull(contentSnapshot)) {
  545. logContent.addProperty(PUBLISHED_CONTENT_SNAPSHOT_FIELD, contentSnapshot);
  546. }
  547. String editContentSnapshot = copyPublishedArticleFile(article.getArticleid(), article.getEditContent(),
  548. "edit-content", snapshotTime);
  549. if (StringUtil.notNull(editContentSnapshot)) {
  550. logContent.addProperty(PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD, editContentSnapshot);
  551. }
  552. return logContent.toString();
  553. }
  554. private boolean isPublishedArticle(Article article) {
  555. return Long.valueOf(dataStatusCode.USE.getCode()).equals(article.getDcode())
  556. && articleStatusCode.HAS_RELEASE.getCode().equals(article.getAscode());
  557. }
  558. private String copyPublishedArticleFile(Long articleid, String filePath, String fileType, Timestamp snapshotTime)
  559. throws Exception {
  560. if (StringUtil.isNull(filePath)) {
  561. return null;
  562. }
  563. File source = new File(getWebRootPath() + filePath);
  564. if (!source.exists() || !source.isFile()) {
  565. return null;
  566. }
  567. String snapshotDir = ARTICLE_PUBLISHED_HISTORY_ROOT + articleid + "/";
  568. FileUtil.makeDirectory(new File(getWebRootPath() + snapshotDir));
  569. String fileExt = FileUtil.getFileExt(filePath);
  570. if (StringUtil.isNull(fileExt)) {
  571. fileExt = "html";
  572. } else if (fileExt.startsWith(".")) {
  573. fileExt = fileExt.substring(1);
  574. }
  575. String snapshotPath = snapshotDir + DateTimeUtil.formatDate(snapshotTime, "yyyyMMddHHmmssSSS") + "-"
  576. + fileType + "." + fileExt;
  577. FileUtil.copyFile(source.getAbsolutePath(), getWebRootPath() + snapshotPath);
  578. return snapshotPath;
  579. }
  580. /**
  581. * 删除指定文章的所有抄送记录
  582. *
  583. * @param gdba
  584. * @param article
  585. * @throws Exception
  586. * @author SPACE
  587. * @date 2020年02月09日
  588. */
  589. private void delArticleCopy(GeneralDBAccess gdba, Article article) throws Exception {
  590. String sql = " delete from article_copy where articleid = :articleid ";
  591. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  592. sqlCmd.setString("articleid", article.getArticleid());
  593. gdba.modify(sqlCmd);
  594. }
  595. /**
  596. * 查询指定文章所抄送的栏目列表
  597. *
  598. * @param gdba
  599. * @param article
  600. * @return
  601. * @throws Exception
  602. * @author SPACE
  603. * @date 2020年02月09日
  604. */
  605. private List<Map<String, Object>> getArticleCopyList(GeneralDBAccess gdba, Article article) throws Exception {
  606. String sql = " SELECT wn.navigationid, wn.name, wn.parent_nid, ac.dcode FROM article_copy ac, website_navigation wn where ac.navigationid =" +
  607. " wn.navigationid and ac.articleid = :articleid ";
  608. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  609. sqlCmd.setString("articleid", article.getArticleid());
  610. return gdba.selectAsMapList(sqlCmd);
  611. }
  612. /**
  613. * 获取可以抄送的栏目
  614. *
  615. * @throws Exception
  616. * @author SPACE
  617. * @date 2020年02月09日
  618. */
  619. public void getCopyNavigation() throws Exception {
  620. GeneralDBAccess gdba = getGdbaInstance();
  621. AppUser appUser = getAppUser();
  622. List<Map<String, Object>> navigationList = AppWebsiteManageUtil.getManageNavigationListByAppUser(gdba, appUser,
  623. 10010L);
  624. ResponsePackage rp = getResponseSuccessDataPackage(getNavigationMap(gdba, navigationList));
  625. write(rp.toJsonWriteNonStringValueAsString());
  626. // getWriter().write(FastjsonUtil.toStringJSON(getResponseSuccessDataPackage(getNavigationMap(gdba, navigationList))));
  627. }
  628. /**
  629. * 获取可以发布的栏目
  630. *
  631. * @throws Exception
  632. * @author SPACE
  633. * @date 2020年02月08日
  634. */
  635. public void getIssueNavigation() throws Exception {
  636. GeneralDBAccess gdba = getGdbaInstance();
  637. AppUser appUser = getAppUser();
  638. List<Map<String, Object>> navigationList = AppWebsiteManageUtil.getManageNavigationListByAppUser(gdba, appUser,
  639. 10000L);
  640. ResponsePackage rsdp = getResponseSuccessDataPackage(getNavigationMap(gdba, navigationList));
  641. write(rsdp.toJsonWriteNonStringValueAsString());
  642. }
  643. /**
  644. * 将栏目的名称修改为:上级栏目链/栏目名称
  645. *
  646. * @param gdba
  647. * @param navigationList
  648. * @throws Exception
  649. * @author SPACE
  650. * @date 2020年02月27日
  651. */
  652. void getNavAllParentName(GeneralDBAccess gdba, List<Map<String, Object>> navigationList) throws Exception {
  653. for (Map<String, Object> map : navigationList) {
  654. Object parNid = map.get("parent_nid");
  655. if (StringUtil.isNull(parNid)) {
  656. continue;
  657. }
  658. map.put("name", getNavAllParentName(gdba, Long.valueOf(map.get("navigationid").toString())));
  659. }
  660. }
  661. /**
  662. * 根据栏目ID获取栏目上级链名称
  663. *
  664. * @param gdba 数据库
  665. * @param navID 栏目ID
  666. * @throws Exception
  667. * @author SPACE
  668. * @date 2020年02月27日
  669. */
  670. String getNavAllParentName(GeneralDBAccess gdba, Long navID) throws Exception {
  671. List<WebsiteNavigation> navParentList = WebsiteTemplateNavUtil.getNavParentList(gdba, navID);
  672. if (navParentList.size() == 1) {
  673. return navParentList.get(0).getName();
  674. }
  675. StringBuilder nameBuilder = new StringBuilder();
  676. for (WebsiteNavigation wn : navParentList) {
  677. nameBuilder.append(wn.getName()).append(" / ");
  678. }
  679. return nameBuilder.substring(0, nameBuilder.length() - 2);
  680. }
  681. Map<String, Object> getNavigationMap(GeneralDBAccess gdba, List<Map<String, Object>> navigationList)
  682. throws Exception {
  683. Map<String, Object> retMap = new HashMap<String, Object>();
  684. if (navigationList == null || navigationList.size() == 0) {
  685. retMap.put("data", new ArrayList<Map<String, Object>>());
  686. return retMap;
  687. }
  688. Set<String> websiteSet = new HashSet<String>();
  689. Map<String, List<Map<String, Object>>> websiteNavigationMap = new LinkedHashMap<String, List<Map<String, Object>>>();
  690. for (Map<String, Object> map : navigationList) {
  691. String website = map.get("websiteid").toString();
  692. websiteSet.add(website);
  693. List<Map<String, Object>> websiteNavigationList = websiteNavigationMap.get(website);
  694. if (websiteNavigationList == null) {
  695. websiteNavigationList = new ArrayList<Map<String, Object>>();
  696. websiteNavigationMap.put(website, websiteNavigationList);
  697. }
  698. websiteNavigationList.add(map);
  699. }
  700. if (websiteSet.size() > 1) {
  701. List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
  702. for (String websiteId : websiteNavigationMap.keySet()) {
  703. Website w = new Website();
  704. w.setWebsiteid(Long.valueOf(websiteId));
  705. gdba.selectById(w);
  706. Map<String, Object> map = new HashMap<String, Object>();
  707. map.put("navigationid", "-1");
  708. map.put("name", w.getName());
  709. map.put("children", buildNavigationTree(websiteNavigationMap.get(websiteId)));
  710. dataList.add(map);
  711. }
  712. retMap.put("data", dataList);
  713. } else {
  714. retMap.put("data", buildNavigationTree(navigationList));
  715. }
  716. return retMap;
  717. }
  718. List<Map<String, Object>> buildNavigationTree(List<Map<String, Object>> navigationList) {
  719. Map<String, Map<String, Object>> navigationMap = new LinkedHashMap<String, Map<String, Object>>();
  720. for (Map<String, Object> navigation : navigationList) {
  721. Map<String, Object> item = new HashMap<String, Object>(navigation);
  722. item.remove("children");
  723. navigationMap.put(item.get("navigationid").toString(), item);
  724. }
  725. List<Map<String, Object>> rootList = new ArrayList<Map<String, Object>>();
  726. for (Map<String, Object> navigation : navigationMap.values()) {
  727. Object parentNid = navigation.get("parent_nid");
  728. Map<String, Object> parent = parentNid == null ? null : navigationMap.get(parentNid.toString());
  729. if (parent == null) {
  730. rootList.add(navigation);
  731. continue;
  732. }
  733. @SuppressWarnings("unchecked")
  734. List<Map<String, Object>> children = (List<Map<String, Object>>) parent.get("children");
  735. if (children == null) {
  736. children = new ArrayList<Map<String, Object>>();
  737. parent.put("children", children);
  738. }
  739. children.add(navigation);
  740. }
  741. setDisabledNavigationList(rootList);
  742. return rootList;
  743. }
  744. private void setDisabledNavigationList(List<Map<String, Object>> navigationList) {
  745. for (Map<String, Object> navigation : navigationList) {
  746. @SuppressWarnings("unchecked")
  747. List<Map<String, Object>> children = (List<Map<String, Object>>) navigation.get("children");
  748. if (children != null && children.size() > 0) {
  749. setDisabledNavigationList(children);
  750. continue;
  751. }
  752. if (!isArticleTargetWntcode(navigation.get("wntcode"))) {
  753. navigation.put("disabled", true);
  754. }
  755. }
  756. }
  757. /**
  758. * 文章信息
  759. *
  760. * @return
  761. * @throws Exception
  762. * @author SPACE
  763. * @date 2020年02月09日
  764. */
  765. public String info() throws Exception {
  766. Article article = (Article) PojoUtil.formatRequest(getRequest(), Article.class);
  767. GeneralDBAccess gdba = getGdbaInstance();
  768. gdba.selectById(article);
  769. List<Map<String, Object>> articleCopyList = getArticleCopyList(gdba, article);
  770. getNavAllParentName(gdba, articleCopyList);
  771. String navAllName = getNavAllParentName(gdba, article.getNavigationid());
  772. Website website = new Website();
  773. website.setWebsiteid(article.getWebsiteid());
  774. gdba.selectById(website);
  775. if (!articleJumpTypeCode.CUSTOM_LINK.getCode().equals(article.getAjtcode())
  776. || (articleJumpTypeCode.CUSTOM_LINK.getCode().equals(article.getAjtcode())
  777. && article.getUrl().startsWith("/"))) {
  778. // 设置查看文章时的 url
  779. String url = appendWebsiteDomain(website.getPort().toString(), website.getDomains(), website.getProtocol(),
  780. article.getUrl());
  781. article.setUrl(url);
  782. }
  783. boolean isShowModifyReleaseTime = false;// 是否显示修改发布时间,默认不显示
  784. // 变动时的发布时间,在用状态、已发布
  785. if (Long.valueOf(dataStatusCode.USE.getCode()).equals(article.getDcode())
  786. && articleStatusCode.HAS_RELEASE.getCode().equals(article.getAscode())) {
  787. isShowModifyReleaseTime = true;
  788. }
  789. // 修改时的发布时间,录入状态、待发布、不是定时发布
  790. if (Long.valueOf(dataStatusCode.INSERT.getCode()).equals(article.getDcode()) && article.getRelease() == 0L) {
  791. isShowModifyReleaseTime = true;
  792. }
  793. boolean isShowpreview = false;// 是否显示预览,默认不显示
  794. // 文章数据状态为 在用,文章状态 已发布
  795. if (article.getDcode().equals(Long.valueOf(dataStatusCode.USE.getCode()))
  796. && articleStatusCode.HAS_RELEASE.getCode().equals(article.getAscode())) {
  797. // 文章动作 跳转正文页 或 自定义链接
  798. if (article.getAjtcode().equals(articleJumpTypeCode.MAIN_BODY.getCode())
  799. || article.getAjtcode().equals(articleJumpTypeCode.CUSTOM_LINK.getCode())) {
  800. isShowpreview = true;
  801. }
  802. }
  803. setRequestAttribute("isShowpreview", isShowpreview);// 是否显示预览
  804. setRequestAttribute("isShowModifyReleaseTime", isShowModifyReleaseTime);// 是否显示修改发布时间
  805. setRequestAttribute("article", article);
  806. setRequestAttribute("navAllName", navAllName);
  807. setRequestAttribute("copyNav", GsonUtil.toJson(articleCopyList));
  808. try {
  809. setRequestAttribute("body", FileUtil.readFile(getWebRootPath() + article.getContent()));
  810. } catch (Exception e) {
  811. setRequestAttribute("body", "内容文件不存在或已被删除");
  812. }
  813. try {
  814. setRequestAttribute("editBody", FileUtil.readFile(getWebRootPath() + article.getEditContent()));
  815. } catch (Exception e) {
  816. setRequestAttribute("editBody", "内容文件不存在或已被删除");
  817. }
  818. ArticleJumpTypeCode ajtCode = new ArticleJumpTypeCode();
  819. ajtCode.setAjtcode(article.getAjtcode());
  820. gdba.selectById(ajtCode);
  821. setRequestAttribute("ajtCode", ajtCode);// 查看文章时用到
  822. return DEFAULT_FORWARD;
  823. }
  824. /**
  825. * 拼接网站域名
  826. *
  827. * @param port 端口
  828. * @param domains 域名
  829. * @param protocol 协议
  830. * @param catalogURI 目录
  831. * @return 带有协议和端口的网站域名
  832. * @author SPACE
  833. * @log Oct 9, 2020 11:16:33 PM
  834. */
  835. private static String appendWebsiteDomain(String port, String domains, String protocol, String catalogURI) {
  836. String href = null;
  837. String[] domainsAry = domains.split(";");
  838. if (domainsAry.length > 0) {
  839. port = "80".equals(port) ? "" : (":" + port); // 非 80 端口进行特殊处理
  840. if (domainsAry[0].endsWith("/")) {
  841. domainsAry[0] = domainsAry[0].substring(0, domainsAry[0].lastIndexOf("/"));
  842. }
  843. href = protocol + "://" + domainsAry[0] + port + catalogURI;
  844. }
  845. // 未找到域名
  846. else {
  847. href = "javascript:alert('website domain is null!');";
  848. }
  849. return href;
  850. }
  851. /**
  852. * 文章置顶周期
  853. *
  854. * @param article
  855. * @throws Exception
  856. * @author SPACE
  857. * @date 2020年02月09日
  858. */
  859. private void initArticleSticktopSchedule(GeneralDBAccess gdba, Article article, AppUser appUser)
  860. throws ParseException, Exception {
  861. // 删除掉已经添加了的定时任务
  862. if (StringUtil.notNull(article.getSticktopSchedulesid())) {
  863. ScheduleImpl.delSchedule(article.getSticktopSchedulesid(), gdba);
  864. }
  865. // 不置顶
  866. if (article.getSticktop() == 0) {
  867. article.setSticktopEndTime(null);
  868. return;
  869. }
  870. // 非在用状态
  871. if (!article.getDcode().equals(61L)) {
  872. return;
  873. }
  874. Map<String, Object> data = new HashMap<String, Object>();
  875. data.put("articleid", article.getArticleid());
  876. Long scheduleid = ScheduleImpl.addSchedule(
  877. "文章“" + article.getArticleid() + "/" + article.getTitle() + "”置顶周期任务",
  878. DateTimeUtil.formatDateYMdHms(article.getSticktopEndTime()),
  879. "space.apps.moudules.website.schedule.ArticleScheduleImpl", "articleSticktop", appUser.getUserId(),
  880. data, gdba);
  881. article.setSticktopSchedulesid(scheduleid);
  882. }
  883. /**
  884. * 文章定时发布任务
  885. *
  886. * @param gdba
  887. * @param article
  888. * @param appUser
  889. * @throws Exception
  890. * @author 陈锋
  891. * @log 2020年9月16日
  892. */
  893. private void initArticleReleaseTaskSchedule(GeneralDBAccess gdba, Article article, AppUser appUser)
  894. throws Exception {
  895. // 删掉已经添加的定时发布文章任务
  896. if (StringUtil.notNull(article.getReleaseTaskSchedulesid())) {
  897. ScheduleImpl.delSchedule(article.getReleaseTaskSchedulesid(), gdba);
  898. article.setReleaseTaskSchedulesid(null);
  899. }
  900. if (article.getRelease() == 0) {
  901. // article.setReleaseTime(null);
  902. return;
  903. }
  904. article.setAscode(articleStatusCode.STAY_RELEASE.getCode());
  905. Map<String, Object> data = new HashMap<String, Object>();
  906. data.put("articleid", article.getArticleid());
  907. Long scheduleid = ScheduleImpl.addSchedule(
  908. "文章“" + article.getArticleid() + "/" + article.getTitle() + "”定时发布任务",
  909. DateTimeUtil.formatDateYMdHms(article.getReleaseTime()),
  910. "space.apps.moudules.website.schedule.ArticleScheduleImpl", "articleRelease", appUser.getUserId(), data,
  911. gdba);
  912. article.setReleaseTaskSchedulesid(scheduleid);
  913. }
  914. /**
  915. * 文章列表
  916. *
  917. * @return
  918. * @throws Exception
  919. * @author SPACE
  920. * @date 2020年02月09日
  921. */
  922. public String list() throws Exception {
  923. AppUser appUser = getAppUser();
  924. StringBuilder sqlBuilder = new StringBuilder(" Select ");
  925. sqlBuilder.append(
  926. " a.articleid, a.dcode, a.thumbnail, (Select name from data_status_code dsc where dsc.dcode = a.dcode) as 'dsname', a.ascode, a" +
  927. ".websiteid, a.title, a.navigationid, a.ajtcode, a.sticktop, a.release_time, a.read_count, a.create_pid, date_format(a" +
  928. ".create_time, '%Y-%m-%d') as 'ctime', concat(per.name, ' / ', dep.name) pname_dname ");
  929. sqlBuilder.append(
  930. " ,(Select name from website_navigation wn where wn.navigationid = a.navigationid ) as 'navname'");
  931. sqlBuilder.append(
  932. " ,(Select name from website w where w.websiteid = a.websiteid ) as 'website_name', dep.departmentid ");
  933. sqlBuilder.append(" from article a ");
  934. sqlBuilder.append(" left join person per on a.create_pid = per.personid ");
  935. sqlBuilder.append(" left join department dep on per.departmentid = dep.departmentid ");
  936. sqlBuilder.append(
  937. " where a.unitid = :unitid and if((a.dcode in (11, 21)), (a.create_pid = :createPid), (a.create_pid > 0)) ");
  938. sqlBuilder.append(" and a.dcode != 72 "); // 过滤已删除的数据
  939. SqlCommandImpl sqlCmd = new SqlCommandImpl();
  940. sqlCmd.setString("unitid", appUser.getUnitId()).setString("createPid", appUser.getUserId());
  941. GdbaQueryUtil.appendLikeSql(sqlBuilder, sqlCmd, "title", getParameter("title")); // 标题
  942. GdbaQueryUtil.appendFieldSql(sqlBuilder, sqlCmd, "a.dcode", getParameter("dcode")); // 数据状态码
  943. // 栏目
  944. String navigation = getParameter("navigation");
  945. if (StringUtil.notNull(navigation)) {
  946. sqlBuilder.append("and (" + GdbaQueryUtil.getInSql(sqlCmd, "navigationid", navigation, ",")
  947. + "or articleid in (Select ac.articleid from website_navigation wn, article_copy ac, article a "
  948. + "where wn.navigationid = ac.navigationid and a.articleid = ac.articleid and ac.dcode = 61 and a.dcode = 61 "
  949. + "and wn.dcode = 61 and " + GdbaQueryUtil.getInSql(sqlCmd, "ac.navigationid", navigation, ",")
  950. + " ))");
  951. }
  952. // 我发布的
  953. String issue = getParameter("issue");
  954. if (StringUtil.notNull(issue) && ConstVars.TRUE.equals(issue)) {
  955. sqlBuilder.append(" and a.create_pid = :cpid ");
  956. sqlCmd.setString("cpid", appUser.getUserId());
  957. }
  958. /* 年月日 */
  959. String dateTime = getParameter("dateTime");
  960. if (StringUtil.notNull(dateTime)) {
  961. String[] dateTimeAry = dateTime.split(",");
  962. sqlBuilder.append(
  963. " and date_format(release_time, '%Y-%m-%d') >= date_format(:startTime, '%Y-%m-%d') and date_format(release_time, '%Y-%m-%d') <=" +
  964. " date_format(:endTime, '%Y-%m-%d') ");
  965. sqlCmd.setString("startTime", dateTimeAry[0]).setString("endTime", dateTimeAry[1]);
  966. }
  967. GeneralDBAccess gdba = getGdbaInstance();
  968. // 获取可管理的网站
  969. List<Map<String, Object>> manageWebsiteList = AppWebsiteManageUtil.getManageWebsiteListByAppUser(gdba,
  970. getAppUser());
  971. if (manageWebsiteList.size() > 0) {
  972. sqlBuilder.append(" and ( ").append(GdbaQueryUtil.getInSql(sqlCmd, "a.websiteid",
  973. AppWebsiteManageUtil.websiteListIDToAry(manageWebsiteList)));
  974. sqlBuilder.append("or (a.create_pid = :cpid2 )) ");
  975. } else {
  976. sqlBuilder.append(" and (a.create_pid = :cpid2 ) ");
  977. }
  978. sqlCmd.setString("cpid2", appUser.getUserId());
  979. // 排序方式
  980. String sort = getParameter("sort");
  981. if (StringUtil.isNull(sort) || "sort_new".equals(sort)) {
  982. sqlBuilder.append(" order by dcode asc, sticktop desc, release_time desc, articleid asc ");
  983. } else if ("sort_top".equals(sort)) {
  984. sqlBuilder.append(" order by dcode asc, read_count desc, articleid asc ");
  985. } else if ("sort_bottom".equals(sort)) {
  986. sqlBuilder.append(" order by dcode asc, read_count asc, articleid asc ");
  987. }
  988. sqlCmd.setExecuteSql(sqlBuilder.toString());
  989. List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, getAppsPageBean());
  990. for (Map<String, Object> map : articleList) {
  991. // 校验是否拥有该网站的管理权限
  992. for (Map<String, Object> websiteMap : manageWebsiteList) {
  993. if (map.get("websiteid").toString().equals(websiteMap.get("websiteid").toString())) {
  994. map.put("isManagerDepartmentArticleFlag", "true");
  995. break;
  996. }
  997. }
  998. map.put("navAllName", getNavAllParentName(gdba, Long.valueOf(map.get("navigationid").toString())));
  999. }
  1000. ResponsePackage rsp = getResponseSuccessPackage();
  1001. rsp.setRequestAttribute("articleList", articleList);
  1002. setResponsePackage(rsp);
  1003. return DEFAULT_FORWARD;
  1004. }
  1005. /**
  1006. * 保存文章抄送
  1007. *
  1008. * @param gdba
  1009. * @param article
  1010. * @param appUser
  1011. * @param navIdList
  1012. * @throws Exception
  1013. * @author SPACE
  1014. * @date 2020年02月09日
  1015. */
  1016. private void saveArticleCopy(GeneralDBAccess gdba, Article article, AppUser appUser, String navIdList)
  1017. throws Exception {
  1018. delArticleCopy(gdba, article);
  1019. if (StringUtil.isNull(navIdList)) {
  1020. return;
  1021. }
  1022. Timestamp nowTimestamp = DateTimeUtil.getNowTimestamp();
  1023. String[] navIdAry = navIdList.split(",");
  1024. Set<String> navIdSet = new HashSet<String>();
  1025. for (String navId : navIdAry) {
  1026. if (navIdSet.contains(navId)) {
  1027. continue;
  1028. }
  1029. navIdSet.add(navId);
  1030. ArticleCopy ac = new ArticleCopy();
  1031. ac.setArticleid(article.getArticleid());
  1032. ac.setNavigationid(Long.valueOf(navId));
  1033. ac.setCreatePid(appUser.getUserId());
  1034. ac.setCreateTime(nowTimestamp);
  1035. ac.setDcode(dataStatusCode.INSERT_AUDIT.getCode());
  1036. gdba.insertByBean(ac);
  1037. }
  1038. }
  1039. /**
  1040. * 文章录入前置服务
  1041. *
  1042. * @return
  1043. * @throws Exception
  1044. * @author SPACE
  1045. * @log Oct 14, 2020 10:32:06 PM
  1046. */
  1047. public String articleAddBefore() throws Exception {
  1048. List<Map<String, Object>> websiteList = AppWebsiteManageUtil.getManageWebsiteListByAppUser(getGdbaInstance(),
  1049. getAppUser());
  1050. // 默认找可管理的第一个网站
  1051. if (websiteList != null && websiteList.size() > 0) {
  1052. setRequestAttribute("article_sources", websiteList.get(0).get("name"));
  1053. }
  1054. return DEFAULT_FORWARD;
  1055. }
  1056. /**
  1057. * 生成文章正文临时预览。
  1058. *
  1059. * @throws Exception
  1060. * @author SPACE
  1061. */
  1062. public void preview() throws Exception {
  1063. String content = getParameterNotXSS("body");
  1064. Long navigationid = getParameterToLong("navigationid");
  1065. if (StringUtil.isNull(content)) {
  1066. write(getResponseWarningMsgPackage("正文内容为空").toJson());
  1067. return;
  1068. }
  1069. if (navigationid == null || navigationid <= 0) {
  1070. write(getResponseWarningMsgPackage("请先选择发布栏目").toJson());
  1071. return;
  1072. }
  1073. GeneralDBAccess gdba = getGdbaInstance();
  1074. WebsiteNavigation wn = getWebsiteNavigationById(gdba, navigationid);
  1075. if (!isArticleTargetNavigation(wn)) {
  1076. write(getResponseWarningMsgPackage("发布栏目必须选择可发布文章的栏目").toJson());
  1077. return;
  1078. }
  1079. if (hasEnabledChildNavigation(gdba, navigationid)) {
  1080. write(getResponseWarningMsgPackage("发布栏目必须选择最下级栏目,请继续向右选择最后一级栏目").toJson());
  1081. return;
  1082. }
  1083. String editPath = WebEditorUtil.createHtmlFile(content);
  1084. String runContent = converArticleContentHtmlLink(getWebRootPath() + editPath,
  1085. space.apps.moudules.website.common.ConstVars.domain);
  1086. String runPath = WebEditorUtil.createHtmlFile(runContent);
  1087. String previewKey = java.util.UUID.randomUUID().toString().replace("-", "");
  1088. setSessionAttribute(ARTICLE_PREVIEW_SESSION_PREFIX + previewKey, runPath);
  1089. Map<String, Object> retMap = new HashMap<String, Object>();
  1090. retMap.put("url", "/apps?ark=website.article_preview_view&key=" + URLEncoder.encode(previewKey, "UTF-8"));
  1091. write(getResponseSuccessDataPackage(retMap).toJsonWriteNonStringValueAsString());
  1092. }
  1093. /**
  1094. * 输出文章正文临时预览内容。
  1095. *
  1096. * @throws Exception
  1097. * @author SPACE
  1098. */
  1099. public void previewView() throws Exception {
  1100. getResponse().setContentType("text/html;charset=UTF-8");
  1101. String key = getParameter("key");
  1102. if (StringUtil.isNull(key)) {
  1103. writePreviewUnavailable();
  1104. return;
  1105. }
  1106. Object previewPathObj = getSession().getAttribute(ARTICLE_PREVIEW_SESSION_PREFIX + key);
  1107. String previewPath = previewPathObj == null ? "" : previewPathObj.toString();
  1108. File previewFile = getSafePreviewFile(previewPath);
  1109. if (previewFile == null) {
  1110. writePreviewUnavailable();
  1111. return;
  1112. }
  1113. write(FileUtil.readFile(previewFile.getPath()));
  1114. }
  1115. private File getSafePreviewFile(String previewPath) throws Exception {
  1116. if (StringUtil.isNull(previewPath)) {
  1117. return null;
  1118. }
  1119. String normalizedPath = previewPath.replace("\\", "/");
  1120. if (!normalizedPath.endsWith(".html")) {
  1121. return null;
  1122. }
  1123. File webRoot = new File(getWebRootPath()).getCanonicalFile();
  1124. File uploadRoot = new File(webRoot, "web/upload").getCanonicalFile();
  1125. File previewFile = normalizedPath.startsWith("/")
  1126. ? new File(webRoot, normalizedPath.substring(1)).getCanonicalFile()
  1127. : new File(webRoot, normalizedPath).getCanonicalFile();
  1128. if (!previewFile.getPath().startsWith(uploadRoot.getPath() + File.separator)) {
  1129. return null;
  1130. }
  1131. return previewFile.isFile() ? previewFile : null;
  1132. }
  1133. private void writePreviewUnavailable() throws Exception {
  1134. write("<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><style>"
  1135. + "body{margin:0;font-family:Arial,'Microsoft YaHei',sans-serif;background:#f5f7fa;color:#606266;}"
  1136. + ".msg{padding:80px 20px;text-align:center;font-size:14px;}"
  1137. + "</style></head><body><div class=\"msg\">预览内容不存在,请刷新预览后再查看。</div></body></html>");
  1138. }
  1139. // 测试的服务
  1140. /**
  1141. * 重置文章运行文件,主要是替换链接的域名,固定替换为 space.apps.moudules.website.common.ConstVars.domain
  1142. *
  1143. * @return
  1144. * @throws Exception
  1145. * @author 陈锋
  1146. * @log 2020年10月19日
  1147. */
  1148. public String resetArticleRunHtmlLink() throws Exception {
  1149. GeneralDBAccess gdba = getGdbaInstance();
  1150. SqlCommandImpl sqlCmd = new SqlCommandImpl("select * from article ");
  1151. @SuppressWarnings("unchecked")
  1152. List<Article> articleList = gdba.selectAsList(sqlCmd, Article.class);
  1153. String webPath = getWebRootPath();
  1154. for (Article article : articleList) {
  1155. File file = new File(webPath + article.getContent());
  1156. if (".DS_Store".equals(file.getName())) {
  1157. continue;
  1158. }
  1159. if (!file.isFile()) {
  1160. continue;
  1161. }
  1162. String content = converArticleContentHtmlLink(file, space.apps.moudules.website.common.ConstVars.domain,
  1163. webPath);
  1164. FileUtil.writeFile(content, file.getPath());
  1165. }
  1166. ResponsePackage rp = getResponseSuccessPackage();
  1167. setResponsePackage(rp);
  1168. return DEFAULT_FORWARD;
  1169. }
  1170. /**
  1171. * 转换文章内容 html 里的链接
  1172. *
  1173. * @param path
  1174. * @param domain
  1175. * @return
  1176. * @throws Exception
  1177. * @author 陈锋
  1178. * @log 2020年10月16日
  1179. */
  1180. private String converArticleContentHtmlLink(File input, String domain, String webPath) throws Exception {
  1181. Document doc = Jsoup.parse(input, "UTF-8", "");
  1182. Elements srcEles = doc.select("img[src]");
  1183. Elements originalEles = doc.select("img[data-original]");
  1184. Elements hrefEles = doc.select("a[href]");
  1185. replaceAllHtmlLink(srcEles, "src", domain);
  1186. replaceAllHtmlLink(originalEles, "data-original", domain);
  1187. replaceAllHtmlLink(hrefEles, "href", domain);
  1188. imgSrcConverLazyloadSrc(doc);
  1189. String html = doc.select("body").eq(0).html();
  1190. String articleTemplate = html;
  1191. if (html.indexOf("jquery-lazyload-container") == -1) {
  1192. articleTemplate = FileUtil.readFile(webPath + "/main/apps/template/article_issue_template.txt");
  1193. articleTemplate = articleTemplate.replace("{article_content}", html);
  1194. }
  1195. return articleTemplate;
  1196. }
  1197. /**
  1198. * 替换全部链接
  1199. *
  1200. * @param attrEles
  1201. * @param attr
  1202. * @param domain
  1203. * @author 陈锋
  1204. * @log 2020年10月19日
  1205. */
  1206. private void replaceAllHtmlLink(Elements attrEles, String attr, String domain) {
  1207. for (Element ele : attrEles) {
  1208. if (ele.tagName().equalsIgnoreCase("img")) {
  1209. ele.removeAttr("width");
  1210. ele.removeAttr("height");
  1211. ele.removeAttr("style");
  1212. ele.removeAttr("title");
  1213. ele.removeAttr("alt");
  1214. ele.removeAttr("onload");
  1215. ele.removeAttr("border");
  1216. ele.removeAttr("vspace");
  1217. resetParentsPTagStyle(ele);
  1218. }
  1219. String attrValue = ele.attr(attr);
  1220. if (StringUtil.isNull(attrValue)) {
  1221. continue;
  1222. }
  1223. if (attrValue.startsWith("http://42.247.50.199")) {
  1224. attrValue = attrValue.replace("http://42.247.50.199", "");
  1225. } else {
  1226. if (shouldKeepOriginalAttrValue(attrValue)) {
  1227. continue;
  1228. }
  1229. }
  1230. attrValue = FileUtil.replacePathSprit(attrValue);
  1231. if (domain.endsWith("/") && attrValue.startsWith("/")) {
  1232. attrValue = domain + attrValue.substring(1);
  1233. } else {
  1234. attrValue = domain + attrValue;
  1235. }
  1236. if ("href".equals(attr)) {
  1237. ele.attr("target", "_blank");
  1238. }
  1239. ele.attr(attr, attrValue);
  1240. }
  1241. }
  1242. /**
  1243. * 只替换 img 标签上的 p 标签,p 标签加 style="text-align: center"
  1244. *
  1245. * @param imgEle
  1246. * @author 陈锋
  1247. * @log 2020年10月19日
  1248. */
  1249. private void resetParentsPTagStyle(Element imgEle) {
  1250. Element parentEle = imgEle.parent();
  1251. for (int i = 0; i < 6; i++) {
  1252. if (parentEle == null) {
  1253. return;
  1254. }
  1255. if (!parentEle.tagName().equals("p")) {
  1256. parentEle = parentEle.parent();
  1257. } else {
  1258. parentEle.attr("style", "text-align: center");
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. @Override
  1264. public boolean isNextServiceFlag() {
  1265. String nsFlag = this.getRequest().getParameter("nextServiceFormFlag");
  1266. return StringUtil.notNull(nsFlag) && nsFlag.equals("true");
  1267. }
  1268. }