<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.0/weui.min.css">
    <link href="/static/css/public/reset.css" rel="stylesheet" />
    <link href="/static/css/public/public.css" rel="stylesheet" />
    <script src="/static/js/public/reset.js"></script>
    <link href="/static/286010/css/main.css?v=5" rel="stylesheet" />
    <title>新华欣酒业</title>
</head>

<body>
    <div class="app">
        <!-- 防伪页面 -->
        <div class="fw-page" id="container" style="width: 100%; height: 100%; display: none">
            <div class="fw-content">
                <img src="/static/286010/img/产品图.jpg" id="ban" style="width: 100%" />
                <div id="ban-single" style="display: none;width: 100%;height: 7rem;"></div>
                <div class="card-box">
                    <!-- AI防伪卡片 -->
                    <div class="fw-card" id="fw">
                        <!-- 未通过 -->
                        <div class="result error" id="warn" style="display: none">
                            <img src="/static/286010/img/未通过.png" />
                            <p>该防伪密码已被查询<span id="queryNum"></span>次，</p>
                            <p>首次查询时间: <span class="firstTime"></span>,</p>
                            <p>最后一次查询时间:<span id="lastTime"></span>,</p>
                            <p>如非本人查询，请仔细核对谨防假冒</p>
                        </div>
                        <!-- 通过 -->
                        <div class="result success" id="success" style="display: none">
                            <img src="/static/286010/img/通过.png" />
                            <p>恭喜，验证成功!</p>
                            <p>首次查询时间: <span class="firstTime"></span>,</p>
                            <p>如非本人查询，请仔细核对谨防假冒</p>
                        </div>

                        <div class="text-center">
                            <span class="btn-link" onclick="showSyPage();">溯源查询<i class="triangle"></i></span>
                        </div>

                        <div class="hr"></div>
                        <div class="records">
                            <div class="subtitle">查询记录</div>
                            <div id="records">
                            </div>
                        </div>
                    </div>
                    <div class="fw-card sy-card" id="sy" style="display: none;">
                        <h3 class="main-title mb-1">溯源信息</h3>
                        <p id="sy-code" style="display: none;">
                            <b class="sy-title">标签序号：</b><span></span>
                        </p>
                        <p id="sy-product-name" style="display: none;">
                            <b class="sy-title">产品名称：</b><span></span>
                        </p>
                        <div class="sy-stk" id="sy-stk" style="display: none;">
                            <b class="sy-title">物流信息：</b>
                            <div id="stk">

                            </div>
                        </div>
                        <div class="btn-back" id="back" onclick="showFwPage();">返回</div>
                    </div>
                    <div id="toActivity" style="text-align: center;">
                        <img src="/static/286010/img/act.png" style="width: 90%" />
                    </div>
                </div>
            </div>
        </div>

        <!-- 输入页面 -->
        <div id="integral_popup" class="input-content" style="display: block">
            <div class="input-card">
                <img src="/static/images/ggy.gif" style="width: 100%" />
                <p style="margin: 1.2rem 0">请输入验证码</p>
                <input id="input_yzm" type="text" placeholder="请输入验证码" class="input" />
                <button class="button" onclick="checkYzm();">确定</button>
            </div>
        </div>
    </div>

    <script src="https://res.wx.qq.com/open/libs/weuijs/1.2.1/weui.min.js"></script>
    <script type="text/javascript" src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <script src="/static/js/jquery-2.1.0.min.js"></script>
    <script src="/static/js/public/public.js"></script>
    <script>

        // tj_public.showLoading();
        // tj_public.hideLoading();
        var uid = tj_public.getCookie('TJWX_consumer_id');
        var consumer_id = tj_public.getCookie('TJWXUSR_286010_0');
        tj_public.setCookie('TJWXUSR_286010_2', consumer_id);
        var label_code = tj_public.getCookie('label_code');
        var jmcpid = tj_public.getCookie('jmcpid');
        var label = jmcpid ? jmcpid : label_code;
        tj_public.wxInit({
            wx_officail_account_id: 6,
            url: window.location.href
        })

        var cid = parseInt(label_code);
        //单枚标
        var isSingle = false;
        if ((cid >= 141004620066 && cid <= 141005720056)) {
            isSingle = true;
            $("#toActivity").hide();
            $("#back").hide();
            $("#ban").hide();
            $("#ban-single").show();
            document.title = "康健一生";
        }

        var pk = tj_public.getCookie('package');
        if (pk === '3') {
            $("#container").show();
            $("#integral_popup").hide();
            $("#toActivity").hide();
            getInfo(lat, lng)
        }

        var lat = 0;
        var lng = 0;
        var fwdata = null;
        wx.ready(function () {
            if (uid) {
                tj_public.getAddress(function (latt, lngt) {
                    lat = latt;
                    lng = lngt;
                })
            }
        })

        function checkYzm() {
            var yzm = $("#input_yzm").val();
            tj_public.showLoading();
            $.ajax({
                type: 'get',
                url: '/wx/ajax/check_yzm/',
                data: { label_code, yzm, company_id: 286010 },
                success: function (res) {
                    tj_public.hideLoading();
                    if (res && res.e === "") {
                        $('.label_code').text(res.vcode);
                        getInfo(lat, lng)
                    } else if (res.e) {
                        alert(res.e);
                    }
                    else {
                        alert('验证码错误！请重新输入')
                    }
                }, error: function () {
                    tj_public.hideLoading();
                    alert('网络超时！请稍后再试一下')
                }
            })
        }

        function getInfo(lat, lng) {
            tj_public.showLoading();
            $.ajax({
                url: '/wx/label/scan_record/',
                data: { lat: lat, lng: lng, uid: uid, compid: 286010, fwcode: label_code, contents: 1 },
                success: function (res) {
                    if (res.e === '') {
                        fwdata = res;
                        $.ajax({
                            url: "/wx/ajax/label/scan_record/",
                            data: {
                                labelcode: label_code,
                                is_first: '0'
                            },
                            success: function (result) {
                                tj_public.hideLoading();
                                if (result.e === "") {
                                    var cnt = result.records.length;
                                    if (cnt == 0) {
                                        cnt = 1;
                                    }
                                    if (cnt == 1) {
                                        $("#success").show();
                                    }
                                    else {
                                        $("#lastTime").text(result.records[0].tm);
                                        $("#warn").show();
                                    }

                                    $("#queryNum").html(cnt);
                                    if (result.records.length > 0) {
                                        $(".firstTime").text(result.records[result.records.length - 1].tm);
                                    }
                                    else {
                                        var myDate = new Date();
                                        $(".firstTime").text(myDate.toLocaleString());
                                    }
                                    var recordHtml = ''
                                    for (var record of result.records) {
                                        recordHtml += `
                      <div class="item">
                        <img class="avator" src="/static/286010/img/avatar.png" />
                        <div class="ip">${record.wx}</div>
                        <div class="tm">${record.tm}</div>
                      </div>
                      `
                                    }
                                    if (isSingle) {
                                        showSyPage()
                                    }
                                    $("#records").html(recordHtml);
                                    $("#integral_popup").hide();
                                    $("#container").fadeIn();
                                } else {
                                    alert('网络超时')
                                }
                            }, error: function () {
                                $("#loading").hide();
                                alert('网络超时')
                            }
                        })
                    } else {
                        alert('网络超时')
                    }
                }, error: function () {
                    tj_public.hideLoading();
                    alert('网络超时')
                }
            })
        }

        $('#toActivity').on('click', function () {
            window.location.href = 'https://davis.china315net.com/static/286010/2/index.html';
        });

        // 展示防伪页面
        function showFwPage() {
            $("#fw").show()
            $("#sy").hide()
        }

        // 展示溯源页面
        function showSyPage() {
            $("#fw").hide()
            $("#sy").show()
            $("#sy-code").show().children('span').text(fwdata.bcode ? fwdata.bcode : fwdata.code)
            if (fwdata.product_name) {
                $("#sy-product-name").show().children('span').text(fwdata.product_name)
            }
            if (fwdata.inventory.length) {
                var html = '';
                for (var item of fwdata.inventory) {
                    if (item.voucher === '成品入库') {
                        html += `<div>
                                ${item.tm}<br />
                                到达<span>【${item.store || item.company}】</span>
                                完成<span>【${item.voucher}】</span>
                                </div>`
                    }
                    else if (item.voucher === '销售出库') {
                        html += `<div>
                                ${item.tm}<br />
                                从<span>【${item.store || item.company}】</span>
                                发往<span>【${item.customer}】</span>
                                </div>`
                    }
                }
                $("#sy-stk").show().children("#stk").html(html)
            }
        }

        // test
        // $("#container").show();
        // $("#integral_popup").hide();
        // $("#toActivity").hide();
        // getInfo(0, 0)
    </script>
</body>

</html>