196 lines
4.8 KiB
JavaScript
196 lines
4.8 KiB
JavaScript
const api = require('../../utils/api')
|
|
|
|
const emptyForm = {
|
|
id: '',
|
|
title: '',
|
|
content: '',
|
|
attendancePointNames: [],
|
|
sortOrder: 0,
|
|
isActive: true,
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
rows: [],
|
|
page: 1,
|
|
pageInput: '1',
|
|
totalPages: 1,
|
|
form: { ...emptyForm },
|
|
formVisible: false,
|
|
formTitle: '新增通知',
|
|
attendancePoints: [],
|
|
refreshing: false,
|
|
},
|
|
onShow() {
|
|
this.init()
|
|
},
|
|
async init() {
|
|
await this.loadAttendancePoints()
|
|
await this.load()
|
|
},
|
|
async loadAttendancePoints() {
|
|
try {
|
|
const attendancePoints = await api.listAccessibleAttendancePoints()
|
|
this.setData({ attendancePoints: attendancePoints || [] })
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '加载考勤点失败', icon: 'none' })
|
|
}
|
|
},
|
|
async load(page = this.data.page) {
|
|
try {
|
|
const result = await api.listNotices({
|
|
includeInactive: true,
|
|
page,
|
|
pageSize: 8,
|
|
})
|
|
this.setData({
|
|
rows: result.rows,
|
|
page: result.page,
|
|
pageInput: String(result.page || 1),
|
|
totalPages: result.totalPages,
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message, icon: 'none' })
|
|
}
|
|
},
|
|
async onPullDownRefresh() {
|
|
this.setData({ refreshing: true })
|
|
try {
|
|
await this.load(1)
|
|
} finally {
|
|
this.setData({ refreshing: false })
|
|
if (wx.stopPullDownRefresh) {
|
|
wx.stopPullDownRefresh()
|
|
}
|
|
}
|
|
},
|
|
noop() {},
|
|
checkedPointMap(names = []) {
|
|
const selected = new Set(names || [])
|
|
return this.data.attendancePoints.map(point => ({
|
|
...point,
|
|
checked: selected.has(point.name),
|
|
}))
|
|
},
|
|
defaultPointNames() {
|
|
return this.data.attendancePoints.map(point => point.name)
|
|
},
|
|
openCreate() {
|
|
const pointNames = this.defaultPointNames()
|
|
this.setData({
|
|
form: { ...emptyForm, attendancePointNames: pointNames },
|
|
attendancePoints: this.checkedPointMap(pointNames),
|
|
formTitle: '新增通知',
|
|
formVisible: true,
|
|
})
|
|
},
|
|
edit(e) {
|
|
const index = e.currentTarget.dataset.index
|
|
const notice = this.data.rows[index]
|
|
this.setData({
|
|
form: { ...notice },
|
|
attendancePoints: this.checkedPointMap(notice.attendancePointNames || []),
|
|
formTitle: '编辑通知',
|
|
formVisible: true,
|
|
})
|
|
},
|
|
closeForm() {
|
|
this.setData({
|
|
formVisible: false,
|
|
form: { ...emptyForm },
|
|
attendancePoints: this.checkedPointMap([]),
|
|
})
|
|
},
|
|
clearForm() {
|
|
this.setData({
|
|
form: { ...emptyForm },
|
|
attendancePoints: this.checkedPointMap([]),
|
|
})
|
|
},
|
|
onInput(e) {
|
|
const field = e.currentTarget.dataset.field
|
|
this.setData({
|
|
[`form.${field}`]: e.detail.value,
|
|
})
|
|
},
|
|
onActiveChange(e) {
|
|
this.setData({
|
|
'form.isActive': e.detail.value,
|
|
})
|
|
},
|
|
onPointCheckboxChange(e) {
|
|
const names = e.detail.value || []
|
|
this.setData({
|
|
'form.attendancePointNames': names,
|
|
attendancePoints: this.checkedPointMap(names),
|
|
})
|
|
},
|
|
async save() {
|
|
const form = {
|
|
...this.data.form,
|
|
sortOrder: Number(this.data.form.sortOrder || 0),
|
|
}
|
|
if (!(form.attendancePointNames || []).length) {
|
|
wx.showToast({ title: '请选择通知考勤点', icon: 'none' })
|
|
return
|
|
}
|
|
wx.showLoading({ title: '保存中' })
|
|
try {
|
|
await api.saveNotice(form)
|
|
wx.showToast({ title: '已保存', icon: 'success' })
|
|
this.setData({
|
|
form: { ...emptyForm },
|
|
attendancePoints: this.checkedPointMap([]),
|
|
formVisible: false,
|
|
})
|
|
await this.load(1)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message, icon: 'none' })
|
|
} finally {
|
|
wx.hideLoading()
|
|
}
|
|
},
|
|
remove(e) {
|
|
const index = e.currentTarget.dataset.index
|
|
const notice = this.data.rows[index]
|
|
wx.showModal({
|
|
title: '删除通知',
|
|
content: notice.title,
|
|
success: async res => {
|
|
if (!res.confirm) {
|
|
return
|
|
}
|
|
try {
|
|
await api.deleteNotice(notice.id)
|
|
await this.load(1)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message, icon: 'none' })
|
|
}
|
|
},
|
|
})
|
|
},
|
|
prevPage() {
|
|
if (this.data.page > 1) {
|
|
this.load(this.data.page - 1)
|
|
}
|
|
},
|
|
nextPage() {
|
|
if (this.data.page < this.data.totalPages) {
|
|
this.load(this.data.page + 1)
|
|
}
|
|
},
|
|
onPageInput(e) {
|
|
this.setData({ pageInput: e.detail.value })
|
|
},
|
|
jumpToPage(e) {
|
|
wx.hideKeyboard()
|
|
const inputValue = e && e.detail && e.detail.value !== undefined ? e.detail.value : this.data.pageInput
|
|
const target = Math.max(1, Math.min(Number(inputValue) || this.data.page, this.data.totalPages))
|
|
if (target === this.data.page) {
|
|
this.setData({ pageInput: String(this.data.page || 1) })
|
|
return
|
|
}
|
|
this.load(target)
|
|
},
|
|
})
|