123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- 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,
- isMapShow: ''
- },
- 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
- });
- // console.log(that.data.hotGoods.length)
- // console.log(wx.getStorageSync('storeId'))
- // console.log(wx.getStorageSync('mapIndexStoreId'))
- if (that.data.hotGoods.length == 0){
- that.getGoodsList();
- }
- console.log("isShare:" + wx.getStorageSync('isShare'))
- if (wx.getStorageSync('storeId') != wx.getStorageSync('mapIndexStoreId') || wx.getStorageSync('isShare')=='true'){
- that.setData({
- hotGoods: [],
- page: 1
- });
- that.getGoodsList();
- wx.setStorageSync('isShare', 'false');
- }
- }
- 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");
- // options.scene = 65;
- // options.scene = 28;
- // options.scene = 8;
- if (options.scene) {
- console.log("have scene");
- var scene = decodeURIComponent(options.scene);
- console.log("scene is ", scene);
- that.setData({
- storeId: scene
- });
- wx.setStorageSync('storeId', scene);
- wx.removeStorageSync('isShare');//扫码进入的小程序则清除是否分享缓存
- } else {
- console.log("wx.getStorageSync('isShare')"+wx.getStorageSync('isShare'));
- if (wx.getStorageSync('isShare')) {//是否分享过商品,有数据则分享过;从首页进入页面则没有分享过,从详情页进入则分享过
- console.log('是分享进入首页门店id:' + wx.getStorageSync('storeId')+',从商品也获取的缓存')
- console.log('是分享进入首页:' + wx.getStorageSync('isShare'))
- } else {
- wx.removeStorageSync('storeId');
- console.log('不是分享进入首页门店id:【' + wx.getStorageSync('storeId')+'】,获取最近门店')
- console.log('不是分享进入首页:' + wx.getStorageSync('isShare'))
- }
- }
- },
- 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) {
- that.setData({
- isMapShow: wxRes.data.data.isMapShow
- });
- wx.setStorageSync('thirdPartyMerchCode', wxRes.data.data.thirdPartyMerchCode);
- // console.log(that.data.isMapShow)
- // 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 }));
- let storeId = wx.getStorageSync('storeId');
- console.log('查询门店id:' + storeId)
- util.request(api.NearbyList, { longitude: lng, latitude: lat, storeId: storeId, thirdPartyMerchCode: '' }).then((res) => {
- let nlist = res.data;
- // wx.removeStorageSync('nearStoreList');
- // wx.removeStorageSync('storeId');
- // wx.removeStorageSync('storeVo');
- // wx.setStorageSync('storeId', storeId);
- if (!wx.getStorageSync('currentCategory')) {
- wx.removeStorageSync('currentCategory');
- }
- if (!nlist.length) {
- wx.removeStorageSync('nearStoreList');
- wx.removeStorageSync('mapIndexStoreId');
- } else {
- wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
- wx.setStorageSync('mapIndexStoreId', nlist[0].id);
- }
- 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]));
- }
- })
- });
- },
- // 更新门店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,storeId:storeId, 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) {
- that.setData({
- goodsVo: res.data.goodsVo,
- specificationList: res.data.specificationList,
- productList: res.data.productList,
- openAttr: !that.data.openAttr,
- retailPrice: retailPrice,
- stockNum: res.data.stockNum,
- 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'
- // })
- wx.navigateTo({
- url: '../ucenter/campMinus/campMinus?campMinusId=' + 9 + '&&storeId=' + 8 + '&&campName=' + '满200减50' + "&&isShare=1"
- })
- },
- 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();
- }
- })
|