京东详情

jd

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://greasyfork.org/scripts/414709-%E4%BA%AC%E4%B8%9C%E8%AF%A6%E6%83%85/code/%E4%BA%AC%E4%B8%9C%E8%AF%A6%E6%83%85.js?version=873743

$(function () {
	if (window.location.host.indexOf("item.jd.com") > -1) {
		let host = window.location.host;
		initJdDetail();
		addJdBasic();
	}
});
// 京东详情初始化入口
function initJdDetail() {
	if (window.location.host.indexOf("item.jd.com") > -1) {
		let id = window.location.pathname;
		let index = id.lastIndexOf("/");
		id = id.substring(index + 1, id.length);
		id = id.match(/(\S*).html/)[1];
		getJdDetail(id);
		likeJd();
		getJdHistory();
	}
}
//获取京东商品信息;
function getJdDetail(id) {
	let url = "https://api.ergirl.com/jdData";
	let params = {
		id: id,
	};
	dtd(url, params, addJdCoupon);
}
// 插入京东优惠券
function addJdCoupon(data) {
	let arr = data.data.list;
	if (arr.length > 0 && arr[0].couponInfo.couponList.length > 0) {
		let changeUrl = jdChangeUrl(
			arr[0].skuId,
			arr[0].couponInfo.couponList[0].link
		);
		let coupon = arr[0].couponInfo.couponList[0];
		let result = JSON.parse(changeUrl)
			.jd_union_open_promotion_common_get_response.result;
		let obj = JSON.parse(result).data;
		let clickURL = "";
		if (JSON.parse(result).code === 200) {
			clickURL = obj.clickURL;
		} else {
			clickURL = coupon.link;
		}
		let html =
			'<a rel=noreferrer href="' +
			clickURL +
			'" target="_blank"><div class="jar-body-coupon-left">满' +
			coupon.quota +
			"元减" +
			coupon.discount +
			"元" +
			"</div></a>" +
			'<div div class="jar-body-coupon-center">' +
			"<div>使用开始时间: <span>" +
			timeFormat(coupon.useStartTime, 1) +
			"</span></div>" +
			"<div>使用结束时间: <span>" +
			timeFormat(coupon.useEndTime, 1) +
			"</span></div>" +
			"</div >" +
			'<div class="jar-button"><a rel=noreferrer href="' +
			clickURL +
			'" target="_blank">点击领取</a></div>';
		$(".jar-body-coupon-top").append(html);
	} else {
        let changeUrl = jdChangeUrl(
            arr[0].skuId,
        );
        let result = JSON.parse(changeUrl)
        .jd_union_open_promotion_common_get_response.result;
        let obj = JSON.parse(result).data;
        let html =
            '<div class="getAgain">' +
            '<div class="again-box">' +
            '<img src="https://gw.alicdn.com/tps/TB11KWxOVXXXXXHXXXXXXXXXXXX-190-150.png" alt="">' +
            '<p>正在查询...</p>' +
            '<p>随后将返回详情页</p>' +
            '</div>' +
            '</div>' +
			'<div class="jar-body-coupon-left">当前商品暂无优惠券</div>' +
			'<div div class="jar-body-coupon-center">' +
			"<div>使用开始时间: <span>0</span></div>" +
			"<div>使用结束时间: <span>0</span></div>" +
			"</div >" +
			'<div class="jar-button"><a data-href="' +
			obj.clickURL +
			'" target="_blank">深度查询</a></div>';
        $(".jar-body-coupon-top").append(html);
        $(".jar-button").click(function(){
            $(".getAgain").show();
            setTimeout (() => {
                $(".getAgain").hide();
                let url = $(this).find("a").attr("data-href");
                redirect(url);
            }, 1000)
        })
	}
}
// 获取相似比价
function likeJd() {
	let goodsReq = {
		eliteId: 2,
		hasCoupon: 1,
		siteId: "4000380964",
	};
	let params = {
		v: "1.0",
		method: "jd.union.open.goods.material.query",
		app_key: "749ec5acf07b3bc2c623a465bc77c0e4",
		sign_method: "md5",
		format: "json",
		timestamp: dateFormat(),
		param_json: JSON.stringify({
			goodsReq: goodsReq,
		}),
	};
	let obj = objKeySort(params);
	let secretkey = "de15ce50b876430b941d3d9d5f307c2b";
	let str = "";
	for (i in obj) {
		if (
			params[i] !== "" &&
			params[i] !== null &&
			params[i] !== undefined
		) {
			str += i + params[i];
		}
	}
	params.sign = md5(secretkey + str + secretkey).toUpperCase();
	let url = "https://www.ergirl.com/qq/api";
	dtd(url, params, addJdLike);
}
// 插入相似
function addJdLike(data) {
	let result = JSON.parse(data)
		.jd_union_open_goods_material_query_response.result;
	let obj = JSON.parse(result).data;
	obj.forEach((item) => {
		let html =
			"<dl>" +
			'<dd><a rel=noreferrer href="' +
			item.promotionInfo.clickURL +
			'" target="_blank">' +
			'<div class="img">' +
			'<img src="' +
			item.imageInfo.imageList[0].url +
			'" alt="">' +
			"</div>" +
			'<div class="infor" >' +
			"<div>价格: <span>" +
			item.priceInfo.price +
			"</span></div>" +
			'<div class="jar-like-coupon">优惠券: <span>' +
			item.couponInfo.couponList[0].discount +
			"</span></div>" +
			"</div>" +
			'<div class="clear"></div>' +
			'<div class="title">' +
			item.skuName +
			"</div>" +
			"</a></dd>" +
			"</dl>";
		$(".jar-like-list").append(html);
	});
}
// ajax
function dtd(url, params, callback, query) {
	let dtd = $.Deferred();
	let wait = function (dtd) {
		$.ajax({
			url: url,
			method: "get",
			data: params,
		})
			.done(function (res) {
				dtd.resolve(res);
			})
			.fail(function () {
				dtd.resolve({ error: true });
			});
		return dtd.promise();
	};
	$.when(wait(dtd)).done(function (res, q = query) {
		callback(res, q);
	});
}
// 获取历史记录
function getJdHistory() {
	let basicAuth = "";
	let secret = "C5C3F201A8E8FC634D37A766A0299218";
	$.ajax({
		url: "https://api.ergirl.com/getHistoryTicketApi",
		type: "get",
		async: false,
		success: function (res) {
			let start = res.substring(res.length - 4, res.length);
			let end = res.substring(0, res.length - 4);
			basicAuth = start + end;
		},
	});
	let params = {
		key: window.location.href,
		method: "getHistoryTrend",
		t: Date.parse(new Date()),
	};
	let str = "";
	for (let i in params) {
		str += i + encodeURIComponent(params[i]);
	}
	params.token = hex_md5(
		(secret + str + secret).toLocaleUpperCase()
	).toUpperCase();
	$.ajax({
		url: "https://api.ergirl.com/historyApi/api.ashx",
		type: "post",
		data: params,
		headers: {
			Authorization: "BasicAuth " + basicAuth,
		},
		success: function (res) {
			addJdHistory(res.data);
		},
	});
}
// 插入历史价格
function addJdHistory(data) {
	if (data.error) {
		$("#historyChart").css({ height: "100px" });
		$("#historyChart").html("暂未收录");
		return false;
	}
	let arr = eval("[" + data.datePrice + "]");
	let seriesData = {
		historyPrice: [],
		time: [],
	};
	let lastPrice = -1;
	arr.forEach((item, index) => {
		const pp = item[1];
		const ts = item[0];
		if (pp != lastPrice) {
			lastPrice = pp;
			seriesData.historyPrice.push(pp);
			seriesData.time.push(timeFormat(ts, 1));
		}
	});
	let maxPrice = Math.max.apply(null, seriesData.historyPrice); //最大值
	seriesData.minNum = data.lowerPrice - 10;
	seriesData.maxNum = maxPrice + 10;
	eachart(seriesData);
}
// 插入基本元素
function addJdBasic() {
	let html =
		'<div class="jar-detail-coupon">' +
		'<div class="jar-tab" >' +
		"<ul>" +
		'<li class="active">领券</li>' +
		"<li>实时热销</li>" +
		"<li>价格趋势</li>" +
		'</ul><span class="jar-show">隐藏</span>' +
		"</div >" +
		'<div class="jar-body">' +
		"<ul>" +
		"<li>" +
		'<div class="jar-body-coupon">' +
		'<div class="jar-body-coupon-top"></div>' +
		'<div class="clear"></div>' +
		'<div class="jar-body-coupon-right">温馨提示: <span><a href="https://www.ergirl.com" target="_blank">点击查询更多优惠券</a></span></div>' +
		"</div>" +
		"</li>" +
		"<li>" +
		'<div class="jar-like-list">' +
		"</div> " +
		"</li>" +
		"<li>" +
		'<div class="jar-history">' +
		'<div id="historyChart" style="width: 468px; height: 300px;"></div>' +
		"</div>" +
		"</li>" +
		"</ul>" +
		"</div>" +
		"</div >";
	if (window.location.host.indexOf("jd") > -1) {
		// 插入淘宝
		$("#J-summary-top").append(html);
	}
	$(".jar-detail-coupon .jar-tab li").click(function () {
		let index = $(this).index();
		$(".jar-detail-coupon .jar-tab li").removeClass("active");
		$(this).addClass("active");
		$(".jar-detail-coupon .jar-body li").hide();
		$(".jar-detail-coupon .jar-body li").eq(index).show();
	});
	$(".jar-show").click(function () {
		if ($(".jar-show").html() == "隐藏") {
			$(".jar-show").html("展开");
		} else {
			$(".jar-show").html("隐藏");
		}
		$(".jar-detail-coupon .jar-body").toggle();
	});
}
// 京东转链
function jdChangeUrl(id, cUrl) {
	let mid = "https://item.jd.com/" + id + ".html";
	let promotionCodeReq = {
		materialId: mid,
		siteId: "4000380964",
	};
	if (cUrl) {
		promotionCodeReq.couponUrl = cUrl;
	}
	let params = {
		v: "1.0",
		method: "jd.union.open.promotion.common.get",
		app_key: "749ec5acf07b3bc2c623a465bc77c0e4",
		sign_method: "md5",
		format: "json",
		timestamp: dateFormat(),
		param_json: JSON.stringify({
			promotionCodeReq: promotionCodeReq,
		}),
	};
	let obj = objKeySort(params);
	let secretkey = "de15ce50b876430b941d3d9d5f307c2b";
	let str = "";
	for (i in obj) {
		if (
			params[i] !== "" &&
			params[i] !== null &&
			params[i] !== undefined
		) {
			str += i + params[i];
		}
	}
	params.sign = md5(secretkey + str + secretkey).toUpperCase();
	let data = {};
	$.ajax({
		url: "https://www.ergirl.com/qq/api",
		method: "get",
		async: false,
		data: params,
		success: function (res) {
			data = res;
		},
	});
	return data;
}