Golang通过JSONP设置localstorage

Posted 衣舞晨风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang通过JSONP设置localstorage相关的知识,希望对你有一定的参考价值。

package localstorage

import (
	"bytes"
	"log"
	"text/template"
)

const authPageTemplate = `
<!doctype html>

<head>
    <title>auth</title>
    <style>
        /* loading */
        .jiankunking_loading-wrp 
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        

        .jiankunking_loading-wrp .jiankunking_loading-box 
            padding: 98px;
            display: flex;
            justify-content: center;
            align-items: center
        

        .jiankunking_loading-dot 
            animation: jiankunking_loading-rotate 1.2s infinite linear;
            transform: rotate(45deg);
            position: relative;
            display: inline-block;
            font-size: 32px;
            width: 32px;
            height: 32px;
            box-sizing: border-box
        

        .jiankunking_loading-dot i 
            width: 14px;
            height: 14px;
            position: absolute;
            display: block;
            background-color: #1890ff;
            border-radius: 100%;
            transform: scale(.75);
            transform-origin: 50% 50%;
            opacity: .3;
            animation: jiankunking_loading-spin-move 1s infinite linear alternate
        

        .jiankunking_loading-dot i:nth-child(1) 
            top: 0;
            left: 0
        

        .jiankunking_loading-dot i:nth-child(2) 
            top: 0;
            right: 0;
            -webkit-animation-delay: .4s;
            animation-delay: .4s
        

        .jiankunking_loading-dot i:nth-child(3) 
            right: 0;
            bottom: 0;
            -webkit-animation-delay: .8s;
            animation-delay: .8s
        

        .jiankunking_loading-dot i:nth-child(4) 
            bottom: 0;
            left: 0;
            -webkit-animation-delay: 1.2s;
            animation-delay: 1.2s
        

        @keyframes jiankunking_loading-rotate 
            to 
                -webkit-transform: rotate(405deg);
                transform: rotate(405deg)
            
        

        @-webkit-keyframes jiankunking_loading-rotate 
            to 
                -webkit-transform: rotate(405deg);
                transform: rotate(405deg)
            
        

        @keyframes jiankunking_loading-spin-move 
            to 
                opacity: 1
            
        

        @-webkit-keyframes jiankunking_loading-spin-move 
            to 
                opacity: 1
            
        
    </style>
</head>

<body>
    <div class="jiankunking_loading-wrp">
         <div class="jiankunking_loading-box">
            <span class="jiankunking_loading-dot jiankunking_loading-dot-spin">
                <i></i><i></i><i></i><i></i>
            </span>
        </div>
    </div>
    <script>
        (function () 
            // 获取重定向地址
            const redirectUrl = " .RedirectUrl " || "/";
			const token= " .Token ";
			const tokenName = " .TokenName ";
            localStorage.setItem(tokenName, token);
            location.href = redirectUrl
        )()
    </script>
</body>

</html>
`

type AuthPageParams struct 
	RedirectUrl string
	Token       string
	TokenName   string


func ExecAuthPageTemplate(params *AuthPageParams) string 
	t := template.Must(template.New("authPage").Parse(authPageTemplate))
	var buf bytes.Buffer
	t.Execute(&buf, params)
	data := buf.String()
	log.Println(data)
	return data


写入响应

func WriteHTML(w http.ResponseWriter, html string) error 
	w.Header().Set("Content-Type", "text/html")
	_, err := w.Write([]byte(html))
	return err


WriteHTML(ctx.Writer, localstorage.ExecAuthPageTemplate(&localstorage.AuthPageParams
		RedirectUrl: redirectUri,
		Token:       token,
		TokenName:   'x_jwt',
	))

以上是关于Golang通过JSONP设置localstorage的主要内容,如果未能解决你的问题,请参考以下文章

Golang通过JSONP设置localstorage

Golang通过JSONP设置localstorage

ajax jsonp请求async设置false同步无效

谈CSRF与JSONP设置header问题

CORS - 没有 JSONP 的跨域 AJAX 通过允许服务器上的 Origin

如何自定义设置 angularjs jsonp 回调名称?