123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- 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
- })
- }
- },
- })
|