<!-- node 版本是18.20.0 -->
<template>
    <div id="app">
        <div class="container">
            <router-view />
        </div>
    </div>
</template>
<script>
export default {
    name: "App",
    data() {
        return {
            loading: null,
            pathname: "",
            parameterObject: {},
            isPc: false,
            socketTask: null,
        }
    },
    watch: { 
        // "$route": {
        //     handler(to, from) {
        //         console.log("1111111");
        //         //避免首次获取不到window._czc
        //         if (window._czc) {
        //             let location = window.location
        //             let contentUrl = location.pathname + location.hash
        //             let refererUrl = "/"

        //             window._czc.push(["_trackPageview", contentUrl, refererUrl])
        //             window._czc.push(["_setAutoPageview", false])
        //         }
        //         if (window._hmt) {
        //             if (to["path"]) window._hmt.push(["_trackPageview", to["fullPath"]])
        //         }
        //     },
        //     // immediate: true, // 首次进入页面即执行
        // },
    },
    mounted() {
        // console.log(new Date());
        console.log("初始化");

        this.$store.dispatch("fetchHistoricalSearch") // 获取历史搜索记录
        this.$store.dispatch("getAllForum", this) // 获取全部板块

        // this.isJudgePc()
        // this.$skipUrl("gggg", false)
        // setTimeout(() => {
        // 	// this.hasMiucmsSessionCookie()
        // 	// this.useSocket
        // 	console.log(this.$store.state['user']);
        // }, 1000)

        let timer = setInterval(() => {
            if (Object.keys(this.$store.state.user).length !== 0) {
                this.useSocket()
                clearInterval(timer)
            }
        }, 1000)
    },

    methods: {
        // 开启socket
        useSocket() {
            let token = this.getMiucmsSessionCookie() || ""
            this.socketTask = new WebSocket(`wss://app.gter.net/socket?token=${token}`)
            this.socketTask.onopen = () => {
                let user = this.$store.state.user || {}
                if (user && token) {
                    this.socketTask.send(
                        JSON.stringify({
                            type: "bind",
                            data: {
                                token,
                                uid: user.uid || 0,
                            },
                        })
                    )
                }
                setTimeout(() => this.timedTransmission(), 50000)
            }
            this.socketTask.onclose = () => {
                console.log("socket关闭了", new Date())
            }
        },

        // 定时发送
        timedTransmission() {
            if (this.socketTask.readyState != 1) return
            this.socketTask.send(JSON.stringify({type: "ping"}))
            setTimeout(() => this.timedTransmission(), 50000)
        },

        getMiucmsSessionCookie() {
            let cookies = document.cookie.split(";")
            for (let i = 0; i < cookies.length; i++) {
                let cookie = cookies[i].split("=")
                if (cookie[0].trim() === "miucms_session") {
                    return cookie[1]
                }
            }
            return null
        },
        isJudgePc() {
            const userAgentInfo = navigator.userAgent
            const Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]
            let flag = true
            for (let i = 0; i < Agents.length; i++) {
                if (userAgentInfo.indexOf(Agents[i]) > 0) {
                    flag = false
                    break
                }
            }

            if (flag) {
                console.log("flag", flag)
                let pathname = window.location.pathname

                const queryString = window.location.search
                const parameters = queryString.substring(1)
                let parameterObject = Object.fromEntries(new URLSearchParams(parameters))
                this.pathname = pathname
                this.parameterObject = parameterObject

                if (this.pathname.indexOf("allSections") != -1 && this.parameterObject["fid"]) {
                    this.handleNeedAllSkipPc()
                    return
                }
                if (this.pathname.indexOf("userIndex") != -1) {
                    this.handleNeedInfo()
                    return
                }

                let url = ""

                if (pathname.indexOf("detailIndex") != -1) url = `https://bbs.gter.net/thread-${parameterObject["tid"]}-${parameterObject["page"] || 1}-1.html`
                if (pathname.indexOf("searchResult") != -1) url = `https://search.gter.net/index.html?type=forum${parameterObject["kw"] ? `&kw=${parameterObject["kw"]}` : ""}`
                if (pathname.indexOf("allSections") != -1 && !parameterObject["fid"]) url = `https://bbs.gter.net/forum.php`
                if (pathname.indexOf("recommend") != -1) url = `https://bbs.gter.net/forum.php`
                if (pathname.indexOf("collect") != -1) url = `https://bbs.gter.net/forum.php`
                if (pathname == "/" || pathname == "" || !pathname) url = `https://bbs.gter.net/forum.php`

                if (url) this.$skipUrl(url, false)
            }
        },

        // 处理需要user信息的跳转PC端
        handleNeedInfo() {
            let user = this.$store.state.user || {}
            let url = ""
            if (JSON.stringify(user) != "{}") {
                url = `https://bbs.gter.net/home.php?mod=space&uid=${user.uin}`
                this.$skipUrl(url, false)
            } else {
                setTimeout(() => {
                    this.handleNeedInfo()
                }, 300)
            }
        },

        // 处理需要全部板块的跳转PC端
        handleNeedAllSkipPc() {
            let allForumList = this.$store.state.allForumList || []

            let url = ""
            let fid = this.parameterObject["fid"] || null

            if (allForumList.length != 0) {
                allForumList.forEach((element, index) => {
                    if (element.fid == fid) url = `https://bbs.gter.net/forum.php?gid=${element.fid}`
                    element.data.forEach((el, i) => {
                        if (el.fid == fid) url = `https://bbs.gter.net/forum-${el.fid}-${this.parameterObject["page"] || 1}.html`
                    })
                })
                this.$skipUrl(url, false)
            } else {
                setTimeout(() => {
                    this.handleNeedAllSkipPc()
                }, 300)
            }
        },
    },
}
</script>

<style lang="scss">
@import "assets/css/normalize.css";
@import "assets/css/public.scss"; //帖子内容样式

#app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    display: flex;
    flex-direction: column;
    margin: 0 auto;
    position: relative;
    // overflow: hidden;
}

a {
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0);
    text-decoration: none;
}

.container {
    // padding: 0 0.35rem;
    background-color: rgba(246, 246, 246, 1);
    min-height: 100vh;
}

.flexflex {
    display: flex;
}

.flexcenter {
    display: flex;
    justify-content: center;
    align-items: center;
}

.flexacenter {
    display: flex;
    align-items: center;
}

.flexcolumn {
    display: flex;
    flex-direction: column;
}

.flex1 {
    flex: 1;
}

.el-message {
    min-width: 7rem !important;
}

// 文字一行显示
.one-line {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -moz-box;
    -moz-box-orient: vertical;
}

// 文字两行显示
.two-lines {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

// 文章三行显示
.three-lines {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
    text-overflow: ellipsis;
}

// 阴影
.shadow {
    -webkit-box-shadow: 0 0 0.3rem rgba(0, 0, 0, 0.1);
    box-shadow: 0 0 0.3rem rgba(0, 0, 0, 0.1);
}

// 弹窗公共类 蒙版
.pop-box {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.64);
    z-index: 100;
    align-items: center;
    justify-content: flex-end;
    overflow: auto;
}

// 清除 input 和 textarea 的默认样式
input,
textarea {
    border: none;
    outline: none;
    background: transparent;
    resize: none;
}
</style>
