為什么要寫配置文件亏拉,復用是根本。每個頁面幾乎都有動態(tài)數(shù)據(jù)褪测,一個大型的項目可能有大幾十個甚至是上百個頁面,后臺會給我們上線接口,本地接口变隔,測試接口规伐,項目走到下一個階段要換接口進行調試更改,那是不是要上百個頁面都改一遍ajax中的url呢匣缘?配置文件很重要猖闪,可以達到改一動百的效果。
config.js:
(function (w) {
//商城
var apiHost='http://192.168.199.107:8081';
//云店
var marketHost='http://192.168.199.107:8083';
//配置項
w.C={};
//商城域名
C.host=apiHost+'/mall/mobile/';
//云店域名
C.market=marketHost+'/market/mobile/';
//token
C.token='2017082718351860892b7312e7b74b40b0a9923fb14aeb6a14';
C.marketToken='2017083115112355218e2b82ee170c4ba6b4b648dc2101aa10';
//獲取微信oppenId
C.getWxUserInfo='http://www.rrfun.com.cn/Uc/getInfo';
//商城接口
C.interface={
//折扣商城首頁
discount:'product/index',
//商品詳情
detail:'product/detail',
//意見反饋
opinion:'uc/feedback/add',
//商品類別
selectType:'index/categorys',
//顯示所有的收貨地址
allAddress:'uc/address/list',
//添加收貨地址
addAddress:'uc/address/add',
//修改收貨地址
insertAddress:'uc/address/edit',
//刪除收貨地址
deleteAddress:'uc/address/delete',
//根據(jù)商品id獲得所有評價信息
detailComment:'product/feedbacks',
//秒殺首頁
seckill:'seckill/list',
//秒殺詳情
seckillDetail:'seckill/detail',
seckillBanner:'seckill/banners'
};
//云店接口
C.marketInterface={
//店鋪分享
share:'uc/store/share',
//我的店鋪
myShop:'uc/store/index',
//東東推個人中心
my:'uc/index',
// 綁定手機號
bindingtel:'uc/phone/bind',
// 設置
setup:'uc/set/index',
//團隊管理
teamadmin:'uc/group/index',
//我的團隊
myteam:'uc/group/peoples',
//我的老師
myTeacher:'uc/teacher/index',
//注冊登錄提交驗證碼
reg:'login/reg',
//獲取公告
notice:'notice/list',
//公告詳情
noticeDetail:'notice/detail',
//商品中心
discount:'product/index',
//商品類別
selectType:'index/categorys',
//推廣中心
promotionCenter:'index/index',
//銷售管理(成功肌厨,失敗培慌,交易中)
orderList:'uc/order/list',
//個人店鋪中刪除商品
delMyShop:'uc/store/del',
//分享給朋友或朋友圈
shareFriend:'product/send',
//微信群發(fā)波次商品
batchShare:'product/weixinBatch',
//微信群發(fā)當前波的商品
weixinBatchSend:'product/weixinBatchSend',
//發(fā)送到微信朋友圈
friendBatch:'product/friendBatch',
//秒殺首頁
seckill:'seckill/list',
//秒殺詳情
seckillDetail:'seckill/detail',
seckillBanner:'seckill/banners'
};
//商城組合接口地址
for (k in C.interface){
C.interface[k]=C.host + C.interface[k];
}
//云店組合接口地址
for (k in C.marketInterface){
C.marketInterface[k]=C.market+C.marketInterface[k];
}
//獲取當前域名
var localHostUrl = window.location.href.replace(/(\?.+?)$/g, '');
localHostUrl = localHostUrl.replace(localHostUrl.split("/").pop(), '');
C.localHostUrl = localHostUrl;
})(window);
正常js文件:
$(document).ready(function () {
//獲取到url后面的productId,這個是標識
function locationSearcher(key) {
var search = location.search.split('?');
if(search.length>1){
var params = search[1].split('&');
for(var i=0;i<params.length; i++){
var item = params[i].split('=');
var k = item[0];
if(key == k){
return item[1];
}
}
}
return null;
}
var detailId=locationSearcher('productId');
// console.log(detailId);
//Handlebars詳情數(shù)據(jù)模板
var detailTpl=$('#detail-template').html();
var detailCmp=Handlebars.compile(detailTpl);
$.ajax({
url:C.interface.detail,
type:'POST',
dataType:'json',
data:{
productId:detailId,
},
success:function (response) {
if (response.result=='success'){
var data=response.data;
//Handlebars詳情數(shù)據(jù)模板
$('#detail-content-box').html(detailCmp(data));
}else if (response.result == 'login') {
alert('您還沒有登錄,請登錄');
} else {
alert(response.errorMsg);
}
}
});
});