const util = require('../../utils/util.js'); const api = require('../../config/api.js'); const user = require('../../services/user.js'); //获取应用实例 const app = getApp(); Page({ data: { groupGoods: [], hotGoods: [], topics: [], brands: [], floorGoods: [], banner: [], channel: [], groupBanner: {}, storeName: '', showPop: false,//活动弹窗 couponVo: {}, storeId: '', page: 1, size: 3, list: [], openAttr: false, }, showCouponPop() { let that = this; this.setData({ showPop: false }); // wx.showToast({ // title: '恭喜获取优惠券一张' + that.data.couponVo.name, // duration: 2000 // }); wx.showModal({ title: '获取优惠券一张', showCancel: false, content: that.data.couponVo.name }) }, onShareAppMessage: function () { return { title: '商业版', desc: '新人好礼送券', path: '/pages/index/index' } }, getIndexData: function () { setTimeout(function () { }, 350) wx.showLoading({ title: '加载中...', }) let that = this; util.request(api.IndexUrl).then(function (res) { if (res.errno === 0) { // console.log(res.data.banner); that.setData({ // newGoods: res.data.newGoodsList, // hotGoods: res.data.hotGoodsList, // topics: res.data.topicList, // brand: res.data.brandList, // floorGoods: res.data.categoryList, banner: res.data.banner, // groupBanner: res.data.groupBanner, channel: res.data.channel }); if (that.data.hotGoods.length==0){ that.getGoodsList(); } } wx.hideLoading(); }); }, getGoodsList: function(){ let that = this; util.request(api.GoodsHot).then(function (res) { if (res.errno == 0) { util.request(api.HotGoodsList, { isHot: 1, page: that.data.page, size: that.data.size, categoryId: 0 }) .then(function (res) { if (res.errno == 0) { let goodsList = that.data.hotGoods.concat(res.data.goodsList); that.setData({ hotGoods: goodsList, list: res.data.goodsList }); } }); wx.hideLoading(); } }); }, getGroupData: function () { let that = this; util.request(api.GroupList).then(function (res) { if (res.errno === 0) { that.setData({ groupGoods: res.data.data, }); } }); }, onLoad: function (options) { let that = this; wx.setStorageSync("navUrl", "/pages/index/index"); if (options.scene) { console.log("has scene"); var scene = decodeURIComponent(options.scene); console.log("scene is ", scene); that.setData({ storeId: scene }); wx.setStorageSync('storeId', scene); } else { console.log("no scene"); } }, onReady: function () { // 页面渲染完成 }, onShow: function () { // 页面显示 let that = this; wx.setStorageSync("navUrl", "/pages/index/index"); if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) { that.syncStore(); } else { wx.navigateTo({ url: '/pages/auth/btnAuth/btnAuth', }) } }, onHide: function () { // 页面隐藏 }, onUnload: function () { // 页面关闭 }, handleStore() { wx.navigateTo({ url: '../map/map', }) }, goSearch() { wx.navigateTo({ url: '../search/search', }) }, goCatalog: function (e) { let url = ''; // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType); app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType; // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType); wx.switchTab({ url: '/pages/catalog/catalog', }); }, onReachBottom: function () { if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) { return false; } this.setData({ bottomLoading: true }); // this.getFloorCategory(); }, reLoad: function () { let that = this; // console.log(wx.getStorageSync('userId')); // console.log(wx.getStorageSync('storeId')); // console.log(wx.getStorageSync('merchSn')); 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"); } }); } that.getIndexData(); that.enableActivity(); that.getGroupData(); } }, // 同步门店 syncStore: function () { let that = this; // console.log(wx.getStorageSync('merchSn')); //获取附件门店信息 util.getLocation((lng, lat) => { wx.setStorageSync('location', JSON.stringify({ lng, lat })); util.request(api.NearbyList, { longitude: lng, latitude: lat, storeId: that.data.storeId }).then((res) => { let nlist = res.data; // console.log(nlist); wx.removeStorageSync('nearStoreList'); wx.removeStorageSync('storeId'); wx.removeStorageSync('storeVo'); // console.log('sasas:' + wx.getStorageSync('currentCategory')); if (!wx.getStorageSync('currentCategory')) { wx.removeStorageSync('currentCategory'); } if (!nlist.length) { wx.removeStorageSync('nearStoreList'); } else { wx.setStorageSync('nearStoreList', JSON.stringify(nlist)); } if (!wx.getStorageSync('storeId')) { if (!nlist.length) { wx.removeStorageSync('storeId'); wx.removeStorageSync('storeVo'); that.setData({ storeName: '附近暂无门店' }) } else { that.setData({ storeName: nlist[0].storeName, storeId: nlist[0].id }) that.chooseStore(nlist[0].id, nlist[0].merchSn); wx.setStorageSync('storeVo', JSON.stringify(nlist[0])); } } else { var storeVo = JSON.parse(wx.getStorageSync('storeVo')); that.chooseStore(storeVo.id, storeVo.merchSn); that.setData({ storeName: storeVo.storeName, storeId: storeVo.id }); } }) }); }, // 更新门店Id chooseStore: function (storeId, merchSn) { let that = this; util.request(api.ChooseStoreId, { storeId: storeId, merchSn: merchSn }, 'POST').then(function (res) { if (res.errno === 0) { wx.setStorageSync('storeId', storeId); wx.setStorageSync('merchSn', merchSn); that.reLoad(); } }); }, //购物车减少 cutNumber: function (e) { let that = this; var goodsId = e.currentTarget.dataset.goodsId; var productId = e.currentTarget.dataset.productId; var hotGoods = that.data.hotGoods; // hotGoods.forEach(function (val, index, arr) { // if (val.product_id == productId) { // val.cart_num = val.cart_num - 1; // if (val.cart_num >= 0) { // hotGoods[index] = val; // } // } // }); // that.setData({ hotGoods: hotGoods }); util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) { if (res.errno === 0 && null != res.data) { var hotGoods = that.data.hotGoods; hotGoods.forEach(function (val, index, arr) { if (val.product_id == productId) { val.cart_num = res.data; hotGoods[index] = val; that.setData({ hotGoods: hotGoods }); } }, that); } }); }, //购物车增加 addNumber: function (e) { let that = this; var goodsId = e.currentTarget.dataset.goodsId; var productId = e.currentTarget.dataset.productId; var hotGoods = that.data.hotGoods; // hotGoods.forEach(function (val, index, arr) { // if (val.product_id == productId) { // val.cart_num = val.cart_num + 1; // hotGoods[index] = val; // } // }); // that.setData({ hotGoods: hotGoods }); util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) { if (res.errno === 0 && null != res.data) { hotGoods.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; hotGoods[index] = val; } }); that.setData({ hotGoods: hotGoods }); }, that); } else { wx.showToast({ title: res.errmsg, icon: 'none' }) } }); }, // 查询是否有活动 enableActivity: function () { let that = this; let couponIds = wx.getStorageSync('couponIds'); if (!couponIds) { couponIds = new Array(); } util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) { // if (res.errno === 0 && null != res.data.showCoupon) { // if (couponIds.contains(res.data.showCoupon.id)) { // return; // } // couponIds.push(res.data.showCoupon.id); // wx.setStorageSync('couponIds', couponIds); // that.setData({ // couponVo: res.data.showCoupon, // showPop: true // }); // } else if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) { that.setData({ couponVo: res.data.takeCoupon, showPop: true }); } }); }, // 商品扫码 scanGoodsCode: function (e) { var that = this; var code; var value; var substrValue; var scanType; // 调起客户端扫码界面进行扫码 wx.scanCode({ // 是否只能从相机扫码 onlyFromCamera: true, // 扫码类型, barCode:一维码, qrCode:二维码 scanType: ['barCode', 'qrCode'], success: function (res) { that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData; that.value = res.result; that.scanType = res.scanType; that.setData({ goodsCode: that.code }); if (that.scanType == 'QR_CODE') {//二维码 that.substrValue = that.value.substring(0, 5); that.value = that.value.substring(5, that.value.length); // var goodId = that.value.substring(18, that.value.length); var scanArray = that.value.split('&'); // console.log(scanArray.length); if (scanArray.length < 2) { wx.showModal({ title: '', content: '您所扫描的商品无效', showCancel: false }); return; } var goodId = scanArray[0].substring(18, that.value.length); var storeId = scanArray[1].substring(8, that.value.length); // console.log("storeId:" + storeId); // console.log("goodId:" + goodId); if (that.substrValue != 'emato') {//../goods/goods?id=&merchSn= wx.showModal({ title: '', content: '您所扫描的商品无效', showCancel: false, success: function (res) { if (res.confirm) { console.log('用户点击确定') } else if (res.cancel) { console.log('用户点击取消') } } }); return; } if (storeId != wx.getStorageSync('storeId')) { wx.showModal({ title: '扫描结果', content: '该商品不属于当前门店', showCancel: false }); return; } util.request(api.GoodsDetail, { id: goodId, referrer: '' }).then(function (res) { if (res.errno === 0) { console.log(res); // 跳转页面 setTimeout(function () { wx.navigateTo({ url: that.value, success: function (e) { console.log('跳转成功'); }, fail: function (e) { console.log('跳转失败'); } }) }, 350) } else { wx.showModal({ title: '扫描结果', content: '商品不存在', showCancel: false }); } }); } else {//其他码 //弹框显示结果 wx.showModal({ title: '扫描结果', content: that.value, showCancel: false }); } }, fail: function () { // 显示提示框 wx.showToast({ title: '扫码失败', icon: 'none', // 提示的延迟时间 duration: 3000 }) } }) }, imgOnLoad: function (e) { let that = this; // console.log('图片加载完成'); // var realthumb = e.target.dataset.thumb; // let list = that.data.list // for (var i = 0; i < list.length; i++) { // if (list[i].thumb == realthumb) { // list[i].loaded = true // } // that.setData({ // list: list // }) // } }, switchAttrPop: function () { this.setData({ openAttr: !this.data.openAttr }) }, hideSwitchAttrPop: function () { this.setData({ openAttr: false }) }, //购物车增加 addCart: function (e) { let that = this; that.setData({ number: 1 }); var goodsId = e.currentTarget.dataset.goodsId; var retailPrice = e.currentTarget.dataset.retailPrice; util.request(api.GoodsSku, { goodsId: goodsId }).then(function (res) { if (res.errno === 0 && null != res.data) { let stockNumbers = 0; if (res.data.goodsVo.goodsBizType == '00') { if (res.data.goodsVo.isStockShare == 1) { stockNumbers = res.data.goodsVo.goods_number; } else { stockNumbers = res.data.goodsVo.stockNum; } } else { stockNumbers = res.data.goodsVo.stockNum; } that.setData({ goodsVo: res.data.goodsVo, specificationList: res.data.specificationList, productList: res.data.productList, openAttr: !that.data.openAttr, retailPrice: retailPrice, stockNum: stockNumbers, cartNumber: res.data.cartNumber, checkedSpecText: res.data.specificationList[0].valueList[0].value }); // let _specificationList = res.data.specificationList; for (let i = 0; i < _specificationList.length; i++) { if (_specificationList[i].valueList.length == 1) { //如果已经选中,则反选 _specificationList[i].valueList[0].checked = true; } } that.setData({ 'specificationList': _specificationList }); } }); }, shows: function (e) { wx.redirectTo({ url: '/pages/images/images' }) }, onReachBottom() { var that = this; if (that.data.list.length > 0) { console.log(that.data.list) wx.showLoading({ title: '加载中...', }) } that.setData({ page: that.data.page + 1 }); that.getGoodsList(); } })