1. 程式人生 > 實用技巧 >ASP.Net Cookie

ASP.Net Cookie

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>協同辦公雲平臺-登入</title>
    <link href="~/Content/Login.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script>
        //
獲取cookie cname代表你的key比如儲存的是code就是用code獲取值,ysid=ssss;code=值 GetCookie(code) //yjs_id=fDE1NzEwMzk0Mzg2NzI; code1=1IVJAM;dept=; function GetCookie(cname) { var strCookie = document.cookie; //沒有找到 if (strCookie.indexOf(";") != 0) { strCookie
= strCookie + ";"; } var c = document.cookie.split(";");//陣列包含yjs_id=fDE1NzEwMzk0Mzg2NzI和 code=1IVJAM var name = cname + "="; for (var i = 0; i < c.length; i++) { var ca = c[i].trim();//去掉空格 if (ca.indexOf(name) == 0) {//查詢是否存在,如果存在返回0不存在返回-1
return ca.substring(name.length, ca.length);//sumString(開始查詢的位置,查詢的長度是多少) } } return ""; } $(function () { $("#login").click(function () { var code = $("#VCode").val(); //獲取使用者名稱 var account = $("#Account").val(); //獲取密碼 var password = $("#Password").val(); //使用者名稱和密碼的判斷 if (account.trim() == "") { alert("請輸入您的使用者名稱"); return; } else if (password.trim() == "") { alert("請輸入您的密碼"); return; } else if (GetCookie("code") == "" || code == "") { alert("請輸入您的驗證碼"); return; } else if (code != GetCookie("code")) { alert("您輸入的驗證碼不正確"); return; } $.ajax({ url: "http://localhost:56848/api/Validate", type: "get", data: { account: account, password: password }, dataType: "json", success: function (result) { if (result == "") { alert("使用者名稱或者密碼錯誤,請重新輸入"); } else { alert("登入成功"); //設定cookie document.cookie = "displayName=" + result.DisplayName; document.cookie = "account=" + result.Account; document.cookie = "sex=" + result.Sex; document.cookie = "DeptName=" + result.DeptName; } } }); }); }); </script> </head> <body> <div id="content"> <div id="box"> <div class="title">協同辦公雲平臺</div> <div class="input"> <input type="text" placeholder="請輸入使用者名稱" id="Account" /> <br /> <input type="password" placeholder="請輸入密碼" id="Password" /> <br /> <input type="text" placeholder="請輸入驗證碼" id="VCode" style="text-align:left;width:50%; float:left; margin-left:53px" maxlength="6" /> <img src="~/Handler/VerificationHanlder.ashx" onclick="this.src = this.src + '?' + Math.random()" style="padding-top:5px;cursor:pointer" /> <br /> <button type="button" id="login">登入</button> </div> </div> </div> </body> </html> <script> alert(document.cookie); </script>
View Code
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>LeaveForm</title>
    <style>
    </style>

    <link href="~/Content/upload.css" rel="stylesheet" />
    <script src="~/Scripts/jquery.1.10.2.min.js"></script>
    <script src="/scripts/jquery/jquery.iframe-transport.js"></script>
    <script src="/scripts/jquery/jquery.ui.widget.js"></script>
    <script src="/scripts/jquery/jquery.xdr-transport.js"></script>
    <script src="/scripts/jquery/jquery.fileupload.js"></script>
    <script>
        function GetCookie(cname) {
            var strCookie = document.cookie;
            //沒有找到
            if (strCookie.indexOf(";") != 0) {
                strCookie = strCookie + ";";
            }
            var c = document.cookie.split(";");//陣列包含yjs_id=fDE1NzEwMzk0Mzg2NzI;code=1IVJAM
            var name = cname + "=";
            for (var i = 0; i < c.length; i++) {
                var ca = c[i].trim();//去掉空格
                if (ca.indexOf(name) == 0) {//查詢是否存在,如果存在返回0不存在返回-1
                    return ca.substring(name.length, ca.length);//sumString(開始查詢的位置,查詢的長度是多少)
                }

            }
            return "";
        }
        $(function () {

            //獲取cookie中的顯示使用者名稱
            $("#txtDisplayName").val(GetCookie("displayName"));
            $("#txtAccount").val(GetCookie("account"));
            var date = new Date();
            $("#txtDeptName").val(GetCookie("DeptName"));
            $("#txtDate").val(date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate());



            $("input[type=file]").fileupload({
                done: function (e, result) {
                    //done方法就是上傳完畢的回撥函式,其他回撥函式可以自行檢視api
                    //注意result要和jquery的ajax的data引數區分,這個物件包含了整個請求資訊
                    //返回的資料在result.result中,假設我們伺服器返回了一個json物件
                    //json物件{"newName": "sss", "oldName": "sdf"}

                    var resultJson = $.parseJSON(result.result)
                    $(e.target).attr("value", resultJson.newName);
                    var uploadDiv = $(e.target).parent().parent().parent();
                    uploadDiv.find(".filestate").show().text(resultJson.oldName);
                },
                progressall: function (e, data) {
                    var maxWidth = 200;
                    var percent = (data.loaded / data.total * 100).toFixed(2);
                    var progress = parseInt(data.loaded / data.total * maxWidth, 10);
                    var uploadDiv = $(e.target).parent().parent().parent();
                    uploadDiv.find(".progress").show();
                    uploadDiv.find(".bar").css("width", progress);
                    uploadDiv.find(".progresspercent").show().text(percent + "%");

                }
            });

        });




        //計算天數
        function DateDiff(sDate1, sDate2) {
            var oDate1, oDate2, iDays
            oDate1 = new Date(sDate1);
            oDate2 = new Date(sDate2);
            console.log(sDate1);
            console.log(sDate2)
            iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
            return iDays;
        }
        function CalcDay() {
            var txtBeginDdate = $("#txtBeginDdate").val();
            var txtEndDate = $("#txtEndDate").val();

            $("#txtDay").val(DateDiff(txtBeginDdate, txtEndDate) + 1);
        }

        function LeaveSubmit() {
            var txtAccount = $("#txtAccount").val();
            var txtDisplayName = $("#txtDisplayName").val();
            var txtDeptName = $("#txtDeptName").val();
            var approveAccount = "tianyou.xiang";
            var approveName = "項天佑";
            var txtBeginDdate = $("#txtBeginDdate").val();
            var txtEndDate = $("#txtEndDate").val();
            var txtDay = $("#txtDay").val();
            var txtDesc = $("#txtDesc").val();
            var dropType = $("#dropType").val();
            $.ajax({
                url: "http://localhost:56848/api/LeaveSubmit",
                data: { AppAccount: txtAccount, AppName: txtDisplayName, AppDetp: txtDeptName, ApproveAccount: approveAccount, ApproveName: approveName, StartDate: txtBeginDdate, EndDate: txtEndDate, day: txtDay, _Desc: txtDesc, LeaveType: dropType },
                type: "post",
                dataType: "text",
                success: function (result) {

                }
            });
        }
     
    </script>
</head>
<body>
    <div style="margin:0">
        <div align="center" style="font-size:20px">請假申請流程</div>
        <div class="upload clearfix">
            <div class="uploadbtnBox clearfix">
                <a href="javascript:;" class="a-upload">
                    <input type="file" data-url="/Handler/upload.ashx" name="files" value="" id="file1"
                           />點選上傳
                </a>
            </div>
            <div class="filestate">
                檔名
            </div>
            <div class="progresspercent">
            </div>
            <div class="progress">
                <div class="bar" style="width: 0%;">
                </div>
            </div>
        </div>

        <div align="center" style="width:auto">
            <table border="1px" cellpadding="0" cellspacing="0">
                <tr><td colspan="4" style="font-weight:100">基本資訊</td></tr>
                <tr>
                    <td>申請單號:</td>
                    <td><input type="text" id="txtLeaveNumber" value="---申請單號自動生成----" disabled /></td>
                    <td>申請人:</td>
                    <td>
                        <input type="text" id="txtDisplayName" disabled />
                        <input type="hidden" id="txtAccount" />
                    </td>
                </tr>
                <tr>
                    <td>所在部門:</td>
                    <td><input type="text" id="txtDeptName" disabled /></td>
                    <td>申請日期:</td>
                    <td>
                        <input type="text" id="txtDate" disabled />
                    </td>
                </tr>
                <tr><td colspan="4" style="font-weight:100">申請資訊</td></tr>
                <tr>
                    <td>假期型別:</td>
                    <td colspan="3">
                        <select id="dropType">
                            <option value="年假">年假</option>
                            <option value="事假">事假</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>請假開始日期:</td>
                    <td>
                        <input type="date" id="txtBeginDdate" onclick="CalcDay()">
                    </td>
                    <td>請假結束日期</td>
                    <td><input id="txtEndDate" type="date" onclick="CalcDay()" /></td>
                </tr>
                <tr>
                    <td>請假天數:</td>
                    <td colspan="3"><input type="text" disabled id="txtDay" /></td>
                </tr>
                <tr>
                    <td>描述:</td>
                    <td colspan="3"><textarea rows="5" id="txtDesc" cols="20"></textarea></td>
                </tr>
            </table>
        </div>
        <div align="left" style="margin-left:500px">
            <input type="button" onclick="LeaveSubmit()" value="提交表單" />
        </div>
    </div>
</body>
</html>
View Code