Files
sphc/pages/index/index.js

680 lines
16 KiB
JavaScript
Raw Permalink Normal View History

2024-05-06 15:34:15 +08:00
// pages/index/index.js
const qqMap = require('../../utils/qqmap-wx-jssdk.js')
const qqmapsdk = new qqMap({
key: 'UTFBZ-PQZLW-AX5R6-OWOG6-SRRBH-7FFEL'
});
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
bgImg: app.globalData.bgImg,
statusBarHeight: app.globalData.statusBarHeight,
bnrUrl: [],
jg: [],
type: [],
goods: [],
goodsId: '',
region: [], //头部地址
addressName: '附近自提点',
page: 1,
imgList: [],
cxGoodsList: [],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//上级id
if (options.pId) {
wx.setStorage({
data: options.pId,
key: 'pId',
})
}
wx.getStorage({
key: 'user',
success: user => {
app.globalData.userId = user.data.id;
console.log(app.globalData.userId)
//获取第二级分类
this.goodsTypList()
this.goodsTypList2()
//促销商品
this.cuxiaoLsit()
},
fail: f => {
//获取第二级分类
this.goodsTypList()
this.goodsTypList2()
//促销商品
this.cuxiaoLsit()
}
})
//获取轮播图
this.banner()
//获取地址
this.map({
jump: true
})
//获取模块图片
this.getBannerImg()
},
//促销商品
cuxiaoLsit() {
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/getHomeGoods',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {
userId: app.globalData.userId
},
success: res => {
wx.hideLoading()
console.log(res)
if (res.data.code == 1) {
var cxGoodsList = res.data.value;
this.setData({
cxGoodsList: cxGoodsList
})
}else {
wx.showModal({
title: '提示!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
//获取模块图片
getBannerImg() {
wx.request({
url: app.globalData.ip + '/wisdommining/api/banner/getBannerImg',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {},
success: res => {
wx.hideLoading()
console.log(res)
if (res.data.code == 1) {
this.setData({
imgList: res.data.value
})
} else {
wx.showModal({
title: '提示!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
wx.getStorage({
key: 'user',
success: user => {
this.setData({
login: true
})
app.globalData.userId = user.data.id;
console.log(app.globalData.userId)
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/getCart',
method: 'POST',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {
userId: app.globalData.userId
},
success: res => {
wx.hideLoading()
console.log(res)
if (res.data.code == 1) {
if (res.data.value.length == 0) {
app.globalData.cartNum = '';
wx.hideTabBarRedDot({
index: 3
})
} else {
app.globalData.cartNum = res.data.value.length.toString()
//购物车数量显示
wx.setTabBarBadge({
index: 3,
text: app.globalData.cartNum
})
}
} else {
wx.showModal({
title: '提示!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
fail: f => {
this.setData({
login: false
})
app.globalData.cartNum = '';
wx.hideTabBarRedDot({
index: 3
})
}
})
},
//分销
fx() {
wx.navigateTo({
url: '/pages/index/welfareImg/welfareImg',
})
},
//套餐领取
fl1() {
// console.log(app.globalData.login)
if(!this.data.login){
app.login_no()
return
}
wx.navigateTo({
url: '/pages/my/coupon/coupon',
})
},
//福利定制
fl2() {
wx.switchTab({
url: '/pages/welfareImg/welfareImg',
})
},
//促销专区
cxDetails(e) {
let id = e.currentTarget.dataset.id;
wx.navigateTo({
url: '/pages/chuxiao/cx_details/goodsDetails?goodsId=' + id,
})
},
//促销专区更多
cx_more(e) {
wx.showLoading({
title: '加载中',
})
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/gethotGoods',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {
type: 3,
userId: app.globalData.userId,
page: 1,
num: 1,
},
success: res => {
console.log(res)
wx.hideLoading()
if (res.data.code == 1) {
if (res.data.value.records.length > 0) {
//促销列表
wx.navigateTo({
url: '/pages/chuxiao/integral',
})
} else {
//促销富文本
wx.navigateTo({
url: '/pages/chuxiao/cx_wxParse/cx_wxParse',
})
}
} else {
wx.showModal({
title: '发生异常请稍后再试!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
//金刚区跳转
jgType(e) {
let id = e.currentTarget.dataset.id;
let pid = e.currentTarget.dataset.pid;
console.log(id)
console.log(pid)
wx.reLaunch({
url: '/pages/classify/classify?pid=' + pid + '&id=' + id,
})
},
//金刚区更多跳转
jgMore(e) {
wx.switchTab({
url: '/pages/classify/classify'
})
},
//获取轮播图 1
banner: function (e) {
wx.showLoading({
title: '加载中',
})
wx.request({
url: app.globalData.ip + '/wisdommining/api/banner/getBanner',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {
type: 1
},
success: res => {
console.log(res)
wx.hideLoading()
if (res.data.code == 1) {
this.setData({
bnrUrl: res.data.value
})
} else {
wx.showModal({
title: '获取数据失败!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
//获取第二级分类
goodsTypList: function (e) {
wx.showLoading({
title: '加载中',
})
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/getTwoGoodsType',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {},
success: res => {
console.log(res)
wx.hideLoading()
if (res.data.code == 1) {
let type = res.data.value.wisdGoodsTypeList;
for (let i = 0; i < type.length; i++) {
type[i].active = false;
}
type[0].active = true;
this.setData({
// type: type,
jg: type,
// goodsId: type[0].id,
})
//客服电话
app.globalData.customerPhone = res.data.value.customerPhone;
// //根据二级分类id获取商品
// this.setData({
// page: 1,
// goods: [],
// })
// this.goodsList()
} else {
wx.showModal({
title: '获取数据失败!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
//获取首页tab第二级分类
goodsTypList2: function (e) {
wx.showLoading({
title: '加载中',
})
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/getHomeTwoGoodsType',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {},
success: res => {
console.log(res)
wx.hideLoading()
if (res.data.code == 1) {
let type = res.data.value.wisdGoodsTypeList;
if (type.length > 0) {
for (let i = 0; i < type.length; i++) {
type[i].active = false;
type[0].active = true;
}
this.setData({
goodsId: type[0].id,
type: type,
})
//根据二级分类id获取商品
this.setData({
page: 1,
goods: [],
})
this.goodsList()
}
} else {
wx.showModal({
title: '获取数据失败!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
//商品类型选择
goodsType(e) {
let index = e.currentTarget.dataset.index;
let id = e.currentTarget.dataset.id;
let type = this.data.type;
for (let i = 0; i < type.length; i++) {
type[i].active = false;
}
type[index].active = true;
this.setData({
type: type,
goodsId: id,
page: 1,
goods: [],
})
//根据二级分类id获取商品
this.goodsList()
},
//根据二级分类id获取商品
goodsList(e) {
wx.showLoading({
title: '加载中',
})
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/getGoodsByTwoType',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {
twoTypeId: this.data.goodsId,
userId: app.globalData.userId,
page: this.data.page,
num: 10,
},
success: res => {
console.log(res)
wx.hideLoading()
if (res.data.code == 1) {
let data = res.data.value.records;
if (data != null && data.length > 0) {
let goods = this.data.goods;
console.log(goods)
for (let i = 0; i < data.length; i++) {
if (data[i].wisdGoodsSpecList[0].specBulyPrice == null) {
data[i].price = data[i].wisdGoodsSpecList[0].specPrice
} else {
data[i].price = data[i].wisdGoodsSpecList[0].specBulyPrice
}
goods.push(data[i]);
}
this.setData({
goods: goods,
page: this.data.page + 1,
})
} else {
if (this.data.page > 1) {
wx.showToast({
title: '暂无更多~',
icon: 'none',
duration: 1000,
})
}
}
} else {
wx.showModal({
title: '获取数据失败!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
//搜索
search() {
wx.navigateTo({
url: '/pages/search_details/search_details?search=true',
})
},
//商品详情
goodsDetails(e) {
let id = e.currentTarget.dataset.id;
let wid = e.currentTarget.dataset.wid;
let type = e.currentTarget.dataset.type; //1商品 2富文本
if (type == 0) {
} else if (type == 1) {
wx.navigateTo({
url: '/pages/goodsDetails/goodsDetails?goodsId=' + id,
})
} else {
wx.navigateTo({
url: '/pages/index/banner_details/welfareImg?wid=' + wid,
})
}
},
//积分商城
integral(e) {
let title = e.currentTarget.dataset.title;
wx.navigateTo({
url: '/pages/integral/integral?title=' + title,
})
},
map(options) {
var that = this;
wx.getSetting({
success(res) {
console.log('get-setting', res.authSetting);
// 只返回用户请求过的授权
let auth = res.authSetting;
if (auth['scope.userLocation']) {
// 已授权,申请定位地址
that.toAddress({
jump: options.jump
});
} else if (auth['scope.userLocation'] === undefined) {
// 用户没有请求过的授权,不需要我们主动弹窗,微信会提供弹窗
that.toAddress({
jump: options.jump
});
} else if (!auth['scope.userLocation']) {
// 没有授权过,需要用户重新授权
// 这个弹窗是为了实现点击不然openSetting会失败
wx.showModal({
title: '是否授权位置信息?',
content: '需要获取您的位置信息,请确认授权',
success: res => {
if (res.confirm) {
wx.openSetting({
success(res) {
console.log('位置信息', res.authSetting);
console.log('open-setting-suc', res.authSetting);
let setting = res.authSetting;
if (!setting['scope.userLocation']) {
wx.showToast({
title: '位置信息授权失败',
icon: 'none',
});
} else {
// 地址授权成功,申请定位地址
that.toAddress({
jump: options.jump
});
}
},
fail(err) {
// 需要点击有时候没有点击是无法触发openSetting
console.log('open-setting-fail', err);
}
});
}
}
});
}
}
});
},
// 调用地址
toAddress: function (options) {
var that = this
wx.showLoading({
title: '加载中..',
mask: true
})
wx.getLocation({
//定位类型 wgs84, gcj02
// wgs84 返回 gps 坐标gcj02 返回可用于wx.openLocation的坐标
type: 'gcj02',
//获取位置成功
success: function (ress) {
console.log(ress) //获取的的当前位置的详细信息
that.setData({
latitude: ress.latitude,
longitude: ress.longitude
})
//经纬度转化为详细位置
qqmapsdk.reverseGeocoder({
location: {
latitude: ress.latitude,
longitude: ress.longitude
},
success: function (resss) {
wx.hideLoading()
console.log(resss)
var region = [];
that.setData({
addressNo: 1, //地址授权
region: region,
addressName: resss.result.address,
})
if (options.jump) {
} else {
wx.navigateTo({
url: '/pages/index/index_address/index_address',
})
}
},
fail: function (err) {
wx.hideLoading()
console.log(err)
}
})
},
//获取位置失败
fail: function (err) {
console.log("获取位置信息失败,请返回重试")
wx.showToast({
title: '获取位置信息失败,请返回重试',
icon: 'none'
})
wx.hideLoading()
},
//接口调用结束的回调函数(调用成功、失败都会执行)
complete: function (info) {
console.log("完成")
},
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
if (this.data.goodsId != '') {
this.goodsList()
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function (e) {
console.log(e)
app.share();
return {
path: '/pages/index/index?pId=' + app.globalData.userId,
title: '三品慧采小程序等你来~',
}
},
})