实例
// Copyright 2013 The Walk Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package mainimport ( "encoding/json" "github.com/lxn/walk" . "github.com/lxn/walk/declarative" "log" "net/url" "strings" "work/app/common/utils/appcryption")type MyMainWindow struct { *walk.MainWindow te *walk.TextEdit teEncrypt *walk.TextEdit}func main() { mw := &MyMainWindow{} if _, err := (MainWindow{ AssignTo: &mw.MainWindow, Title: "APP数据加解密工具", //Size: Size{Width: 600, Height: 600}, Layout: VBox{ Margins:Margins{ Left: 100, Top: 20, Right: 30, Bottom: 40, }, }, Children: []Widget{ HSplitter{ Children: []Widget{ TextEdit{ AssignTo: &mw.te, ReadOnly: false, MaxLength:9999999, }, TextEdit{ AssignTo: &mw.teEncrypt, ReadOnly: true, Name: "testss张三", }, }, }, HSplitter{ Children: []Widget{ PushButton{ Text: "请求数据", MinSize: Size{Width: 50, Height: 30}, MaxSize: Size{Width: 50, Height: 30}, Row: 3, Column: 5, ColumnSpan: 2, OnClicked: mw.getResultReq, Font: Font{ Family: "宋体", PointSize: 13, Bold: true, Italic: false, Underline: false, StrikeOut: false, }, }, PushButton{ Text: "结果数据", MinSize: Size{Width: 50, Height: 30}, MaxSize: Size{Width: 50, Height: 30}, Row: 3, OnClicked: mw.getResultRes, }, }, }, }, }).Run(); err != nil { log.Fatal(err) }}func (mw *MyMainWindow) decrypt(data string) string { datanew := strings.Replace(data, "\\", "", strings.Count(data, "\\")) //urldecode if strings.Count(data, "%") > 0 { datanew, _ = url.QueryUnescape(data) } datanew = strings.Trim(datanew, " ") if datanew == "" { walk.MsgBox(mw, "提示", "数据为空", walk.MsgBoxIconWarning) return "" } //解析datas数据 jsonStr := appcryption.AesCryption().Decryption(datanew) if jsonStr == nil { walk.MsgBox(mw, "错误", "数据解析失败", walk.MsgBoxIconError) return "" } return string(jsonStr)}func (mw *MyMainWindow) getResultReq() { _ = mw.teEncrypt.SetText("") res := mw.decrypt(mw.te.Text()) _ = mw.teEncrypt.SetText(res)}type resStruct struct { Data string `json:"data"` Sign string `json:"sign"`}func (mw *MyMainWindow) getResultRes() { _ = mw.teEncrypt.SetText("") res := mw.decrypt(mw.te.Text()) if res == "" { return } tp := &resStruct{} err := json.Unmarshal([]byte(res), tp) if err != nil { walk.MsgBox(mw, "错误", "数据解析失败:"+err.Error(), walk.MsgBoxIconError) return } if tp.Data == "" { walk.MsgBox(mw, "错误", "数据解析失败:返回结果data为空", walk.MsgBoxIconError) return } res1 := mw.decrypt(tp.Data) if res1 == "" { return } _ = mw.teEncrypt.SetText(res1)}