Last active
February 5, 2020 06:33
-
-
Save shenqihui/f6494cf8d66550967bb498340235018e to your computer and use it in GitHub Desktop.
问卷题目配置规则
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const healthExamObj = { | |
// begin_time: 0, | |
// end_time: 999999999999, | |
exam_id: 1, | |
issues: [ | |
{ | |
key: 'name', // 后面我想直接去掉这个 key ,不过如果只用 issue_id 的话,不可读,麻烦。 | |
issue_id: 0, | |
title: '姓名', | |
type: 'input', | |
}, | |
{ | |
key: 'area', | |
issue_id: 1, | |
title: '自2020年1月20日起,学生所在的地区', | |
type: 'area', | |
}, | |
{ | |
key: 'area_mult', | |
issue_id: 1, | |
title: '自2020年1月20日起,学生坐车所路过城市', | |
type: 'area_mult', | |
}, | |
{ | |
key: 'status', | |
issue_id: 2, | |
title: '学生是否感觉身体不适?', | |
type: 'radio', | |
options: [ | |
{ | |
value: 1, | |
label: '是', | |
}, | |
{ | |
value: 0, | |
label: '否', | |
}, | |
], | |
}, | |
{ | |
key: 'is_touch', | |
issue_id: 3, | |
title: '自2020年1月20日起,是否接触过肺炎疑似症状者?', | |
// 单选 | |
type: 'radio', | |
options: [ | |
{ | |
value: 1, | |
label: '是', | |
}, | |
{ | |
value: 0, | |
label: '否', | |
}, | |
], | |
}, | |
{ | |
key: 'health', | |
issue_id: 4, | |
title: '目前学生身体状况', | |
type: 'checkbox', // 同上,多选 | |
options: [ | |
{ | |
value: 1, | |
label: '发热', | |
}, | |
{ | |
value: 2, | |
label: '干咳', | |
}, | |
{ | |
value: 3, | |
label: '乏力', | |
}, | |
], | |
}, | |
], | |
rules: [ | |
{ | |
// 只有满足条件才会show,如果不满足,就设置成为 false。有先后rule规则。 | |
issue_from: 'status', | |
issue_from_value: [1], | |
issue_to: 'is_touch', | |
}, | |
{ | |
// 只有满足条件才会show,如果不满足,就设置成为 false。有先后rule规则。 | |
issue_from: 'status', | |
issue_from_value: [1], | |
issue_to: 'health', | |
}, | |
], | |
}; | |
const currentHealthExamAnswer = { | |
name: '张三', | |
area: 620700000000, | |
area_mult: [620700000000, 620700000001], | |
status: 0, | |
is_touch: 1, | |
health: 4, | |
}; | |
// examObj 为考卷的东西 | |
function getExamIssues(examObj, answerObj) { | |
const issues = []; | |
const oldIssues = [..._.cloneDeep(_.get(examObj, 'issues', []))]; | |
const oldIssuesKey = {}; | |
// 创建哈希索引,key不能相同,后面弄成 id 最好 | |
_.map(oldIssues, (issue) => { | |
oldIssuesKey[issue.key] = issue; | |
}); | |
const rules = _.get(examObj, 'rules', []); | |
_.map(rules, (rule) => { | |
const issueAnswer = answerObj[rule.issue_from]; | |
if (oldIssuesKey[rule.issue_from] && oldIssuesKey[rule.issue_to]) { | |
let isValidRule = false; | |
// 数组,交集,就是满足条件 | |
if (_.isArray(issueAnswer)) { | |
if (0 < _.intersection(issueAnswer, rule.issue_from_value).length) { | |
isValidRule = true; | |
} | |
} | |
// 数组包含值,满足条件 | |
else if (_.includes(rule.issue_from_value, issueAnswer)) { | |
isValidRule = true; | |
} | |
if (isValidRule) { | |
oldIssuesKey[rule.issue_to].show = true; | |
} | |
else { | |
oldIssuesKey[rule.issue_to].show = false; | |
} | |
} | |
}); | |
// 最后整理出要显示的题目 | |
_.map(oldIssues, (issue) => { | |
if ('show' in issue) { | |
if (issue.show) { | |
issues.push(issue); | |
} | |
} | |
else { | |
issues.push(issue); | |
} | |
}); | |
return issues; | |
} | |
window.console.log('healthExamObj', healthExamObj); | |
window.console.log('currentHealthExamAnswer', currentHealthExamAnswer) | |
window.console.log('getExamIssues', getExamIssues(healthExamObj, {status: 0})); | |
window.console.log('getExamIssues', getExamIssues(healthExamObj, {status: 1})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment