实例

  1. // Copyright 2013 The Walk Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "encoding/json"
  7. "github.com/lxn/walk"
  8. . "github.com/lxn/walk/declarative"
  9. "log"
  10. "net/url"
  11. "strings"
  12. "work/app/common/utils/appcryption"
  13. )
  14. type MyMainWindow struct {
  15. *walk.MainWindow
  16. te *walk.TextEdit
  17. teEncrypt *walk.TextEdit
  18. }
  19. func main() {
  20. mw := &MyMainWindow{}
  21. if _, err := (MainWindow{
  22. AssignTo: &mw.MainWindow,
  23. Title: "APP数据加解密工具",
  24. //Size: Size{Width: 600, Height: 600},
  25. Layout: VBox{
  26. Margins:Margins{
  27. Left: 100,
  28. Top: 20,
  29. Right: 30,
  30. Bottom: 40,
  31. },
  32. },
  33. Children: []Widget{
  34. HSplitter{
  35. Children: []Widget{
  36. TextEdit{
  37. AssignTo: &mw.te,
  38. ReadOnly: false,
  39. MaxLength:9999999,
  40. },
  41. TextEdit{
  42. AssignTo: &mw.teEncrypt,
  43. ReadOnly: true,
  44. Name: "testss张三",
  45. },
  46. },
  47. },
  48. HSplitter{
  49. Children: []Widget{
  50. PushButton{
  51. Text: "请求数据",
  52. MinSize: Size{Width: 50, Height: 30},
  53. MaxSize: Size{Width: 50, Height: 30},
  54. Row: 3,
  55. Column: 5,
  56. ColumnSpan: 2,
  57. OnClicked: mw.getResultReq,
  58. Font: Font{
  59. Family: "宋体",
  60. PointSize: 13,
  61. Bold: true,
  62. Italic: false,
  63. Underline: false,
  64. StrikeOut: false,
  65. },
  66. },
  67. PushButton{
  68. Text: "结果数据",
  69. MinSize: Size{Width: 50, Height: 30},
  70. MaxSize: Size{Width: 50, Height: 30},
  71. Row: 3,
  72. OnClicked: mw.getResultRes,
  73. },
  74. },
  75. },
  76. },
  77. }).Run(); err != nil {
  78. log.Fatal(err)
  79. }
  80. }
  81. func (mw *MyMainWindow) decrypt(data string) string {
  82. datanew := strings.Replace(data, "\\", "", strings.Count(data, "\\"))
  83. //urldecode
  84. if strings.Count(data, "%") > 0 {
  85. datanew, _ = url.QueryUnescape(data)
  86. }
  87. datanew = strings.Trim(datanew, " ")
  88. if datanew == "" {
  89. walk.MsgBox(mw, "提示", "数据为空", walk.MsgBoxIconWarning)
  90. return ""
  91. }
  92. //解析datas数据
  93. jsonStr := appcryption.AesCryption().Decryption(datanew)
  94. if jsonStr == nil {
  95. walk.MsgBox(mw, "错误", "数据解析失败", walk.MsgBoxIconError)
  96. return ""
  97. }
  98. return string(jsonStr)
  99. }
  100. func (mw *MyMainWindow) getResultReq() {
  101. _ = mw.teEncrypt.SetText("")
  102. res := mw.decrypt(mw.te.Text())
  103. _ = mw.teEncrypt.SetText(res)
  104. }
  105. type resStruct struct {
  106. Data string `json:"data"`
  107. Sign string `json:"sign"`
  108. }
  109. func (mw *MyMainWindow) getResultRes() {
  110. _ = mw.teEncrypt.SetText("")
  111. res := mw.decrypt(mw.te.Text())
  112. if res == "" {
  113. return
  114. }
  115. tp := &resStruct{}
  116. err := json.Unmarshal([]byte(res), tp)
  117. if err != nil {
  118. walk.MsgBox(mw, "错误", "数据解析失败:"+err.Error(), walk.MsgBoxIconError)
  119. return
  120. }
  121. if tp.Data == "" {
  122. walk.MsgBox(mw, "错误", "数据解析失败:返回结果data为空", walk.MsgBoxIconError)
  123. return
  124. }
  125. res1 := mw.decrypt(tp.Data)
  126. if res1 == "" {
  127. return
  128. }
  129. _ = mw.teEncrypt.SetText(res1)
  130. }