Files
cardtest/pages/index/index.js
zhangmanman 540d76bdb8 [更新]
2021-08-09 15:57:37 +08:00

391 lines
12 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 本时生活
*/
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
swiperCurrent :0,
current :0, //轮播图当前的下标
dots :true,
cityAll : '',
statusBarHeight : getApp().globalData.systInfo.statusBarHeight,
longitude : 0, //经度
latitude : 0, //纬度
adverts : [], //轮播图
stateType : "silver",
cardArr : [], //权益数组
activities : [], //周五福利
isUser : false, //用户登录状态
noticeData : '', //首页公告
loading : true, //骨架屏加载
subState : '', //订阅状态
address : {
city: "", //市
area: "", //区
city_code: "", //市编号
area_code: "" //区编号
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
// 判断是否由分享进入
if(options.parent_id !== ""){
// 写入缓存
wx.setStorage({
key : 'parentId',
data : options.parent_id
})
}
},
/**
* 生命周期函数--监听页面显示
*/
onShow () {
this.locaTion()
// 获取用户登录状态
this.setData({
isUser : getApp().globalData.isUser
})
},
/**
* 获取位置
*/
locaTion(){
wx.getLocation({
success: res => {
this.setData({
latitude    : res.latitude,
                    longitude   : res.longitude
                })
},
complete: () => {
// 获取卡权益
this.indexInfo();
}
})
},
/**
* 卡权益
*/
indexInfo() {
if(this.data.stateType == 'shaky') {
wx.$api.index.index(this.data.city, this.data.longitude, this.data.latitude).then(res=>{
this.setData({
adverts : res.data.adverts,
cardArr : res.data.rights,
activities : res.data.activities,
noticeData : res.data.notice,
loading : false,
subState :res.data.welfare_subscribe,
address : {
city: res.data.location.city_name,
area: res.data.location.district_name || "",
city_code: res.data.location.city_id,
area_code: res.data.location.district_id || ""
}
})
wx.stopPullDownRefresh()
}).catch(err=>{
if(!err.login){
// 写入缓存
wx.setStorage({
key : 'token',
data : ''
})
}
})
} else {
wx.$api.index.choice(this.data.stateType,this.data.city, this.data.longitude, this.data.latitude).then(res=>{
this.setData({
adverts : res.data.adverts,
cardArr : res.data.categorys,
noticeData : res.data.notice,
cityName : res.data.city_name,
loading : false,
address : {
city: res.data.location.city_name,
area: res.data.location.district_name || "",
city_code: res.data.location.city_id,
area_code: res.data.location.district_id || ""
}
})
wx.stopPullDownRefresh()
}).catch(err=>{
if(!err.login){
// 写入缓存
wx.setStorage({
key : 'token',
data : ''
})
}
})
}
},
/**
* tabs
*/
orderTab(e){
if(this.data.stateType != e.currentTarget.dataset.state){
this.setData({
stateType: e.currentTarget.dataset.state
})
// 获取卡权益
this.indexInfo()
// 重置轮播图
this.setData({
current : 0,
swiperCurrent: 0
});
}
},
/**
* 处理权益未登录时的转跳
*/
userNav(e){
let user_lng = this.data.longitude, //经度
user_lat = this.data.latitude, //纬度
area_name = this.data.address.area//城市名称
let id = e.currentTarget.dataset.id
wx.getStorage({
key : 'token',
success:res=>{
wx.navigateTo({
url: '/pages/classify/classify?id=' + id + '&userlng=' + user_lng + '&latitude=' + user_lat + '&areaname=' + area_name
})
},
fail: (err) => {
wx.navigateTo({
url: "/pages/login/login"
})
}
})
},
/**
* 处理未登录时的转跳
*/
userUrl(e){
let pageUrl = e.currentTarget.dataset.url
if(this.data.isUser){
wx.navigateTo({
url: pageUrl
})
}else{
// 去登录
wx.navigateTo({
url: "/pages/login/login"
})
}
},
/**
* 点击轮播图片
*/
clickImg(e) {
let newChannel = e.currentTarget.dataset.channel,
newImg = e.currentTarget.dataset.img,
newUrl = e.currentTarget.dataset.url
// 点击图片放大
if (newChannel == 'image') {
if(newImg == "") return
let imgUrl = [newImg]
wx.previewImage({
urls : imgUrl, //需要预览的图片http链接列表注意是数组
current : imgUrl[0] // 当前显示图片的http链接默认是第一个
})
}
// 跳转内网
if (newChannel == 'pages') {
wx.navigateTo({
url: newUrl
})
}
// 跳转外网
if (newChannel == 'url') {
let url= encodeURIComponent(newUrl)
wx.redirectTo({
// 跳转到webview页面
url: `/pages/webView/webView?url=${url}`
});
}
},
/**
* 周五福利抢购
*/
Snapup(e) {
let canBuy = e.currentTarget.dataset.can,
canText = e.currentTarget.dataset.text,
canId = e.currentTarget.dataset.id,
canFrom = e.currentTarget.dataset.from,
canRightid = e.currentTarget.dataset.rightid
if(!canBuy) {
wx.showToast({
title : canText,
icon : 'none',
duration: 2000
})
return
}
wx.getStorage({
key : 'token',
success:res=>{
if(canFrom == 'washcar') {
wx.$api.index.washcarUrl(canId, canRightid).then(res=>{
const newUrl = res.data
let url= encodeURIComponent(newUrl)
wx.redirectTo({
// 跳转到webview页面
url: `/pages/washcar/washcar?url=${url}`
});
}).catch(err=>{
if(!err.login){
// 写入缓存
wx.setStorage({
key : 'token',
data : ''
})
}
})
} else {
// 跳转分类页
wx.navigateTo({
url: '/pages/welfare/welfare?id=' + canId
})
}
},
fail: (err) => {
wx.navigateTo({
url: "/pages/login/login"
})
}
})
},
/**
* 活动中心权益跳转详情
*/
rightNav(e) {
let id = e.currentTarget.dataset.id,
from = e.currentTarget.dataset.from,
rightid = e.currentTarget.dataset.rightid
wx.getStorage({
key : 'token',
success:res=>{
if(from == 'washcar') {
wx.$api.index.washcarUrl(id, rightid).then(res=>{
const newUrl = res.data
let url= encodeURIComponent(newUrl)
wx.redirectTo({
// 跳转到webview页面
url: `/pages/washcar/washcar?url=${url}`
});
}).catch(err=>{
if(!err.login){
// 写入缓存
wx.setStorage({
key : 'token',
data : ''
})
}
})
} else {
// 跳转分类页
wx.navigateTo({
url: '/pages/rights/rights?id=' + id
})
}
},
fail: (err) => {
wx.navigateTo({
url: "/pages/login/login"
})
}
})
},
/**
* 下拉刷新
*/
onPullDownRefresh(){
// 获取卡权益
this.indexInfo();
this.locaTion();
},
/**
* 防止swiper控件卡死
*/
swiperChange(e){
if (this.data.current == 0 && this.data.swiperCurrent>1 ) {//卡死时重置current为正确索引
this.setData({ current: this.data.swiperCurrent });
}
else {//正常轮转时,记录正确页码索引
this.setData({ swiperCurrent: e.detail.current });
}
},
/**
* 订阅
*/
subscribeTap() {
if(!this.data.isUser) {
// 去登录
wx.navigateTo({
url: "/pages/login/login"
})
return
}
wx.requestSubscribeMessage({
// 6SJqWbasj42O_d7yET1s5G1eg62Z4SS3pByBeuXekSo 正式
// g8cEL-Tf2_Pdh8VyBdsYbhv09VVm8om-ZblhXIaVDiY 测试
tmplIds: ['g8cEL-Tf2_Pdh8VyBdsYbhv09VVm8om-ZblhXIaVDiY'],
success: res=> {
if(res["g8cEL-Tf2_Pdh8VyBdsYbhv09VVm8om-ZblhXIaVDiY"] == "accept") {
wx.$api.index.subscribe(1, 'mini').then(res=>{
wx.showLoading({
title: res.data,
icon : 'none',
})
// 获取卡权益
this.indexInfo();
setTimeout(function () {
wx.hideLoading();
}, 1000)
}).catch(err => {});
}
},
fail: err => {
}
})
}
})