MVC收藏店铺
@{
ViewBag.Title = "GoodsInfoDetailIndex";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>GoodsInfoDetailIndex</h2>
<script>
var GId = "";
$(function () {
GId = this.location.search.split("?")[1].split("=")[1];
DataBound();
})
function DataBound() {
$.ajax({
url: "http://localhost:57190/api/Sale/GetGoodsInfos",
data: { GId: GId },
type: "get",
dataType: "json",
success: function (d) {
$(d).each(function (i, n) {
$("#GImg").attr("src", "http://localhost:57190" + n.GImg);
$("#GName").text(n.GName);
$("#GNum").text(n.GNum);
$("#SName").text(n.SName);
$("input:hidden[id='SId']").val(n.SId)
})
}
})
}
//收藏宝贝
function Goodcollect() {
//判断是否登录
if (getCookie("UsersInfo") == null) {
this.location.href = '/Sale/LoginIndex';
}
else {
var list = JSON.parse(getCookie("UsersInfo"));
UId = list[0].UId;
$.ajax({
url: "http://localhost:57190/api/Sale/DecideGoodsCollect",
data: { UId: UId, GId: GId },
type: "get",
dataType: "json",
success: function (d) {
if (d > 0) {
var obj = {
GId: GId,
UId: UId,
CGState: 0
}
$.ajax({
url: "http://localhost:57190/api/Sale/AddGoodsCollectInfo",
data: obj,
type: "post",
dataType: "json",
success: function (d) {
if (d > 0) {
alert('收藏宝贝成功');
$("#Goodcollect").attr("display", "display");
}
else {
alert('收藏失败');
}
}
})
}
else {
alert('已收藏过此商品');
}
}
})
}
}
function Storecollect() {
//判断是否登录
if (getCookie("UsersInfo") == null) {
this.location.href = '/Sale/LoginIndex';
}
else {
var list = JSON.parse(getCookie("UsersInfo"));
UId = list[0].UId;
$.ajax({
url: "http://localhost:57190/api/Sale/DecideStoreCollect",
data: { UId: UId, SId: $("input:hidden[id='SId']").val() },
type: "get",
dataType: "json",
success: function (d) {
if (d > 0) {
var obj = {
SId: $("input:hidden[id='SId']").val(),
UId: UId,
CState: 0
}
$.ajax({
url: "http://localhost:57190/api/Sale/AddStroesCollectInfo",
data: obj,
type: "post",
dataType: "json",
success: function (d) {
if (d > 0) {
alert('收藏店铺成功');
$("#Storecollect").attr("display", "display");
}
else {
alert('收藏失败');
}
}
})
}
else {
alert('已收藏过此店铺');
}
}
})
}
}
/* 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 style="width: 50%;height: 50%;float:left">
<div style="float:left">
<img id="GImg" width='100px' height='100px' />
</div>
<div style="float:left;margin-left:20px">
<h3><lable id="GName"></lable></h3>
<lable id="GNum"></lable><br />
<lable id="SName"></lable><br />
<input id="SId" type="hidden" />
<input id="UId" type="hidden" />
</div>
</div>
<div style="width: 50%;height: 50%;float:right">
<label>我的收藏:</label>
<br />
<a href="/Sale/GoodcollectIndex">收藏de宝贝</a>
<a href="/Sale/StorecollectIndex">收藏de店铺</a>
<br /><hr />
<input id="Goodcollect" type="button" value="收藏宝贝" onclick="Goodcollect()" />
<input id="Storecollect" type="button" value="收藏店铺" onclick="Storecollect()" />
</div>