min-project/component/headerNav/headerNav.js
2024-12-12 18:53:37 +08:00

347 lines
11 KiB
JavaScript

// template/headerNav/index.js
import util from '../../utils/util'
var app = getApp()
Component({
/**
* 组件的属性列表
*/
properties: {
showTab: String, // 需要显示的 列表按钮 offerlist summarylist mjlist
notShowIndex: Boolean,
isIndexPage: Boolean,
haveSearch: Boolean,
customBack: Boolean,
needback: Boolean,
bgcolor: String,
searchBtnShow: {
value: false,
type: Boolean
},
isToggleState: {
value: false,
type: Boolean
},
isUser: Boolean,
isUserAlone: Boolean,
islogin: {
type: Boolean,
value: true
},
user: {
type: Object,
value: {},
observer: function (newVal, oldVal) {
let {
initializeLoginState,
iswechatmessagepush,
popup,
offerkaipingadvertisement,
offerkaipingadvertisementState,
} = app.globalData
if (!offerkaipingadvertisementState) {
let data = offerkaipingadvertisement || {}
if (Date.now() < data.deadline * 1000) {
this.setData({
openDate: data
})
}
}
// if (newVal && newVal['uid'] > 0 && iswechatmessagepush == null) {
// if (app.globalData.iswechatmessagepushState >= 0) return
// app.globalData.iswechatmessagepushState = 0
// app.judgeWechatAlertsPop().then(res => {
// let iswechatmessagepush = res.iswechatmessagepush
// let capsulePopState = false
// if (iswechatmessagepush == 0 && app.globalData['ishongkonganswer'] === 0) {
// capsulePopState = true
// } else {
// capsulePopState = false
// }
// this.setData({
// capsulePopState
// })
// })
// }
if (iswechatmessagepush == 0 && app.globalData['ishongkonganswer'] === 0) {
this.setData({
capsulePopState: true
})
}
}
},
isNewVersions: { // 是否是新版本
type: Boolean,
value: false
},
isNeedSign: { // 是否需要签到按钮
type: Boolean,
value: false
},
isMessageCenter: { // 是否显示未读信息
type: Boolean,
value: false
},
isNeedUnread: { // 是否需要未读数
type: Boolean,
value: false,
},
newHeadOperate: Boolean, // 是否是 新的 二级页面 顶部 返回按钮
iswhitecolor: { // 是否显示白色图标
type: Boolean,
value: false,
},
},
/**
* 组件的初始数据
*/
data: {
// templateHeader:'templateHeadersss',
screen_data: {
windowHeight: 0,
totalTopHeight: 0,
statusBarHeight: 0,
titleBarHeight: 0,
},
// isIndexPage:false,
isTabPage: false,
capsulePopState: false, // 微信提醒弹窗状态
// reminderState: false, // 不再提醒的状态
socketTimer: null, // 轮询的定时器
messageCount: 0, // 未读消息
showTabObj: {
offerlist: "Offer列表",
summarylist: "总结列表",
mjlist: "面经列表",
votelist: "投票列表",
questionslist: "问答列表",
treelist: "笔记列表",
},
rentPopState: false,
openDate: {}, // 开屏广告数据
},
pageLifetimes: {
show: function () { // 页面被展示
setTimeout(() => {
this.pollPopup()
}, 200)
if (this.data.isMessageCenter || this.data.isNeedUnread) this.sendSocketMessageCount()
},
hide: function () { // 页面被隐藏
clearTimeout(this.data.socketTimer)
}
},
lifetimes: {
attached: function () {
this.attachedIn()
},
detached: function () {
// 在组件实例被从页面节点树移除时执行
clearTimeout(this.data.socketTimer)
},
},
attached: function () {
this.attachedIn()
},
/**
* 组件的方法列表
*/
methods: {
pollPopup() {
const popup = app.globalData.popup
if (popup === undefined) setTimeout(() => this.pollPopup(), 1000);
else {
if (popup != null && !popup['state']) {
app.globalData.popup['state'] = 1
this.setData({
rentPopState: true,
popup,
})
this.socketPopupOver()
}
}
},
// 只是弹窗后发生 回执
socketPopupOver() {
if (app.globalData.socketTask && app.globalData.socketTask.readyState == 1) {
app.globalData.socketTask.send({
data: JSON.stringify({
type: 'popupOver'
})
})
} else setTimeout(() => this.socketPopupOver(), 1000);
},
// 发送 获取问答信息
sendSocketMessageCount() {
// 主动发一个 获取 未读消息
if (app.globalData.socketTask && app.globalData.socketTask.readyState == 1) {
app.globalData.socketTask.send({
data: JSON.stringify({
type: 'messageCount'
})
})
// 发送后等 0.5s 后再去全局拿数据
setTimeout(() => this.getSocketMessageCount(), 500)
} else setTimeout(() => this.sendSocketMessageCount(), 1000);
},
// 轮询获取未读信息
getSocketMessageCount() {
// 先在全局获取数据
if (app.globalData.unreadMessageCount != 0) {
this.setData({
messageCount: app.globalData.unreadMessageCount
})
app.globalData.unreadMessageCount = 0
}
// 再开启一个定时器
this.data.socketTimer = setTimeout(() => this.getSocketMessageCount(), 3000);
},
attachedIn() {
this.getwindowHeight();
},
getwindowHeight() {
if (app.globalData.screen_data.totalTopHeight > 30) {
this.setData({
screen_data: app.globalData.screen_data
})
} else {
try {
app.computeNavigateBarHeight();
} catch (e) {
// 获取系统信息失败
} finally {
if (app.globalData.screen_data.totalTopHeight > 30) {
this.setData({
screen_data: app.globalData.screen_data
})
}
this.setData({
screen_data: {
bottomLift: 34,
statusBarHeight: 48,
titleBarHeight: 32,
totalTopHeight: 86,
windowHeight: 812,
windowWidth: 375,
}
})
}
}
},
backHome() {
wx.reLaunch({
url: `/pages/projectLibrary/projectLibrary`,
})
},
navigategoBack() {
if (this.properties.needback) {
this.triggerEvent('back', {}, {})
return false
}
var pages = getCurrentPages();
if (this.properties.customBack) {
this.triggerEvent('back', {}, {})
return false;
}
if (pages.length == 1) {
wx.reLaunch({
url: `/pages/projectLibrary/projectLibrary`
});
} else {
wx.navigateBack({
changed: true
}); //返回上一页
}
},
// 点击跳转搜索页面
toSearchBtn() {
wx.navigateTo({
url: '/pagesSquare/pages/searchall/searchall',
})
},
// 点击切换账号方法过几天就能删除了
toggle() {
this.triggerEvent("toggle")
},
// 跳转我的页面
toUserPage() {
wx.nextTick(() => {
this.setData({
messageCount: 0
})
})
if (!this.data.islogin) {
this.triggerEvent("openLogin")
this.triggerEvent("handleUnlockState") // offer详细和总结详细的
return
}
wx.navigateTo({
url: "/pages/user/user",
})
},
// 用户点击登录
userClickLogin(e) {
this.setData({
islogin: true,
})
},
// 跳转消息中心
goMessage() {
if (!this.data.islogin) {
this.triggerEvent("openLogin")
return
}
clearTimeout(this.data.socketTimer)
wx.navigateTo({
url: '/pagesLoginRequired/pages/messageCenter/messageCenter',
})
wx.nextTick(() => {
this.setData({
messageCount: 0
})
})
},
// 头部 跳转 中转
skipTransfer(e) {
const key = e.currentTarget.dataset.key
const pages = getCurrentPages();
if (key == 'back' && pages.length > 1) {
wx.navigateBack()
return
}
let url = ''
if (key == 'index' || key == 'back') url = '/pages/projectLibrary/projectLibrary'
if (key == 'offerlist') url = '/pages/victoryList/victoryList'
if (key == 'summarylist') url = '/pages/summaryList/summaryList'
if (key == 'mjlist') url = '/pages/mjList/mjList'
if (key == 'votelist') url = '/pages/voteList/voteList'
if (key == 'questionslist') url = '/pages/questionsList/questionsList'
if (key == 'treelist') url = '/pages/treeList/treeList'
wx.redirectTo({
url,
})
},
}
})