min-project/pages/projectAllList/projectAllList.js
DESKTOP-RQ919RC\Pc be2dfd313e 对齐offer
2025-03-04 18:53:58 +08:00

528 lines
15 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.

// pages/projectAllList/projectAllList.js
const miucms = require('../../utils/miucms.js');
import common from '../../utils/commonMethod'
import util from "../../utils/util"
var app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
informationState: false, // 授权后可能需要弹出完成信息框 个人背景那些
islogin: false, //是否登录
isloginBtnState: false, // 登录弹窗的状态
realizeState: false, // 了解弹窗状态
user: {},
classifyType: "school", // school subject
classify: "school", // school subject
comData: [],
com: {},
comOption: {},
comSum: 0,
majData: [],
maj: {},
majOption: {},
majSum: 0,
majObj: {},
list: [{}, {}, {}, ],
rankingObj: {
"QS": "ranking-qs-icon.png",
"USNEWS": "ranking-us-icon.png",
"泰晤士": "ranking-times-icon.png",
"软科": "ranking-soft-icon.png",
"麦考林": "ranking-macleans-icon.png",
},
rankingskeyVlaue: {},
majPitch: {
major: "",
organ: "",
},
screenState: false, // 选择框的状态
organList: [{
name: "QS",
key: "qs",
text: "由英国的Quacquarelli Symonds公司编制侧重于学术声誉、雇主声誉、师生比例等指标"
}, {
name: "泰晤士",
key: "times",
text: "以其全面的数据收集和分析著称,涵盖了教学、研究、国际化等多个方面‌"
}, {
name: "U.S. News",
key: "usnews",
text: "由美国新闻与世界报道出版,注重学术研究影响力、学术声誉等",
}, {
name: "‌软科",
key: "ruanke",
text: "由中国上海交通大学发布,强调科研表现和学术影响力",
}, {
name: "麦考林",
key: "mecoxLane",
text: "以全面和权威著称,主要关注加拿大国内大学的综合表现,并将大学分为医博类、综合类和基础类",
}],
letterList: {}, // 字母列表
screen_data: {},
letterKey: "A",
letterFixed: false,
},
/**
* 生命周期函数--监听页面加载
*/
windowHeight: 0,
onLoad(options) {
miucms.pageStart(app).then(() => {
const screen_data = app.globalData.screen_data
this.setData({
islogin: app.globalData.user.uid > 0 ? true : false,
user: app.globalData.user,
screen_data,
})
this.windowHeight = screen_data.windowHeight
common.xgBasicData(this, app).then(data => {
this.setData({
rankingskeyVlaue: data.rankings || {},
})
this.getRankings()
this.getComprehensiveRanking()
this.getMajorRanking()
})
})
},
// 专业
getMajorRanking() {
util.wxget("/api/ranking/getMajorRanking", {}).then(res => {
if (res.code != 200) return
const data = res.data
let list = data.list || []
list.forEach(element => {
element['systemObj'] = {}
element.lists.forEach(ele => {
if (!Array.isArray(element['systemObj'][ele.system])) element['systemObj'][ele.system] = []
element['systemObj'][ele.system].push(ele)
})
})
list.sort((a, b) => {
if (a.subject < b.subject) {
return -1;
}
if (a.subject > b.subject) {
return 1;
}
return 0;
});
// 为每个对象添加首字母标识
let currentInitial = null;
list.forEach(item => {
const initial = item.subject[0].toUpperCase()
if (initial !== currentInitial) {
item.initial = initial;
currentInitial = initial;
}
})
// 用于记录每个首字母的数量
const initialCount = {};
// 为每个对象添加首字母标识并统计首字母数量
list.forEach(item => {
const initial = item.subject[0].toUpperCase();
if (!initialCount[initial]) {
initialCount[initial] = {
initial: 0,
sum: 0,
}
}
initialCount[initial]['initial'] += 1
initialCount[initial]['sum'] += Object.keys(item.systemObj).length || 0
});
this.setData({
majSum: data.count,
majData: list,
letterList: initialCount,
}, () => {
this.calculateSectionTops()
})
})
},
// 综合
getComprehensiveRanking() {
util.wxget("/api/ranking/getComprehensiveRanking", {}).then(res => {
if (res.code != 200) return
const data = res.data
this.setData({
comSum: data.count,
comData: data.list,
})
})
},
// 获取 配置信息
getRankings() {
wx.showLoading({
title: '加载中...',
})
util.wxget("/api/project.rankings", {}).then(res => {
if (res.code != 200) return
const data = res.data
const comprehensive = data.comprehensive || {}
// const comSum = this.countKeysInNestedObjects(comprehensive)
let com = this.data.com
com['jg'] = Object.keys(comprehensive)[0]
let yearsSet = [...this.collectYears(comprehensive)].sort((a, b) => b - a);
com['year'] = yearsSet[0]
com['token'] = comprehensive[com.jg][com.year]
const discipline = data.discipline || {}
// const majSum = this.countKeysInNestedObjects(discipline)
// const majObj = this.transform(discipline)
let maj = this.data.maj
const [dOrganizationKey, dOrganizationValue] = Object.entries(discipline)[0]
maj['jg'] = dOrganizationKey
maj['major'] = Object.entries(dOrganizationValue)[0][0]
const dYear = [...this.collectYears(discipline)].sort((a, b) => b - a);
maj['year'] = dYear[0]
maj['token'] = discipline[maj.jg][maj.major][maj.year]
this.setData({
comOption: comprehensive,
// comSum,
com,
majOption: discipline,
// majSum,
// majObj,
maj,
isInitFinish: true,
})
}).finally(() => wx.hideLoading())
},
transform(input) {
const result = {};
const subjectMap = new Map();
// 收集学科对应的机构
for (const [agency, subjects] of Object.entries(input)) {
for (const subject of Object.keys(subjects)) {
if (!subjectMap.has(subject)) subjectMap.set(subject, new Set());
subjectMap.get(subject).add(agency);
}
}
// 构建结果
for (const [agency, subjects] of Object.entries(input)) {
for (const [subject, data] of Object.entries(subjects)) {
const agencies = subjectMap.get(subject);
if (!result[subject]) result[subject] = {};
// if (agencies.size > 1) {
// console.log("99", agencies.size);
// }
result[subject][agency] = data
}
}
return result;
},
// 计算对象里对象的数量
countKeysInNestedObjects(obj) {
let count = 0;
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
if (typeof value === 'object' && value !== null) {
count += Object.keys(value).length;
count += this.countKeysInNestedObjects(value);
}
}
}
return count;
},
// 计算出 对象 所有 二级 key 不重复
collectYears(obj, arr = new Set()) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (!isNaN(key) && key.length === 4) arr.add(key);
const value = obj[key];
if (typeof value === 'object' && value !== null) this.collectYears(value, arr);
}
}
return [...arr];
},
openYear(e) {
const major = e.currentTarget.dataset.major
const organ = e.currentTarget.dataset.organ
this.setData({
majPitch: {
major,
organ,
}
})
},
// 选择专业排名
selectMaj(e) {
let major = e.currentTarget.dataset.major
let organ = e.currentTarget.dataset.organ
const year = e.currentTarget.dataset.year
major = encodeURIComponent(major)
organ = encodeURIComponent(organ)
common.goPage(`/pages/projectList/projectList?type=subject&system=${organ}&subject=${major}&year=${year}`)
this.closeYear()
},
// 选择综合排名
selectCom(e) {
let mechanism = e.currentTarget.dataset.mechanism || ''
const year = e.currentTarget.dataset.year
mechanism = encodeURIComponent(mechanism)
common.goPage(`/pages/projectList/projectList?type=school&mechanism=${mechanism}&year=${year}`)
},
closeYear() {
this.setData({
majPitch: {
major: "",
organ: "",
}
})
},
// 切换了解状态
cutRealizeState() {
this.setData({
realizeState: !this.data.realizeState,
})
},
// 切换查看类型
cutClassify(e) {
const classify = e.currentTarget.dataset.type
if (classify == this.data.classify) return
// let discipline = this.data.discipline || {}
// 切换 专业排名时 判断 是否需要 显示选择
// if (classify == 'subject' && discipline.list.length == 0) this.haveDiscipline()
// let comprehensive = this.data.comprehensive || {}
// if (classify == 'school' && comprehensive.list.length == 0) this.haveComprehensive()
this.setData({
classify,
})
},
// 点击
cutScreenState(e) {
const type = e.currentTarget.dataset.type
this.setData({
classifyType: type,
screenState: !this.data.screenState,
})
},
// 点击 筛选的选好了
haveChosen(e) {
const detail = e.detail || {}
const classify = this.data.classifyType
this.setData({
[classify == "school" ? 'com' : 'maj']: detail,
screenState: false,
})
const jg = encodeURIComponent(detail.jg || '')
if (classify == "school") common.goPage(`/pages/projectList/projectList?type=school&mechanism=${jg}&year=${detail.year}`)
else common.goPage(`/pages/projectList/projectList?type=subject&system=${jg}&subject=${encodeURIComponent(detail.major || '')}&year=${detail.year}`)
},
userClickLogin(e) {
let data = e.detail.data
this.setData({
islogin: true,
isloginBtnState: false,
informationState: data.regdatastep == 'success' ? false : true,
})
this.onLoad(this.data.options)
},
// 子组件传值 修改 完善信息组件的状态
revampInformationState() {
this.setData({
informationState: false
})
},
// 关闭授权登录事件
popClose() {
this.setData({
isloginBtnState: !this.data.isloginBtnState
})
},
openLoginState() {
this.setData({
isloginBtnState: true
})
},
sideHeight: {},
// 计算每个字母区域的位置
calculateSectionTops() {
const screen_data = this.data.screen_data
const query = wx.createSelectorQuery()
let headHeight = screen_data.totalTopHeight || 0
query.select(`.header`).boundingClientRect()
query.select(`.item-header-screen`).boundingClientRect()
query.select(`.screen`).boundingClientRect()
query.exec(res => {
if (!res) return
res.forEach(element => {
headHeight += element.height
})
headHeight += util.rpxTopx(165)
let sideHeight = {}
const letterList = this.data.letterList
let h = util.pxToRpx(headHeight)
for (const key in letterList) {
const element = letterList[key]
let height = 108 * element.initial + 106 * element.sum
sideHeight[key] = util.rpxTopx(h)
h += height
}
this.sideHeight = sideHeight
})
},
// 点击字母跳转
jumpToIndex(e) {
const key = e.currentTarget.dataset.key
const sideHeight = this.sideHeight
console.log("sideHeight", sideHeight);
wx.pageScrollTo({
scrollTop: sideHeight[key],
})
},
// touchToIndex(e) {
// console.log("e", e);
// const key = e.target.dataset.key
// if (!key) return
// console.log("key", key);
// const sideHeight = this.sideHeight
// wx.pageScrollTo({
// scrollTop: sideHeight[key],
// })
// },
onPageScroll(e) {
const scrollTop = e.scrollTop
const sideHeight = this.sideHeight
const sideHeightList = Object.keys(sideHeight) || []
if (sideHeightList.length == 0) return
const keys = Object.keys(sideHeight);
// 对键数组进行倒序排列
let closestValue = keys.reduce((acc, key) => {
const diff = sideHeight[key] - scrollTop;
if (diff <= 5) return key
return acc;
}, null);
const letterKey = closestValue || 'A'
if (letterKey != this.data.letterKey) {
this.setData({
letterKey,
})
}
// console.log("scrollTop", scrollTop, this.windowHeight / 4);
// if (scrollTop > this.windowHeight / 4) {
// this.setData({
// letterFixed: true,
// })
// } else {
// this.setData({
// letterFixed: false,
// })
// }
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})