questionnaireFill.vue 7.7 KB

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