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