<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="https://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="https://www.thymeleaf.org ">
<head>
    <title>微信授权</title>
    <template th:replace="components/head-link"></template>
</head>
<body>
<input type="hidden" th:value="${spiServer}" id="spiServer">
<input type="hidden" th:value="${agent}" id="userAgent">
<template th:replace="components/foot-link"></template>
<script charset="utf-8" type="text/javascript">
    // 如果用户同意授权，页面将跳转至 redirect_uri/?code=CODE&state=STATE
    var wx_mode = Number(getBaseCookie("weChatAutoLogin"))===1 ? 1 : 2;
    var wx_code = getUrlParam('code');
    var wx_state = getUrlParam('state');
    var userData = '';
    $.get(spiServer+'/user/authorizationLogin?code='+wx_code+'&state='+wx_state+'&mode='+wx_mode, function(r){
        if(r.code===0){
            console.log(r.data);
            userData = {
                account: r.data.account,
                email: r.data.email,
                phone: r.data.bindMobile,
                name: r.data.name,
                userName: r.data.userName,
                userId: r.data.userID,
                spId: r.data.serviceProviderID,
                clubId: r.data.clubID,
                shopId: r.data.shopID,
                userIdentity: r.data.userIdentity,
                permission: r.data.userPermission,
                token: r.data.token
            };
            localStorage.setItem('userInfo',JSON.stringify(userData));
            tobeforePage();
        }else{
            if (wx_mode===2 && r.code===-4){
                //微信绑定页面
                window.location.href = '/bind.html';
            }else if (wx_mode===1) {
                tobeforePage();
            }else{
                CAIMEI.Alert(r.msg,'确定',true, function(){
                    tobeforePage();
                });
            }
        }
    });
    function tobeforePage(){
        var loginBeforePath = getBaseCookie("loginBeforePath");
        if (loginBeforePath && loginBeforePath!='undefined' && loginBeforePath.indexOf('.html')>0) {
            delBaseCookie("loginBeforePath");
            window.location.href = loginBeforePath;
        } else if (userData && userData.userIdentity*1 === 3) {
            location.href = '/supplier/dashboard.html';
        } else if (userData && (userData.userIdentity*1 === 2 || userData.userIdentity*1 === 4)) {
            location.href = '/user/dashboard.html';
        } else {
            location.href="/index.html";
        }
    }
</script>
</body>
</html>
