backgroundSurvey.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. function selectedOption(question, optionCode) {
  2. const options = question && Array.isArray(question.options) ? question.options : []
  3. return options.find(option => String(option.code) === String(optionCode)) || null
  4. }
  5. function validateBackgroundAnswer(question, optionCode, customText) {
  6. const option = selectedOption(question, optionCode)
  7. if (!option) return '请选择当前题目的答案'
  8. if (option.customAllowed && !String(customText || '').trim()) return '请填写其他选项的具体内容'
  9. return ''
  10. }
  11. function createBackgroundAnswerPayload(question, optionCode, customText) {
  12. const option = selectedOption(question, optionCode)
  13. return {
  14. detailId: question && question.detailId,
  15. optionCode: optionCode || '',
  16. customText: option && option.customAllowed ? String(customText || '').trim() : ''
  17. }
  18. }
  19. function encodeQuery(params) {
  20. return Object.keys(params || {})
  21. .filter(key => params[key] !== undefined && params[key] !== null && params[key] !== '')
  22. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(decodeQueryValue(params[key]))}`)
  23. .join('&')
  24. }
  25. function decodeQueryValue(value) {
  26. let decoded = String(value == null ? '' : value)
  27. for (let i = 0; i < 5; i += 1) {
  28. try {
  29. const next = decodeURIComponent(decoded)
  30. if (next === decoded) break
  31. decoded = next
  32. } catch (error) {
  33. break
  34. }
  35. }
  36. return decoded
  37. }
  38. function backgroundIntroUrl(params) {
  39. return `/pagesPublish/backgroundSurveyIntro?${encodeQuery(Object.assign({ stage: 'background' }, params))}`
  40. }
  41. function perillIntroUrl(params) {
  42. return `/pagesPublish/backgroundSurveyIntro?${encodeQuery(Object.assign({ stage: 'perill' }, params))}`
  43. }
  44. function questionnaireFillUrl(params) {
  45. return `/pagesPublish/questionnaireFill?${encodeQuery(params)}`
  46. }
  47. function isPublishablePerillQuestionnaire(questionnaire) {
  48. if (!questionnaire) return false
  49. return !questionnaire.purpose || questionnaire.purpose === 'PERILL'
  50. }
  51. module.exports = {
  52. selectedOption,
  53. validateBackgroundAnswer,
  54. createBackgroundAnswerPayload,
  55. encodeQuery,
  56. decodeQueryValue,
  57. backgroundIntroUrl,
  58. perillIntroUrl,
  59. questionnaireFillUrl,
  60. isPublishablePerillQuestionnaire
  61. }