MVC判断是否登录
@{
ViewBag.Title = "GoodcollectIndex";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>GoodcollectIndex</h2>
<script>
var UId = 0;
$(function () {
//判断是否登录
if (getCookie("UsersInfo") == null) {
alert('请先登录');
this.location.href = '/Sale/LoginIndex';
}
else {
var list = JSON.parse(getCookie("UsersInfo"));
UId = list[0].UId;
DataBound();
}
})
function DataBound() {
$.ajax({
url: "http://localhost:57190/api/Sale/GetGoodsCollectInfos",
data: { UId: UId },
type: "get",
dataType: "json",
success: function (d) {
var str = "";
$("#DataRow").empty();
$(d).each(function (i, n) {
str += "<div style='float:left;margin-left:20px;margin-top:20px'><input id='GId' type='hidden' value='" + n.GId + "'/><img src='http://localhost:57190" + n.GoodsInfoGId.GImg + "' width='85px' height='85px'/><br/> <a href='/Sale/GoodsInfoDetailIndex?GId=" + n.GId + "''><lable>" + n.GoodsInfoGId.GName + "</lable></a></div>"
})
$("#DataRow").append(str);
}
})
}
/* cookie中存值
*/
function setCookie(name, value) {
if (value) {
var days = 1; //定义一天
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
// 写入Cookie, toGMTString将时间转换成字符串
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
}
};
/**
* cookie中取值
* */
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段
if (arr = document.cookie.match(reg)) {
return unescape(arr[2]);
} else {
return null;
}
};
</script>
<div id="DataRow" style="width: 20%;height: 20%">
</div>