unserver-unify
Version:
218 lines (214 loc) • 6.94 kB
JavaScript
angular.module('bamboo.product').controller('ProductListCtrl', function($scope, ApiService, loginService, $state, $document, $rootScope, $stateParams) {
var _self = this;
console.log("---- ProductListInCategoryCtrl -----");
this.defaultPhoto = "assets/images/product.png";
this.productpath = ApiService.SHOST + '/product/';
this.items = [];
this.itemNumber = 6;
this.page = 0;
this.currentPage = 1;
this.category;
this.productcategory = ApiService.productcategory;
this.productkeys = ApiService.school.productkeys;
this.q;
this.banners = [];
this.id = ApiService.gid;
this.bannerPath = ApiService.SHOST + "/public/" + ApiService.RES + "/school/" + _self.id + "/";
_self.currency = (loginService.school.currency&&loginService.school.currency.sign)?loginService.school.currency.sign:'$';
console.log(this.bannerPath);
var sortmethod;
var searchmethod;
$scope.setting = {
listName: "product",
pageSize: _self.itemNumber,
currentPage: (_self.page + 1)
};
var currentkey;
if (loginService.school.homepage == 'genx' || loginService.subDomain == 'poiz') {
console.log('-- full width --');
$rootScope.fullwidthflag = true;
// setTabPage();
}
// $rootScope.fullwidthflag = true;
$scope.$on("$destroy", function() {
console.log('-- destroy --');
if ($state.current.name == 'index.advertise' || $state.current.name == 'index.store' || $state.current.name == 'index.store.detail' || $state.current.name.indexOf('index.home') > -1) {
$rootScope.fullwidthflag = true;
} else {
$rootScope.fullwidthflag = false;
}
});
console.info($rootScope.fullwidthflag);
this.getAllItems = function() {
var info = {limit:100};
info.action = "getschoolproducts";
console.log(info)
ApiService.post("/business", info).then(function(result) {
if (result.data.success) {
console.log(result.data.data);
_self.items = result.data.data.items;
_self.totalProdutcCount = result.data.data.counter;
angular.forEach(_self.items, function(item, index) {
if (item.sellingprice) {
_self.items[index].deals = 100 * (item.price - item.sellingprice) / item.price;
}
});
console.log(_self.items)
classifyProduct(_self.items);
}
});
};
getCategorylist();
this.getitems = function(key) {
currentkey = key;
var info = {
limit: _self.itemNumber,
start: _self.itemNumber * (_self.currentPage - 1),
};
console.log($state.current.name);
if (key) {
info.action = "getproductsbycategory";
info.key = key;
} else {
info.action = "getschoolproducts";
}
if (sortmethod) {
info.sort = sortmethod;
}
if (searchmethod) {
info.search = searchmethod;
}
console.log(info)
ApiService.post("/business", info).then(function(result) {
if (result.data.success) {
console.log(result.data.data);
_self.items = result.data.data.items;
if (!_self.allcounter || _self.allcounter < result.data.data.counter) {
_self.allcounter = result.data.data.counter;
}
_self.catCount = result.data.data.counter;
if (!_self.totalCount || _self.totalCount < result.data.data.counter) {
_self.totalCount = result.data.data.counter;
}
angular.forEach(_self.items, function(item, index) {
if (item.sellingprice) {
_self.items[index].deals = 100 * (item.price - item.sellingprice) / item.price;
}
});
console.log(_self.items)
}
});
};
this.categorylist = [];
function getCategorylist() {
var info = {
action: "getproductcategoryinfo"
};
ApiService.post("/business", info).then(function(result) {
console.log(result);
if (result.data.success) {
if($state.current.name=='index.product'){
_self.categorylist = result.data.data;
} else {
var _result = result.data.data;
for (var key in _result) {
var info = {
key: key,
counter: _result[key],
};
}
_self.categorylist = Object.keys(_result);
_self.option = _self.categorylist && _self.categorylist[0];
}
console.log(_self.categorylist);
}
});
}
//getInfo();
this.pageChanged = function(page) {
console.log(page);
_self.currentPage = page;
_self.getitems(_self.option);
};
function classifyProduct(productList) {
console.log(productList);
var productDict = {};
angular.forEach(productList, function(val, idx) {
if (val.type) {
if (!productDict[val.type]) {
productDict[val.type] = [];
productDict[val.type].push(val);
} else {
productDict[val.type].push(val);
}
}
});
console.log(productDict);
_self.productDict = productDict;
}
this.list = function(key) {
console.log(key);
_self.q = null;
_self.option = key;
sortmethod = null;
searchmethod = null;
_self.getitems(key);
};
this.search = function(tosearch) {
if(tosearch) {
searchmethod = {
name: tosearch
};
} else {
searchmethod = null;
}
sortmethod = null;
_self.option = null;
_self.getitems();
};
this.get = function(flag, reverse) {
console.log(flag, reverse);
_self.page = 1;
sortmethod = {
predicate: flag,
reverse: reverse
};
_self.q = null;
searchmethod = null;
_self.getitems();
};
this.clearSearch = function() {
_self.q = null;
_self.option = null;
searchmethod = null;
_self.getitems();
};
$scope.scrollTo = function(id) {
if (!id) return;
var duration = 1000; //milliseconds
var offset = 100; //pixels; adjust for floating menu, context etc
//Scroll to #some-id with 30 px "padding"
//Note: Use this in a directive, not with document.getElementById
var someElement = angular.element(document.getElementById(id));
$document.scrollToElement(someElement, offset, duration);
};
function _refreshSchoolInfo() {
ApiService.getSchoolInfo().then(function(school) {
$scope.school = _self.school = school;
console.log($scope.school.marketslides);
_self.banners = $scope.school.marketslides;
console.log($scope.school.marketslides);
console.log("------_refreshSchoolInfo-----------");
});
};
_refreshSchoolInfo();
if($state.current.name=='index.product'){
if($stateParams.key){
_self.list($stateParams.key);
} else {
_self.getitems();
}
} else {
_self.getAllItems();
}
});