min-project/pages/projectLibrary/projectLibrary.js

844 lines
24 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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.

// pages/projectLibrary/projectLibrary.js
var miucms = require('../../utils/miucms.js');
let app = getApp()
const util = require('../../utils/util')
const common = require('../../utils/commonMethod')
Page({
/**
* 页面的初始数据
*/
data: {
totalTopHeight: null,
isFirstPattern: true, // 是否是首屏模式
classify: "school", // school subject
current: 0,
discipline: [], // 学科
university: [], // 学校
bezier: {},
page: 1,
list: [],
listLeft: [],
listRight: [],
fateProject: [], // 缘分项目
admissionList: [], // 招生官项目
contrastcount: 0, // 项目对比 数量
informationState: false, // 授权后可能需要弹出完成信息框 个人背景那些
islogin: false,
isloginBtnState: false,
isInitFinish: false,
user: {},
isShow: false,
banner: [],
swiperHeightList: {}, // 轮播图高度
swiperCurrent: 0, // 轮播图下标
rankingList: [],
rankingKey: common.rankingKey,
},
/**
* 生命周期函数--监听页面加载
*/
headHeight: 0, // 头部高度
rpx15: 15,
searchBoxTop: 0,
rankingsObj: {},
options: {},
onLoad(options) {
this.options = options
miucms.pageStart(app).then(() => {
const screen_data = app.globalData.screen_data
const totalTopHeight = screen_data.totalTopHeight || 0
this.searchBoxTop = util.rpxTopx(313.5) + totalTopHeight
this.headHeight = util.rpxTopx(313.5)
this.rpx15 = util.rpxTopx(15)
this.setData({
islogin: app.globalData.user.uid > 0 ? true : false,
user: app.globalData.user,
totalTopHeight,
isInitFinish: true,
bottomLift: screen_data.bottomLift,
})
this.windowHeight = screen_data.windowHeight || 812
common.xgBasicData(this, app, true).then(data => {
this.rankingsObj = data.rankings || {}
this.setData({
contrastcount: data.contrastcount || 0,
university: data.university,
discipline: data.discipline,
})
})
this.getFate()
this.getAdmission()
this.addRandom()
this.getProjectData()
this.bannerData()
this.getRankingData()
const fateProject = (this.data.fateProject || []).map(element => ({
...element,
random: app.randomString(6)
}));
this.setData({
fateProject,
})
})
},
bannerData() {
util.wxget("https://api.gter.net/v1/program/getBanner").then(res => {
if (res.code != 200) return
const data = res.data || []
this.setData({
banner: data
})
})
},
// 新 轮播图 点击事件
handleSwiper(e) {
const banner = this.data.banner
const index = e.currentTarget.dataset.index
const target = banner[index] || {}
// 跳转视频号
if ((target.type == 5) || (target.type == 'adv' && target.advtype == "videofeed_5")) {
wx.openChannelsActivity({
finderUserName: target.finderusername,
feedId: target.feedid
})
return false
}
// 跳转直播
if ((target.type == 4) || (target.type == 'adv' && target.advtype == "videofeed_4")) {
wx.openChannelsLive({
finderUserName: target.finderusername
})
return false
}
// 判断跳转小程序
if (target.appid) {
if (target.url.charAt(0) !== '/') target.url = "/" + target.url
wx.navigateToMiniProgram({
appId: target.appid,
path: target.url
})
return false;
}
// 判断是否是h5页面的
if (target.url && target.url.indexOf('http') != -1) {
wx.navigateTo({
url: `/pages/webview/webview?url=${ target.url }`,
})
return false;
}
if (target.url) {
if (target.url.charAt(0) !== '/') target.url = "/" + target.url
wx.navigateTo({
url: target.url
})
return false;
}
},
// 新 轮播图 自动滑动事件,修改下标
bindchangeSwiper(e) {
let current = e.detail.current
// this.startCountdown(this.data.urls[current]['timestamp']) // 6天 06:28:54
this.setData({
swiperCurrent: current
})
},
swiperHighest: 0,
// 轮播图的加载事件
loadHeadSwiper(e) {
const index = e.target.dataset.index
let ratio = app.globalData.screen_data['windowWidth'] / 750
let originalWidth = e.detail.width;
let originalHeight = e.detail.height;
let height = originalHeight / originalWidth * 690 * ratio
let swiperHeightList = this.data.swiperHeightList
swiperHeightList[index] = height
if (height > this.swiperHighest) this.swiperHighest = height
this.setData({
swiperHeightList
})
},
addRandom() {
let list = this.data.list || []
list.forEach(element => {
element['random'] = app.randomString(6)
})
this.setData({
list
})
},
leftheight: 0,
rightheight: 0,
// 处理瀑布流
handleLazyLoading(list) {
this.getHeightState = true
const length = list.length
const eighty = Math.floor(length * 0.8)
let left = []
let right = []
// 变量决定第一个元素是放入 left 还是 right
const startWithLeft = this.leftheight <= this.rightheight; // 如果为 true第一个元素放入 left否则放入 right
const pointList = list.slice(0, eighty)
pointList.forEach((element, index) => {
if ((index % 2 === 0 && startWithLeft) || (index % 2 !== 0 && !startWithLeft)) left.push(element);
else right.push(element);
})
this.setData({
listLeft: this.data.listLeft.concat(left),
listRight: this.data.listRight.concat(right),
})
this.getHeight(list, eighty)
},
queryLeft: null,
getHeightState: false, // 瀑布流计算中
// 获取高度
getHeight(list, index) {
if (!list[index]) {
this.getHeightState = false
return
}
this.createSelectorQuery().select(".waterfall .waterfall-left").boundingClientRect(res => {
this.leftheight = res.height
this.createSelectorQuery().select(".waterfall .waterfall-right").boundingClientRect(res => {
this.rightheight = res.height
if (this.leftheight <= this.rightheight) {
this.data.listLeft.push(list[index])
this.setData({
listLeft: this.data.listLeft
})
} else {
this.data.listRight.push(list[index])
this.setData({
listRight: this.data.listRight
})
}
wx.nextTick(() => {
this.getHeight(list, index + 1)
})
}).exec();
}).exec();
},
// 获取今日缘分
getFate() {
wx.showLoading({
title: '加载中...',
})
util.wxget("https://api.gter.net/v1/program/todayFateProject").then(res => {
if (res.code != 200) return
let data = res.data || []
data.forEach(element => {
element['projectid'] = []
element.data.forEach(ele => {
element['projectid'].push(ele.id)
})
})
this.setData({
fateProject: data,
fateProjectShow: [...data].sort(() => 0.5 - Math.random()).slice(0, 2)
})
}).finally(() => {
wx.hideLoading()
})
},
updateFate() {
this.setData({
fateProjectShow: [...this.data.fateProject].sort(() => 0.5 - Math.random()).slice(0, 2)
})
},
goContrast(e) {
const ids = e.currentTarget.dataset.ids
common.goPage(`/pages/projectComparison/projectComparison?ids=${ids}`)
},
// 获取 招生官项目
getAdmission() {
util.wxget("https://api.gter.net/v1/program/getList", {
limit: 20,
page: 1,
admissionsproject: 1,
}).then(res => {
if (res.code != 200) return
const data = res.data
const list = data.data || []
const admissionList = (list || []).map(element => ({
...element,
random: app.randomString(6)
}));
const chunkArray = (array, size) => {
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
};
const groupedAdmissionList = chunkArray(admissionList, 4);
this.setData({
admissionList: groupedAdmissionList,
})
})
},
// 获取项目数据
getProjectData() {
if (this.data.page == 0) return
wx.showLoading({
title: '加载中...',
})
util.wxget("https://api.gter.net/v1/program/getList", {
limit: 20,
page: this.data.page,
}).then(res => {
if (res.code != 200) return
const data = res.data
const date = new Date()
const month = date.getMonth() + 1
const year = date.getFullYear()
let list = data.data || []
list = (list).map(element => ({
...element,
random: app.randomString(6),
semesterState: (year < element.semester.year) || (year === element.semester.year && month < element.semester.month),
}));
this.setData({
list: this.data.list.concat(list),
page: data.count > data.limit * data.page ? this.data.page + 1 : 0,
})
this.handleLazyLoading(list)
if (!this.indexSidebar) this.indexSidebar = this.selectComponent('#index-sidebar')
}).finally(() => {
wx.hideLoading()
})
},
// 物品的点击事件
handleClick(e) {
if (!this.data.islogin) {
this.openLoginBtnState()
return
}
const id = e.currentTarget.dataset.id
const index = e.currentTarget.dataset.index || 0
const i = e.currentTarget.dataset.i || 0
const status = e.currentTarget.dataset.status || 0
const type = e.currentTarget.dataset.type
const random = e.currentTarget.dataset.random
const query = this.createSelectorQuery();
if (status == 1) return
query.select('#add' + random).boundingClientRect();
if (status == 0) {
this.setData({
bezier: {},
})
query.exec((res) => {
const data = res[0]
//定义手指点击位置
this.setData({
bezier: {
x: data.left + data.width / 2 - this.rpx15,
y: data.top + data.height / 2 - this.rpx15,
},
})
});
}
let url = "https://api.gter.net/v1/program/addContrast"
util.wxpost(url, {
projectid: id
}).then(res => {
if (res.code != 200) return
const data = res.data
if (type === 'admission') {
const admissionList = this.data.admissionList
if (admissionList[index][i]['contraststatus'] == null) admissionList[index][i]['contraststatus'] = {}
admissionList[index][i]['contraststatus'] = {
status: 1,
ismanage: 1,
}
this.setData({
admissionList,
})
}
if (type === 'left') {
const listLeft = this.data.listLeft
if (listLeft[index]['contraststatus'] == null) listLeft[index]['contraststatus'] = {}
listLeft[index]['contraststatus'] = {
status: 1,
ismanage: 1,
}
this.setData({
listLeft,
})
}
if (type === 'right') {
const listRight = this.data.listRight
if (listRight[index]['contraststatus'] == null) listRight[index]['contraststatus'] = {}
listRight[index]['contraststatus'] = {
status: 1,
ismanage: 1,
}
this.setData({
listRight,
})
}
if (type == 'fate') {
let fateProject = this.data.fateProject
fateProject[index]['state'] = 1
this.setData({
fateProject,
})
}
this.setData({
contrastcount: data.count,
})
app.globalData.basicData['contrastcount'] = data.count
common.toast(res.message)
})
},
// 切换查看类型
cutClassify(e) {
const classify = e.currentTarget.dataset.type
if (classify == this.data.classify) return
this.setData({
classify
})
},
swiperchange(e) {
this.setData({
current: e.detail.current,
})
this.closeMoreSelectAll('admission')
},
// 点击跳转 招生官频道
goAdmission() {
wx.navigateToMiniProgram({
appId: "wxa9296b07391c2bc7",
path: "/pages/admissionList/admissionList"
})
},
// 点击 查看 学校
goClassifyValue(e) {
const classify = this.data.classify
const value = e.currentTarget.dataset.value
let url = ""
if (classify == 'school') url = `/pages/projectSchoolHomepage/projectSchoolHomepage?id=${value}`
else url = `/pages/projectSubjectList/projectSubjectList?id=${value}`
common.goPage(url)
},
// 点击项目详情
goDetails(e) {
const uniqid = e.currentTarget.dataset.uniqid
common.goPage(`/pages/projectDetails/projectDetails?uniqid=${uniqid}`)
},
// 点击跳转群主
jumpGroup() {
this.indexSidebar.setData({
groupState: true,
})
// util.statistics({
// name: "side-group"
// })
},
indexSidebar: null,
windowHeight: 812,
onPageScroll(e) {
const scrollTop = e.scrollTop
let isFirstPattern = true
if (scrollTop > this.headHeight) isFirstPattern = false
if (this.data.isFirstPattern != isFirstPattern) {
this.setData({
isFirstPattern
})
}
let sidebarState = this.indexSidebar.data.sidebarState
if (scrollTop > this.windowHeight * 3 && sidebarState !== 3) sidebarState = 3
if (scrollTop < this.windowHeight * 3 && sidebarState == 3) sidebarState = 2
// 同一搜集 修改的 sidebarState
if (sidebarState !== this.indexSidebar.data.sidebarState) {
this.indexSidebar.setData({
sidebarState
})
}
this.indexSidebar.openSidebarTwoHide()
},
goPage(e) {
const url = e.currentTarget.dataset.url
common.goPage(url)
},
openMoreSelect(e) {
const index = e.currentTarget.dataset.index || 0
const i = e.currentTarget.dataset.i || 0
const type = e.currentTarget.dataset.type
if (type == 'admission') {
const admissionList = this.data.admissionList
admissionList.forEach(element => {
element.forEach(ele => {
ele['moreState'] = false
})
})
admissionList[index][i]['moreState'] = true
this.setData({
admissionList,
})
}
if (type == 'left') {
let listLeft = this.data.listLeft
listLeft.forEach(element => {
element['moreState'] = false
})
listLeft[index]['moreState'] = true
this.setData({
listLeft,
})
}
if (type == 'right') {
let listRight = this.data.listRight
listRight.forEach(element => {
element['moreState'] = false
})
listRight[index]['moreState'] = true
this.setData({
listRight,
})
}
},
closeMoreSelect(e) {
const type = e.currentTarget.dataset.type
this.closeMoreSelectAll(type)
},
// 关闭所有 状态 选择 弹出框
closeMoreSelectAll(type) {
if (type == 'admission') {
const admissionList = this.data.admissionList
admissionList.forEach(element => {
element.forEach(ele => {
ele['moreState'] = false
})
})
this.setData({
admissionList,
})
}
if (type == 'left') {
let listLeft = this.data.listLeft
listLeft.forEach(element => {
element['moreState'] = false
})
this.setData({
listLeft,
})
}
if (type == 'right') {
let listRight = this.data.listRight
listRight.forEach(element => {
element['moreState'] = false
})
this.setData({
listRight,
})
}
},
addProject(e) {
const index = e.currentTarget.dataset.index
const i = e.currentTarget.dataset.i
const id = e.currentTarget.dataset.id
const type = e.currentTarget.dataset.type
util.wxpost("https://api.gter.net/v1/program/addContrast", {
projectid: id
}).then(res => {
if (res.code != 200) return
const data = res.data
if (type == 'admission') {
let list = this.data.admissionList
if (list[index][i]['contraststatus'] == null) list[index][i]['contraststatus'] = {}
list[index][i]['contraststatus'] = {
status: 1,
ismanage: 1,
}
this.setData({
admissionList: list,
})
}
if (type == 'left') {
let list = this.data.listLeft
if (list[index]['contraststatus'] == null) list[index]['contraststatus'] = {}
list[index]['contraststatus'] = {
status: 1,
ismanage: 1,
}
this.setData({
listLeft: list,
})
}
if (type == 'right') {
let list = this.data.listRight
if (list[index]['contraststatus'] == null) list[index]['contraststatus'] = {}
list[index]['contraststatus'] = {
status: 1,
ismanage: 1,
}
this.setData({
listRight: list,
})
}
this.setData({
contrastcount: data.count,
})
app.globalData.basicData['contrastcount'] = data.count
common.toast(res.message)
})
},
// 打开 授权按钮
openLoginBtnState() {
this.setData({
isloginBtnState: true,
})
},
// 关闭授权登录事件
popClose() {
this.setData({
isloginBtnState: !this.data.isloginBtnState
})
},
userClickLogin(e) {
let data = e.detail.data
this.setData({
islogin: true,
isloginBtnState: false,
informationState: data.regdatastep == 'success' ? false : true,
})
this.onLoad(this.options)
},
// 子组件传值 修改 完善信息组件的状态
revampInformationState() {
this.setData({
informationState: false
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
if (app.globalData.basicDataState) {
this.setData({
contrastcount: app.globalData.basicData['contrastcount'],
})
}
this.setData({
isShow: true,
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
this.setData({
isShow: false,
})
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
wx.stopPullDownRefresh()
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
if (!this.getHeightState) this.getProjectData()
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
return {
title: "聚焦港校项目,助你迈向国际名校之路!",
}
},
onShareTimeline() {
return {
title: "聚焦港校项目,助你迈向国际名校之路!",
}
},
getRankingData() {
util.wxpost("https://api.gter.net/v1/ranking/homeRankingRecommend").then(res => {
if (res.code != 200) {
common.toast(res.message || '')
return
}
const data = res.data || []
data.forEach(element => {
element['mechanism'] = this.rankingsObj[element.mechanism]?.name || element.mechanism
})
let arr = [data.slice(0, 5), data.slice(5, 10), data.slice(10, 15)]
this.setData({
rankingList: arr
})
})
},
bindchangeSwiper(e) {
const current = e.detail.current || 0
this.setData({
swiperCurrent: current,
})
},
cutSwiper(e) {
const type = e.currentTarget.dataset.type
const swiperCurrent = this.data.swiperCurrent
const length = this.data.rankingList.length
let newCurrent = swiperCurrent
if (type == 'right') newCurrent = (swiperCurrent + 1) % length
else if (type == 'left') newCurrent = (swiperCurrent - 1 + length) % length
this.setData({
swiperCurrent: newCurrent,
})
},
goProjectAllList() {
common.goPage(`/pages/projectAllList/projectAllList`)
},
handRanking(e) {
const index = e.currentTarget.dataset.index
const i = e.currentTarget.dataset.i
const list = this.data.rankingList
const target = list[index][i]
if (index < 2) common.goPage(`/pages/projectList/projectList?type=subject&system=${encodeURIComponent(target.mechanism)}&subject=${encodeURIComponent(target.subject)}&year=${target.year}`)
else common.goPage(`/pages/projectList/projectList?type=school&mechanism=${encodeURIComponent(target.mechanism)}&year=${target.year}`)
},
})