实例
/**
* @Author: XGH
* @Email: 55821284@qq.com
* @Date: 2019/12/13 9:42
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/xgh2012/tools/xencryp"
"github.com/xgh2012/tools/xsort"
"github.com/zserge/webview"
"time"
)
type myForm struct {
Colors []string `form:"colors[]"`
}
func main() {
r := gin.Default()
gin.SetMode(gin.DebugMode)
//设置HTML目录
r.LoadHTMLGlob("html/*")
//单独加载某个HTML文件也可以使用以下
//r.LoadHTMLFiles("public/html/index.html")
r.GET("/index", index)
r.POST("/index", formHandler)
//启动,使用默认的8080端口
go r.Run(":9090")
openwebview()
select {
}
}
func index(c *gin.Context) {
c.HTML(200, "index.html", gin.H{
"title": "工具测试",
"msg": "我是一条神奇的消息",
})
}
func formHandler(c *gin.Context) {
c.Request.ParseForm()
params := make(map[string]interface{})
for k, v := range c.Request.PostForm {
if k == "api_secret" {
continue
}
params[k] = v[0]
}
logs := make(map[string]string)
logs["step1"] = xsort.SortParamByKey(params, "S")
logs["step2"] = xencryp.Md5StringLower(logs["step1"])
logs["step3"] = logs["step2"] + "&api_script=" + c.PostForm("api_secret")
logs["step4"] = xencryp.Md5StringLower(logs["step3"])
bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
jsonEncoder.Encode(logs)
fmt.Println(bf.String())
c.String(200, bf.String())
//var fakeForm myForm
//c.ShouldBind(&fakeForm)
//c.JSON(200, gin.H{"color": fakeForm.Colors})
}
func openwebview() {
time.Sleep(1 * time.Second)
webview.Open("WebView", "http://127.0.0.1:9090/index", 900, 600, true)
}
Html
<html>
<h1>
SRC - {{ .title }} ----- 开始12
</h1>
<form action="/index" method="POST">
<p>Check some colors</p>
<table>
<tr>
<td>
app_id
</td>
<td>
<input name="app_id">
</td>
</tr>
<tr>
<td>
api_script
</td>
<td>
<input name="api_secret">
</td>
</tr>
<tr>
<td>
id_card
</td>
<td>
<input name="id_card">
</td>
</tr>
<tr>
<td>
real_name
</td>
<td>
<input name="real_name">
</td>
</tr>
<tr>
<td>
time
</td>
<td>
<input name="time">
</td>
</tr>
<tr><td colspan="2"><input type="submit"></td></tr>
</table>
</form>
</html>