1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
epp/weixin-miniprogram/pages/index/index.js

182 lines
4.2 KiB
JavaScript

// pages/index/index.js
const menuItemDict = require('../../utils/menuList.js')
const getUserGroupByRole = require('../../utils/getUserGroupByRole.js')
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
debugText: "调试",
displayUsername: "",
userGroup: "unknown",
filterMenuItems: [],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log("index/index onLoad", options)
// console.log("wx.getLaunchOptionsSync()", wx.getLaunchOptionsSync())
let userInfo = wx.getStorageSync("userInfo")
if (!userInfo) {
console.log("用户未登录")
wx.redirectTo({
url: '/pages/index/login'
})
return
} else {
// 用户已登录
let userGroup = getUserGroupByRole(userInfo.roleId)
this.setData({
debugText: JSON.stringify(options, null, 4),
userInfo: userInfo,
userGroup: userGroup,
displayUsername: userInfo?.username ?? "请登录",
filterMenuItems: this.getFilterMenuItems(menuItemDict, userGroup)
})
console.log("menuItemDict", menuItemDict)
console.log("filterMenuItems", this.data.filterMenuItems)
//判断用户是否是通过扫小程序码进来的
if (options && options.scene) {
// 扫门禁的小程序码
if (options.scene.split('%')[0] == "guard") {
options.scene = null
wx.navigateTo({
url: "/pages/scan/entrance"
})
return
}
}
// // 开发模式下自动跳转到指定页面,节省开发时间
// console.log("app.globalData", app.globalData)
// if (app.globalData.debugMode) {
// wx.switchTab({
// // url: '/pages/residents/report'
// // url: '/pages/person/person'
// url: '/pages/shop/shop'
// })
// return
// }
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
console.log("index/index onReady")
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
console.log("index/index onShow")
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
console.log("index/index onHide")
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
console.log("index/index onUnload")
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
console.log("index/index onPullDownRefresh")
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
console.log("index/index onReachBottom")
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
console.log("index/index onShareAppMessage")
},
getFilterMenuItems(menuItemDict, userGroup) {
return Object.values(menuItemDict)
.filter((item) => item.for.indexOf(userGroup) != -1)
},
switchTo(event) {
let dataset = event.target.dataset
console.log("dataset", dataset)
switch (dataset.switchfunc) {
case 'switchTab':
wx.switchTab({
url: dataset.pageurl
})
break;
case 'navigateTo':
wx.navigateTo({
url: dataset.pageurl
})
break;
case 'redirectTo':
wx.redirectTo({
url: dataset.pageurl
})
break;
default:
console.log("切换页面失败", dataset)
break;
}
},
goCode() {
wx.switchTab({
url: '/pages/residents/code'
})
},
toggleDot(event, ownerInstance, iconName = 'code', status = undefined) {
console.log("menuItemDict[iconName]", menuItemDict[iconName])
console.log("this.data.userGroup", this.data.userGroup)
if (typeof (status) === 'undefined') {
menuItemDict[iconName].addDot = !menuItemDict[iconName].addDot;
} else {
menuItemDict[iconName].addDot = !!status;
}
this.setData({
filterMenuItems: this.getFilterMenuItems(menuItemDict, this.data.userGroup)
})
},
magicButton() {
wx.navigateTo({
// 扫码进社区
// url: "/pages/scan/entrance?a=1"
// 订单确认页
// url: "/pages/shop/orderDetail?orderId=1748800678865801225"
// 我的订单
url: '/pages/shop/myOrder'
})
}
})