index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import * as api from '@/api'
  2. import Custom from './custom'
  3. export default {
  4. data() {
  5. return {
  6. TITLE: '问卷调查',
  7. data: {
  8. result: [],
  9. topic: {}
  10. },
  11. loading: {
  12. data: false,
  13. empty: false,
  14. submit: false
  15. },
  16. show: {
  17. submit: false
  18. },
  19. codeMess: {
  20. num: 3,
  21. message: '自动跳转',
  22. disabled: false,
  23. t: null
  24. }
  25. };
  26. },
  27. watch: {
  28. $route(to, from) {
  29. if (from.name === 'FormVolume') {
  30. clearInterval(this.codeMess.t)
  31. }
  32. }
  33. },
  34. destroyed() {
  35. clearInterval(this.codeMess.t)
  36. },
  37. created() {
  38. this.vueWatchRoute()
  39. this.btnOpenSuccess(() => this.btnLinkBack())
  40. },
  41. methods: {
  42. vueWatchRoute () {
  43. this.inQuery()
  44. this.inData()
  45. },
  46. // 秒倒计时
  47. loopCode(num, callback) {
  48. let msg = '自动跳转'
  49. clearInterval(this.codeMess.t)
  50. function loopNum() {
  51. if (num < 1) {
  52. this.codeMess['message'] = msg
  53. this.codeMess['disabled'] = false
  54. clearInterval(this.codeMess.t)
  55. callback && callback()
  56. return
  57. }
  58. this.codeMess['disabled'] = true
  59. this.codeMess['message'] = `${num}s ${msg}`
  60. num = num - 1
  61. }
  62. loopNum.call(this)
  63. this.codeMess.t = setInterval(() => loopNum.call(this), 1000)
  64. },
  65. inQuery () {
  66. let { title } = this.$route.query
  67. if (title) {
  68. this.TITLE = title
  69. }
  70. },
  71. isForm (callback) {
  72. return new Promise((resolve, reject) => {
  73. let state = this.data.result.some(item => {
  74. if (!item.required) {
  75. return false
  76. }
  77. let b = item.val instanceof Array ? !!item.val.length : !!item.val
  78. !b && callback && callback(item)
  79. return !b
  80. })
  81. state ? reject() : resolve()
  82. })
  83. },
  84. async inData() {
  85. this.loading.data = true
  86. this.loading.empty = false
  87. try {
  88. let { result_id } = this.$route.query
  89. let pm = result_id
  90. ? api.apiGetVolumeResult({ result_id })
  91. : api.apiGetVolumeInfo(this.$route.query)
  92. let res = await pm
  93. let data = res.data
  94. let configType = {
  95. 'radio': '单选',
  96. 'checkbox': '多选'
  97. }
  98. data.result.forEach(item => {
  99. item.layout_align = item.layout_align || data.topic.layout_align || 'left'
  100. item.layout_inline = item.layout_inline || data.topic.layout_inline || 1
  101. item.type_name = configType[item.type] || ''
  102. // 自定义组件处理
  103. Custom(item)
  104. })
  105. if (data.topic.memo) {
  106. data.topic.memo = data.topic.memo.replace(/\n/g, '<br />')
  107. }
  108. if (this.layoutType) {
  109. this.layoutCurrent = this.layoutType[data.topic.layout_type] || 'van-empty'
  110. }
  111. this.$set(this, 'data', data)
  112. } catch (error) {
  113. this.loading.empty = true
  114. console.log(error)
  115. }
  116. this.loading.data = false
  117. },
  118. btnOpenSuccess(callback) {
  119. this.show.submit = true
  120. // this.loopCode(this.codeMess.num, async () => {
  121. // this.show.submit = false
  122. // await this.$nextTick()
  123. // callback && callback()
  124. // })
  125. },
  126. async btnSubmit() {
  127. this.loading.submit = true
  128. let ld = null
  129. try {
  130. await this.isForm(item => {
  131. this.$dialog.alert({
  132. title: '必填项',
  133. message: `${item.title}`,
  134. })
  135. })
  136. await this.$dialog.confirm({
  137. title: '提示',
  138. message: '请确认无误提交?',
  139. })
  140. ld = this.$toast.loading({ message: '提交中...', duration: 0 })
  141. let result = this.data.result.map(item => ({ detail_id: item.detail_id, val: item.val }))
  142. let query = {
  143. ...this.$route.query,
  144. data: JSON.stringify(result)
  145. }
  146. await api.apiPostVolumeInfo(query)
  147. this.btnOpenSuccess(() => this.btnLinkBack())
  148. } catch (error) {
  149. console.log(error)
  150. }
  151. ld && ld.clear()
  152. this.loading.submit = false
  153. },
  154. btnLinkBack() {
  155. let { go, replace } = this.$route.query
  156. replace
  157. ? this.$router.replace({ name: replace })
  158. : this.$router.go(go || -1)
  159. }
  160. }
  161. }