Mac 3 years ago
parent
commit
144d5ec6a7
  1. 1
      conf/app.conf
  2. 16
      consts/Common.go
  3. 56
      controllers/BaseController.go
  4. 1
      controllers/DetailController.go
  5. 1
      controllers/ListController.go
  6. 27
      controllers/MenuController.go
  7. 1
      controllers/RoleController.go
  8. 1
      controllers/UserController.go
  9. 30
      controllers/default.go
  10. BIN
      models/.DS_Store
  11. 21
      models/BaseModel.go
  12. 12
      models/Common.go
  13. 24
      models/MenuModel.go
  14. 8
      models/init.go
  15. 5
      routers/router.go
  16. 31
      sysinit/initDB.go
  17. 2
      sysinit/sysinit.go
  18. BIN
      xcms

1
conf/app.conf

@ -2,7 +2,6 @@ appname = xcms
httpport = 8080
runmode = dev
#mysql
db_alias = "default"
db_name = "xcms"

16
consts/Common.go

@ -0,0 +1,16 @@
package consts
type JsonResultCode int
const (
JRCodeSucc JsonResultCode = iota
JRCodeFailed
JRCode302 = 302 //跳转至地址
JRCode401 = 401 //未授权访问
)
const (
Deleted = iota - 1
Disabled
Enabled
)

56
controllers/BaseController.go

@ -1,7 +1,10 @@
package controllers
import (
"strings"
"github.com/astaxie/beego"
"github.com/ziyoubiancheng/xcms/consts"
"github.com/ziyoubiancheng/xcms/models"
)
@ -11,3 +14,56 @@ type BaseController struct {
actionName string
curUser models.UserModel
}
func (c *BaseController) Prepare() {
//附值
c.controllerName, c.actionName = c.GetControllerAndAction()
// TODO 保存用户数据
}
// 设置模板
// 第一个参数模板,第二个参数为layout
func (c *BaseController) setTpl(template ...string) {
var tplName string
layout := "shared/layout_page.html"
switch {
case len(template) == 1:
tplName = template[0]
case len(template) == 2:
tplName = template[0]
layout = template[1]
default:
//不要"Controller"这个10个字母
ctrlName := strings.ToLower(c.controllerName[0 : len(c.controllerName)-10])
actionName := strings.ToLower(c.actionName)
tplName = ctrlName + "/" + actionName + ".html"
}
c.Layout = layout
c.TplName = tplName
}
func (c *BaseController) jsonResult(code consts.JsonResultCode, msg string, obj interface{}) {
r := &models.JsonResult{code, msg, obj}
c.Data["json"] = r
c.ServeJSON()
c.StopRun()
}
// 重定向
func (c *BaseController) redirect(url string) {
c.Redirect(url, 302)
c.StopRun()
}
// 重定向 去错误页
func (c *BaseController) pageError(msg string) {
errorurl := c.URLFor("HomeController.Error") + "/" + msg
c.Redirect(errorurl, 302)
c.StopRun()
}
// 重定向 去登录页
func (c *BaseController) pageLogin() {
url := c.URLFor("HomeController.Login")
c.Redirect(url, 302)
c.StopRun()
}

1
controllers/DetailController.go

@ -0,0 +1 @@
package controllers

1
controllers/ListController.go

@ -0,0 +1 @@
package controllers

27
controllers/MenuController.go

@ -0,0 +1,27 @@
package controllers
import (
"github.com/ziyoubiancheng/xcms/consts"
"github.com/ziyoubiancheng/xcms/models"
)
type MenuController struct {
BaseController
}
func (c *MenuController) Index() {
models.MenuModel.List()
c.jsonResult(consts.JRCodeSucc, "1", "b")
}
func (c *MenuController) Add() {
c.jsonResult(consts.JRCodeSucc, "1", "b")
}
func (c *MenuController) Delete() {
c.jsonResult(consts.JRCodeSucc, "1", "b")
}
func (c *MenuController) Edit() {
c.jsonResult(consts.JRCodeSucc, "1", "b")
}

1
controllers/RoleController.go

@ -0,0 +1 @@
package controllers

1
controllers/UserController.go

@ -0,0 +1 @@
package controllers

30
controllers/default.go

@ -1,11 +1,10 @@
package controllers
import (
"database/sql"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql" // import your used driver
)
@ -14,13 +13,26 @@ type MainController struct {
}
func (c *MainController) Get() {
orm.RegisterDataBase("default", "mysql", "root:135246@tcp(127.0.0.1:3307)/xcms?charset=utf8", 30)
// o := orm.NewOrm()
// var r RawSeter
//r := o.Raw("INSERT INTO xcms_formats (name) VALUES ('?');", "test2").Exec()
fmt.Println("hello")
// orm.RegisterDataBase("default", "mysql", "root:135246@tcp(127.0.0.1:3307)/xcms?charset=utf8", 30)
// // o := orm.NewOrm()
// // var r RawSeter
// //r := o.Raw("INSERT INTO xcms_formats (name) VALUES ('?');", "test2").Exec()
// fmt.Println("hello")
db, err := sql.Open("mysql", "root:135246@tcp(127.0.0.1:3307)/xcms?charset=utf8")
if err != nil {
fmt.Println(err)
}
defer db.Close()
rows, _ := db.Query("select mid,name from xcms_menu limit 10")
mid := 0
name := " "
for rows.Next() {
rows.Scan(&mid, &name)
fmt.Println(mid, name)
}
c.Data["Website"] = "beego.me"
c.Data["Email"] = "astaxie@gmail.com"

BIN
models/.DS_Store vendored

Binary file not shown.

21
models/BaseModel.go

@ -0,0 +1,21 @@
package models
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type BaseModel struct {
}
func (m *BaseModel) exec(query string, args ...interface{}) (*sql.Rows, error) {
db, err := sql.Open("mysql", "root:135246@tcp(127.0.0.1:3307)/xcms?charset=utf8") //TODO
if err != nil {
fmt.Println(err)
}
defer db.Close()
return db.Query(query, args...)
}

12
models/Common.go

@ -0,0 +1,12 @@
package models
import (
"github.com/ziyoubiancheng/xcms/consts"
)
// JsonResult 用于返回ajax请求的基类
type JsonResult struct {
Code consts.JsonResultCode `json:"code"`
Msg string `json:"msg"`
Obj interface{} `json:"obj"`
}

24
models/MenuModel.go

@ -0,0 +1,24 @@
package models
import (
"fmt"
)
type MenuModel struct {
BaseModel
}
func (m *MenuModel) TableName() string {
return "xcms_menu"
}
func (m *MenuModel) List() {
rows, err := m.BaseModel.exec("select mid,name from xcms_menu limit 10")
mid := 0
name := " "
for rows.Next() {
rows.Scan(&mid, &name)
fmt.Println(mid, name)
}
fmt.Println(err)
}

8
models/init.go

@ -1,8 +0,0 @@
package models
// "github.com/astaxie/beego"
// "github.com/astaxie/beego/orm"
func init() {
// orm.RegisterModel(new(Test))
}

5
routers/router.go

@ -7,4 +7,9 @@ import (
func init() {
beego.Router("/", &controllers.MainController{})
//menu
beego.Router("/menu", &controllers.MenuController{}, "Get:Index")
beego.Router("/menu/add", &controllers.MenuController{}, "Post:Add")
beego.Router("/menu/edit", &controllers.MenuController{}, "Post:Edit")
beego.Router("/menu/delete", &controllers.MenuController{}, "Post:Delete")
}

31
sysinit/initDB.go

@ -1,31 +0,0 @@
package sysinit
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/ziyoubiancheng/xcms/models"
)
func initDB() {
//连接名称
dbAlias := beego.AppConfig.String("db_alias")
//数据库名称
dbName := beego.AppConfig.String("db_name")
//数据库连接用户名
dbUser := beego.AppConfig.String("db_user")
//数据库连接用户名
dbPwd := beego.AppConfig.String("db_pwd")
//数据库IP(域名)
dbHost := beego.AppConfig.String("db_host")
//数据库端口
dbPort := beego.AppConfig.String("db_port")
//数据库编码
dbCharset := beego.AppConfig.String("db_charset")
orm.RegisterDataBase(dbAlias, "mysql", dbUser+":"+dbPwd+"@tcp("+dbHost+":"+dbPort+")/"+dbName+"?charset="+dbCharset, 30)
isDev := (beego.AppConfig.String("runmode") == "dev")
if isDev {
orm.Debug = isDev
}
}

2
sysinit/sysinit.go

@ -1,5 +1,5 @@
package sysinit
func init() {
initDB()
}

BIN
xcms

Binary file not shown.
Loading…
Cancel
Save