// pages/appointments/appointments.js
import {
formatTime
} from '../../utils/util'
const userTbl = wx.cloud.database().collection('lab-user');
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
dateVisible: false,
noonVisible: false,
checkerVisible: false,
name: '',
teacher: '',
date: '',
defaultDate: new Date().getTime(),
minDate: new Date().getTime(),
noon: '',
remark: '',
checker: '',
checkerOpenID: null,
checkerList: [],
noonList: [{
name: '全天'
}, {
name: '上午'
}, {
name: '下午'
}, {
name: '晚上'
}]
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
userTbl.get().then((res) => {
console.warn(res);
// 只返回有openid的审批人或管理员,否则无法发送订阅消息
let list = res.data.filter(x => x.userOpenID != null && (x.role === "审批人" || x.role === "管理员"))
let checkerList = list.map(x => {
return {
name: x.name,
openID: x.userOpenID
}
});
this.setData({
checkerList
})
})
},
showDate() {
this.setData({
dateVisible: true
})
},
onDateClose(e) {
this.setData({
dateVisible: false
})
},
onDateInput(e) {
this.setData({
defaultDate: e.detail,
});
},
onDateConfirm(e) {
let date = formatTime(new Date(e.detail));
this.setData({
dateVisible: false,
date
});
},
showNoon() {
this.setData({
noonVisible: true
})
},
onNoonClose(e) {
this.setData({
noonVisible: false
})
},
onNoonSelect(e) {
this.setData({
noonVisible: false,
noon: e.detail.name
})
},
showChecker() {
this.setData({
checkerVisible: true
})
},
onCheckerClose(e) {
this.setData({
checkerVisible: false
})
},
onCheckerSelect(e) {
this.setData({
checkerVisible: false,
checker: e.detail.name,
checkerOpenID: e.detail.openID
})
},
doAdd() {
if (this.data.name.length === 0 ||
this.data.teacher.length === 0 ||
this.data.date.length === 0 ||
this.data.noon.length === 0 ||
this.data.checker.length === 0
) {
wx.showToast({
title: '审评人、导师、日期、时段和审批人均不能为空哦',
icon: 'none'
})
return
}
wx.cloud.callFunction({
name: 'getOpenID'
}).then(res => {
wx.cloud
.database()
.collection('lab-order')
.add({
data: {
name: this.data.name,
teacher: this.data.teacher,
date: this.data.date,
createdAt: wx.cloud.database().serverDate(),
noon: this.data.noon,
remark: this.data.remark,
checker: this.data.checker,
status: 0, // 0:待审核,1:通过,2:驳回
openID: res.result.openid // 申请人的opendid,发送订阅消息时使用
}
}).then(res => {
wx.showToast({
title: '申请成功',
})
// this.setData({
// name: '',
// date: '',
// noon: '',
// remark: '',
// })
if (!this.data.checkerOpenID) {
console.warn('审批人openid为空,暂不能向其发送消息');
} else
wx.cloud.callFunction({
name: 'sendMsg',
data: {
openID: this.data.checkerOpenID,
page: 'pages/login/login',
tmplId: 'WUPLfFmGhxZhP_R-ezlwkpw6SXAq0LtS1Kf_FH_jJHI',
data: {
name2: { // 提交人
value: this.data.name
},
thing3: { // 提交事宜
value: `${this.data.name}申请使用实验室`
},
time4: { // 提交时间
value: formatTime(new Date(), true)
},
}
}
}).then(s => {
console.warn('send:', s);
}).catch(error => {
console.warn('err:', error);
})
})
})
}
})