questionnaireFill2.vue 8.3 KB

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