<template>
    <view
        class="med-container"
        :class="{'med-container-gap': useGap, 'app': isInApp}"
        :style="{
            'background-color': bgColor,
            'padding-bottom': ((query && query.bottom_btn_type) || (bottomBtn && bottomBtn.length)) && '60px',
            'min-height': minHeight,
        }"
        :network="network"
    >
        <!-- H5分享回流bar -->
        <template-mars target="h5">
            <!-- // TODO 内测阶段，分享回流关闭 -->
            <!-- <share-bar
                v-if="!isInBaiduApp && !isInApp" :show="shareBar && shareBarShow && !closeShareBar"
                @onCloseShareBar="onCloseShareBar"
            ></share-bar> -->
        </template-mars>
        <!-- H5自定义头部 -->
        <template-mars target="h5">
            <view
                v-if="!hidePageHeader && !hideHeader"
                class="med-title med-title-hei c-row-tile"
                :style="{
                    'color': naviFontColor,
                    'background-color': naviBgColor,
                    'background-image': bgImage,
                    'height': navigationBarHeight + statusBarHeight + 'px',
                    'paddingTop': statusBarHeight + 'px',
                }"
            >
                <!-- 左侧按钮 -->
                <view v-if="!isInApp" class="med-title-left c-gap-inner-left">
                    <c-icon
                        v-if="isShowBack"
                        name="left"
                        font-size="20px"
                        class="c-inline-block icon-back"
                        @tap="onTapBack"
                    />
                    <view
                        v-if="isShowHome"
                        name="home"
                        class="m-icon m-icon-home c-inline-block left-icon"
                        @tap="onTapHome"
                    />
                </view>
                <view class="c-line-clamp1 med-title-inner">{{ text }}</view>
                <!-- 右侧菜单 -->
                <view v-if="!isInApp" class="med-title-right c-gap-inner-right">
                    <template-mars target="h5">
                        <c-menu
                            v-if="menu && menu.length"
                            :menu="menu"
                            :leave-text="leaveText"
                            :leave-btn="leaveBtn"
                            :leave-type="leaveType"
                            :leave-style-config="leaveStyleConfig"
                        >
                            <slot slot="leave-dialog-cont" name="leave-dialog-cont"></slot>
                        </c-menu>
                    </template-mars>
                </view>
            </view>
            <view
                v-if="!hidePageHeader && !hideHeader"
                class="med-title-gap"
                :style="{
                    height: (navigationBarHeight + statusBarHeight) + 'px',
                    lineHeight: (navigationBarHeight + statusBarHeight) + 'px',
                }"
            />
        </template-mars>
        <!-- 网络错误 -->
        <view
            v-if="network === 'error'"
            class="med-network--error"
        >
            <b-middle-page v-if="!isInApp" @btnTap="reloadPage"/>
        </view>
        <block v-else>
            <!-- 骨架屏-->
            <template-mars target="h5">
                <view
                    v-if="sktTpl && isSktShow"
                >
                    <h5-skeleton
                        :skt-tpl="sktTpl"
                        :skt-style="sktStyle"
                    />
                </view>
                <view
                    v-else
                    :class="sktTpl ? 'ctn-show' : ''"
                >
                    <slot></slot>
                </view>
            </template-mars>
            <template-mars target="swan">
                <view>
                    <slot></slot>
                </view>
            </template-mars>
        </block>
        <!-- 未登录模块 -->
        <view v-if="innerLogin && loginStatus === 'nologin'">
            <login-block :hide-header="hideHeader"/>
        </view>
        <!-- 图片上传容器-->
        <view class="med-upload"/>
        <!-- 调起地图容器-->
        <view class="med-map"/>
        <!-- 背景色 -->
        <view
            class="med-bg"
            :style="bgSty"
        >
            <view :style="{'background-color': bgColor, 'height': '100%'}"/>
        </view>
        <!-- 图片上传容器-->
        <view class="med-upload"/>
        <!-- 页面挽留弹窗 -->
        <im-retaining-modal
            v-if="retainingModal && retainingModal.type"
            v-show="isShowLeave && isRetainingModal"
            :is-show-leave="isShowLeave"
            :data="retainingModal"
            @closeModal="onCloseRetainingModal"
        />
        <c-dialog
            v-if="isPerventLeave"
            :show="isShowLeave && !isRetainingModal"
            :gap-top="15"
            :title-height="44"
            :btn-opts="leaveBtn"
            :gap-slot-bottom="leaveStyleConfig.gapSlotBottom"
            :width="leaveStyleConfig.width"
            @btntap="onClosePrevent"
        >
            <view
                v-if="leaveType === 'normal'"
                slot="title"
                class="c-tac c-gap-inner-bottom c-font-sigma"
            >温馨提示</view>
            <view v-if="leaveType === 'normal'" class="c-color-gray c-font-15">{{ leaveText }}</view>
            <slot name="leave-dialog-cont"></slot>
        </c-dialog>
        <!-- 获取授权弹框 -->
        <auth-popup
            v-if="false"
            :is-show="authPopupShow"
            @close="onAuthPupupClose"
            @onLoginSuccess="onLoginSuccess"
            @onLoginFail="onLoginFail"
        />

        <!-- 反馈入口 -->
        <feedback-btn
            :feedback-is-show="feedbackIsShow"
            :is-upfold="isUpfold"
            :to-feedback-url="toFeedbackUrl"
            :ufo-feedback-url="ufoFeedbackUrl"
            :complaint-text="complaintText"
            :feedback-text="feedbackText"
            :feedback-ubc-log="feedbackUbcLog"
            :no-jump="noJump"
            :visit-bottom="visitBottom"
            @noJump="handleNoJump"
        />
        <!-- 底部button -->
        <share-btn
            v-if="(query && query.bottom_btn_type) || (bottomBtn && bottomBtn.length)"
            :page="page"
            :type="(query && query.bottom_btn_type)"
            :sf-ref="query && query.sf_ref"
            :custom="bottomBtn"
        />
        <!-- 打卡任务倒计时 -->
        <daka-task-view
            v-if="query.daka_task"
            :task-id="query.daka_task"
            :time="query.daka_time"
            @openDakaModal="openDakaModal"
        />
        <!-- 运营弹窗 -->
        <daka-modal-cont
            v-if="isShowDakaModal"
            type="center"
            :daka="dakaModal"
            :ue-conf="dakaUeConf"
            @onClose="onDakaModalClose"
            @onTipTap="onModalTipTap"
        />
        <!-- 新人活动返回 -->
        <act-return v-if="query.from === 'newuseractivity' || query.actReturnBtn === 'true'" :url="query.actUrl"/>
    </view>
</template>
<script>
/**
 * @file 视图容器
 * @author haoyan
 * @categories 通用组件
 * @tags [component]
 */
/* global getCurrentPages getApp swan */
import CIcon from '@baidu/med-ui/components/Icon/Icon';
import CDialog from '@baidu/med-ui/components/Dialog/Dialog';
import CMenu from './menu';
import H5Skeleton from './h5skeleton';
import AuthPopup from '../../components/common/auth-popup';
import FeedbackBtn from '../../components/common/feedback-btn';
import BMiddlePage from '../../components/common/MiddlePage';
import ShareBtn from '../../components/act/share-btn';
import DakaTaskView from '../../components/act/daka/DakaTaskView';
import DakaModalCont from '../../components/act/daka/modal-cont';
import ActReturn from '../../components/common/ActReturn';
import LoginBlock from '../../components/common/login-block';
import ImRetainingModal from '@components/im/im-retaining-modal';
// import ShareBar from '../../components/common/share-bar';
import {waterUeConf} from '../../utils/constant';
import * as core from '../../utils/core';
import {navigate} from '../../utils/navigate';
import {request} from '../../utils/request';
import {getSystemInfo} from '@baidu/med-utils/lib/Mars/system';
import * as conf from '../../utils/conf';
import wxShare from '../../utils/share';
import * as log from '../../utils/log';

// 反馈入口动效延迟时间
const feedbackDealy = 500;
// 反馈入口下拉开始时间点
let feedbackBeginTime = null;

export default {
    config: {
        component: true
    },
    options: {
        addGlobalClass: true
    },
    components: {
        CMenu,
        CIcon,
        CDialog,
        BMiddlePage,
        H5Skeleton,
        AuthPopup,
        FeedbackBtn,
        ShareBtn,
        DakaTaskView,
        DakaModalCont,
        ActReturn,
        LoginBlock,
        ImRetainingModal
        // ShareBar
    },
    props: {
        /**
         * 页面标题
         */
        title: {
            'type': String,
            'default': ''
        },
        /**
         * 是否隐藏页面头部
         */
        hideHeader: {
            'type': Boolean,
            'default': false
        },
        /**
         * H5隐藏后退
         */
        hideBack: {
            'type': Boolean,
            'default': false
        },
        /**
         * H5隐藏头部
         */
        hideHome: {
            'type': Boolean,
            'default': false
        },
        /**
         * H5定制头部右上角菜单
         */
        menu: {
            type: Array,
            default() {
                return [];
            }
        },
        /**
         * 是否使用页面左右17px padding
         */
        useGap: {
            'type': Boolean,
            'default': false
        },
        /**
         * 头部字体色
         */
        naviFontColor: {
            'type': String,
            'default': ''
        },
        /**
         * 头部背景色
         */
        naviBgColor: {
            'type': String,
            'default': ''
        },
        /**
         * 页面背景色
         */
        bgColor: {
            'type': String,
            'default': ''
        },
        /** 背景图bgImage */
        bgImage: {
            type: String,
            default: ''
        },
        /**
         * 背景样式
         */
        bgSty: {
            type: Object,
            default() {
                return {};
            }
        },
        /**
         * 是否使用骨架屏
         */
        isSktShow: {
            'type': Boolean,
            'default': true
        },
        /**
         * 骨架屏类型
         */
        sktTpl: {
            'type': String,
            'default': ''
        },
        /**
         * h5 骨架屏外层样式
         */
        sktStyle: {
            type: Object,
            default() {
                return {};
            }
        },
        /**
         * 退出页面前挽留文案
         */
        leaveText: {
            'type': String,
            'default': ''
        },
        /**
         * 退出页面前挽留弹窗按钮
         */
        leaveBtn: {
            type: Array,
            default() {
                return [];
            }
        },
        /**
         * 是否需要授权弹框
         */
        auth: {
            type: Object,
            default() {
                return {};
            }
        },
        /**
         * 分享
         */
        shareConfig: {
            type: Object,
            default() {
                return {};
            }
        },
        /**
         * 是否显示反馈入口
         */
        feedbackIsShow: {
            type: Boolean,
            default: false
        },
        /** 投诉默认文案 */
        complaintText: {
            type: String,
            default: '投诉'
        },
        /** 反馈默认文案 */
        feedbackText: {
            type: String,
            default: '反馈'
        },
        /**
         * 反馈入口跳转路径
         */
        toFeedbackUrl: {
            type: String,
            /* eslint-disable max-len */
            default: '/pages/brand/feedbackform/index?pd=med&openapi=1&from_sf=1&resource_id=5216&vn=med&atn=feedbackform&title=我要反馈&word=我要反馈&cat=5'
        },
        /**
         * 反馈入口跳转路径
         */
        ufoFeedbackUrl: {
            type: String,
            /* eslint-disable max-len */
            default: ''
        },
        /**
         * 反馈入口点击时ubc打点参数
         */
        feedbackUbcLog: {
            type: Object,
            default() {
                return {};
            }
        },
        /**
         * 反馈入口点击时是否跳转
         */
        noJump: {
            type: Boolean,
            default: false
        },
        /**
         * 底部分享btn
         */
        bottomBtn: {
            type: Array,
            default() {
                return [];
            }
        },
        /**
         * 遗留，挽留弹窗内容现在未曾使用
         */
        detainConfig: {
            type: Object,
            default() {
                return {};
            }
        },
        /**
         * 挽留弹窗类型，用于区分是slot还是文本
         */
        leaveType: {
            type: String,
            default: 'normal'
        },
        /**
         * 主诉挽留弹窗
         */
        retainingModal: {
            type: Object,
            default: null
        },
        /**
         * 主诉挽留弹窗返回类型，back：回退、home：回首页
         */
        retainingModalBackType: {
            type: String,
            default: ''
        },
        /**
         * home跳转指定页面
         */
        newHomeUrl: {
            type: String,
            default: ''
        },
        /**
         * 是否内置未登录模块
         */
        innerLogin: {
            type: Boolean,
            default: false
        },
        /**
         * 是否有分享topbar
         */
        shareBar: {
            type: Boolean,
            default: false
        },
        /** minHeight */
        minHeight: {
            type: String,
        },
        /**
         * 出诊信息添加特定底部高度
         */
        visitBottom: {
            type: String,
            default: '22%'
        },
        // leaveDelta 返回的层数
        leaveDelta: {
            type: Number,
            default: 1
        },
        /**
         * 判断引用页面是否是ydzjt
         */
        judgePage: {
            type: String,
            default: ''
        },
        /**
         * 挽留弹框回退是否需要emit函数
        */
        needEmit: {
            type: Boolean,
            default: false
        },
        /**
         * container组件所在页面，如triage、chat等
         */
        page: {
            type: String,
            default: ''
        },
        delaySetNavBar: {
            type: Boolean,
            default: false
        }
    },
    data() {
        Object.assign(this, {
            tpl: '',
            leaveCb: null,
            authPopupCallback: null,
            loginFailStatus: false,
            upDistance: 0,
            firstFeedback: true,
        });
        return {
            text: '',
            isShowBack: false,
            isShowHome: false,
            statusBarHeight: 0,
            navigationBarHeight: 0,
            isPerventLeave: false,
            isShowLeave: false,
            // 显示主诉挽留弹窗还是普通挽留弹窗
            isRetainingModal: false,
            leaveCb: null,
            authPopupShow: false,
            hidePageHeader: false,
            isUpfold: true,
            network: 1,
            loginStatus: 1,
            query: null,
            leaveStyleConfig: {
                width: 0.8,
                gapSlotBottom: 25
            },
            isShowDakaModal: false,
            dakaModal: {},
            dakaUeConf: waterUeConf,
            isInApp: false,
            // shareBarShow: true,
            // isInBaiduApp: true,
            shareBarHeight: 0,
            closeShareBar: false
        };
    },
    watch: {
        title(v) {
            this.setTitle(v);
        },
        leaveText: {
            handler(v) {
                if (this.leaveType !== 'normal') {
                    this.leaveStyleConfig.width = 0.72;
                    this.leaveStyleConfig.gapSlotBottom = 15;
                }
                this.isPerventLeave = !!v;
            },
            immediate: true
        },
        shareConfig(v) {
            if (v && v.title && process.env.MARS_ENV === 'h5') {
                this.onShareInit();
            }
        },
        loginStatus(v) {
            if (v === 'nologin') {
                if (process.env.MARS_ENV === 'swan') {
                    this.$api.setNavigationBarTitle({
                        title: '登录'
                    });
                }
                else if (process.env.MARS_ENV === 'h5') {
                    this.text = '';
                }
            }
            else {
                this.setTitle(this.title);
            }
            this.loginStatus = v;
        },
        retainingModalBackType(v) {
            if (v === 'back') {
                this.onTapBack();
            }
            else if (v === 'home') {
                this.onTapHome();
            }
        },
        delaySetNavBar(v) {
            if (!v) {
                // 设置title
                this.setTitle(this.title);
                this.setHeaderColor();
            }
        }
    },
    beforeCreate() {
        // 此处不对proxy作polyfill处理
        // 设置网络状态标示，初始化为1，不能为null
        try {
            /* eslint-disable no-redeclare */
            getApp().globalData.network = new Proxy({status: 1, cxt: this}, {
                // 设置网络标示代理
                set(target, key, value) {
                    if (key === 'status') {
                        target.cxt.network = value;
                        if (target.cxt.isInApp && value === 'error') {
                            window.location.reload && window.location.reload();
                        }
                    }
                    return Reflect.set(target, key, value);
                }
            });

            getApp().globalData.loginStatus = new Proxy({status: 1, cxt: this}, {
                // 设置网络标示代理
                set(target, key, value) {
                    if (key === 'status') {
                        target.cxt.loginStatus = value;
                        if (value === 'nologin') {
                            if (process.env.MARS_ENV === 'swan') {
                                target.cxt.$api.setNavigationBarTitle({
                                    title: '登录'
                                });
                            }
                            else if (process.env.MARS_ENV === 'h5') {
                                this.text = '';
                            }
                        }
                        else {
                            target.cxt.setTitle(target.cxt.title);
                        }
                    }
                    return Reflect.set(target, key, value);
                }
            });
            /* eslint-enable no-redeclare */
        } catch (error) {
            console.error(error);
        }
    },
    pageLifetimes: {
        show() {
            if (!this.isSktShow && process.env.MARS_ENV === 'swan') {
                try {
                    this.$parent && this.$parent.$mp.scope.removeSkeleton && this.$parent.$mp.scope.removeSkeleton();
                }
                catch (err) {}
            }
            if (this.innerLogin
                && process.env.MARS_ENV === 'swan'
                && this.$api.isLoginSync().isLogin
            ) {
                this.loginStatus = 'login';
            }

        },
        onUnload() {
            // H5页面 unload 时，重置登录态度
            process.env.MARS_ENV === 'h5' && (this.loginStatus = 1);
        }
    },
    created() {
        const sys = getSystemInfo();
        // this.isInBaiduApp = sys.host === 'baiduboxapp' || /\bbaidubox/.test((navigator || {}).userAgent || '');
        this.statusBarHeight = sys.statusBarHeight || 0;
        this.navigationBarHeight = sys.navigationBarHeight || 44;
        // TODO 内测阶段，分享回流关闭
        // this.shareBarHeight = process.env.MARS_ENV === 'h5' && (!this.isInBaiduApp) && (!this.isInApp) && this.shareBar ? 60 : 0;

        // APP头部兼容
        this.isInApp = getApp().globalData.isInApp;

        // 设置title
        if (!this.delaySetNavBar) {
            this.setTitle(this.title);
            this.setHeaderColor();
        }
        // 获取定制参数
        this.getQueryToCustomized();
        // 微信 + 问诊定制
        if (process.env.MARS_ENV === 'h5') {
            const ua = require('@searchfe/user-agent');
            const pages = getCurrentPages();
            const curPage = pages && pages.length ? pages[pages.length - 1] : {};

            if (!/\/wz.+?\//.test(curPage.route)) {
                return;
            }
            if (ua.isWeixinApp()) {
                this.hidePageHeader = true;
            }
        }
    },
    mounted() {
        /**
         * 获取topbar高度
         */
        this.$emit('topbarHeightHandle', this.statusBarHeight + this.navigationBarHeight + this.shareBarHeight);
    },
    methods: {
        onLoginSuccess() {
            this.authPopupCallback && this.authPopupCallback();
        },
        onLoginFail() {
            this.loginFailStatus = true;
        },
        onPageScroll(scrollTop) {
            // TODO 内测阶段，分享回流关闭
            // if (process.env.MARS_ENV === 'h5' && !this.isInBaiduApp && !this.isInApp) {
            //     if (scrollTop > 5) {
            //         this.shareBarShow = false;
            //         this.shareBarHeight = 0;
            //     }
            //     else {
            //         this.shareBarShow = true;
            //         this.shareBarHeight = 60;
            //     }
            //     this.$emit('topbarHeightHandle', this.statusBarHeight + this.navigationBarHeight + this.shareBarHeight);
            // }
        },
        onCloseShareBar() {
            this.closeShareBar = true;
        },
        onAuthPupupClose(el) {
            this.authPopupShow = false;
            if (el && el.onlyClosePopup) {
                return;
            }
            setTimeout(() => {
                if (this.auth.toHome) {
                    navigate({url: '/pages/brand/home/index', openType: 'switchTab'});
                }
                else {
                    if (process.env.MARS_ENV === 'h5' && el && el.event === 'allow') {
                        this.authPopupCallback && this.authPopupCallback();
                    }
                }
            }, 100);
        },
        onTapBack() {
            this.go(() => {
                if (this.needEmit) {
                    this.$emit('goBack');
                };
                let pages = getCurrentPages();
                if (pages.length === 1 && process.env.MARS_ENV === 'swan') {
                    swan.exit();
                }
                else {
                    navigate({
                        openType: 'navigateBack',
                        delta: this.leaveDelta
                    });
                }
            });
        },
        onTapHome() {
            // 指定了页面
            if (this.newHomeUrl) {
                if (!this.judgePage) {
                    navigate({
                        url: this.newHomeUrl
                    });
                } else if (this.judgePage) {
                    navigate({
                        url: this.newHomeUrl,
                        log: {
                            page: this.page,
                            type: 'clk',
                            value: 'ydzjt_dingbuhome'
                        }
                    });
                }
            }
            else {
                const query = this.query;
                this.go(() => {
                    navigate({
                        openType: 'relaunch',
                        url: '/pages/brand/home/index?' + core.getQueryStr(query)
                    });
                });
            }
        },
        onClosePrevent(info) {
            this.isShowLeave = false;
            this.$emit('closeRetainingModal');
            // 确认退出
            if (info.btn.event === 'confirm') {
                if (this.leaveType === 'wzInfoDialog') {
                    this.$api.removeStorageSync('wz-leave-modal');
                }
                if (typeof this.leaveCb === 'function') {
                    let timeid = setTimeout(() => {
                        this.leaveCb();
                        clearTimeout(timeid);
                    }, 300);
                }
            }
            log.send({
                type: 'b',
                action: 'leave-modal-btn',
                extra: {
                    leavetype: this.leaveType,
                    btnType: info.btn.event
                }
            });
        },
        go(cb) {
            this.isRetainingModal = this.retainingModal && this.retainingModal.type && this.getRetainingModalStatus();
            if (this.isRetainingModal) {
                this.isShowLeave = true;
                this.leaveCb = cb;
            }
            else if (this.isPerventLeave) {
                // 取上次的问诊挽留弹窗类型
                const leaveKey = this.$api.getStorageSync('wz-leave-modal');
                // 存储当前问诊弹窗类型
                this.$api.setStorage({
                    key: 'wz-leave-modal',
                    data: this.leaveType
                });
                if (this.leaveType === 'wzPhoneDialog' && leaveKey === 'wzInfoDialog' ||
                    this.leaveType === 'wzInfoDialog' && leaveKey === 'wzPhoneDialog'
                ) {
                    this.$api.removeStorageSync('wz-leave-modal');
                    cb();
                } else {
                    log.send({
                        type: 'b',
                        action: 'leave-modal-show',
                        extra: {
                            type: this.leaveType
                        }
                    });
                    this.isShowLeave = true;
                    this.leaveCb = cb;
                }
            }
            else {
                if (typeof cb === 'function') {
                    cb();
                }
            }
        },

        /**
         * 获取上次关闭挽留弹窗的时间是否大于24h，大于24h才显示
         */
        getRetainingModalStatus() {
            const lastTimeStap = this.$api.getStorageSync(`${this.page}RetainingModalCloseTime`);
            const timeNow = new Date().getTime();
            return !lastTimeStap || (timeNow - lastTimeStap > 24 * 60 * 60 * 1000);
        },

        /**
         * 关闭挽留弹窗
         *
         * @param {boolean} isStay 是否停留在当前页面
         */
        onCloseRetainingModal(isStay) {
            this.isShowLeave = false;
            this.$emit('closeRetainingModal');
            this.$api.setStorageSync(`${this.page}RetainingModalCloseTime`, new Date().getTime());
            if (isStay) {
                return;
            }

            if (typeof this.leaveCb === 'function') {
                let timeid = setTimeout(() => {
                    this.leaveCb();
                    clearTimeout(timeid);
                }, 300);
            }
        },
        setTitle(title) {
            let pages = getCurrentPages();
            let curPage = pages[pages.length - 1];
            let query = {};

            if (process.env.MARS_ENV === 'h5') {
                query = core.getQueryObject(curPage.path);
                this.text = title || decodeURIComponent(query.title || '百度健康');

                const globalHideHome = getApp().globalData.hdhome || window.sessionStorage.getItem('hdhome');

                // 显示icon
                this.isShowBack = !this.hideBack && pages.length > 1;
                this.isShowHome = !this.hideHome && curPage.uri.indexOf('brand/home/index') === -1 && !globalHideHome;
            }
            else if (process.env.MARS_ENV === 'swan') {
                query = curPage.options;
                this.$api.setNavigationBarTitle({
                    title: title || decodeURIComponent(query.title || '百度健康')
                });
            }
        },
        setHeaderColor() {
            if (this.isInApp) {
                this.$api.setNavigationBarColor({frontColor: '#000000', backgroundColor: '#ffffff'});
            }
        },
        toHexColor(color = '') {
            if (/#\w{3}/.test(color)) {
                return '#' + color.replace('#', '').split('').map(d => d + d).join('');
            }
            else if ((/#\w{6}/.test(color))) {
                return color;
            }
            else if (/rgb?\(.+\)/.test(color)) {
                const arr = [];
                color.replace(/(\d+)/g, (w, $1) => arr.push($1));
                return '#' + arr.map(d => parseInt(d, 10).toString(16)).join('');
            }
            return '';
        },
        handleAuthModal(callback) {
            callback && callback();
            // const allowAgreement = this.$api.getStorageSync('allowAgreement');
            // this.authPopupShow = !allowAgreement && !!this.auth.needAuth;
            // if (allowAgreement) {
            //     setTimeout(() => {
            //         !this.loginFailStatus && callback && callback();
            //         this.loginFailStatus = false;
            //     }, 100);
            // }
            // else {
            //     this.authPopupCallback = callback;
            // }
        },
        onShareInit() {
            request({
                url: conf.apiHost + '/med/weixin/jssdksign',
                isNeedLogin: false,
                isFirstScreen: false
            }).then(res => {
                const {title, shareUrl, content, imageUrl} = this.shareConfig;
                const {timestamp, nonceStr, signature} = res.data || {};
                wxShare({
                    title,
                    url: shareUrl || window.location.href,
                    content,
                    imageUrl
                }, res.data ? {
                    wx: true,
                    debug: false,
                    timestamp,
                    nonceStr,
                    signature
                } : {});
            });
        },
        feedbackStyle(e) {
            if (!this.firstFeedback) {
                const diff = e.scrollTop - this.upDistance;
                if (this.upDistance === 0) {
                    this.upDistance = e.scrollTop;
                    return;
                }
                // 上滑到顶部，初始为展开状态
                if (e.scrollTop === 0) {
                    this.isUpfold = true;
                }
                if (Math.abs(diff) < 44 || e.scrollTop < 0) {
                    return;
                }
                // 下拉
                if (diff > 44) {
                    this.isUpfold = false;
                    feedbackBeginTime = Date.now();
                }
                // 上滑, 滑动距离大于44，且下拉与上滑的间隔超过 feedbackDealy，且 上次记录的下拉时间为空
                else if (diff < -44 && (Date.now() - feedbackBeginTime > feedbackDealy || !feedbackBeginTime)) {
                    feedbackBeginTime = null;
                    this.isUpfold = true;
                }
                this.upDistance = e.scrollTop;
            } else {
                this.firstFeedback = false;
            }
        },
        reloadPage() {
            if (process.env.MARS_ENV === 'h5') {
                window.location.reload && window.location.reload();
                return;
            }
            const pages = getCurrentPages();
            const curPage = pages && pages.length ? pages[pages.length - 1] : {};
            curPage.onLoad && curPage.onLoad(curPage.options);
            // pageShow 放入下一事件循环执行
            setTimeout(() => curPage.onShow && curPage.onShow());
            // 释放网络状态标示
            try {
                getApp().globalData.network
                    && (getApp().globalData.network.cxt = this, setTimeout(() => getApp().globalData.network.status = 1));
            } catch (error) {
                throw error;
            }
        },
        getQueryToCustomized() {
            let query = {};
            if (process.env.MARS_ENV === 'h5') {
                query = core.getQueryObject(window.location.href);
            } else if (this.$api.getURLQuery) {
                query = this.$api.getURLQuery();
            }
            this.query = query;
        },
        openDakaModal(modal) {
            this.dakaModal = modal;
            this.isShowDakaModal = true;
        },
        onDakaModalClose() {
            this.isShowDakaModal = false;
            this.dakaModal = {};
        },
        onModalTipTap(type, extra) {
            this.onDakaModalClose();
            if (extra.btnUrl) {
                navigate({url: extra.btnUrl});
            }
        },
        handleNoJump() {
            this.$emit('noJump');
        }
    }
};
</script>
<style lang="less" scoped>
.med-bg {
    position: fixed;
    z-index: -1;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #fff;
}

.med-container-gap {
    padding: 0 .17rem;
}

.med-network--error {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    background: #fff;
    z-index: 9999;
}
</style>
<style lang="less" target="h5">
.med-title {
    color: #333;
    background-color: #fff;
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    z-index: 920;

    &-left {
        flex: 1;
    }

    &-right {
        flex: 1;
    }

    &-inner {
        width: 50%;
        text-align: center;
        font-weight: 700;
        font-size: 0.18rem;
        margin: 0 auto;
    }
}

.med-title-hei {
    line-height: 44px;
    height: 44px;
}

.med-title-gap {
    height: 44px;
}

.icon-back {
    margin-right: 30px;
}

.left-icon {
    font-size: 20px;
}
</style>
<style lang="less" target="swan">
.ctn-show {
    animation: ctnShow .5s;
}
@keyframes ctnShow {
    0% {
        opacity: 0;
    }

    100% {
        opacity: 1;
    }
}
</style>
