实例

  1. /**
  2. * @Author: XGH
  3. * @Email: 55821284@qq.com
  4. * @Date: 2019/12/13 9:42
  5. */
  6. package main
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "github.com/xgh2012/tools/xencryp"
  13. "github.com/xgh2012/tools/xsort"
  14. "github.com/zserge/webview"
  15. "time"
  16. )
  17. type myForm struct {
  18. Colors []string `form:"colors[]"`
  19. }
  20. func main() {
  21. r := gin.Default()
  22. gin.SetMode(gin.DebugMode)
  23. //设置HTML目录
  24. r.LoadHTMLGlob("html/*")
  25. //单独加载某个HTML文件也可以使用以下
  26. //r.LoadHTMLFiles("public/html/index.html")
  27. r.GET("/index", index)
  28. r.POST("/index", formHandler)
  29. //启动,使用默认的8080端口
  30. go r.Run(":9090")
  31. openwebview()
  32. select {
  33. }
  34. }
  35. func index(c *gin.Context) {
  36. c.HTML(200, "index.html", gin.H{
  37. "title": "工具测试",
  38. "msg": "我是一条神奇的消息",
  39. })
  40. }
  41. func formHandler(c *gin.Context) {
  42. c.Request.ParseForm()
  43. params := make(map[string]interface{})
  44. for k, v := range c.Request.PostForm {
  45. if k == "api_secret" {
  46. continue
  47. }
  48. params[k] = v[0]
  49. }
  50. logs := make(map[string]string)
  51. logs["step1"] = xsort.SortParamByKey(params, "S")
  52. logs["step2"] = xencryp.Md5StringLower(logs["step1"])
  53. logs["step3"] = logs["step2"] + "&api_script=" + c.PostForm("api_secret")
  54. logs["step4"] = xencryp.Md5StringLower(logs["step3"])
  55. bf := bytes.NewBuffer([]byte{})
  56. jsonEncoder := json.NewEncoder(bf)
  57. jsonEncoder.SetEscapeHTML(false)
  58. jsonEncoder.Encode(logs)
  59. fmt.Println(bf.String())
  60. c.String(200, bf.String())
  61. //var fakeForm myForm
  62. //c.ShouldBind(&fakeForm)
  63. //c.JSON(200, gin.H{"color": fakeForm.Colors})
  64. }
  65. func openwebview() {
  66. time.Sleep(1 * time.Second)
  67. webview.Open("WebView", "http://127.0.0.1:9090/index", 900, 600, true)
  68. }

Html

  1. <html>
  2. <h1>
  3. SRC - {{ .title }} ----- 开始12
  4. </h1>
  5. <form action="/index" method="POST">
  6. <p>Check some colors</p>
  7. <table>
  8. <tr>
  9. <td>
  10. app_id
  11. </td>
  12. <td>
  13. <input name="app_id">
  14. </td>
  15. </tr>
  16. <tr>
  17. <td>
  18. api_script
  19. </td>
  20. <td>
  21. <input name="api_secret">
  22. </td>
  23. </tr>
  24. <tr>
  25. <td>
  26. id_card
  27. </td>
  28. <td>
  29. <input name="id_card">
  30. </td>
  31. </tr>
  32. <tr>
  33. <td>
  34. real_name
  35. </td>
  36. <td>
  37. <input name="real_name">
  38. </td>
  39. </tr>
  40. <tr>
  41. <td>
  42. time
  43. </td>
  44. <td>
  45. <input name="time">
  46. </td>
  47. </tr>
  48. <tr><td colspan="2"><input type="submit"></td></tr>
  49. </table>
  50. </form>
  51. </html>