<!--
 * @Date: 2023-02-24 14:18:25
 * @LastEditors: JinxChen
 * @LastEditTime: 2023-04-08 16:35:10
 * @FilePath: \TelpoH5FrontendWeb\src\views\package-home\index.vue
 * @description:
-->
<template>
    <div class="package-home">

    </div>
</template>

<script>
import { APIPay } from "@/api/pay";
import APICore from "@/api/core";
import APIWx from "@/api/wx";
import { isNotNull } from "@/utils/index";
import AppId from "@/config/appId";
export default {
    name: "",
    data() {
        return {
            params: {}, //获取路由的参数
            isRecharge: null, //是否已经支付
        };
    },
    created() {
        this.getToken();
        this.getAuth();
        this.checkImei();
        this.getParams();
    },
    methods: {
        checkImei() {
            let reqBody = {
                imei: this.params.imei,
                iccid: this.params.iccid || ''
            };
            APIWx.CheckImei(reqBody).then(res =>{
                let data = res.data;
                if(data) {
                    this.isRecharge = data.isRecharge;
                };
            }).catch(e => {
                console.log("e", e.message);
            })
        },

        // checkBrowser 检查扫码的浏览器内核
        checkBrowser() {
            const userAgent = window.navigator.userAgent;
            console.log("浏览器内核", userAgent);
            if (/AlipayClient/.test(userAgent)) {
                console.log("支付宝");
            } else if (/MicroMessenger/.test(userAgent)) {
                console.log("微信");
                let url = window.location.href.split("?code=")[1];
                console.log("获取授权code的url", url);
                if (
                    isNotNull(url) ||
                    window.location.href.indexOf("code") > -1
                ) {
                    let timeStamp = new Date().getTime();
                    let code = url.split("&")[0];
                    console.log("code", code);
                    if (isNotNull(code)) {
                        this.getWxCode();
                    }
                } else {
                    this.getWxCode();
                }
            } else {
                this.getWxCode();
                console.log("当前浏览器内核并非支付宝或者微信");
            }
        },
        // 获取b端接口的token
        getAuth() {
            let manufactorId = "5bf13062-a41e-4d00-ba14-1101aad12650";
            APICore.getAuth({ manufactorId: manufactorId }).then(res => {
                this.$store.commit("gatewayToken", res.data.data);
            });
        },
         // 获取token
        getToken() {
            let manufacturerNo = '9f166b07-ff83-4991-84dc-ca6ad4a6b95b';
            APIPay.getToken(manufacturerNo).then(res => {
                console.log("token的数据", res.data)
                let data = res.data;
                if(data.code === 20000) {
                    this.$store.commit("token", data.token);
                    console.log("token的数据",  localStorage.getItem('token'))
                }
            })
        },
        // 根据code获取openId
        getOpenId(code) {
            //let code = this.$store.getters.wxAuthCode;
            APIPay.getOpenId(code).then(res => {
                let data= res.data;
                if(data.code === 20000) {
                    this.$store.commit("openId", data.data.openId);
                }
            })
        },
        // 获取微信code
        getWxCode() {
            let params = this.params;
            let commonUrl = process.env.VUE_APP_BASE_API;
            // todo 暂时去掉
            if(this.isRecharge) {
                // 如果是已经支付，但未激活，跳转激活界面
                this.$router.replace({
                    name: 'cardActive',
                    query: {
                        imei: params.imei,
                        iccid: params.iccid
                    }
                })
            } else {
                let redUrl = encodeURIComponent(`${commonUrl}/h5-frontendweb/#/${params.routerName}?imei=${params.imei}&iccid=${params.iccid}`);
                let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${AppId}&redirect_uri=${redUrl}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
                window.location.href = url;
            }

        },
        // 获取url传过来的参数
        getParams() {
            let params = this.$route.query;
            if (params) {
                console.log("params", params);
                this.params = {...params};
                this.$store.commit("appId", AppId);
                if(params.reqUrl) {
                    const reqUrl = encodeURI(params.reqUrl);
                    console.log("reqUrl", params.reqUrl, reqUrl);
                    window.location.href = reqUrl;
                } else {
                    console.log("不存在redUrl");
                    this.checkBrowser();
                }
                console.log("this.params", this.params);
            }
        },
    }
};
</script>

<style scoped lang="scss">
</style>
