您可以通過對帳戶訂單歷史記錄頁面執行GET請求來實現此目的。用戶必須登錄,並且您請求的頁面必須使用HTTP。
下面的這個演示被插入在default.html頁面上,它加載了直接鏈接到過去的文件下載。您需要做的唯一修改是使用您自己的URL作爲var pastOrdersUrl
,並確保它是HTTPS。
演示
HTML容器,以便容納結果
<!-- Downloadable Products -->
<div id="dlp">
<h3> Download Previous Purchases </h3>
<img id="dlp-loading" style="display:none;" src="http://www.arabianbusiness.com/skins/ab.main/gfx/loading_spinner.gif"/>
<p id="dlp-login" style="display:none; color:red;"> Please login in order to download past purchases</p>
<div id="results"></div> <!-- Hold the downloadable products -->
</div>
JAVASCRIPT
<!-- Load ES6 Promise Library to better Manage Async Requests -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.0.2/es6-promise.min.js"></script>
<script type="text/javascript">
// If Customer Signed In:
if ("%%GLOBAL_CurrentCustomerEmail%%") {
$('#dlp-loading').show(); //Show loading gif (nice effect):
var pastOrdersUrl = 'https://store-abc123.mybigcommerce.com/account.php?action=order_status'; //Replace with your own HTTPS url.
get(pastOrdersUrl)
.then(function(res) {
$(res).find('.OrderItemList > li > a').each(function() { //For each downloadable product on the order history page...
$('#dlp-loading').show(); //Continue to show the loading gif.
get($(this).attr('href')) //Grab it's url (via href attr) & Get content from the item download page...
.then(function(dl_page) {
//*** Append the Download Name and Direct Download Link To Results Container ***//
$('#results').append('<li>' +$(dl_page).find('.DownloadItem > strong').html() +'</li>');
$('#dlp-loading').hide();
});
});
});
} else {
$('#dlp-login').show(); //Tell user to log-in, if not so already.
}
/**
* Performs an HTTP GET request to the provided URL.
* @param url string - The url to GET.
* @return Promise - Fulfilled with request's response data.
*/
function get(url) {
return new Promise(function(fulfill,reject) {
$.ajax({
url: url,
success: function(res) {
fulfill(res);
}
});
});
}
</script>
視頻演示: http://screencast.com/t/8fZmSQVl
抓取每個下載url的請求都是異步發生的,所以它非常快。希望這有助於:-)
你能提供更多信息嗎?這相當模糊。 – Alyss
Alyss我在我的問題中添加更多細節。 Plz看看。謝謝 – pank