Skip to content

Commit

Permalink
🦄 fix otale#150
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Mar 15, 2017
1 parent c3efb04 commit 0265110
Show file tree
Hide file tree
Showing 20 changed files with 489 additions and 247 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ demo website:https://tale.biezhi.me
[![Build Status](https://img.shields.io/travis/otale/tale.svg?style=flat-square)](https://travis-ci.org/otale/tale)
[![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/otale/tale/blob/master/LICENSE)
[![@biezhi on weibo](https://img.shields.io/badge/weibo-%40biezhi-red.svg?style=flat-square)](http://weibo.com/u/5238733773)
[![Gitter](https://badges.gitter.im/biezhi/tale-group.svg)](https://gitter.im/tale-group)

[QuickStart](https://github.com/otale/tale/wiki/QuickStart)  |  [Contribution](https://github.com/otale/tale/issues/new)  |  [Donate](donate.md)  |  [Video](video.md)  |  [中文](README_ZH.md)

Expand Down Expand Up @@ -44,6 +45,7 @@ demo website:https://tale.biezhi.me

## Thanks

+ [dongm2ez](https://github.com/dongm2ez)
+ [pkwenda](https://github.com/pkwenda)
+ [typecho](https://github.com/typecho/typecho)
+ [pinghsu](https://github.com/chakhsu/pinghsu)
Expand Down
2 changes: 2 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[![Build Status](https://img.shields.io/travis/otale/tale.svg?style=flat-square)](https://travis-ci.org/otale/tale)
[![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/otale/tale/blob/master/LICENSE)
[![@biezhi on weibo](https://img.shields.io/badge/weibo-%40biezhi-red.svg?style=flat-square)](http://weibo.com/u/5238733773)
[![Gitter](https://badges.gitter.im/biezhi/tale-group.svg)](https://gitter.im/tale-group)

[开始使用](https://github.com/otale/tale/wiki)  |  [参与贡献](contribution.md)  |  [捐赠](donate.md)  |  [视频教程](video.md)  |  [English](README.md)

Expand Down Expand Up @@ -46,6 +47,7 @@

## 感谢

+ [dongm2ez](https://github.com/dongm2ez)
+ [pkwenda](https://github.com/pkwenda)
+ [typecho](https://github.com/typecho/typecho)
+ [pinghsu](https://github.com/chakhsu/pinghsu)
Expand Down
1 change: 1 addition & 0 deletions donate.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
| 潇哥一阵风 | 66.6 | 支付宝 | 2017/02/27 | 项目和作者都比较6,哈哈 |
| 大馍祖师 | 10.0 | 支付宝 | 2017/02/28 ||
| 微信匿名 | 3.0 | 微信 | 2017/03/01 | 零钱少,能否参加项目 |
| 微信匿名 | 15.0 | 微信 | 2017/03/12 | 谢谢你给我更多选择 |
12 changes: 3 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

<groupId>org.junicorn</groupId>
<artifactId>tale</artifactId>
<version>1.2.10</version>
<version>1.2.11</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sqlite.version>3.15.1</sqlite.version>
<druid.version>1.0.24</druid.version>
<blade.version>1.7.1-release</blade.version>
<blade.version>1.7.2-alpha</blade.version>
<blade-jdbc.version>0.1.5</blade-jdbc.version>
<blade-tpl.verion>0.0.9</blade-tpl.verion>
<blade-embed-jetty.version>0.1.2</blade-embed-jetty.version>
<blade-embed-jetty.version>0.1.3</blade-embed-jetty.version>
</properties>

<dependencies>
Expand All @@ -26,12 +26,6 @@
<version>${blade.version}</version>
</dependency>

<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-kit</artifactId>
<version>1.4.1-alpha</version>
</dependency>

<!-- 数据库操作 -->
<dependency>
<groupId>org.xerial</groupId>
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/com/tale/controller/admin/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -86,16 +85,6 @@ public String index(Request request) {
public String setting(Request request) {
Map<String, String> options = optionsService.getOptions();
request.attribute("options", options);
// 读取主题
String themesDir = AttachController.CLASSPATH + "templates/themes";
File[] themesFile = new File(themesDir).listFiles();
List<String> themems = new ArrayList<>(themesFile.length);
for(File f : themesFile){
if(f.isDirectory()){
themems.add(f.getName());
}
}
request.attribute("themes", themems);
return "admin/setting";
}

Expand All @@ -113,9 +102,6 @@ public RestResponse saveSetting(@QueryParam String site_theme, Request request)
config.addAll(optionsService.getOptions());
TaleConst.OPTIONS = config;

if (StringKit.isNotBlank(site_theme)) {
BaseController.THEME = "themes/" + site_theme;
}
logService.save(LogActions.SYS_SETTING, JSONKit.toJSONString(querys), request.address(), this.getUid());
return RestResponse.ok();
} catch (Exception e) {
Expand Down Expand Up @@ -219,18 +205,6 @@ public RestResponse backup(@QueryParam String bk_type, @QueryParam String bk_pat
}
}

/**
* 后台高级选项页面
*
* @return
*/
@Route(value = "advanced", method = HttpMethod.GET)
public String advanced(Request request){
Map<String, String> options = optionsService.getOptions();
request.attribute("options", options);
return "admin/advanced";
}

/**
* 保存高级选项设置
* @return
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/com/tale/controller/admin/ThemeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.tale.controller.admin;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.FileKit;
import com.blade.kit.StringKit;
import com.blade.kit.base.Config;
import com.blade.kit.json.JSONKit;
import com.blade.mvc.annotation.Controller;
import com.blade.mvc.annotation.JSON;
import com.blade.mvc.annotation.QueryParam;
import com.blade.mvc.annotation.Route;
import com.blade.mvc.http.HttpMethod;
import com.blade.mvc.http.Request;
import com.blade.mvc.view.RestResponse;
import com.tale.controller.BaseController;
import com.tale.dto.LogActions;
import com.tale.dto.ThemeDto;
import com.tale.exception.TipException;
import com.tale.ext.Commons;
import com.tale.init.TaleConst;
import com.tale.service.LogService;
import com.tale.service.OptionsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 主题控制器
*/
@Controller("admin/themes")
public class ThemeController extends BaseController {

private static final Logger LOGGER = LoggerFactory.getLogger(ThemeController.class);

@Inject
private OptionsService optionsService;

@Inject
private LogService logService;

@Route(value = "", method = HttpMethod.GET)
public String index(Request request) {
// 读取主题
String themesDir = AttachController.CLASSPATH + "templates/themes";
File[] themesFile = new File(themesDir).listFiles();
List<ThemeDto> themes = new ArrayList<>(themesFile.length);
for(File f : themesFile){
if(f.isDirectory()){
ThemeDto themeDto = new ThemeDto(f.getName());
if(FileKit.exist(f.getPath() + "/setting.html")){
themeDto.setHasSetting(true);
}
themes.add(themeDto);
}
}
request.attribute("current_theme", Commons.site_theme());
request.attribute("themes", themes);
return "admin/themes";
}

/**
* 主题设置页面
* @param request
* @return
*/
@Route(value = "setting", method = HttpMethod.GET)
public String setting(Request request) {
Map<String, String> themeOptions = optionsService.getOptions("theme_option_");
request.attribute("theme_options", themeOptions);
return this.render("setting");
}

/**
* 保存主题配置项
* @param request
* @return
*/
@Route(value = "setting", method = HttpMethod.POST)
@JSON
public RestResponse saveSetting(Request request) {
try {
Map<String, String> querys = request.querys();
optionsService.saveOptions(querys);

Config config = new Config();
config.addAll(optionsService.getOptions());
TaleConst.OPTIONS = config;

logService.save(LogActions.THEME_SETTING, JSONKit.toJSONString(querys), request.address(), this.getUid());
return RestResponse.ok();
} catch (Exception e) {
String msg = "主题设置失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
}

/**
* 激活主题
* @param request
* @param site_theme
* @return
*/
@Route(value = "active", method = HttpMethod.POST)
@JSON
public RestResponse activeTheme(Request request, @QueryParam String site_theme) {
try {
optionsService.saveOption("site_theme", site_theme);
TaleConst.OPTIONS.put("site_theme", site_theme);
BaseController.THEME = "themes/" + site_theme;
logService.save(LogActions.THEME_SETTING, site_theme, request.address(), this.getUid());
return RestResponse.ok();
} catch (Exception e) {
String msg = "激活主题失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/tale/dto/LogActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface LogActions {

String SYS_SETTING = "保存系统设置";

String THEME_SETTING = "主题设置";

String INIT_SITE = "初始化站点";

String RELOAD_SYS = "重启系统";
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/tale/dto/ThemeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tale.dto;

import java.io.Serializable;

/**
* Created by biezhi on 2017/3/15.
*/
public class ThemeDto implements Serializable {

/**
* 主题名称
*/
private String name;

/**
* 是否有设置项
*/
private boolean hasSetting;

public ThemeDto(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean isHasSetting() {
return hasSetting;
}

public void setHasSetting(boolean hasSetting) {
this.hasSetting = hasSetting;
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/tale/ext/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public static String site_url() {
return site_url("");
}

/**
* 返回当前主题名称
*
* @return
*/
public static String site_theme() {
return site_option("site_theme", "default");
}

/**
* 返回网站链接下的全址
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/tale/init/TaleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static void loadThemes(BConfig bConfig) {
File[] dir = new File(themeDir).listFiles();
for (File f : dir) {
if (f.isDirectory() && FileKit.isDirectory(f.getPath() + "/static")) {
bConfig.addStatic(new String[]{"/templates/themes/" + f.getName() + "/static"});
String themePath = "/templates/themes/" + f.getName();
bConfig.addStatic(new String[]{themePath + "/style.css", themePath + "/screenshot.png", themePath + "/static"});
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/tale/service/OptionsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public interface OptionsService {
*/
Map<String, String> getOptions();

/**
* 根据key前缀查找配置项
* @param key
* @return
*/
Map<String, String> getOptions(String key);

/**
* 根据key删除配置项
* @param key
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/tale/service/impl/OptionsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,19 @@ public void saveOption(String key, String value) {

@Override
public Map<String, String> getOptions() {
return getOptions(null);
}

@Override
public Map<String, String> getOptions(String key) {
Map<String, String> options = new HashMap<>();
List<Options> optionsList = activeRecord.list(new Options());
for (Options option : optionsList) {
options.put(option.getName(), option.getValue());
Take take = new Take(Options.class);
if(StringKit.isNotBlank(key)){
take.like("name", key + "%");
}
List<Options> optionsList = activeRecord.list(take);
if(null != optionsList){
optionsList.forEach(option -> options.put(option.getName(), option.getValue()));
}
return options;
}
Expand Down
Loading

0 comments on commit 0265110

Please sign in to comment.