123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- var util = require('../../utils/util.js');
- var api = require('../../config/api.js');
- //获取应用实例
- const app = getApp();
- Page({
- data: {
- navList: [],
- categoryList: [],
- currentCategory: {},
- brandList:[],
- scrollLeft: 0,
- scrollTop: 0,
- goodsCount: 0,
- scrollHeight: 0,
- //
- filterCategory: [],
- goodsList: [],
- categoryId: 0,
- currentSortType: 'default',
- currentSortOrder: 'desc',
- filterDiscount: 0,// 0不限 1特价 2活动
- goodsBizType: app.globalData.appGoodsBizType,//业务类型
- page: 1,
- size: 4,
- showNavList: false,
- footCart: {},
- referrer: 0,
- sourceKey: '',
- openCoupon: false,
- refresh: false,
- hidden: false
- },
- toggleNav() {
- this.setData({
- showNavList: !this.data.showNavList
- })
- },
- switchNav(event) {
- let name = event.currentTarget.dataset.name;
- wx.switchTab({
- url: `/pages/${name}/${name}`,
- });
- },
- onLoad: function (options) {
- let that = this;
- // 页面初始化 options为页面跳转所带来的参数
- if (options.referrer){
- that.setData({
- referrer: parseInt(options.referrer),
- sourceKey: options.sourceKey,
- openCoupon: true
- });
- }
- // that.setData({
- // goodsBizType: app.globalData.appGoodsBizType
- // });
- wx.setNavigationBarTitle({
- title: '分类'
- });
- },
- getCatalog: function () {
- wx.showLoading({
- title: '加载中...',
- })
- //CatalogList
- let that = this;
- util.request(api.CatalogList,{
- storeId: wx.getStorageSync('storeId')
- }).then(function (res) {
- if (!wx.getStorageSync('currentCategory')) {
- console.log(res.data.currentCategory);
- console.log(res.data.categoryList);
- that.setData({
- navList: res.data.categoryList,
- currentCategory: res.data.currentCategory,
- brandList: res.data.brandList
- });
- wx.setStorageSync('currentCategory', res.data.currentCategory);
- } else {
- if (that.data.currentCategory && that.data.currentCategory.id > 0) {
- that.setData({
- navList: res.data.categoryList
- });
- console.log(res.data.categoryList);
- console.log(res.data.currentCategory);
- console.log(res.data.brandList);
- } else {
- that.setData({
- navList: res.data.categoryList,
- currentCategory: res.data.currentCategory,
- brandList: res.data.brandList
- });
- console.log(res.data.currentCategory);
- console.log(res.data.currentCategory.id);
- }
- }
- if (that.data.goodsList.length == 0 || wx.getStorageSync('storeId') != wx.getStorageSync('mapCatalogStoreId')) {
- wx.setStorageSync('mapCatalogStoreId', wx.getStorageSync('storeId'));
- that.setData({
- page: 1
- });
- that.getCategoryData();
- }
- // if (wx.getStorageSync('storeId') != wx.getStorageSync('mapCatalogStoreId')) {
- // wx.setStorageSync('mapCatalogStoreId', wx.getStorageSync('storeId'));
- // that.setData({
- // page: 1
- // });
- // that.getCategoryData();
- // }
-
- wx.hideLoading();
- });
- util.request(api.GoodsCount).then(function (res) {
- that.setData({
- goodsCount: res.data.goodsCount
- });
- });
- },
- getCurrentCategory: function (id) {
- let that = this;
- util.request(api.CatalogCurrent, { id: id })
- .then(function (res) {
- that.setData({
- currentCategory: res.data.currentCategory,
- brandList: res.data.brandList,
- goodsList: [],
- page: 1
- });
- // console.log(res.data.brandList);
- that.getGoodsList();
- });
- },
- onReady: function () {
- // 页面渲染完成
- },
- onShow: function () {
- let that = this;
- that.setData({
- goodsBizType: app.globalData.appGoodsBizType
- });
- // 页面显示
- if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
- if (wx.getStorageSync('storeId')) {
- util.request(api.ChooseStoreId, {
- storeId: wx.getStorageSync('storeId'),
- merchSn: wx.getStorageSync('merchSn')
- }, 'POST').then(function (res) {
- if (res.errno === 0) {
- wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
- wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
- that.reLoad();
- }
- });
- that.getFootCart();
- that.getCatalog();
- }
- } else {
- wx.navigateTo({
- url: '/pages/auth/btnAuth/btnAuth',
- })
- }
- },
- onHide: function () {
- // 页面隐藏
- this.setData({
- showNavList: false
- })
- },
- onUnload: function () {
- // 页面关闭
- },
- reLoad: function () {
- let that = this;
- if (wx.getStorageSync('storeId')) {
- if (wx.getStorageSync('userId')) {
- wx.request({
- url: api.updateLoginUser,
- data: {
- userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
- },
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- },
- success: function (wxRes) {
- if (wxRes.data.errno === 0) {
- // console.log("用户信息更新成功");
- }
- },
- fail: function (err) {
- console.log("failed");
- }
- });
- }
- }
- },
- getList: function () {
- var that = this;
- util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.id)
- .then(function (res) {
- that.setData({
- categoryList: res.data,
- });
- });
- },
- switchCate: function (event) {
- var that = this;
- var currentTarget = event.currentTarget;
- if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
- return false;
- }
- this.getCurrentCategory(event.currentTarget.dataset.id);
- },
- //
- getCategoryData: function () {
- let that = this;
- that.setData({
- goodsList: []
- });
- that.getGoodsList();
- },
- getGoodsList() {
- // wx.showLoading({
- // title: '加载中...',
- // });
- var that = this;
- if (that.data.currentCategory.id > 0){
- util.request(api.CatalogProductList, {
- page: that.data.page,
- size: that.data.size,
- order: that.data.currentSortOrder,
- sort: that.data.currentSortType,
- // discount: that.data.filterDiscount,
- goodsBizType: that.data.goodsBizType,
- categoryId: that.data.currentCategory.id
- })
- .then(function (res) {
- if (res.errno === 0) {
- let goodsList = that.data.goodsList.concat(res.data.data);
- that.setData({
- goodsList: goodsList,
- filterCategory: res.data.filterCategory,
- refresh: true
- });
- if(that.data.page == 4){
- that.setData({refresh: true});
- }else{
- that.setData({hidden: true })
- }
- }
- });
- }
- wx.hideLoading();
- },
- getFootCart: function () {
- let that = this;
- util.request(api.GetFootCart).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- footCart: res.data,
- });
- }
- });
- },
- //购物车减少
- cutNumber: function (e) {
- let that = this;
- var goodsId = e.currentTarget.dataset.goodsId;
- var productId = e.currentTarget.dataset.productId;
- var goodsList = that.data.goodsList;
- // goodsList.forEach(function (val, index, arr) {
- // if (val.product_id == productId) {
- // val.cart_num = val.cart_num - 1;
- // if (val.cart_num >= 0) {
- // goodsList[index] = val;
- // that.setData({goodsList: goodsList});
- // }
- // }
- // });
- // that.setData({goodsList: goodsList});
- util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
- if (res.errno === 0 && null != res.data) {
- var goodsList = that.data.goodsList;
- that.getFootCart();
- goodsList.forEach(function (val, index, arr) {
- if (val.product_id == productId) {
- val.cart_num = res.data;
- goodsList[index] = val;
- if(val.cart_num == 0){
- util.showErrorToast('不能再减了');
- }
- }
- }, that);
- that.setData({ goodsList: goodsList });
- }
- });
- },
- //购物车增加
- addNumber: function (e) {
- let that = this;
- var goodsId = e.currentTarget.dataset.goodsId;
- var productId = e.currentTarget.dataset.productId;
- var goodsList = that.data.goodsList;
- // goodsList.forEach(function (val, index, arr) {
- // if (val.product_id == productId) {
- // val.cart_num = val.cart_num + 1;
- // goodsList[index] = val;
- // }
- // });
- // that.setData({goodsList: goodsList});
- util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
- if (res.errno === 0 && null != res.data) {
- that.getFootCart();
- goodsList.forEach(function (val, index, arr) {
- res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
- if (val.product_id == cartVal.product_id) {
- val.cart_num = cartVal.number;
- goodsList[index] = val;
- }
- });
- }, that);
- that.setData({ goodsList: goodsList });
- }
- });
- },
- openSortFilter: function (event) {
- let that = this;
- let currentId = event.currentTarget.id;
- console.log('currentId:'+currentId);
- switch (currentId) {
- case 'defaultActivity':
- that.setData({
- 'goodsBizType': '00',
- goodsList: [],
- page: 1
- });
- app.globalData.appGoodsBizType = '00';
- this.getGoodsList();
- break;
- case 'discountActivity':
- that.setData({
- 'goodsBizType': '02',
- goodsList: [],
- page: 1
- });
- app.globalData.appGoodsBizType = '02';
- this.getGoodsList();
- break;
- case 'groupActivity':
- that.setData({
- 'goodsBizType': '10',
- goodsList: [],
- page: 1
- });
- app.globalData.appGoodsBizType = '10';
- this.getGoodsList();
- break;
- case 'ordActivity':
- that.setData({
- 'goodsBizType': '11',
- goodsList: [],
- page: 1
- });
- app.globalData.appGoodsBizType = '11';
- this.getGoodsList();
- break;
- case 'sellSort':
- let tmpSortOrder = 'asc';
- if (this.data.currentSortOrder == 'asc') {
- tmpSortOrder = 'desc';
- }
- this.setData({
- 'currentSortType': 'sell',
- 'currentSortOrder': tmpSortOrder,
- goodsList: [],
- page: 1
- });
- this.getGoodsList();
- break;
- case 'priceSort':
- tmpSortOrder = 'asc';
- if (this.data.currentSortOrder == 'asc') {
- tmpSortOrder = 'desc';
- }
- this.setData({
- 'currentSortType': 'price',
- 'currentSortOrder': tmpSortOrder,
- goodsList: [],
- page: 1
- });
- this.getGoodsList();
- break;
- default:
- //综合排序
- this.setData({
- 'currentSortType': 'default',
- 'currentSortOrder': 'desc',
- goodsList: [],
- page: 1
- });
- this.getGoodsList();
- }
- },
- takeShareCoupon() {
- let that = this;
- util.request(api.CouponTransActivit, {
- referrer: that.data.sourceKey,
- sourceKey: that.data.sourceKey
- }).then(function (res) {
- if (res.errno === 0) {
- util.showErrorToast("领取成功");
- that.setData({
- couponList: res.data,
- openCoupon: false
- });
- } else if (res.errno === 2) {
- util.showErrorToast(res.errmsg)
- that.setData({
- couponList: res.data,
- openCoupon: false
- });
- }
- });
- },
- closeCoupon: function () {
- var that = this;
- that.setData({
- openCoupon: false
- });
- },
- bindtapGoodsDetail: function (e) {
- var that = this;
- let url = '';
- var itemId = e.currentTarget.dataset.itemId;
- if (that.data.filterDiscount != 2) {
- url = '/pages/goods/goods?id=' + itemId;
- } else {
- url = '/pages/groupDetail/groupDetail?id=' + itemId;
- }
- wx.navigateTo({
- url: url,
- })
- },
- sercherCategory: function (e) {
- var that = this;
- let url = '';
- var replyType = e.currentTarget.dataset.replyType;
- var currentIndex = e.currentTarget.dataset.currentIndex;
- // console.log('replyType:' + e.currentTarget.dataset.replyType);
- // console.log('goodsBizType:' + that.data.goodsBizType);
- // 跳转页面
- wx.navigateTo({
- url: '/pages/category/category?id=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&¤tIndex=' + currentIndex
- })
- },
- sercherCategoryByBrand: function (e) {
- var that = this;
- let url = '';
- var replyType = e.currentTarget.dataset.replyType;
- var currentIndex = e.currentTarget.dataset.currentIndex;
- console.log('replyType:' + replyType);
- console.log('goodsBizType:' + that.data.goodsBizType);
- // 跳转页面
- wx.navigateTo({
- url: '/pages/categoryBrand/categoryBrand?brandId=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&¤tIndex=' + currentIndex
- })
- },
- // onReachBottom() {
- // var that = this;
- // wx.showLoading({
- // title: '加载中...',
- // })
- // that.setData({
- // page: that.data.page + 1
- // });
- // if(that.data.page > that.data.total){
- // that.setData({
- // hidden: false
- // });
- // }
- // that.getGoodsList();
- // },
- onLoaderMoreMovies: function () {
- var that = this;
- wx.showNavigationBarLoading();
- if (that.data.goodsList.length > 0) {
- wx.showLoading({
- title: '加载中...',
- });
- that.setData({
- page: that.data.page + 1
- });
- }
- console.log('加载');
- that.getGoodsList();
- wx.hideNavigationBarLoading()
- }
- })
|