questionnaireFill2.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="page" :style="{ 'min-height': h + 'px', 'padding-top': mt + 'px' }">
  3. <cus-header title="问卷填写" :backAlert="true"></cus-header>
  4. <div class="top adffcacjc">
  5. <p>{{ title }}</p>
  6. <p class="tip">
  7. 第
  8. <span>{{ currentIndex + 1 }}</span>
  9. /{{ list.length }} 题,已作答
  10. <span style="font-weight: bold">{{ answerNum }}</span>
  11. 题,请耐心选择!
  12. </p>
  13. </div>
  14. <scroll-view class="list" scroll-y="true" :scroll-top="scrollTop" :style="{ height: h - 180 - mt + 'px' }">
  15. <div v-if="isLoading" class="loading-container adfacjc">
  16. <div class="adfac">
  17. <u-loading-icon size="42"></u-loading-icon>
  18. <text style="margin-left: 10rpx; font-size: 34rpx; color: #666666">问卷加载中...</text>
  19. </div>
  20. </div>
  21. <template v-else>
  22. <div v-if="currentQuestion" class="l_item" :id="'question-' + currentIndex">
  23. <QuestionItem :item="currentQuestion" :index="currentIndex" @change="handleAnswerChange"></QuestionItem>
  24. </div>
  25. </template>
  26. </scroll-view>
  27. <div class="bottom adfacjb">
  28. <view class="zt_btn secondary" :class="{disabled: currentIndex === 0 || isSubmitting}" @tap="previousQuestion">上一题</view>
  29. <view class="zt_btn" :class="{disabled: isSubmitting}" @tap="nextQuestion">
  30. {{ currentIndex === list.length - 1 ? (isSubmitting ? '提交中...' : '提交问卷') : '下一题' }}
  31. </view>
  32. </div>
  33. </view>
  34. </template>
  35. <script>
  36. import QuestionItem from '@/components/QuestionItem/index2.vue';
  37. export default {
  38. components: { QuestionItem },
  39. data() {
  40. return {
  41. title: '',
  42. teamQuestionnaireId: '',
  43. list: [],
  44. currentIndex: 0,
  45. questionnaire: null,
  46. isLoading: true,
  47. isSubmitting: false,
  48. scrollTop: 0, // scroll-view 的滚动位置
  49. oldScrollTop: 0, // 辅助值,确保即使滚动到相同位置也能触发
  50. userAnswerTemp: []
  51. };
  52. },
  53. onLoad(option) {
  54. this.title = option.title;
  55. this.teamQuestionnaireId = option.teamQuestionnaireId;
  56. this.getList();
  57. },
  58. computed: {
  59. answerNum() {
  60. return this.list.filter(l => l?.answer.filter(a => a?.value).length == this.userAnswerTemp.length).length || 0;
  61. },
  62. currentQuestion() {
  63. return this.list[this.currentIndex] || null;
  64. }
  65. },
  66. methods: {
  67. handleAnswerChange(e) {
  68. const item = this.list[e.index];
  69. if (item) {
  70. let answer = JSON.parse(JSON.stringify(item.answer));
  71. answer.forEach(a=>{
  72. a.value = a.assessmentMethod==e.assessmentMethod?e.value:a.value
  73. })
  74. this.$set(item, 'answer', answer);
  75. if (item.warn) {
  76. this.$set(item, 'warn', false);
  77. }
  78. }
  79. this.setQuestionnaireCache();
  80. },
  81. getList() {
  82. let questionnaire = null;
  83. try {
  84. const cacheStr = uni.getStorageSync('questionnaire');
  85. if (cacheStr) {
  86. questionnaire = JSON.parse(cacheStr);
  87. }
  88. } catch (e) {
  89. console.error('解析问卷缓存失败:', e);
  90. uni.removeStorageSync('questionnaire');
  91. }
  92. this.isLoading = true;
  93. this.$api
  94. .get('/core/team/member/answer/listByUser/' + this.teamQuestionnaireId)
  95. .then((res) => {
  96. if (res.data.code !== 0) return this.$showToast(res.data.msg);
  97. const answerMap = new Map();
  98. if (questionnaire && this.teamQuestionnaireId == questionnaire.key) {
  99. questionnaire.list.forEach((q) => answerMap.set(q.id, q.answer));
  100. }
  101. this.userAnswerTemp = res.data.data[0].userAnswer.map(u=>{
  102. return {
  103. assessmentMethod:u.assessmentMethod,
  104. value:''
  105. }
  106. })
  107. let uaTemp = JSON.parse(JSON.stringify(this.userAnswerTemp))
  108. this.list = res.data.data.map(item => {
  109. let _a = answerMap.get(item.id);
  110. return {
  111. ...item,
  112. answer:_a?uaTemp.map(u=>{
  113. return {
  114. assessmentMethod:u.assessmentMethod,
  115. value:_a.find(a=>a.assessmentMethod==u.assessmentMethod)?.value||''
  116. }
  117. }):this.userAnswerTemp,
  118. warn: false
  119. }
  120. });
  121. this.list.forEach(l=>{
  122. let _a = answerMap.get(l.id);
  123. l.userAnswer.forEach(u=>{
  124. u.answer = _a?(_a.find(a=>a.assessmentMethod==u.assessmentMethod)?.value||''):''
  125. })
  126. })
  127. const firstUnansweredIndex = this.list.findIndex(l =>
  128. l.answer.filter(a => a && a.value).length !== this.userAnswerTemp.length)
  129. this.currentIndex = firstUnansweredIndex === -1 ? 0 : firstUnansweredIndex
  130. })
  131. .catch(() => {
  132. this.isLoading = false;
  133. this.$showToast('网络异常,请稍后重试');
  134. })
  135. .finally(() => {
  136. this.isLoading = false;
  137. });
  138. },
  139. previousQuestion() {
  140. if (this.currentIndex === 0 || this.isSubmitting) return
  141. this.currentIndex--
  142. this.scrollTop = 0
  143. },
  144. nextQuestion() {
  145. if (this.isSubmitting || !this.currentQuestion) return
  146. const current = this.currentQuestion
  147. const isAnswered = current.answer.filter(a => a && a.value).length === this.userAnswerTemp.length
  148. this.$set(current, 'warn', !isAnswered)
  149. if (!isAnswered) {
  150. this.$showToast(`第 ${this.currentIndex + 1} 项未选择完整答案,请选择。`)
  151. return
  152. }
  153. if (this.currentIndex < this.list.length - 1) {
  154. this.currentIndex++
  155. this.scrollTop = 0
  156. return
  157. }
  158. this.submitWj()
  159. },
  160. submitWj() {
  161. if (this.isSubmitting) return;
  162. let firstUnansweredIndex = -1;
  163. this.list.forEach((l, i) => {
  164. const isAnswered = l.answer.filter(a => a?.value).length==this.userAnswerTemp.length?true:false;
  165. this.$set(l, 'warn', !isAnswered);
  166. if (!isAnswered && firstUnansweredIndex === -1) {
  167. firstUnansweredIndex = i;
  168. }
  169. });
  170. if (firstUnansweredIndex > -1) {
  171. this.currentIndex = firstUnansweredIndex
  172. this.scrollTop = 0
  173. this.$showToast(`第 ${firstUnansweredIndex + 1} 项未选择完整答案,请选择。`)
  174. return; // 终止提交
  175. }
  176. const submitList = this.list.map((l) => {
  177. let {answer,userAnswer,warn,...other} = JSON.parse(JSON.stringify(l));
  178. userAnswer.forEach(u=>{
  179. u.questionOption.forEach(q=>{
  180. q.userAnswer=answer.find(a=>a.assessmentMethod==u.assessmentMethod)?.value==q.questionOption;
  181. })
  182. })
  183. return {
  184. ...other,
  185. isAnswer: '1',
  186. userAnswer
  187. };
  188. });
  189. this.isSubmitting = true;
  190. this.$api
  191. .post('/core/team/member/answer/submit', submitList)
  192. .then((res) => {
  193. if (res.data.code !== 0) {
  194. this.isSubmitting = false;
  195. return this.$showToast(res.data.msg);
  196. }
  197. uni.removeStorageSync('questionnaire');
  198. uni.redirectTo({
  199. url: '/pages/questionnaireResult'
  200. });
  201. })
  202. .catch((err) => {
  203. this.$showToast('网络异常,请稍后重试');
  204. })
  205. .finally(() => {
  206. this.isSubmitting = false;
  207. });
  208. },
  209. setQuestionnaireCache() {
  210. const answeredList = this.list.filter((l) => l.answer.filter(a => a?.value).length>0).map((l) => ({ id: l.id, answer: l.answer }));
  211. if (answeredList.length === 0) {
  212. uni.removeStorageSync('questionnaire');
  213. return;
  214. }
  215. const qinfo = {
  216. key: this.teamQuestionnaireId,
  217. list: answeredList
  218. };
  219. uni.setStorageSync('questionnaire', JSON.stringify(qinfo));
  220. }
  221. }
  222. };
  223. </script>
  224. <style scoped lang="less">
  225. .loading-container {
  226. width: 100%;
  227. height: 100%;
  228. }
  229. .page {
  230. background: #f7f2f6;
  231. box-sizing: border-box;
  232. /* 【重要】让页面本身不可滚动,所有滚动都交给scroll-view处理 */
  233. height: 100vh;
  234. overflow: hidden;
  235. display: flex;
  236. flex-direction: column;
  237. .top {
  238. p {
  239. font-family: PingFang-SC, PingFang-SC;
  240. font-weight: bold;
  241. font-size: 42rpx;
  242. color: #252525;
  243. line-height: 51rpx;
  244. text-align: center;
  245. margin-top: 48rpx;
  246. }
  247. .tip {
  248. font-family: PingFangSC, PingFang SC;
  249. font-weight: 400;
  250. font-size: 26rpx;
  251. color: #646464;
  252. line-height: 26rpx;
  253. text-align: center;
  254. margin-top: 36rpx;
  255. span {
  256. margin: 0 10rpx;
  257. }
  258. }
  259. }
  260. .list {
  261. width: 100%;
  262. /* flex: 1; 如果使用flex布局,可以这样自适应高度 */
  263. /* overflow-y: auto; scroll-view自带滚动,无需此属性 */
  264. margin-top: 28rpx;
  265. .l_item {
  266. margin-top: 20rpx;
  267. width: 100%;
  268. background: #ffffff;
  269. padding: 6rpx;
  270. box-sizing: border-box;
  271. &:first-child {
  272. margin-top: 0;
  273. }
  274. }
  275. }
  276. .bottom {
  277. width: calc(100% - 80rpx);
  278. height: 88rpx;
  279. position: fixed;
  280. left: 40rpx;
  281. bottom: 40rpx;
  282. gap: 20rpx;
  283. .zt_btn {
  284. flex: 1;
  285. margin: 0;
  286. &.secondary {
  287. background: #FFFFFF;
  288. border: 2rpx solid #33A7A7;
  289. color: #33A7A7;
  290. }
  291. &.disabled {
  292. opacity: .5;
  293. }
  294. }
  295. }
  296. }
  297. </style>