拓展类型

This commit is contained in:
2024-09-25 17:45:38 +08:00
parent a0e898e46c
commit 3b69ba93ad
15 changed files with 214 additions and 40 deletions
+1
View File
@@ -22,6 +22,7 @@ func InitDB() {
&models.History{},
&models.Users{},
&models.UserFollows{},
&models.BaseCard{},
)
if err != nil {
panic(err)
+3 -2
View File
@@ -1,7 +1,6 @@
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
@@ -12,6 +11,7 @@ import (
"work_cation/views"
)
// 本地缓存 上次打开目录
const preferenceCurrentTutorial = "currentTutorial"
func main() {
@@ -102,13 +102,14 @@ func makeNav(setTutorial func(tutorial views.Tutorial), loadPrevious bool) fyne.
if unsupportedTutorial(t) {
return
}
// 存储默认打开
a.Preferences().SetString(preferenceCurrentTutorial, uid)
fmt.Println(t)
setTutorial(t)
}
},
}
// 读取存储默认打开
if loadPrevious {
currentPref := a.Preferences().StringWithFallback(preferenceCurrentTutorial, "welcome")
tree.Select(currentPref)
+23
View File
@@ -0,0 +1,23 @@
package models
import (
"time"
"work_cation/pkg/gormx"
)
type BaseCard struct {
UUID string `json:"uuid"` // id ip加时间戳生成
UserID string `json:"user_id"` // 用户id 标识
Title string `json:"title"` // 标题
Text string `json:"text"` // 内容
Covers gormx.ListString `json:"covers"` // 封面
ToolType string `json:"tool_type"` // 工具类型
UpdateTx time.Time `json:"update_tx"` // 更新时间
Number int `json:"number"` // 数量
Goods int `json:"goods"` // 点赞数
Collection int `json:"collection"` // 收藏数
}
const (
ToolTypeErlang = "erlangCard" // 卡片
)
+1 -5
View File
@@ -4,7 +4,7 @@ import "work_cation/pkg/gormx"
type ErlangCards struct {
UUID string `json:"uuid"` // id ip加时间戳生成
UserIp string `json:"user_ip"` // 用户ip
UserID string `json:"user_id"` // 用户id
Title string `json:"title"` // 标题
Text string `json:"text"` // 内容
Covers gormx.ListString `json:"covers"` // 封面
@@ -12,8 +12,4 @@ type ErlangCards struct {
VarName gormx.ListString `json:"var_name"` // 变量
VarContent gormx.ListString `json:"var_content"` // 变量内容
IsShowOut bool `json:"is_show_out"` // 是否展示
Number int `json:"number"` // 数量
Goods int `json:"goods"` // 点赞
Collection int `json:"collection"` // 收藏
}
+2 -1
View File
@@ -1,7 +1,8 @@
package models
type Users struct {
Ip string `gorm:"primarykey" json:"ip"`
ID string `gorm:"primarykey" json:"id"`
Ip string `json:"ip"`
Name string `json:"name"` // 昵称
Avatar string `json:"avatar"` // 头像
Cover string `json:"cover"` // 封面
+20
View File
@@ -0,0 +1,20 @@
package repo
import (
"gorm.io/gorm"
"work_cation/models"
)
type baseCardRepo struct{}
var BaseCard *baseCardRepo
func (*baseCardRepo) FindAll(db *gorm.DB) []models.BaseCard {
var cards []models.BaseCard
db.Find(&cards)
return cards
}
func (*baseCardRepo) Create(db *gorm.DB, baseCard *models.BaseCard) error {
return db.Create(baseCard).Error
}
+6
View File
@@ -15,6 +15,12 @@ func (*erlangCard) FindAll(db *gorm.DB) []models.ErlangCards {
return cards
}
func (*erlangCard) Find(db *gorm.DB, uuid string) models.ErlangCards {
var card models.ErlangCards
db.Where("uuid = ?", uuid).First(&card)
return card
}
func (*erlangCard) Create(db *gorm.DB, erlangCard *models.ErlangCards) error {
return db.Create(erlangCard).Error
}
+3 -1
View File
@@ -13,14 +13,16 @@ var User *userRepo
func (*userRepo) GetUserInfo(db *gorm.DB) *models.Users {
var users models.Users
db.Find(&users)
ip := utils.IP.Get192Ip()
db.Where("ip = ?", ip).Find(&users)
if users.Ip != ip {
current, err := user.Current()
if err != nil {
current = &user.User{Name: ip}
}
users = models.Users{
ID: utils.Uuid.CreateUUID(),
Ip: ip,
Name: current.Username,
Avatar: "",
+22 -4
View File
@@ -2,8 +2,10 @@ package service
import (
"encoding/json"
"gorm.io/gorm"
"os"
"path/filepath"
"time"
"work_cation/cfg"
"work_cation/global"
"work_cation/models"
@@ -31,11 +33,27 @@ func (*erlangCardService) Create(erlangCard *models.ErlangCards) error {
if err != nil {
return err
}
err = repo.ErlangCardRepo.Create(global.DB, erlangCard)
if err != nil {
return global.DB.Transaction(func(tx *gorm.DB) error {
err = repo.ErlangCardRepo.Create(global.DB, erlangCard)
if err != nil {
return err
}
var baseCard = models.BaseCard{
UUID: erlangCard.UUID,
UserID: erlangCard.UserID,
Title: erlangCard.Title,
Text: erlangCard.Text,
Covers: erlangCard.Covers,
ToolType: models.ToolTypeErlang,
UpdateTx: time.Now(),
Number: 0,
Goods: 0,
Collection: 0,
}
err = repo.BaseCard.Create(global.DB, &baseCard)
return err
}
return nil
})
}
func (*erlangCardService) GetInfoPath(erlangCard *models.ErlangCards) string {
+30
View File
@@ -0,0 +1,30 @@
package views
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"sort"
)
// CreateCards 创建卡片列表
func CreateCards(w fyne.Window) fyne.CanvasObject {
var itemList []fyne.CanvasObject
keys := make([]string, 0, len(CardTypeMap))
for key, _ := range CardTypeMap {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
info := CardTypeMap[key]
itemList = append(itemList, widget.NewButton(info.Name, func() {
info := CardTypeMap[key]
cWin := fyne.CurrentApp().NewWindow(info.Name)
cWin.SetContent(info.createView(cWin))
cWin.Resize(info.createSize)
cWin.CenterOnScreen()
cWin.Show()
}))
}
return container.NewCenter(container.NewVBox(itemList...))
}
@@ -1,4 +1,4 @@
package views
package createView
import (
"fmt"
@@ -9,8 +9,10 @@ import (
"fyne.io/fyne/v2/widget"
"regexp"
"strings"
"work_cation/global"
"work_cation/models"
"work_cation/pkg/utils"
"work_cation/repo"
"work_cation/service"
)
@@ -20,7 +22,7 @@ func CreateErlangCard(w fyne.Window) fyne.CanvasObject {
if erlangCard == nil {
erlangCard = &models.ErlangCards{
UUID: utils.Uuid.CreateUUID(),
UserIp: utils.IP.Get192Ip(),
UserID: repo.User.GetUserInfo(global.DB).ID,
}
}
var (
+57
View File
@@ -0,0 +1,57 @@
package createView
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"work_cation/global"
"work_cation/models"
"work_cation/pkg/utils"
"work_cation/repo"
"work_cation/service"
)
func CreateExecFile(w fyne.Window) fyne.CanvasObject {
if erlangCard == nil {
erlangCard = &models.ErlangCards{
UUID: utils.Uuid.CreateUUID(),
UserID: repo.User.GetUserInfo(global.DB).ID,
}
}
var (
// 基础表单
formBase = &widget.Form{}
// 变量表单
formVars = &widget.Form{}
)
formBase.AppendItem(&widget.FormItem{Text: "名称", Widget: newDefaultEntry(&erlangCard.Title)})
formBase.AppendItem(&widget.FormItem{Text: "描述", Widget: newDefaultEntry(&erlangCard.Text)})
formBase.AppendItem(&widget.FormItem{Text: "封面", Widget: newDefaultEntry(&erlangCard.Covers), HintText: "暂未支持可不填"})
formBase.AppendItem(&widget.FormItem{Text: "是否默认展示", Widget: newDefaultEntry(&erlangCard.IsShowOut)})
formBase.AppendItem(&widget.FormItem{Text: "模版内容", Widget: newDefaultEntryCallback(&erlangCard.Template, func() { generateVariableForm1(erlangCard, formVars) })})
// 刷新变量表单
generateVariableForm1(erlangCard, formVars)
// 保存按钮
buttonSave := widget.NewButton("保存", func() {
err := service.ErlangCard.Create(erlangCard)
if err != nil {
dialog.ShowError(err, w)
return
}
dialog.ShowInformation("ok", "创建成功", w)
// 更换
erlangCard.UUID = utils.Uuid.CreateUUID()
})
return container.NewBorder(container.NewHBox(widget.NewLabel("新建"), widget.NewSeparator()),
nil, nil, nil, container.NewScroll(
container.NewVBox(
formBase,
formVars,
buttonSave,
)))
}
+18 -1
View File
@@ -2,6 +2,8 @@ package views
import (
"fyne.io/fyne/v2"
"work_cation/models"
"work_cation/views/createView"
)
// Tutorial 定义教程的数据结构
@@ -16,7 +18,7 @@ var (
Tutorials = map[string]Tutorial{
"welcome": {"主页", "", UserViews, true},
"canvas": {"我的", "", myCardsViews, true},
"create": {"新建", "", CreateErlangCard, true},
"create": {"新建", "", CreateCards, true},
}
// TutorialIndex 定义我们的教程应该如何在索引树中布局
@@ -27,3 +29,18 @@ var (
//"widgets": {"accordion", "button", "card", "entry", "form", "input", "progress", "text", "toolbar"},
}
)
type cardInfo struct {
Name string
createView func(w fyne.Window) fyne.CanvasObject
createSize fyne.Size
}
var (
CardTypeMap = map[string]cardInfo{
models.ToolTypeErlang: {"erlang代码脚本1", createView.CreateErlangCard, fyne.NewSize(600, 400)},
"2": {"erlang代码脚本2", createView.CreateErlangCard, fyne.NewSize(600, 400)},
"3": {"erlang代码脚本3", createView.CreateErlangCard, fyne.NewSize(600, 400)},
"4": {"erlang代码脚本4", createView.CreateErlangCard, fyne.NewSize(600, 400)},
}
)
+23
View File
@@ -0,0 +1,23 @@
package views
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"work_cation/global"
"work_cation/models"
"work_cation/repo"
"work_cation/views/showView"
)
func myCardsViews(w fyne.Window) fyne.CanvasObject {
var items []fyne.CanvasObject
for _, baseCard := range repo.BaseCard.FindAll(global.DB) {
switch baseCard.ToolType {
case models.ToolTypeErlang:
erlangCard := repo.ErlangCardRepo.Find(global.DB, baseCard.UUID)
items = append(items, showView.ErlangCardView(w, &erlangCard))
}
}
return container.NewBorder(nil, nil, nil, nil, container.NewScroll(
container.NewGridWrap(fyne.NewSize(200, 200), items...)))
}
@@ -1,4 +1,4 @@
package views
package showView
import (
"fyne.io/fyne/v2"
@@ -8,33 +8,10 @@ import (
"fyne.io/fyne/v2/widget"
"github.com/atotto/clipboard"
"strings"
"work_cation/global"
"work_cation/models"
"work_cation/pkg/utils"
"work_cation/repo"
)
func myCardsViews(w fyne.Window) fyne.CanvasObject {
var items []fyne.CanvasObject
for _, i := range repo.ErlangCardRepo.FindAll(global.DB) {
var i2 = i
items = append(items, ErlangCardView(w, &i2))
}
return container.NewBorder(nil, nil, nil, nil, container.NewScroll(
container.NewGridWrap(fyne.NewSize(200, 200), items...)))
}
func ErlangCardView(w fyne.Window, data *models.ErlangCards) fyne.CanvasObject {
if data == nil {
data = &models.ErlangCards{
UUID: utils.Uuid.CreateUUID(),
Title: "玩家获取",
Text: "玩家获取脚本",
VarName: []string{"服务器ID", "玩家UID"},
Template: "z_db_lib:get(z_db_lib:get_table('role', $var), $var).",
}
}
if len(data.VarName) == 0 {
return widget.NewCard(data.Title, data.Text, widget.NewButton("复制", func() {
err := clipboard.WriteAll(data.Template)