mirror of https://github.com/bjdgyc/anylink.git
修改表结构
This commit is contained in:
parent
712f57940c
commit
88a3d35784
server/dbdata
|
@ -1,8 +1,6 @@
|
|||
package dbdata
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/bjdgyc/anylink/base"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
|
@ -41,17 +39,24 @@ func initData() {
|
|||
)
|
||||
|
||||
// 判断是否初次使用
|
||||
s := &Setting{}
|
||||
err = One("name", InstallName, s)
|
||||
if err == nil && s.Data == InstallData {
|
||||
install := &SettingInstall{}
|
||||
err = SettingGet(install)
|
||||
|
||||
if err == nil && install.Installed {
|
||||
// 已经安装过
|
||||
return
|
||||
}
|
||||
|
||||
// 发生错误
|
||||
if err != ErrNotFound {
|
||||
base.Fatal(err)
|
||||
}
|
||||
|
||||
err = addInitData()
|
||||
if err != nil {
|
||||
base.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func addInitData() error {
|
||||
|
@ -74,9 +79,7 @@ func addInitData() error {
|
|||
From: "vpn@xx.com",
|
||||
Encryption: "None",
|
||||
}
|
||||
v, _ := json.Marshal(smtp)
|
||||
s := &Setting{Name: StructName(smtp), Data: string(v)}
|
||||
_, err = sess.InsertOne(s)
|
||||
err = SettingSessAdd(sess, smtp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -87,16 +90,14 @@ func addInitData() error {
|
|||
Banner: "您已接入公司网络,请按照公司规定使用。\n请勿进行非工作下载及视频行为!",
|
||||
AccountMail: accountMail,
|
||||
}
|
||||
v, _ = json.Marshal(other)
|
||||
s = &Setting{Name: StructName(other), Data: string(v)}
|
||||
_, err = sess.InsertOne(s)
|
||||
err = SettingSessAdd(sess, other)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Install
|
||||
install := &Setting{Name: InstallName, Data: InstallData}
|
||||
_, err = sess.InsertOne(install)
|
||||
install := &SettingInstall{Installed: true}
|
||||
err = SettingSessAdd(sess, install)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -3,12 +3,12 @@ package dbdata
|
|||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
const (
|
||||
InstallName = "Install"
|
||||
InstallData = "OK"
|
||||
)
|
||||
type SettingInstall struct {
|
||||
Installed bool `json:"installed"`
|
||||
}
|
||||
|
||||
type SettingSmtp struct {
|
||||
Host string `json:"host"`
|
||||
|
@ -36,29 +36,30 @@ func StructName(data interface{}) string {
|
|||
return name
|
||||
}
|
||||
|
||||
func SettingAdd(data interface{}) error {
|
||||
func SettingSessAdd(sess *xorm.Session, data interface{}) error {
|
||||
name := StructName(data)
|
||||
v, _ := json.Marshal(data)
|
||||
s := Setting{Name: name, Data: string(v)}
|
||||
err := Add(&s)
|
||||
s := &Setting{Name: name, Data: v}
|
||||
_, err := sess.InsertOne(s)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func SettingSet(data interface{}) error {
|
||||
name := StructName(data)
|
||||
v, _ := json.Marshal(data)
|
||||
s := Setting{Data: string(v)}
|
||||
err := Update("name", name, &s)
|
||||
s := &Setting{Data: v}
|
||||
err := Update("name", name, s)
|
||||
return err
|
||||
}
|
||||
|
||||
func SettingGet(data interface{}) error {
|
||||
name := StructName(data)
|
||||
s := Setting{Name: name}
|
||||
err := One("name", name, &s)
|
||||
s := &Setting{Name: name}
|
||||
err := One("name", name, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal([]byte(s.Data), data)
|
||||
err = json.Unmarshal(s.Data, data)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package dbdata
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
Id int `json:"id" xorm:"pk autoincr not null"`
|
||||
Name string `json:"name" xorm:"varchar(60) not null unique(inx_name)"`
|
||||
Name string `json:"name" xorm:"varchar(60) not null unique"`
|
||||
Note string `json:"note" xorm:"varchar(255)"`
|
||||
AllowLan bool `json:"allow_lan" xorm:"Bool"`
|
||||
ClientDns []ValData `json:"client_dns" xorm:"Text"`
|
||||
|
@ -21,7 +22,7 @@ type Group struct {
|
|||
|
||||
type User struct {
|
||||
Id int `json:"id" xorm:"pk autoincr not null"`
|
||||
Username string `json:"username" xorm:"varchar(60) not null unique(inx_username)"`
|
||||
Username string `json:"username" xorm:"varchar(60) not null unique"`
|
||||
Nickname string `json:"nickname" xorm:"varchar(255)"`
|
||||
Email string `json:"email" xorm:"varchar(255)"`
|
||||
// Password string `json:"password"`
|
||||
|
@ -37,8 +38,8 @@ type User struct {
|
|||
|
||||
type IpMap struct {
|
||||
Id int `json:"id" xorm:"pk autoincr not null"`
|
||||
IpAddr string `json:"ip_addr" xorm:"varchar(32) not null unique(inx_ip_addr)"`
|
||||
MacAddr string `json:"mac_addr" xorm:"varchar(32) not null unique(inx_mac_addr)"`
|
||||
IpAddr string `json:"ip_addr" xorm:"varchar(32) not null unique"`
|
||||
MacAddr string `json:"mac_addr" xorm:"varchar(32) not null unique"`
|
||||
Username string `json:"username" xorm:"varchar(60)"`
|
||||
Keep bool `json:"keep" xorm:"Bool"` // 保留 ip-mac 绑定
|
||||
KeepTime time.Time `json:"keep_time" xorm:"DateTime"`
|
||||
|
@ -48,8 +49,8 @@ type IpMap struct {
|
|||
}
|
||||
|
||||
type Setting struct {
|
||||
Id int `json:"id" xorm:"pk autoincr not null"`
|
||||
Name string `json:"name" xorm:"varchar(60) not null unique(inx_name)"`
|
||||
Data string `json:"data" xorm:"Text"`
|
||||
UpdatedAt time.Time `json:"updated_at" xorm:"DateTime updated"`
|
||||
Id int `json:"id" xorm:"pk autoincr not null"`
|
||||
Name string `json:"name" xorm:"varchar(60) not null unique"`
|
||||
Data json.RawMessage `json:"data" xorm:"Text"`
|
||||
UpdatedAt time.Time `json:"updated_at" xorm:"DateTime updated"`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue