美文网首页Go
go实现urlencode与urldecode

go实现urlencode与urldecode

作者: php转go | 来源:发表于2022-01-25 16:50 被阅读0次

    做微信公众号开发时,前后端分离,需要前端传把URL过来,这边需要urldecode

    urldecode与urlencode主要用到go官方的net/url库

    func main()  {
            var urlStr string = "https://www.test.com?aaa=bbb&ccc=ddd#123"
            escapeUrl := url.QueryEscape(urlStr)
            fmt.Println("urlencode:",escapeUrl)
            enEscapeUrl, _ := url.QueryUnescape(escapeUrl)
            fmt.Println("urldecode:",enEscapeUrl)
    }
    

    公众号示例

    package main
     
    import(
            "fmt"
          url2  "net/url"
    )
     
    func GetSignPackage(ctx *gin.Context) {
        timestamp := strconv.FormatInt(int64(time.Now().Unix()), 10)
        code, ticket := common.GetTicket()
        if code != 0 {
            fmt.Println("code=", code)
            response.Fail(ctx, gin.H{}, "微信权限不够")
            return
        }
        nonceStr := util.RandomString(16)
        url,_:=url2.QueryUnescape(ctx.Query("url"))
        jsapiTicket := "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "&timestamp=" + timestamp + "&url=" + url
        signPackage := SignPackage{
            AppId:     viper.GetString("wx.appid"),
            NonceStr:  nonceStr,
            Timestamp: timestamp,
            Signature: util.SHA1(jsapiTicket),
            Url:       url,
        }
        response.Success(ctx, gin.H{"package": signPackage}, "success")
        return
    }
    

    相关文章

      网友评论

        本文标题:go实现urlencode与urldecode

        本文链接:https://www.haomeiwen.com/subject/oniwhrtx.html