// pages/residents/report.js import utils from '../../utils/util' const toggleCustomTabBar = require('../../custom-tab-bar/toggleCustomTabBar') const app = getApp(); Page({ /** * 页面的初始数据 */ data: { isSkipOnShowFunc: false, // 是否跳过 onShow userInfo: null, timeInterval: null, formData: { userId: '', userRealname: '', address: '', time: '', timestamp: '', temperature: 0, }, // 是否填报过 isFilled: true, filledMsg: "", // 是否正在提交中,用于隐藏提交按钮,避免出现多次提交 isSubmitting: false, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { toggleCustomTabBar.toggle(this) console.log("residents/report onLoad") }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { console.log("residents/report onReady") }, /** * 生命周期函数--监听页面显示 */ onShow() { toggleCustomTabBar.toggle(this) console.log("residents/report onShow") if (this.data.isSkipOnShowFunc) { this.setData({ isSkipOnShowFunc: false, }) return } let userInfo = wx.getStorageSync("userInfo") console.log("userInfo", userInfo) this.setData({ userInfo: userInfo, "formData.userId": userInfo.id, "formData.userRealname": userInfo.realname, timeInterval: setInterval(this.updateTime, 1000) }) console.log("formData", this.data.formData) this.updateTime(); // 获取用户当日是否填报过 wx.showLoading({ title: '加载中' }) var that = this; wx.request({ url: `${app.globalData.baseUrl}/access/report/getLatestRecord`, method: "POST", header: { "Content-Type": "application/x-www-form-urlencoded" //用于post }, data: { userId: this.data.userInfo.id }, success: function (d) { wx.hideLoading() let result = d.data console.log("result", result) if (result.success) { console.log("result.data", result.data); if (result.data == null || new Date(result.data.time) < new Date(new Date().toISOString().substring(0, 10))) { // 如果没有填报记录,或者填报记录不是今天,那么说明没有填报过 that.setData({ isFilled: false }) } else { // 有今日记录,说明已经填报过 that.setData({ isFilled: true, filledMsg: "您已完成今日体温填报" }) } } else { wx.showToast({ title: "加载填报信息失败", icon: 'error', duration: 2000 }) } }, fail: function () { wx.hideLoading() wx.showToast({ title: "请求失败", icon: 'error', duration: 2000 }) }, complete: function () { wx.hideNavigationBarLoading(); } }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { console.log("residents/report onHide") clearInterval(this.data.timeInterval) }, /** * 生命周期函数--监听页面卸载 */ onUnload() { console.log("residents/report onUnload") clearInterval(this.data.timeInterval) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, bindTemperatureChanged(e) { this.setData({ "formData.temperature": e.detail.value }) }, updateTime() { let timestamp = Date.now() this && this.setData({ "formData.timestamp": timestamp, "formData.time": utils.formatTime(new Date(timestamp)) }) }, chooseLocation: async function () { wx.showLoading({ title: '正在获取权限' }) await new Promise((resolve, reject) => { wx.getSetting({ success(res) { wx.hideLoading() if (!res.authSetting['scope.userLocation']) { wx.authorize({ scope: 'scope.userLocation', // scope: 'scope.userLocationBackground', success() { resolve(); }, fail(err) { wx.showModal({ title: '定位失败', content: '您已拒绝定位授权,小程序无法获取您的位置,请在微信首页长按删除小程序,重新打开再试', showCancel: false, complete: (res) => { reject(); } }) } }) } else { resolve(); } } }) }); wx.showLoading({ title: '正在定位' }) var that = this; this.setData({ isSkipOnShowFunc: true, // 关闭位置选择器后,会触发 onShow,这里避免掉 }) wx.chooseLocation({ success: function (res) { console.log("wx.chooseLocation success") that.setData({ "formData.address": res.address }) }, fail: function () { console.log("wx.chooseLocation fail", that.data.formData) if (!that.data.formData.address) { wx.showToast({ title: "请获取当前位置", icon: 'error' }) } }, }) wx.hideLoading() }, // 体温上报 提交按钮 report() { console.log("点击提交", "this.data.formData", this.data.formData) if (!this.data.formData.address) { wx.showToast({ title: "请获取当前位置", icon: 'error' }) return; } wx.showLoading({ title: '加载中' }) var that = this; that.setData({ isSubmitting: true, }) wx.request({ url: `${app.globalData.baseUrl}/access/report/submit`, method: "POST", header: { "Content-Type": "application/x-www-form-urlencoded" //用于post }, data: { ...this.data.formData, }, success: function (d) { console.log("begin success") wx.hideLoading() let result = d.data; if (result.success) { // 填报完成 that.setData({ isFilled: true, filledMsg: "您已完成今日体温填报" }) console.log("result.data", result.data); wx.showToast({ title: "填报成功", icon: 'success', duration: 2000 }) } else { wx.showToast({ title: result.msg || "出错啦", icon: 'error', duration: 2000 }) } that.isShow = '' console.log("end success") }, fail: function () { console.log("begin fail") wx.hideLoading() wx.showToast({ title: "请求失败", icon: 'error', duration: 2000 }) console.log("end fail") }, complete: function () { that.setData({ isSubmitting: false, }) console.log("begin complete") if (typeof (callback) === "function") callback(); wx.hideNavigationBarLoading(); console.log("end complete") } }) }, // 历史填报 myreport() { wx.navigateTo({ url: "/pages/residents/reportHistory" }) }, // 删除今日填表 withdrawReport() { wx.showLoading({ title: '加载中' }) var that = this; that.setData({ isSubmitting: true, }) wx.request({ url: `${app.globalData.baseUrl}/access/report/withdraw`, method: "POST", header: { "Content-Type": "application/x-www-form-urlencoded" //用于post }, data: { userId: this.data.userInfo.id }, success: function (d) { console.log("begin success") wx.hideLoading() let result = d.data; if (result.success) { // 填报完成 that.setData({ isFilled: false, filledMsg: "", "formData.address": '', }) console.log("result.data", result.data); wx.showToast({ title: "删除成功", icon: 'success', duration: 2000, }) // wx.showModal({ // title: '删除成功', // content: '', // showCancel: false, // complete: (res) => { // } // }) } else { wx.showToast({ title: result.msg || "出错啦", icon: 'error', duration: 2000 }) } that.isShow = '' console.log("end success") }, fail: function () { console.log("begin fail") wx.hideLoading() wx.showToast({ title: "请求失败", icon: 'error', duration: 2000 }) console.log("end fail") }, complete: function () { that.setData({ isSubmitting: false, }) console.log("begin complete") if (typeof (callback) === "function") callback(); wx.hideNavigationBarLoading(); console.log("end complete") } }) } })