<template>
  <view class="pc-page">
      <web-view :src="url" @message="handleGetMessage" v-if="url"/>
  </view>
</template>

<script>
import {pcBaseUrl} from '../utils/config'
const mapObj=new Map([
    ['pages/activity/activity','/activity/list'],
	 ['pages/pricedriven/pricedriven','/pricedriven'],
    ['pages-activity/detail','/activity/detail'],
    ['pages/report/report','/report/index'],
    ['pages-report/classify','/report/classify'],
    ['pages-report/reportList','/report/list'],
    ['pages-report/reportDetail','/report/detail'],
    ['pages-report/chapterDetail','/report/chapterdetail'],
    ['pages-report/specialColumn/list','/report/specialcolumnlist'],
    ['pages-report/specialColumn/detail','/report/specialcolumndetail'],
])//map映射小程序页面路径对应h5页面路径
import {apiUserInfo} from '@/api/user'
export default {
    data () {
        return {
            url:'',
            msgObj:{},//{path:小程序页面地址,params:页面参数,title:分享的标题,shareImg:分享的图片}

            times:0,//检查token次数
        }
    },
    onLoad(options) {
        this.init(options)
    },
    onShareAppMessage({webViewUrl}) {
        // console.log(webViewUrl);
        let paramsStr=''
        for(const key in this.msgObj.params){
            if(!paramsStr){
                paramsStr=`${key}=${this.msgObj.params[key]}`
            }else{
                paramsStr=`${paramsStr}&${key}=${this.msgObj.params[key]}`
            }
        }
        return {
            title: this.msgObj.title||'弘则研究',
            path: `${this.msgObj.path}?${paramsStr}`,
            imageUrl:this.msgObj.shareImg||''
        }
    },

    methods: {
        // 获取到用户点击转发时从h5页面传来的参数
        handleGetMessage(e){
            const data=e.detail.data[e.detail.data.length-1]
            console.log('h5传来的数据',data);
            this.msgObj=data
        },

        async init(options){
            // 检查token是否有效 超过十次不在检测，提示重启小程序
            const res=await apiUserInfo()
            this.times++
            if(res.code!==200){
                console.log('pc页面检查token次：',this.times);
                if(this.times<11){
                    setTimeout(() => {
                        this.init(options)
                    }, 1000);
                }else{
                    uni.showToast({
                        title: '请重启小程序',
                        icon: 'none'
                    })
                }
                return
            }

            console.log('pc页面onload数据',options);
            let paramsObj={
                ...options,
                token:this.$store.state.user.token||uni.getStorageSync("token"),
                timestamp:new Date().getTime(),//防止缓存
            }
            delete paramsObj.xcxPath
            console.log('要处理的参数',paramsObj);
            let paramsObjStr=''
            for (const key in paramsObj) {
                if(!paramsObjStr){
                    paramsObjStr=`${key}=${paramsObj[key]}`
                }else{
                    paramsObjStr=`${paramsObjStr}&${key}=${paramsObj[key]}`
                }
            }
            console.log('拼接字符串：',paramsObjStr);
            this.url=`${pcBaseUrl}${mapObj.get(decodeURIComponent(options.xcxPath))||'/'}?${paramsObjStr}#wechat_redirect`
        }
    },

    onShow() {
        uni.hideHomeButton({
            fail:(e)=>{
                console.log(e);
            }
        })
    }
}
</script>
