您的当前位置:首页正文

Golang通过JSONP设置localstorage

2024-11-27 来源:个人技术集锦
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',
	}))
显示全文