forked from otale/tale
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1de335
commit 1789d8a
Showing
8 changed files
with
181 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/com/tale/controller/admin/TemplateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.tale.controller.admin; | ||
|
||
import com.blade.ioc.annotation.Inject; | ||
import com.blade.kit.StringKit; | ||
import com.blade.mvc.Const; | ||
import com.blade.mvc.annotation.*; | ||
import com.blade.mvc.http.HttpMethod; | ||
import com.blade.mvc.http.Request; | ||
import com.blade.mvc.http.Response; | ||
import com.blade.mvc.ui.RestResponse; | ||
import com.tale.controller.BaseController; | ||
import com.tale.extension.Commons; | ||
import com.tale.service.ContentsService; | ||
import com.tale.service.SiteService; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* 编辑模板 | ||
* <p> | ||
* Created by biezhi on 2017/2/21. | ||
*/ | ||
@Slf4j | ||
@Path("admin/template") | ||
public class TemplateController extends BaseController { | ||
|
||
@Inject | ||
private ContentsService contentsService; | ||
|
||
@Inject | ||
private SiteService siteService; | ||
|
||
@Route(value = "", method = HttpMethod.GET) | ||
public String index(Request request) { | ||
String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme(); | ||
try { | ||
List<String> files = Files.list(Paths.get(themePath)) | ||
.map(path -> path.getFileName().toString()) | ||
.filter(path -> path.endsWith(".html")) | ||
.collect(Collectors.toList()); | ||
|
||
List<String> partial = Files.list(Paths.get(themePath + File.separatorChar + "partial")) | ||
.map(path -> path.getFileName().toString()) | ||
.filter(path -> path.endsWith(".html")) | ||
.map(fileName -> "partial/" + fileName) | ||
.collect(Collectors.toList()); | ||
|
||
List<String> statics = Files.list(Paths.get(themePath + File.separatorChar + "static")) | ||
.map(path -> path.getFileName().toString()) | ||
.filter(path -> path.endsWith(".js") || path.endsWith(".css")) | ||
.map(fileName -> "static/" + fileName) | ||
.collect(Collectors.toList()); | ||
|
||
files.addAll(partial); | ||
files.addAll(statics); | ||
|
||
request.attribute("tpls", files); | ||
} catch (IOException e) { | ||
log.error("找不到模板路径"); | ||
} | ||
return "admin/tpl_list"; | ||
} | ||
|
||
@GetRoute("content") | ||
public void getContent(@Param String fileName, Response response) { | ||
try { | ||
String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme(); | ||
String filePath = themePath + File.separatorChar + fileName; | ||
String content = Files.readAllLines(Paths.get(filePath)).stream().collect(Collectors.joining("\n")); | ||
response.text(content); | ||
} catch (IOException e) { | ||
log.error("获取模板文件失败", e); | ||
} | ||
} | ||
|
||
@Route(value = "save", method = HttpMethod.POST) | ||
@JSON | ||
public RestResponse saveTpl(@Param String fileName, @Param String content) { | ||
if (StringKit.isBlank(fileName)) { | ||
return RestResponse.fail("缺少参数,请重试"); | ||
} | ||
String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme(); | ||
String filePath = themePath + File.separatorChar + fileName; | ||
try { | ||
if (Files.exists(Paths.get(filePath))) { | ||
byte[] rf_wiki_byte = content.getBytes("UTF-8"); | ||
Files.write(Paths.get(filePath), rf_wiki_byte); | ||
} else { | ||
Files.createFile(Paths.get(filePath)); | ||
byte[] rf_wiki_byte = content.getBytes("UTF-8"); | ||
Files.write(Paths.get(filePath), rf_wiki_byte); | ||
} | ||
return RestResponse.ok(); | ||
} catch (Exception e) { | ||
log.error("写入文件失败", e); | ||
return RestResponse.fail("写入文件失败: " + e.getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include('./header.html',{active:'template', title:'编辑模板'}) | ||
<div class="row"> | ||
<div class="col-sm-12"> | ||
<h4 class="page-title">编辑模板</h4> | ||
</div> | ||
<div class="col-md-12"> | ||
|
||
<select id="tplSelect" onchange="showContent(this)"> | ||
<option selected value="">请选择模板文件</option> | ||
#for(tpl : tpls) | ||
<option value="${tpl}">${tpl}</option> | ||
#end | ||
</select> | ||
|
||
<button class="btn btn-primary waves-effect waves-light m-b-5" onclick="saveTpl()">保存修改</button> | ||
|
||
<div> | ||
<textarea id="tpl-preview" rows="20" cols="100%" placeholder="选择模板后进行修改"></textarea> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
#include('./footer.html') | ||
<script> | ||
var tale = new $.tale(); | ||
|
||
function showContent(obj) { | ||
var fileName = obj.value; | ||
if (fileName && fileName != '') { | ||
$.get('/admin/template/content', {fileName: fileName}, function (data) { | ||
$('#tpl-preview').val(data); | ||
}); | ||
} else { | ||
$('#tpl-preview').val(''); | ||
} | ||
} | ||
|
||
function saveTpl() { | ||
var fileName = $('#tplSelect').val(); | ||
if (fileName && fileName != '') { | ||
tale.post({ | ||
url: '/admin/template/save', | ||
data: { | ||
fileName: fileName, | ||
content: $('#tpl-preview').val() | ||
}, | ||
success: function (result) { | ||
if (result && result.success) { | ||
tale.alertOk('模板保存成功'); | ||
} else { | ||
tale.alertError(result.msg || '模板保存失败'); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
</script> | ||
</body> | ||
</html> |