Add some new api of preference.

This commit is contained in:
zicla 2017-12-25 14:32:18 +08:00
parent 00472c6b4d
commit a7e502ba9c
9 changed files with 199 additions and 11 deletions

View File

@ -3,7 +3,7 @@ server{
server_name tank.zicpo.cn; server_name tank.zicpo.cn;
location / { location / {
proxy_pass http://127.0.0.1:9090; proxy_pass http://127.0.0.1:6010;
proxy_set_header host $host; proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass_request_headers on; proxy_pass_request_headers on;

View File

@ -1,5 +1,5 @@
{ {
"ServerPort": 9090, "ServerPort": 6010,
"LogToConsole": true, "LogToConsole": true,
"MysqlPort": 3306, "MysqlPort": 3306,
"MysqlHost": "127.0.0.1", "MysqlHost": "127.0.0.1",

View File

@ -1,13 +1,13 @@
package rest package rest
import ( import (
"fmt"
"github.com/json-iterator/go"
"time"
"unsafe"
"io/ioutil"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"github.com/json-iterator/go"
"io/ioutil"
"time"
"unsafe"
) )
const ( const (
@ -21,12 +21,22 @@ const (
VERSION = "1.0.0" VERSION = "1.0.0"
) )
/*
如果你需要在本地127.0.0.1创建默认的数据库和账号使用以下语句
create database tank;
grant all privileges on tank.* to tank identified by 'tank123';
flush privileges;
*/
/*
你也可以在运行时的参数中临时修改一些配置项
-MysqlHost=127.0.0.1 -MysqlPort=3306 -MysqlSchema=tank -MysqlUserName=tank -MysqlPassword=tank123
*/
var ( var (
CONFIG = &Config{ CONFIG = &Config{
//以下内容是默认配置项。 //以下内容是默认配置项。
//默认监听端口号 //默认监听端口号
ServerPort: 9090, ServerPort: 6010,
//将日志输出到控制台。 //将日志输出到控制台。
LogToConsole: true, LogToConsole: true,
//mysql相关配置。 //mysql相关配置。
@ -40,7 +50,7 @@ var (
MysqlUserName: "tank", MysqlUserName: "tank",
//密码 //密码
MysqlPassword: "tank123", MysqlPassword: "tank123",
//数据库连接信息。 //数据库连接信息。这一项是上面几项组合而得,不可直接配置。
MysqlUrl: "%MysqlUserName:%MysqlPassword@tcp(%MysqlHost:%MysqlPort)/%MysqlSchema?charset=utf8&parseTime=True&loc=Local", MysqlUrl: "%MysqlUserName:%MysqlPassword@tcp(%MysqlHost:%MysqlPort)/%MysqlSchema?charset=utf8&parseTime=True&loc=Local",
//超级管理员用户名,只能包含英文和数字 //超级管理员用户名,只能包含英文和数字
AdminUsername: "admin", AdminUsername: "admin",

View File

@ -104,6 +104,11 @@ func (this *Context) registerBeans() {
this.registerBean(new(MatterDao)) this.registerBean(new(MatterDao))
this.registerBean(new(MatterService)) this.registerBean(new(MatterService))
//preference
this.registerBean(new(PreferenceController))
this.registerBean(new(PreferenceDao))
this.registerBean(new(PreferenceService))
//session //session
this.registerBean(new(SessionDao)) this.registerBean(new(SessionDao))

View File

@ -5,8 +5,8 @@ import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/nu7hatch/gouuid" "github.com/nu7hatch/gouuid"
"time"
"regexp" "regexp"
"time"
) )
//首次运行的时候,将自动安装数据库等内容。 //首次运行的时候,将自动安装数据库等内容。
@ -46,6 +46,18 @@ func InstallDatabase() {
LogInfo("创建Matter表") LogInfo("创建Matter表")
} }
preference := &Preference{}
hasTable = db.HasTable(preference)
if !hasTable {
createPreference := "CREATE TABLE `tank10_preference` (`uuid` char(36) NOT NULL,`name` varchar(45) DEFAULT NULL COMMENT '网站名称',`logo_url` varchar(255) DEFAULT NULL,`favicon_url` varchar(255) DEFAULT NULL,`footer_line1` varchar(1024) DEFAULT NULL,`footer_line2` varchar(1024) DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网站偏好设置表';"
db = db.Exec(createPreference)
if db.Error != nil {
LogPanic(db.Error)
}
LogInfo("创建Preference表")
}
session := &Session{} session := &Session{}
hasTable = db.HasTable(session) hasTable = db.HasTable(session)
if !hasTable { if !hasTable {
@ -87,7 +99,6 @@ func InstallDatabase() {
LogPanic("超级管理员邮箱必填!") LogPanic("超级管理员邮箱必填!")
} }
createUser := "CREATE TABLE `tank10_user` (`uuid` char(36) NOT NULL,`role` varchar(45) DEFAULT 'USER',`username` varchar(255) DEFAULT NULL COMMENT '昵称',`password` varchar(255) DEFAULT NULL COMMENT '密码',`email` varchar(45) DEFAULT NULL COMMENT '邮箱',`phone` varchar(45) DEFAULT NULL COMMENT '电话',`gender` varchar(45) DEFAULT 'UNKNOWN' COMMENT '性别,默认未知',`city` varchar(45) DEFAULT NULL COMMENT '城市',`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像链接',`last_time` datetime DEFAULT NULL COMMENT '上次登录使劲按',`last_ip` varchar(45) DEFAULT NULL,`status` varchar(45) DEFAULT 'OK',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表描述';" createUser := "CREATE TABLE `tank10_user` (`uuid` char(36) NOT NULL,`role` varchar(45) DEFAULT 'USER',`username` varchar(255) DEFAULT NULL COMMENT '昵称',`password` varchar(255) DEFAULT NULL COMMENT '密码',`email` varchar(45) DEFAULT NULL COMMENT '邮箱',`phone` varchar(45) DEFAULT NULL COMMENT '电话',`gender` varchar(45) DEFAULT 'UNKNOWN' COMMENT '性别,默认未知',`city` varchar(45) DEFAULT NULL COMMENT '城市',`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像链接',`last_time` datetime DEFAULT NULL COMMENT '上次登录使劲按',`last_ip` varchar(45) DEFAULT NULL,`status` varchar(45) DEFAULT 'OK',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表描述';"
db = db.Exec(createUser) db = db.Exec(createUser)
if db.Error != nil { if db.Error != nil {

View File

@ -0,0 +1,74 @@
package rest
import (
"net/http"
)
type PreferenceController struct {
BaseController
preferenceDao *PreferenceDao
preferenceService *PreferenceService
}
//初始化方法
func (this *PreferenceController) Init(context *Context) {
this.BaseController.Init(context)
//手动装填本实例的Bean. 这里必须要用中间变量方可。
b := context.GetBean(this.preferenceDao)
if b, ok := b.(*PreferenceDao); ok {
this.preferenceDao = b
}
b = context.GetBean(this.preferenceService)
if b, ok := b.(*PreferenceService); ok {
this.preferenceService = b
}
}
//注册自己的路由。
func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request) {
routeMap := make(map[string]func(writer http.ResponseWriter, request *http.Request))
//每个Controller需要主动注册自己的路由。
routeMap["/api/preference/fetch"] = this.Wrap(this.Fetch, USER_ROLE_GUEST)
routeMap["/api/preference/edit"] = this.Wrap(this.Edit, USER_ROLE_ADMINISTRATOR)
return routeMap
}
//查看某个偏好设置的详情。
func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *http.Request) *WebResult {
preference := this.preferenceDao.Fetch()
return this.Success(preference)
}
//修改
func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http.Request) *WebResult {
//验证参数。
name := request.FormValue("name")
if name == "" {
panic("name参数必填")
}
logoUrl := request.FormValue("logoUrl")
faviconUrl := request.FormValue("faviconUrl")
footerLine1 := request.FormValue("footerLine1")
footerLine2 := request.FormValue("footerLine2")
preference := this.preferenceDao.Fetch()
preference.Name = name
preference.LogoUrl = logoUrl
preference.FaviconUrl = faviconUrl
preference.FooterLine1 = footerLine1
preference.FooterLine2 = footerLine2
preference = this.preferenceDao.Save(preference)
return this.Success(preference)
}

55
rest/preference_dao.go Normal file
View File

@ -0,0 +1,55 @@
package rest
import (
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/nu7hatch/gouuid"
"time"
)
type PreferenceDao struct {
BaseDao
}
//按照Id查询偏好设置
func (this *PreferenceDao) Fetch() *Preference {
// Read
var preference = &Preference{}
db := this.context.DB.First(preference)
if db.Error != nil {
if db.Error.Error() == "record not found" {
preference.Name = "蓝眼云盘"
this.Create(preference)
return preference
} else {
return nil
}
}
return preference
}
//创建
func (this *PreferenceDao) Create(preference *Preference) *Preference {
timeUUID, _ := uuid.NewV4()
preference.Uuid = string(timeUUID.String())
preference.CreateTime = time.Now()
preference.ModifyTime = time.Now()
db := this.context.DB.Create(preference)
this.PanicError(db.Error)
return preference
}
//修改一个偏好设置
func (this *PreferenceDao) Save(preference *Preference) *Preference {
preference.ModifyTime = time.Now()
db := this.context.DB.Save(preference)
this.PanicError(db.Error)
return preference
}

15
rest/preference_model.go Normal file
View File

@ -0,0 +1,15 @@
package rest
type Preference struct {
Base
Name string `json:"name"`
LogoUrl string `json:"logoUrl"`
FaviconUrl string `json:"faviconUrl"`
FooterLine1 string `json:"footerLine1"`
FooterLine2 string `json:"footerLine2"`
}
// set File's table name to be `profiles`
func (Preference) TableName() string {
return TABLE_PREFIX + "preference"
}

View File

@ -0,0 +1,18 @@
package rest
//@Service
type PreferenceService struct {
Bean
preferenceDao *PreferenceDao
}
//初始化方法
func (this *PreferenceService) Init(context *Context) {
//手动装填本实例的Bean. 这里必须要用中间变量方可。
b := context.GetBean(this.preferenceDao)
if b, ok := b.(*PreferenceDao); ok {
this.preferenceDao = b
}
}