<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'],
    ['pages/video/videoList','/video/list'],
	['pages-sandTable/sandTable','/sandBox/list'],
    ['pages/voice/voice','/voice/list'],
    ['pages-voice/voiceDetail','/voice/detail'],
    ['pages-roadShow/video/list','/roadshow/video/list'],
    ['pages/roadShow/video/list','/roadshow/video/list'],
    ['pages-report/reportForVariety/list','/report/varietyreportlist']
])//map映射小程序页面路径对应h5页面路径
import {apiUserInfo} from '@/api/user'
import {apiGetSceneToParams} from '@/api/common'
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);
            // 如果是识别海报的则要解密
            if(options.scene){
                const resScene=await apiGetSceneToParams({scene_key:options.scene})
                if(resScene.code===200){
                    const obj=JSON.parse(resScene.data)
                    delete options.scene
                    options={...options,...obj}
                    console.log('pc页面解密scene数据',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);
			uni.getSystemInfo({
					success: (data) => {
						// 企业微信会额外返回一个 environment 字段 值为 wxwork 在企业微信PC版中，一旦后缀带上#wechat_redirect ，就打不开，不知道为何，社区也没有找到什么结果
						this.url=`${pcBaseUrl}${mapObj.get(decodeURIComponent(options.xcxPath))||'/'}?${paramsObjStr}${data.environment=='wxwork'?'':'#wechat_redirect'}`
                        // console.log(`${pcBaseUrl}${mapObj.get(decodeURIComponent(options.xcxPath))||'/'}?${paramsObjStr}${data.environment=='wxwork'?'':'#wechat_redirect'}`);
					},
					fail: (err) => {
						this.url=`${pcBaseUrl}${mapObj.get(decodeURIComponent(options.xcxPath))||'/'}?${paramsObjStr}#wechat_redirect`
						console.log(err);
					}
			})
            
        }
    },

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