// pages/ourCanteen/index.js import { imgUrl } from "../api/request" import { request, baseUrl } from "../api/canteen-request.js"; Page({ /** * 页面的初始数据 */ data: { canteenEmpty: false, isLoading: false, imgUrl: imgUrl, baseUrl: baseUrl + '/', tab: 0, item: '', canteenList: [], isManager: false, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 查询用户所属角色 request({ url: '/mini/worker/getUserType', method: 'GET', data: { userId: wx.getStorageSync('userid') } }).then(res => { if (res.result) { let userType = res.data; // 政府工作人员 if ("0" == userType || '2' == userType) { this.setData({ isManager: false }); this.canteenList(); } else if ('1' == userType) { this.setData({ isManager: true }); this.canteenList(); } else { wx.navigateBack(); } } else { wx.navigateBack(); } }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.setData({ item: 0, isLoading: false, canteenEmpty: false }) if (this.data.loginReturnFlag) { this.setData({ loginReturnFlag: false }) this.onLoad() } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { return { title: '食堂' } }, imageError(e) { let temp = this.data.canteenList; temp[e.currentTarget.dataset.index].canteenPhotoPath = ''; this.setData({ canteenList: temp }) }, changeItem: function (e) { if (e.currentTarget.dataset.item == 1) { wx.navigateTo({ url: '/pages/deliciousList/deliciousList' }) } else { this.setData({ item: e.currentTarget.dataset.item }) } }, changeTab: function (e) { this.setData({ tab: e.detail.current }) }, toMenu: function (e) { var canteenId = e.currentTarget.dataset.item; var canteenName = e.currentTarget.dataset.name; var time = e.currentTarget.dataset.time; if (canteenId && canteenId != '') { wx.navigateTo({ url: `/pagesPublic/pages/menu/menu?canteenId=${canteenId}&canteenName=${canteenName}&time=${time}` }) } }, // 阻止手动拖动 catchTouchMove: function (res) { return true }, islogin() { let userid = wx.getStorageSync('userId'); if (userid === null || userid === '') { showDialog(); } }, showDialog() { wx.showModal({ title: '掌新南岸欢迎您!', content: '使用完整服务需要登录!', confirmText: '登录', cancelText:"取消", success: function (res) { if (res.confirm) { wx.navigateTo({ url: '/pages/login/login', }); }if (res.cancel){ wx.navigateTo({ url: '/pages/index/index', }) } }, }); }, back() { wx.navigateBack({ delta: 1, }) }, canteenList() { this.setData({ isLoading: true }) let reqData = {}; if (this.data.isManager) { reqData = { userId: wx.getStorageSync('userid') }; } request({ url: '/mini/canteen/getCanteenList', method: 'GET', data: reqData, }).then(res => { if (res.result) { let data = res.data if (data.length < 1) { this.setData({ canteenEmpty: true }) } for (let i = 0; i < data.length; i++) { const element = data[i]; var timeFrame = ""; var timeFrameinfo = []; var supplyTime = element.supplyTimeMaps.sort((a, b) => { return a.timeNode - b.timeNode }) for (let j = 0; j < supplyTime.length; j++) { const childElement = supplyTime[j]; switch (childElement.timeNode) { case 1: timeFrame += "早"; childElement.timeNode = "早上"; break case 2: timeFrame += "中"; childElement.timeNode = "中午"; break case 3: timeFrame += "晚"; childElement.timeNode = "晚上"; break case 4: timeFrame += "外卖"; childElement.timeNode = "外卖"; break } if (j != supplyTime.length - 1) { timeFrame += "/" } timeFrameinfo.push(childElement) } data[i].timeFrame = timeFrame; data[i].timeFrameinfo = timeFrameinfo; } this.setData({ isLoading: false, canteenList: data }) } else { this.setData({ isLoading: false, }) } }) } })