You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
505 lines
12 KiB
505 lines
12 KiB
// pages/index/index.js
|
|
var app = getApp();
|
|
var zi = 0;
|
|
var cont_time = 0; //首页tab点击
|
|
var WXAPI = require('../../utils/server.js');
|
|
var util = require('../../utils/util.js')
|
|
Page({
|
|
data: {
|
|
barHeight: wx.getSystemInfoSync().statusBarHeight,
|
|
pop: null,
|
|
inforList: [], //公告
|
|
banner: [],
|
|
indicatorDots: true, // 是否显示面板指示点
|
|
autoplay: true, // 是否自动切换
|
|
interval: 5000, // 自动切换时间间隔
|
|
duration: 1000, // 滑动动画时长
|
|
circular: true, // 是否采用衔接滑动
|
|
scrollLeft: 0, //tab标题的滚动条位置
|
|
current: 0, //当前选中的Tab项
|
|
scrollTop: 0,
|
|
page: 1,
|
|
index: 1,
|
|
cont: 1,
|
|
tabid: 58,
|
|
loading: false,
|
|
remind: '加载中',
|
|
showModal: false,
|
|
plug: [],
|
|
timestamp: 0,
|
|
searchView: false,
|
|
images: {},
|
|
zjList: {},
|
|
zjList_box: false,
|
|
cart: 0, //购物车数量
|
|
region: '',
|
|
latitude: 31.772752,
|
|
longitude: 119.946973,
|
|
},
|
|
//下拉事件
|
|
onPullDownRefresh: function () {
|
|
var that = this;
|
|
that.getMore();
|
|
},
|
|
navToArea: function () {
|
|
wx.navigateTo({
|
|
url: "../chooseArea/chooseArea?"
|
|
})
|
|
},
|
|
imgW: function (e) {
|
|
var $width = e.detail.width, //获取图片真实宽度
|
|
$height = e.detail.height,
|
|
ratio = $width / $height; //图片的真实宽高比例
|
|
var viewWidth = 718, //设置图片显示宽度,左右留有16rpx边距
|
|
viewHeight = 718 / ratio; //计算的高度值
|
|
var image = this.data.images;
|
|
//将图片的datadata-index作为image对象的key,然后存储图片的宽高值
|
|
image[e.target.dataset.index] = {
|
|
width: viewWidth,
|
|
height: viewHeight
|
|
}
|
|
this.setData({
|
|
images: image
|
|
})
|
|
},
|
|
getMore: function () {
|
|
var that = this;
|
|
var page = that.data.page;
|
|
var cid = that.data.tabid;
|
|
var current = that.data.current;
|
|
that.setData({
|
|
page: page + 1
|
|
});
|
|
WXAPI.indexGetMore({
|
|
page: page,
|
|
cid: cid,
|
|
}).then(res => {
|
|
if (res.code == 200) {
|
|
var data = res.data;
|
|
if (data == '') {
|
|
that.setData({
|
|
loading: false,
|
|
});
|
|
return;
|
|
} else {
|
|
var titleList = that.data.titleList;
|
|
titleList[current].list.push(...prolist)
|
|
that.setData({
|
|
loading: false,
|
|
titleList: titleList,
|
|
});
|
|
}
|
|
wx.hideNavigationBarLoading() //完成停止加载
|
|
wx.stopPullDownRefresh() //停止下拉刷新
|
|
}
|
|
});
|
|
},
|
|
inputConfirm: function (e) {
|
|
var that = this,
|
|
value = e.detail.value;
|
|
this.setData({
|
|
value: e.detail.value,
|
|
});
|
|
|
|
if (value != '') {
|
|
wx.navigateTo({
|
|
url: "../listdetail/listdetail?keyword=" + value
|
|
})
|
|
}
|
|
},
|
|
navToSearch() {
|
|
wx.navigateTo({
|
|
url: '/pages/search/search'
|
|
})
|
|
},
|
|
searchView: function (e) {
|
|
console.log(e);
|
|
this.setData({
|
|
searchView: !this.data.searchView,
|
|
});
|
|
},
|
|
inputBlur: function (e) {
|
|
this.setData({
|
|
value: e.detail.value,
|
|
});
|
|
this.searchView();
|
|
},
|
|
search_cancel: function (e) {
|
|
var formId = e.detail.formId;
|
|
var value = e.detail.value.search_value;
|
|
console.log(this.data.value, e);
|
|
if (value.length > 0) {
|
|
wx.navigateTo({
|
|
url: "../listdetail/listdetail?keyword=" + value
|
|
})
|
|
} else {
|
|
wx.showToast({
|
|
title: '请输入关键词!',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
if (formId != 'the formId is a mock one') {
|
|
var page = 'pages/index/index'
|
|
WXAPI.productSaveFormid({
|
|
from_id: formId,
|
|
userid: app.globalData.userInfo.openid
|
|
}).then(res => {
|
|
|
|
});
|
|
}
|
|
},
|
|
search: function () {
|
|
var that = this,
|
|
value = this.data.value;
|
|
if (value != '') {
|
|
wx.navigateTo({
|
|
url: "../listdetail/listdetail?keyword=" + value
|
|
})
|
|
}
|
|
},
|
|
|
|
loadProductDetail: function () {
|
|
var that = this;
|
|
// var userinfo = wx.getStorageSync('userInfo');
|
|
WXAPI.homeIndex({
|
|
accessId: app.globalData.accessId,
|
|
storeId: 239,
|
|
storeType: 1,
|
|
latitude: that.data.latitude,
|
|
longitude: that.data.longitude,
|
|
}).then(res => {
|
|
if (res.code == 200) {
|
|
var data = res.data;
|
|
var titleList = data.list2; //标题导航栏
|
|
app.globalData.logoimg = data.logo
|
|
that.setData({
|
|
inforList: data.notice, //滚动公告条
|
|
banner: data.banner, // 轮播图
|
|
titleList: titleList,
|
|
r_mch: data.r_mch, //优选店铺
|
|
plug: data.nav_list, //首页类型模块
|
|
mch_name: data.title,
|
|
logo: data.logo,
|
|
zjList: data.Marketing_list,
|
|
memberPlugin: data.memberPlugin //是否显示会员权益条
|
|
});
|
|
that.setData({ remind: '' });
|
|
// console.log(data.Marketing_list.length)
|
|
if (data.Marketing_list.length) {
|
|
that.listnsg();
|
|
}
|
|
} else {
|
|
wx.showToast({
|
|
title: res.message,
|
|
duration: 2000
|
|
});
|
|
}
|
|
});
|
|
//新品上市
|
|
WXAPI.newArrival({
|
|
accessId: app.globalData.accessId,
|
|
storeId: 239,
|
|
storeType: 1,
|
|
}).then(res => {
|
|
if (res.code == 200) {
|
|
var data = res.data
|
|
that.setData({
|
|
newImgList: data.list
|
|
})
|
|
}
|
|
});
|
|
//好物优选
|
|
WXAPI.recommend({
|
|
accessId: app.globalData.accessId,
|
|
storeId: 239,
|
|
storeType: 1,
|
|
}).then(res => {
|
|
if (res.code == 200) {
|
|
var data = res.data
|
|
that.setData({
|
|
recommendImgList: data.list
|
|
})
|
|
}
|
|
});
|
|
},
|
|
navigatorAct: function(e) {
|
|
var data = e.currentTarget.dataset
|
|
wx.navigateTo({
|
|
url:"/pages/new/new?title="+data.title,
|
|
})
|
|
},
|
|
//找相似
|
|
discoverSimilarProducts: function (e) {
|
|
var goodsId = e.currentTarget.dataset;
|
|
console.log(goodsId)
|
|
wx.navigateTo({
|
|
url:"/pages/similar/similar?id="+goodsId.id
|
|
})
|
|
},
|
|
navToShopList: function (e) {
|
|
wx.navigateTo({
|
|
url:"/pages/shop/shop"
|
|
})
|
|
},
|
|
onHide: function () {
|
|
clearTimeout();
|
|
},
|
|
listnsg: function () {
|
|
var zjList = this.data.zjList;
|
|
var that = this;
|
|
var time = 1500;
|
|
if (zjList[zi].type == 2) {
|
|
time = 6000;
|
|
} else {
|
|
time = 1500;
|
|
}
|
|
that.setData({
|
|
zjList_box: false
|
|
});
|
|
that.listnsg();
|
|
that.setData({
|
|
headimgurl: zjList[zi].headimgurl,
|
|
user_name: zjList[zi].user_name,
|
|
pname: zjList[zi].name,
|
|
zjList_box: true
|
|
})
|
|
zi++;
|
|
if (zi == zjList.length) {
|
|
zi = 0;
|
|
}
|
|
},
|
|
//上拉事件
|
|
onReachBottom: function () {
|
|
var that = this;
|
|
that.getMore();
|
|
},
|
|
obm: function () {
|
|
var that = this;
|
|
var timestamp = Date.parse(new Date());
|
|
// console.log(timestamp, that.data.timestamp)
|
|
if (timestamp - that.data.timestamp > 2000) {
|
|
that.setData({
|
|
timestamp: timestamp,
|
|
});
|
|
}
|
|
that.setData({
|
|
loading: true,
|
|
remind: ''
|
|
});
|
|
that.getMore();
|
|
},
|
|
/**
|
|
* Tab的点击切换事件
|
|
*/
|
|
tabItemClick: function (e) {
|
|
//防止点击过快带来的闪屏问题
|
|
var timestamp = Date.parse(new Date());
|
|
timestamp = timestamp / 1000;
|
|
var data = e.currentTarget.dataset;
|
|
var that = this;
|
|
if (cont_time) {
|
|
if (timestamp - cont_time >= 1) {
|
|
that.nextpic(data);
|
|
cont_time = timestamp;
|
|
} else {
|
|
cont_time = timestamp;
|
|
}
|
|
} else {
|
|
that.nextpic(data);
|
|
cont_time = timestamp;
|
|
}
|
|
// that.checkCor();
|
|
},
|
|
nextpic: function (data) {
|
|
this.setData({
|
|
current: data.pos,
|
|
tabid: data.tabid,
|
|
});
|
|
},
|
|
//设置点击tab大于第七个是自动跳到后面
|
|
checkCor: function (e) {
|
|
if (this.data.current > 4) {
|
|
this.setData({
|
|
scrollLeft: 800
|
|
})
|
|
} else {
|
|
this.setData({
|
|
scrollLeft: 0
|
|
})
|
|
}
|
|
},
|
|
/**
|
|
* 内容区域swiper的切换事件
|
|
*/
|
|
contentChange: function (e) {
|
|
var that = this;
|
|
var id = e.detail.current;
|
|
var tabid = that.data.titleList[id].cid;
|
|
this.setData({
|
|
current: id,
|
|
tabid: tabid,
|
|
page: 1,
|
|
})
|
|
// this.checkCor();
|
|
},
|
|
onShow: function () {
|
|
console.log(app)
|
|
var indexchase = app.d.indexchase;
|
|
var that = this;
|
|
if (indexchase) {
|
|
that.onLoad();
|
|
app.d.indexchase = false;
|
|
}
|
|
that.setData({
|
|
region: wx.getStorageSync('region')
|
|
})
|
|
// util.getUesrBgplus(that, app, false)
|
|
},
|
|
onReady: function () {
|
|
this.pop = this.selectComponent("#pop")
|
|
},
|
|
onLoad: function (e) {
|
|
var that = this;
|
|
that.loadProductDetail();
|
|
},
|
|
|
|
preventTouchMove: function () {
|
|
|
|
},
|
|
|
|
go: function () {
|
|
this.setData({
|
|
showModal: false
|
|
})
|
|
},
|
|
navigate_sign: function () {
|
|
wx.navigateTo({
|
|
url: '../sign_in/sign_in',
|
|
})
|
|
},
|
|
|
|
login: function () {
|
|
var that = this;
|
|
//取出本地存储用户信息,解决需要每次进入小程序弹框获取用户信息
|
|
var userInfo = wx.getStorageSync('userInfo');
|
|
wx.login({
|
|
success: res => {
|
|
app.globalData.code = res.code
|
|
wx.setStorageSync('code', res.code)
|
|
},
|
|
fail: function () {
|
|
wx.showToast({
|
|
title: '系统提示:网络错误!',
|
|
icon: 'warn',
|
|
duration: 1500,
|
|
})
|
|
}
|
|
})
|
|
|
|
wx.getSetting({
|
|
success: (res) => {
|
|
// 没有授权需要弹框
|
|
if (!res.authSetting['scope.userInfo']) {
|
|
that.setData({
|
|
isLogin: true
|
|
});
|
|
} else {
|
|
//判断用户已经授权。不需要弹框
|
|
if (app.globalData.isLogin) {
|
|
that.setData({
|
|
isLogin: false
|
|
})
|
|
app.globalData.isLogin = true;
|
|
wx.setStorageSync('isLogin', true);
|
|
} else {
|
|
that.setData({
|
|
isLogin: true
|
|
});
|
|
}
|
|
}
|
|
},
|
|
fail: function () {
|
|
wx.showToast({
|
|
title: '系统提示:网络错误!',
|
|
icon: 'warn',
|
|
duration: 1500,
|
|
})
|
|
}
|
|
})
|
|
},
|
|
getOP: function (res) {
|
|
//提交用户信息 获取用户id
|
|
let that = this
|
|
let userInfo = res;
|
|
var user = app.globalData.userInfo;
|
|
app.globalData.userInfo['avatarUrl'] = userInfo.avatarUrl; // 头像
|
|
app.globalData.userInfo['nickName'] = userInfo.nickName; // 昵称
|
|
app.globalData.userInfo['gender'] = userInfo.gender; // 性别
|
|
wx.setStorageSync('userInfo', app.globalData.userInfo);
|
|
//写入缓存
|
|
var nickName = userInfo.nickName;
|
|
var avatarUrl = userInfo.avatarUrl;
|
|
var gender = userInfo.gender; //性别 0:未知、1:男、2:女
|
|
WXAPI.userMaterial({
|
|
openid: app.globalData.userInfo.openid,
|
|
nickName: nickName,
|
|
avatarUrl: avatarUrl,
|
|
gender: gender
|
|
}).then(res => {
|
|
if (res.code == 200) {
|
|
wx.showToast({
|
|
title: '授权成功!',
|
|
success: 2000
|
|
});
|
|
that.onLoad();
|
|
}
|
|
});
|
|
},
|
|
getqx(event) {
|
|
let name = event.currentTarget.dataset.name
|
|
|
|
if (name === "会员中心" || name === "钱包") {
|
|
var isLogin = wx.getStorageSync('isLogin')
|
|
if (!isLogin) {
|
|
wx.navigateTo({
|
|
url: '/pages/login/login'
|
|
})
|
|
}
|
|
}
|
|
wx.navigateTo({
|
|
url: event.currentTarget.dataset.path,
|
|
})
|
|
},
|
|
/**
|
|
* 点击按钮,滚动条回到页面顶部
|
|
*/
|
|
backTop:function(){
|
|
this.setData({
|
|
scrollTop:0
|
|
})
|
|
},
|
|
onShareAppMessage: function (res) {
|
|
// var that = this;
|
|
// var id = that.data.productId;
|
|
// var type1 = that.data.type1;
|
|
// var uname = app.globalData.userInfo.nickName ? app.globalData.userInfo.nickName : '';
|
|
// var title = uname + '邀请你来' + that.data.mch_name;
|
|
// var user_id = app.globalData.userInfo.user_id;
|
|
// if (res.from === 'button') {
|
|
// // 来自页面内转发按钮
|
|
// }
|
|
// return {
|
|
// title: title,
|
|
// imageUrl: that.data.logo,
|
|
// path: 'pages/index/index?userid=' + user_id,
|
|
// success: function (_res) {
|
|
// console.log('转发成功');
|
|
// },
|
|
// fail: function (_res) {
|
|
// console.log('转发失败')
|
|
// }
|
|
// }
|
|
},
|
|
|
|
});
|