1.需求
公司有一套系统,其中license数量(加密存储)有几项值是客户购买才能获得的,正常流程为客户购买,运维部准备一个UKEY,由系统灌注license相关数据送到客户现场,客户将UKEY插入服务器进行授权。
现需要开发一个小工具,供测试使用,测试输入liciense几项数据,生成对应密文数据。以下为另一个产品小工具截图
2.实现
上述图片为python开发,由于本人不会python,也不想做成黑色命令框那种。于是找了下go的gui库,发现fyne.io/fyne/v2使用率较高,简单看了下介绍感觉不错,便决定使用该库。
2.1解决中文乱码问题
func init() {
//设置中文字体:解决中文乱码问题
_ = sm4.SetIV(ivCbc)
fontPaths := findfont.List()
for _, path := range fontPaths {
if strings.Contains(path, "msyh.ttf") || strings.Contains(path, "simhei.ttf") || strings.Contains(path, "simsun.ttc") || strings.Contains(path, "simkai.ttf") {
os.Setenv("FYNE_FONT", path)
break
}
}
}
2.2 数字和文本框一行显示
重点为layout.NewFormLayout()
pcEntry := widget.NewEntry()
pcEntry.SetPlaceHolder("输入pc端license数量")
pcLabel := widget.NewLabel(" PC端数量:")
pcContainer := fyne.NewContainerWithLayout(layout.NewFormLayout(), pcLabel, pcEntry)
2.3 按钮居中
fyne.NewContainerWithLayout(layout.NewCenterLayout(), myButton)
2.4设置布局大小
fyne.NewContainerWithLayout(layout.NewGridWrapLayout(fyne.NewSize(500, 110)), resEntry)
2.5 代码
// Path:
// FileName: main.go
// Created by dkedTeam
// Author: GJing
// Date: 2024/2/22$ 17:16$
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/flopp/go-findfont"
"os"
"strconv"
"strings"
)
func init() {
//设置中文字体:解决中文乱码问题
_ = sm4.SetIV(ivCbc)
fontPaths := findfont.List()
for _, path := range fontPaths {
if strings.Contains(path, "msyh.ttf") || strings.Contains(path, "simhei.ttf") || strings.Contains(path, "simsun.ttc") || strings.Contains(path, "simkai.ttf") {
os.Setenv("FYNE_FONT", path)
break
}
}
}
// 打包命令:go build -ldflags="-H windowsgui" main.go
func main() {
a := app.New()
a.Settings().SetTheme(theme.DefaultTheme())
myWindow := a.NewWindow("mck license授权工具,禁止外传!")
myWindow.Resize(fyne.NewSize(400, 300))
//手机端
phoneEntry := widget.NewEntry()
//myLayout.MinSize([]fyne.CanvasObject{phoneEntry})
phoneEntry.SetPlaceHolder("输入手机端license数量")
phoneLabel := widget.NewLabel(" 手机端数量:")
phoneContainer := fyne.NewContainerWithLayout(layout.NewFormLayout(), phoneLabel, phoneEntry)
fmt.Println("---------", phoneContainer.Size())
//PC端
pcEntry := widget.NewEntry()
pcEntry.SetPlaceHolder("输入pc端license数量")
pcLabel := widget.NewLabel(" PC端数量:")
pcContainer := fyne.NewContainerWithLayout(layout.NewFormLayout(), pcLabel, pcEntry)
//类型 1永久2临时
//licenseType := widget.NewEntry()
//licenseType.SetPlaceHolder("final:永久,temp:临时")
licenseTypeLable := widget.NewLabel(" 授权类型:")
//typeContainer := fyne.NewContainerWithLayout(layout.NewFormLayout(), licenseTypeLable, licenseType)
typeSelect := widget.NewSelect([]string{"永久", "试用"}, func(s string) {
})
typeSelect.SetSelected("永久")
typeContainer1 := fyne.NewContainerWithLayout(layout.NewFormLayout(), licenseTypeLable, typeSelect)
//日期
date := widget.NewEntry()
date.SetPlaceHolder("YYYY-MM-DD(ex:2024-01-14)")
dateLabel := widget.NewLabel("试用截止日期:")
dateContainer := fyne.NewContainerWithLayout(layout.NewFormLayout(), dateLabel, date)
//加密结果
resEntry := widget.NewEntry()
//myLayout.MinSize([]fyne.CanvasObject{phoneEntry})
resEntry.SetPlaceHolder("mck 授权工具,仅限测试内容使用,禁止外传!!!" +
"\n使用说明:" +
"\n1.按提示输入需要加密的字段" +
"\n2.点击按钮后生成对应字段对应密文" +
"\n3.将生成的密文数据存储到数据库mck_config表对应字段,重启mck-server容器")
//加密按钮,影响加密内容
myButton := widget.NewButtonWithIcon("加密运算", theme.StorageIcon(), func() {
var phoneEnNum, pcEnNum, typeEnNum, dateEnNum string
if len(phoneEntry.Text) > 0 {
phoneNum, _ := strconv.Atoi(phoneEntry.Text)
phoneEnNum = LicenseCipher(phoneCbc, phoneNum)
}
if len(pcEntry.Text) > 0 {
pcNum, _ := strconv.Atoi(pcEntry.Text)
pcEnNum = LicenseCipher(pcCbc, pcNum)
}
typeStr := typeSelect.Selected
if typeStr == "试用" {
typeEnNum = SetLicenseStrValue("temp")
} else {
typeEnNum = SetLicenseStrValue("final")
}
if len(date.Text) > 0 {
dateEnNum = SetLicenseStrValue(date.Text)
}
resEntry.SetText("license_phone:" + phoneEnNum + "" +
"\n" + "license_pc:" + pcEnNum + "" +
"\n" + "license_type:" + typeEnNum + "" +
"\n" + "license_deadline_time:" + dateEnNum)
})
content := container.NewVBox(
phoneContainer,
pcContainer,
typeContainer1,
dateContainer,
fyne.NewContainerWithLayout(layout.NewCenterLayout(), myButton),
fyne.NewContainerWithLayout(layout.NewGridWrapLayout(fyne.NewSize(500, 110)), resEntry),
)
myWindow.SetContent(content)
myWindow.ShowAndRun()
}
func LicenseCipher(key []byte, num int) string {
return ""
}
func SetLicenseStrValue(v string) string {
return ""
}
评论区