const WXAPI = require('apifm-wxapi') const TOOLS = require('../../utils/tools.js') const AUTH = require('../../utils/auth') const APP = getApp() Page({ data: { inputVal: "", // 搜索框内容 goodsRecommend: [], // 推荐商品 kanjiaList: [], //砍价商品列表 pingtuanList: [], //拼团商品列表 loadingHidden: false, // loading selectCurrent: 0, categories: [], goods: [], loadingMoreHidden: true, coupons: [], curPage: 1, pageSize: 20 }, tabClick(e) { // 商品分类点击 const category = this.data.categories.find(ele => { return ele.id == e.currentTarget.dataset.id }) if (category.vopCid1 || category.vopCid2) { wx.navigateTo({ url: '/pages/goods/list-vop?cid1=' + (category.vopCid1 ? category.vopCid1 : '') + '&cid2=' + (category.vopCid2 ? category.vopCid2 : ''), }) } else { wx.setStorageSync("_categoryId", category.id) wx.switchTab({ url: '/pages/category/category', }) } }, tabClickCms(e) { // 文章分类点击 const category = this.data.cmsCategories[e.currentTarget.dataset.idx] wx.navigateTo({ url: '/pages/cms/list?categoryId=' + category.id, }) }, toDetailsTap: function(e) { console.log(e); const id = e.currentTarget.dataset.id const supplytype = e.currentTarget.dataset.supplytype const yyId = e.currentTarget.dataset.yyid if (supplytype == 'cps_jd') { wx.navigateTo({ url: `/packageCps/pages/goods-details/cps-jd?id=${id}`, }) } else if (supplytype == 'vop_jd') { wx.navigateTo({ url: `/pages/goods-details/vop?id=${yyId}&goodsId=${id}`, }) } else if (supplytype == 'cps_pdd') { wx.navigateTo({ url: `/packageCps/pages/goods-details/cps-pdd?id=${id}`, }) } else if (supplytype == 'cps_taobao') { wx.navigateTo({ url: `/packageCps/pages/goods-details/cps-taobao?id=${id}`, }) } else { wx.navigateTo({ url: `/pages/goods-details/index?id=${id}`, }) } }, tapBanner: function(e) { const url = e.currentTarget.dataset.url if (url) { wx.navigateTo({ url }) } }, adClick: function(e) { const url = e.currentTarget.dataset.url if (url) { wx.navigateTo({ url }) } }, bindTypeTap: function(e) { this.setData({ selectCurrent: e.index }) }, onLoad: function(e) { wx.showShareMenu({ withShareTicket: true, }) const that = this // 读取分享链接中的邀请人编号 if (e && e.inviter_id) { wx.setStorageSync('referrer', e.inviter_id) } // 读取小程序码中的邀请人编号 if (e && e.scene) { const scene = decodeURIComponent(e.scene) if (scene) { wx.setStorageSync('referrer', scene.substring(11)) } } // 静默式授权注册/登陆 AUTH.checkHasLogined().then(isLogined => { if (!isLogined) { AUTH.authorize().then( aaa => { AUTH.bindSeller() TOOLS.showTabBarBadge() }) } else { AUTH.bindSeller() TOOLS.showTabBarBadge() } }) this.initBanners() this.categories() this.cmsCategories() // https://www.yuque.com/apifm/nu0f75/wg5t98 WXAPI.goodsv2({ recommendStatus: 1 }).then(res => { if (res.code === 0){ that.setData({ goodsRecommend: res.data.result }) } }) that.getCoupons() that.getNotice() that.kanjiaGoods() that.pingtuanGoods() this.adPosition() // 读取系统参数 this.readConfigVal() getApp().configLoadOK = () => { this.readConfigVal() } }, readConfigVal() { wx.setNavigationBarTitle({ title: wx.getStorageSync('mallName') }) this.setData({ mallName:wx.getStorageSync('mallName')?wx.getStorageSync('mallName'):'', show_buy_dynamic: wx.getStorageSync('show_buy_dynamic') }) }, async miaoshaGoods(){ // https://www.yuque.com/apifm/nu0f75/wg5t98 const res = await WXAPI.goodsv2({ miaosha: true }) if (res.code == 0) { res.data.result.forEach(ele => { const _now = new Date().getTime() if (ele.dateStart) { ele.dateStartInt = new Date(ele.dateStart.replace(/-/g, '/')).getTime() - _now } if (ele.dateEnd) { ele.dateEndInt = new Date(ele.dateEnd.replace(/-/g, '/')).getTime() -_now } }) this.setData({ miaoshaGoods: res.data.result }) } }, async initBanners(){ const _data = {} // 读取头部轮播图 const res1 = await WXAPI.banners({ type: 'index' }) if (res1.code == 700) { wx.showModal({ title: '提示', content: '请在后台添加 banner 轮播图片,自定义类型填写 index', showCancel: false }) } else { _data.banners = res1.data } this.setData(_data) }, onShow: function(e){ this.setData({ navHeight: APP.globalData.navHeight, navTop: APP.globalData.navTop, windowHeight: APP.globalData.windowHeight, menuButtonObject: APP.globalData.menuButtonObject //小程序胶囊信息 }) this.setData({ shopInfo: wx.getStorageSync('shopInfo') }) // 获取购物车数据,显示TabBarBadge TOOLS.showTabBarBadge() this.goodsDynamic() this.miaoshaGoods() const refreshIndex = wx.getStorageSync('refreshIndex') if (refreshIndex) { this.onPullDownRefresh() wx.removeStorageSync('refreshIndex') } }, async goodsDynamic(){ const res = await WXAPI.goodsDynamic(0) if (res.code == 0) { this.setData({ goodsDynamic: res.data }) } }, async categories(){ const res = await WXAPI.goodsCategory() let categories = []; if (res.code == 0) { const _categories = res.data.filter(ele => { return ele.level == 1 }) categories = categories.concat(_categories) } this.setData({ categories: categories, curPage: 1 }); this.getGoodsList(0); }, async getGoodsList(categoryId, append) { if (categoryId == 0) { categoryId = ""; } wx.showLoading({ "mask": true }) // https://www.yuque.com/apifm/nu0f75/wg5t98 const res = await WXAPI.goodsv2({ categoryId: categoryId, page: this.data.curPage, pageSize: this.data.pageSize }) wx.hideLoading() if (res.code == 404 || res.code == 700) { let newData = { loadingMoreHidden: false } if (!append) { newData.goods = [] } this.setData(newData); return } let goods = []; if (append) { goods = this.data.goods } for (var i = 0; i < res.data.result.length; i++) { goods.push(res.data.result[i]); } this.setData({ loadingMoreHidden: true, goods: goods, }); }, getCoupons: function() { var that = this; WXAPI.coupons().then(function (res) { if (res.code == 0) { that.setData({ coupons: res.data }); } }) }, onShareAppMessage: function() { return { title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'), path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid') } }, getNotice: function() { var that = this; WXAPI.noticeList({pageSize: 5}).then(function (res) { if (res.code == 0) { that.setData({ noticeList: res.data }); } }) }, onReachBottom: function() { this.setData({ curPage: this.data.curPage + 1 }); this.getGoodsList(0, true) }, onPullDownRefresh: function() { this.setData({ curPage: 1 }); this.getGoodsList(0) wx.stopPullDownRefresh() }, // 获取砍价商品 async kanjiaGoods(){ // https://www.yuque.com/apifm/nu0f75/wg5t98 const res = await WXAPI.goodsv2({ kanjia: true }); if (res.code == 0) { const kanjiaGoodsIds = [] res.data.result.forEach(ele => { kanjiaGoodsIds.push(ele.id) }) const goodsKanjiaSetRes = await WXAPI.kanjiaSet(kanjiaGoodsIds.join()) if (goodsKanjiaSetRes.code == 0) { res.data.result.forEach(ele => { const _process = goodsKanjiaSetRes.data.find(_set => { return _set.goodsId == ele.id }) if (_process) { ele.process = 100 * _process.numberBuy / _process.number ele.process = ele.process.toFixed(0) } }) this.setData({ kanjiaList: res.data.result }) } } }, goCoupons: function (e) { wx.switchTab({ url: "/pages/coupons/index" }) }, pingtuanGoods(){ // 获取团购商品列表 const _this = this // https://www.yuque.com/apifm/nu0f75/wg5t98 WXAPI.goodsv2({ pingtuan: true }).then(res => { if (res.code === 0) { _this.setData({ pingtuanList: res.data.result }) } }) }, goSearch(){ wx.navigateTo({ url: '/pages/search/index' }) }, goNotice(e) { const id = e.currentTarget.dataset.id wx.navigateTo({ url: '/pages/notice/show?id=' + id, }) }, async adPosition() { let res = await WXAPI.adPosition('indexPop') if (res.code == 0) { this.setData({ adPositionIndexPop: res.data }) } res = await WXAPI.adPosition('index-live-pic') if (res.code == 0) { this.setData({ adPositionIndexLivePic: res.data }) } }, clickAdPositionIndexLive() { if (!this.data.adPositionIndexLivePic || !this.data.adPositionIndexLivePic.url) { return } wx.navigateTo({ url: this.data.adPositionIndexLivePic.url, }) }, closeAdPositionIndexPop() { this.setData({ adPositionIndexPop: null }) }, clickAdPositionIndexPop() { const adPositionIndexPop = this.data.adPositionIndexPop this.setData({ adPositionIndexPop: null }) if (!adPositionIndexPop || !adPositionIndexPop.url) { return } wx.navigateTo({ url: adPositionIndexPop.url, }) }, async cmsCategories() { // https://www.yuque.com/apifm/nu0f75/slu10w const res = await WXAPI.cmsCategories() if (res.code == 0) { const cmsCategories = res.data.filter(ele => { return ele.type == 'index' // 只筛选类型为 index 的分类 }) this.setData({ cmsCategories }) } }, })