consume-editable-policy.js
2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import sysConfigApi from '@/apis/modules/sys-config.js'
export const CONSUME_EDITABLE_POLICY_KEY = 'consumeEditablePolicy'
const DEFAULT_POLICY = {
editableDays: 3,
allowCrossMonth: false
}
export function parseConsumeEditablePolicy(data = {}) {
const days = parseInt(data.consumeEditableDays, 10)
return {
editableDays: isNaN(days) || days < 1 ? DEFAULT_POLICY.editableDays : days,
allowCrossMonth: Number(data.consumeAllowCrossMonthEdit) === 1
}
}
/**
* 与后端 ConsumeEditableDaysHelper 口径一致:先校验天数,再校验跨月
*/
export function canConsumeMiniProgramEdit(row, policy = DEFAULT_POLICY) {
if (!row) return false
const baseTime = parseConsumeBaseTime(row)
if (!baseTime) return false
const now = Date.now()
if (now > baseTime.getTime() + policy.editableDays * 86400000) {
return false
}
if (!policy.allowCrossMonth) {
const nowDate = new Date()
if (
baseTime.getFullYear() !== nowDate.getFullYear() ||
baseTime.getMonth() !== nowDate.getMonth()
) {
return false
}
}
return true
}
export function parseConsumeBaseTime(row) {
const raw = row.hksj || row.Hksj || row.createTime || row.CreateTime
if (!raw) return null
const date = new Date(raw)
return isNaN(date.getTime()) ? null : date
}
export function loadConsumeEditablePolicy(force = false) {
if (!force) {
const cached = uni.getStorageSync(CONSUME_EDITABLE_POLICY_KEY)
if (cached && typeof cached === 'object') {
refreshConsumeEditablePolicy()
return Promise.resolve(cached)
}
}
return refreshConsumeEditablePolicy()
}
export function refreshConsumeEditablePolicy() {
return sysConfigApi.getSysConfig()
.then(res => {
const policy = parseConsumeEditablePolicy((res && res.data) || {})
uni.setStorageSync(CONSUME_EDITABLE_POLICY_KEY, policy)
uni.$emit && uni.$emit('consume-editable-policy-change', policy)
return policy
})
.catch(() => {
const cached = uni.getStorageSync(CONSUME_EDITABLE_POLICY_KEY)
return cached && typeof cached === 'object' ? cached : { ...DEFAULT_POLICY }
})
}
export function getConsumeEditablePolicy() {
const cached = uni.getStorageSync(CONSUME_EDITABLE_POLICY_KEY)
return cached && typeof cached === 'object' ? cached : { ...DEFAULT_POLICY }
}
export function clearConsumeEditablePolicy() {
uni.removeStorageSync(CONSUME_EDITABLE_POLICY_KEY)
}
export default {
CONSUME_EDITABLE_POLICY_KEY,
parseConsumeEditablePolicy,
canConsumeMiniProgramEdit,
parseConsumeBaseTime,
loadConsumeEditablePolicy,
refreshConsumeEditablePolicy,
getConsumeEditablePolicy,
clearConsumeEditablePolicy
}