questionnaireFill.vue 7.8 KB

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