<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <title>真伪鉴别</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="/static/js/17738/jquery.cookie.js"></script>
    <link rel="stylesheet" href="/static/17738/css/main.css" />
    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <!-- <link href="https://cdn.bootcdn.net/ajax/libs/minireset.css/0.0.2/minireset.css" rel="stylesheet" /> -->
    <link href="/static/css/public/reset.css" rel="stylesheet" />
    <script src="/static/js/public/reset.js"></script>
    <script src="/static/js/public/public.js"></script>
</head>

<body>
    <div class="app">
        <div class="topbar">
            <img src="/static/assets/17738/v2/tmy_logo.png" style="width: 30%;" />
        </div>
        <p class="footer"><img src="/static/assets/17738/v2/footer.png" style="width: 48%" /></p>

        <div style="flex: 1;position: relative;">

            <div class="container-1 container">
                <img src="/static/assets/17738/v2/logo.png" style="width: 100%" />
                <div id="scan" class="scan btn">
                    <img src="/static/assets/17738/v2/scan.png" style="width: 65%" />
                </div>
            </div>

            <div class="container-2 container">
                <div class="before-ai">
                    <div class="label_image">
                        <img id="label_image" src="" style="width: 100%;" />
                    </div>
                    <div class="identify-inner" style="padding: 1rem 0;">
                        <img src="/static/assets/17738/v2/tips.png" style="width: 80%;">
                        <span id="label_times" style="display: none;"></span>
                        <div style="display:flex;justify-content: center;">
                            <div id="ai_verify_2" class="ai-button btn" style="margin-top: 1rem;">
                                <!-- <img src="/static/assets/17738/v2/ai.png" style="width: 32%;"> -->
                            </div>
                        </div>
                    </div>
                </div>
                <div class="success">
                    <img src="/static/assets/17738/v2/success.png" style="width: 55%;">
                    <div class="btn back" style="width: 75%;margin-top: 5rem;">
                        <img src="/static/assets/17738/v2/back.png" style="width: 35%;">
                    </div>
                </div>
            </div>
            <div class="container-3 container">
                <div class="identify-mid before-ai" style="flex-wrap: wrap;align-content: center;">
                    <img src="/static/assets/17738/v2/over.png" style="width: 75%;margin: 4rem 0 2rem;">
                    <div style="display:flex;justify-content: center;width: 100%;">
                        <div id="ai_verify" class="ai-button btn" style="margin-top: 1rem;">
                            <!-- <img src="/static/assets/17738/v2/ai.png" style="width: 32%;"> -->
                        </div>
                    </div>
                </div>
                <div class="success">
                    <img src="/static/assets/17738/v2/success.png" style="width: 55%;">
                    <div class="btn back" style="width: 75%;margin-top: 5rem;">
                        <img src="/static/assets/17738/v2/back.png" style="width: 35%;">
                    </div>
                </div>
            </div>
            <div class="container-4 container">
                <div class="identify-mid">
                    <div>
                        <input placeholder="刮开标签上的涂层输入6位验证码" class="verifyInput" id="vcode-input" />
                        <div class="btn" id="verifyInputConfirm" style="width: 13rem;margin-top: 1rem;">
                            <img src="/static/assets/17738/v2/yanzheng.png" style="width: 35%;">
                        </div>
                    </div>
                    <div class="notVerifyNotice">
                        <div>
                            <p>
                                您输入的验证码有误
                            </p>
                            <p>
                                请重新输入!
                            </p>
                        </div>
                        <div class="notVerifyNotice-close">
                            <p>确定</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // $('.container-1').hide();
        // $('.container-2').show();  //标签图片页面
        // $('.container-3').show();
        // $('.before-ai').hide();
        // $('.success').show();
        // $('.container-4').show(); //验证中页面
        // $('.notVerifyNotice').show();


        var vcodeMatched = false;
        var label_code;
        var uid = "{{consumer_id}}";
        (function () {
            wxConfig();
            $('#verifyInputConfirm').click(function () {
                if ($('#vcode-input').val() != '') {
                    matchVerifyCode();
                }
            })
            $('.notVerifyNotice-close').click(function () {
                $('.notVerifyNotice').hide();
            })

            $('.back').click(function () {
                $('.success').hide();
                $('.before-ai').show();
            })

            $('#test').click(function () {
                label_code = "904900319055";
                $('.container-4').show();
            })
            // $('#ai_verify').click(function () {
            //     window.location.href = "https://tjfnew.china315net.com/tjswm/ai/route/route.ashx?compid=36340&cpid=" + label_code;
            // });
            // $('#ai_verify_2').click(function () {
            //     window.location.href = "https://tjfnew.china315net.com/tjswm/ai/route/route.ashx?compid=36340&cpid=" + label_code;
            // });
            if (location.search.length > 0) {
                console.log('>0')
                if (location.search.substring(1).split('&').indexOf("access=1") >= 0) {
                    $('.before-ai').hide();
                    $('.success').show();
                } else {
                    $('.success').hide();
                }
            }
        })();

        function access() {
            $('.before-ai').hide();
            $('.success').show();
        }

        function labelcodeProcess(resultString) {
            // var result = res.resultStr;
            var result = resultString;

            if (result.indexOf('http://tj1.me/?t=') == -1) {
                // $('.topbar').hide();
                // $('.container-1').css('top', '0');
                alert("您扫描的不是婷曼逸系列产品防伪标签");
                return;
            }
            label_code = result.slice(result.indexOf("?t=") + 3);
            if (result.indexOf('9047') == -1 && result.indexOf('9048') == -1 && result.indexOf('9049') == -1 &&
                result.indexOf('6680') == -1) {
                alert("您扫描的不是婷曼逸系列产品防伪标签");
                return;
            }
            $('.container-1').hide();

            {% if registered %}
            var isAccess = location.search.substring(1).split('&').indexOf("access=1") >= 0
            //应到输入验证码页面
            if ((result.indexOf('9047') >= 0 || result.indexOf('9049') >= 0 || result.indexOf('6680') >= 0) && !isAccess) {
                //新标，输入验证码页面
                $('.container-4').show(); //验证中页面
            } else {
                $('.container-2').show();  //标签图片页面
                refreshData(label_code);
            }
            {% else %}
            window.location.href = 'https://davis.china315net.com/wx/process/label/scan/17738/?label_code=' + label_code + '&consumer_id={{consumer_id}}&openid={{openid}}&from_official_account=true';
            {% endif %}
        }


        var paramsList = location.search.substring(1).split('&');
        var params = {};
        paramsList.forEach((v, i) => {
            var k = v.split('=')[0];
            let val = v.split('=')[1];
            params[k] = val;
        });

        var cpid = params["cpid"];
        console.log('params:', params);
        if (cpid) {
            console.log('cpid existed');
            labelcodeProcess("http://tj1.me/?t=" + cpid);
        }

        wx.ready(function () {
            // location.search.substring(1).split('&').indexOf("cpid") >= 0
            $('#scan').click(function () {
                wx.scanQRCode({
                    needResult: 1,                    //1返回扫描结果, 0则微信跳转
                    scanType: ["qrCode"],
                    success: function (res) {
                        var resultString = res.resultStr;
                        // resultString = "http://tj1.me/?t=904701520660";
                        labelcodeProcess(resultString);
                    }
                })
            })
        })

        function matchVerifyCode() {
            $.ajax({
                url: 'https://davis.china315net.com/wx/ajax/matchvcode',
                method: 'GET',
                data: {
                    'labelcode': label_code,
                    'vcode': $('#vcode-input').val()
                },
                success: function (res) {
                    if (res.e == '') {
                        if (res.status == '1') {
                            vcodeMatched = true;
                            $('.container-4').hide();
                            $('.container-2').show();
                            refreshData(label_code);
                        } else {
                            vcodeMatched = false;
                            $('.notVerifyNotice').show();
                        }
                    } else {
                        alert('网络请求发生错误');
                    }
                },
                fail: function (err) {
                    alert(err);
                }
            })
        }
        function wxConfig() {
            $.ajax({
                url: 'https://davis.china315net.com/wx/ajax/js/sign/3/',
                method: 'GET',
                data: { url: window.location.href },
                success: function (res) {

                    wx.config({
                        debug: false,
                        appId: res.appid,
                        timestamp: res.timestamp,
                        nonceStr: res.noncestr,
                        signature: res.signature,
                        jsApiList: ['scanQRCode'],
                        openTagList: ['wx-open-launch-weapp']
                    });
                }
            });
        }
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        function refreshData(label) {
            {% if registered %}
            $.ajax({
                url: '/wx/ajax/image/',
                data: {
                    fwcode: label,
                    record: 1,
                    history: 1
                },
                success: function (data) {
                    $('#label_name').text("婷曼逸塑身内衣");
                    $('#label_company').text("四川婷曼逸科技集团有限公司");
                    $('#label_firstQuery').text(data.tm_1st);
                    $('#label_times').text(data.cnt);
                    $('#label_image').attr('src', data.img);
                    showForgeAlert();
                    firstTimeQuery();
                }
            });
            {% else %}
            alert("数据丢失，请刷新重试");
            {% endif %}
        };

        function firstTimeQuery() {
            if ($('#label_times').text() == 0) {
                $('.firstQueryHide').hide();
            }
        }
        function showForgeAlert() {
            // if (location.search.substring(1).split('&').indexOf("access=1") >= 0) {
            if ($('#label_times').text() > 1) {
                $('.container-1').hide();
                $('.container-2').hide();
                $('.container-3').show();
                $('#ai_verify').html(`
                        <wx-open-launch-weapp id="launch-btn" style="display: block;width: 32%;">
                            <template>
                                <style></style>
                                <img src="https://davis.china315net.com/static/assets/17738/v2/ai.png" style="width: 100%;">
                            </template>
                        </wx-open-launch-weapp>
                    `);
            } else {
                $('.container-1').hide();
                $('.container-2').show();
                $('.container-3').hide();
                $('#ai_verify_2').html(`
                        <wx-open-launch-weapp id="launch-btn" style="display: block;width: 32%;">
                            <template>
                                <style></style>
                                <img src="https://davis.china315net.com/static/assets/17738/v2/ai.png" style="width: 100%;">
                            </template>
                        </wx-open-launch-weapp>
                    `);
            }
            // } else {
            //     if ($('#label_times').text() > 0) {
            //         $('.container-1').hide();
            //         $('.container-2').hide();
            //         $('.container-3').show();
            //     }
            // }

            var uuid = tj_public.getUuid(8, 16);
            tj_public.configLaunchWeappBtn({
                el: 'launch-btn',
                username: 'gh_4290b605146b',
                extraData: {
                    cpid: label_code,
                    uid,
                    uuid,
                    os: 'davis'
                },
                onLaunch: function () {
                    tj_public.checkMiniprogramAIStatus(
                        {
                            label_code,
                            consumer_id: uid,
                            uuid,
                        }, function (state) {
                            if (state == 1) {
                                access();
                            }
                        })
                }
            })

        }


        /*
        function showRegisterNotice() {
            $('.container-1').hide();
            $('.container-3').show();
        }
        */
    </script>
</body>

</html>