questionnaireFill.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <template>
  2. <view class="default_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='填写问卷'></cus-header>
  4. <view class="top">
  5. <view class="top-title">{{'GW+MC PREILL36测评题库版本'}}</view>
  6. <view class="top-progress adfac">
  7. <view class="top-progress-text">测评进度</view>
  8. <view class="top-progress-box">
  9. <view class="top-progress-box-current" :style="{'width':(answerNum/list.length*100)+'%'}"></view>
  10. </view>
  11. <view class="top-progress-num"><span>{{answerCount}}</span>/{{list.length}}</view>
  12. </view>
  13. </view>
  14. <view class="memo adfac" v-if="turnType==='questionnaire'" @click="noticeShow=true">
  15. <text>问卷答题说明</text>
  16. <image :src="imgBase+'icon_memo.png'"></image>
  17. </view>
  18. <scroll-view class="list" scroll-y="true" :scroll-top="scrollTop" :style="{ height: h - 203 - mt + 'px' }">
  19. <div v-if="isLoading" class="loading-container adfacjc">
  20. <div class="adfac">
  21. <u-loading-icon size="42"></u-loading-icon>
  22. <text style="margin-left: 10rpx; font-size: 34rpx; color: #666666">问卷加载中...</text>
  23. </div>
  24. </div>
  25. <template v-else>
  26. <view>
  27. <div class="l_item" v-for="(item, index) in list" :key="item.id" :id="'question-' + index">
  28. <question-item :item="item" :index="index" @change="handleAnswerChange"></question-item>
  29. </div>
  30. </view>
  31. </template>
  32. </scroll-view>
  33. <!-- <view class="list">
  34. <div class="l_item" v-for="(item, index) in list" :key="item.id" :id="'question-' + index">
  35. <question-item :item="item" :index="index" @change="handleAnswerChange"></<question-item>
  36. </div>
  37. </view> -->
  38. <view class="bottom adfacjb">
  39. <view class="bottom-left adffcac" @click="showTeamInfo" v-if="type==1">
  40. <image :src="imgBase+'questionnaire_info.png'"></image>
  41. <text>团队信息</text>
  42. </view>
  43. <view class="bottom-left adfac" v-else-if="turnType==='questionnaire'">
  44. <view class="bottom-left-pre adffcac" @click="showTeamInfo">
  45. <image :src="imgBase+'questionnaire_info.png'"></image>
  46. <text>团队信息</text>
  47. </view>
  48. <view class="bottom-left-pre adffcac" @click="userShow=true">
  49. <image :src="imgBase+'questionnaire_users.png'"></image>
  50. <text>团队人员</text>
  51. </view>
  52. </view>
  53. <view class="bottom-right" :class="{'questionnaire':turnType==='questionnaire'}" @click="confirmSubmit">提交问卷</view>
  54. </view>
  55. <cus-notice title="问卷答题说明" :show="noticeShow" :content="noticeContent"
  56. @close="noticeShow=false" @handleKnow="noticeShow=false"></cus-notice>
  57. <cus-team-info :show="teamShow" :teamInfo="teamInfo" @close="teamShow=false"></cus-team-info>
  58. <cus-team-user :show="userShow" :list="teamUserList" @close="userShow=false"></cus-team-user>
  59. </view>
  60. </template>
  61. <script>
  62. import QuestionItem from '@/components/QuestionItem/index3.vue'
  63. import CusNotice from '@/components/CusNotice/index.vue'
  64. import CusTeamInfo from '@/components/CusTeamInfo/index.vue'
  65. import CusTeamUser from '@/components/CusTeamUser/index.vue'
  66. export default {
  67. components:{ CusNotice, QuestionItem, CusTeamInfo, CusTeamUser },
  68. data(){
  69. return {
  70. type:'',
  71. turnType:'',
  72. questionnaireId:'',
  73. noticeShow:false,
  74. noticeContent:`问卷答题说明<br/><br/>问卷答题说明<br/><br/>问卷答题说明<br/><br/>问卷答题说明`,
  75. teamShow:false,
  76. userShow:false,
  77. teamUserList:[],
  78. teamQuestionnaireId: '',
  79. list: [],
  80. questionnaire: null,
  81. isLoading: true,
  82. isSubmitting: false,
  83. scrollTop: 0, // scroll-view 的滚动位置
  84. oldScrollTop: 0, // 辅助值,确保即使滚动到相同位置也能触发
  85. userAnswerTemp: [],
  86. teamId:'',
  87. teamInfo:{},
  88. teamScaleData:[],
  89. teamLevelData:[],
  90. answerCount:0,
  91. }
  92. },
  93. onLoad(options) {
  94. this.type = options.type;
  95. this.turnType = options.turnType;
  96. this.teamQuestionnaireId = options.teamQuestionnaireId;
  97. this.teamId = options.teamId;
  98. this.getList();
  99. },
  100. methods:{
  101. handleAnswerChange(e) {
  102. const item = this.list[e.index];
  103. if (item) {
  104. // 检查这道题在更新前是否已经回答完整
  105. const wasAnswered = item.answer.filter(a => a?.value).length === this.userAnswerTemp.length;
  106. let answer = JSON.parse(JSON.stringify(item.answer));
  107. answer.forEach(a => {
  108. a.value = a.assessmentMethod == e.assessmentMethod ? e.value : a.value
  109. });
  110. this.$set(item, 'answer', answer);
  111. // 检查这道题在更新后是否回答完整
  112. const isAnsweredNow = item.answer.filter(a => a?.value).length === this.userAnswerTemp.length;
  113. // 只有当题目从“未答完”变为“已答完”时,才增加计数
  114. if (!wasAnswered && isAnsweredNow) {
  115. this.answerCount++;
  116. }
  117. if (item.warn) {
  118. this.$set(item, 'warn', false);
  119. }
  120. }
  121. this.$nextTick(() => {
  122. this.setQuestionnaireCache();
  123. });
  124. },
  125. getList() {
  126. let questionnaire = null;
  127. try {
  128. const cacheStr = uni.getStorageSync('questionnaire');
  129. if (cacheStr) {
  130. questionnaire = JSON.parse(cacheStr);
  131. }
  132. } catch (e) {
  133. console.error('解析问卷缓存失败:', e);
  134. uni.removeStorageSync('questionnaire');
  135. }
  136. this.isLoading = true;
  137. this.$api
  138. .get('/core/team/member/answer/listByUser/' + this.teamQuestionnaireId)
  139. .then((res) => {
  140. if (res.data.code !== 0) return this.$showToast(res.data.msg);
  141. if(res.data.data.length===0) return this.$showToast('暂无问卷');
  142. const answerMap = new Map();
  143. if (questionnaire && this.teamQuestionnaireId == questionnaire.key) {
  144. questionnaire.list.forEach((q) => answerMap.set(q.id, q.answer));
  145. }
  146. this.userAnswerTemp = res.data.data[0].userAnswer.map(u=>{
  147. return {
  148. assessmentMethod:u.assessmentMethod,
  149. value:''
  150. }
  151. })
  152. let uaTemp = JSON.parse(JSON.stringify(this.userAnswerTemp))
  153. this.list = res.data.data.map(item => {
  154. let _a = answerMap.get(item.id);
  155. return {
  156. ...item,
  157. answer:_a?uaTemp.map(u=>{
  158. return {
  159. assessmentMethod:u.assessmentMethod,
  160. value:_a.find(a=>a.assessmentMethod==u.assessmentMethod)?.value||''
  161. }
  162. }):this.userAnswerTemp,
  163. warn: false
  164. }
  165. });
  166. this.list.forEach(l=>{
  167. let _a = answerMap.get(l.id);
  168. l.userAnswer.forEach(u=>{
  169. u.answer = _a?(_a.find(a=>a.assessmentMethod==u.assessmentMethod)?.value||''):''
  170. })
  171. })
  172. this.answerCount = this.list.filter(l => l?.answer.filter(a => a?.value).length == this.userAnswerTemp.length).length;
  173. this.isLoading = false;
  174. })
  175. .catch(() => {
  176. this.isLoading = false;
  177. this.$showToast('获取异常,请稍后重试');
  178. })
  179. .finally(() => {
  180. this.isLoading = false;
  181. });
  182. },
  183. scrollToQuestion(index) {
  184. const query = uni.createSelectorQuery().in(this);
  185. const targetId = `#question-${index}`;
  186. query.select(targetId).boundingClientRect();
  187. query.select('.list').scrollOffset(); // 获取 scroll-view 的滚动信息
  188. query.select('.list').boundingClientRect(); // 获取 scroll-view 自身的位置信息
  189. query.exec((res) => {
  190. // 增加安全校验
  191. if (!res || !res[0] || !res[1] || !res[2]) {
  192. return;
  193. }
  194. // res[0]: 目标元素 (#question-N) 的位置信息
  195. const itemRect = res[0];
  196. // res[1]: 滚动容器 (.list) 的滚动信息
  197. const scrollViewScroll = res[1];
  198. // res[2]: 滚动容器 (.list) 自身的位置信息
  199. const scrollViewRect = res[2];
  200. // 计算目标滚动位置 = 当前滚动距离 + (目标元素顶部 - 滚动容器顶部) - 预留间距
  201. const targetPosition = scrollViewScroll.scrollTop + itemRect.top - scrollViewRect.top - 10; // 减10px作为顶部留白
  202. // 通过先设置为旧值,再在 nextTick 中设置为新值的方式,确保滚动能够触发
  203. this.scrollTop = this.oldScrollTop;
  204. this.$nextTick(() => {
  205. this.scrollTop = targetPosition;
  206. this.oldScrollTop = targetPosition;
  207. });
  208. });
  209. },
  210. confirmSubmit() {
  211. if (this.isSubmitting) return;
  212. let firstUnansweredIndex = -1;
  213. this.list.forEach((l, i) => {
  214. const isAnswered = l.answer.filter(a => a?.value).length==this.userAnswerTemp.length?true:false;
  215. this.$set(l, 'warn', !isAnswered);
  216. if (!isAnswered && firstUnansweredIndex === -1) {
  217. firstUnansweredIndex = i;
  218. }
  219. });
  220. if (firstUnansweredIndex > -1) {
  221. uni.showModal({
  222. title: '提示',
  223. content: `第 ${firstUnansweredIndex + 1} 项未选择答案,请选择。`,
  224. showCancel: false,
  225. success: (res) => {
  226. if (res.confirm) {
  227. // 调用新的滚动方法
  228. this.scrollToQuestion(firstUnansweredIndex);
  229. }
  230. }
  231. });
  232. return; // 终止提交
  233. }
  234. const submitList = this.list.map((l) => {
  235. let {answer,userAnswer,warn,...other} = JSON.parse(JSON.stringify(l));
  236. userAnswer.forEach(u=>{
  237. u.questionOption.forEach(q=>{
  238. q.userAnswer=answer.find(a=>a.assessmentMethod==u.assessmentMethod)?.value==q.questionOption;
  239. })
  240. })
  241. return {
  242. ...other,
  243. isAnswer: '1',
  244. userAnswer
  245. };
  246. });
  247. this.isSubmitting = true;
  248. this.$api
  249. .post('/core/team/member/answer/submit', submitList)
  250. .then((res) => {
  251. if (res.data.code !== 0) {
  252. this.isSubmitting = false;
  253. return this.$showToast(res.data.msg);
  254. }
  255. uni.removeStorageSync('questionnaire');
  256. let url = '/pagesPublish/questionnaireResult'
  257. if(this.type == 2) url = '/pagesPublish/submitResult'
  258. uni.navigateTo({ url })
  259. })
  260. .catch((err) => {
  261. this.$showToast('网络异常,请稍后重试');
  262. })
  263. .finally(() => {
  264. this.isSubmitting = false;
  265. });
  266. },
  267. setQuestionnaireCache() {
  268. const answeredList = this.list.filter((l) => l.answer.filter(a => a?.value).length>0).map((l) => ({ id: l.id, answer: l.answer }));
  269. if (answeredList.length === 0) {
  270. uni.removeStorageSync('questionnaire');
  271. return;
  272. }
  273. const qinfo = {
  274. key: this.teamQuestionnaireId,
  275. list: answeredList
  276. };
  277. uni.setStorageSync('questionnaire', JSON.stringify(qinfo));
  278. },
  279. async getTeamScaleData(){
  280. return new Promise((resolve,reject)=>{
  281. this.$api.get('/getListByType/team_scale').then(({data:res})=>{
  282. if(res.code!==0) return this.$showToast(res.msg)
  283. this.teamScaleData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  284. resolve()
  285. })
  286. })
  287. },
  288. async getTeamHierarchyData(){
  289. return new Promise((resolve,reject)=>{
  290. this.$api.get('/getListByType/team_hierarchy').then(({data:res})=>{
  291. if(res.code!==0) return this.$showToast(res.msg)
  292. this.teamLevelData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  293. resolve()
  294. })
  295. })
  296. },
  297. async showTeamInfo(){
  298. await this.getTeamScaleData()
  299. await this.getTeamHierarchyData()
  300. this.$api.get(`/core/user/team/${this.teamId}`).then(({data:res})=>{
  301. if(res.code!==0) return this.$showToast(res.msg)
  302. this.teamInfo = res.data;
  303. this.teamInfo.functionsName = res.data.functions.map(f=>f.functionName).join('、');
  304. this.teamInfo.organizationsName = res.data.organizations.map(f=>f.orgName).join('、');
  305. this.teamInfo.address = res.data.provinceName+res.data.cityName;
  306. this.teamInfo.scaleName = this.teamScaleData.find(d=>d.id==res.data.scale).name;
  307. this.teamInfo.hierarchyName = this.teamLevelData.find(d=>d.id==res.data.hierarchy).name;
  308. this.teamShow = true;
  309. })
  310. }
  311. }
  312. }
  313. </script>
  314. <style scoped lang="scss">
  315. .loading-container {
  316. width: 100%;
  317. height: 100%;
  318. }
  319. .default_page{
  320. padding: 0 0 192rpx;
  321. background: #FFFFFF;
  322. box-sizing: border-box;
  323. height: 100vh;
  324. overflow: hidden;
  325. display: flex;
  326. flex-direction: column;
  327. .top{
  328. padding: 40rpx 24rpx 50rpx;
  329. &-title{
  330. font-family: PingFang-SC, PingFang-SC;
  331. font-weight: bold;
  332. font-size: 40rpx;
  333. color: #002846;
  334. line-height: 51rpx;
  335. }
  336. &-progress{
  337. margin-top: 35rpx;
  338. &-text{
  339. font-family: PingFangSC, PingFang SC;
  340. font-weight: 400;
  341. font-size: 30rpx;
  342. color: #002846;
  343. line-height: 30rpx;
  344. }
  345. &-box{
  346. flex: 1;
  347. width: 100%;
  348. height: 14rpx;
  349. margin: 0 20rpx;
  350. background: #F0F2F8;
  351. border-radius: 8rpx;
  352. position: relative;
  353. &-current{
  354. height: 14rpx;
  355. background: #FFD750;
  356. border-radius: 8rpx;
  357. position: absolute;
  358. left: 0;
  359. top: 0;
  360. }
  361. }
  362. &-num{
  363. font-family: PingFangSC, PingFang SC;
  364. font-weight: 400;
  365. font-size: 26rpx;
  366. color: #667E90;
  367. line-height: 26rpx;
  368. span{
  369. font-size: 36rpx;
  370. color: #199C9C;
  371. line-height: 36rpx;
  372. }
  373. }
  374. }
  375. }
  376. .memo{
  377. margin: -20rpx 0 50rpx 24rpx;
  378. text{
  379. font-family: PingFangSC, PingFang SC;
  380. font-weight: 400;
  381. font-size: 24rpx;
  382. color: #536387;
  383. line-height: 30rpx;
  384. }
  385. image{
  386. width: 24rpx;
  387. height: 24rpx;
  388. margin-left: 9rpx;
  389. }
  390. }
  391. .list{
  392. width: 100%;
  393. padding: 0 24rpx;
  394. box-sizing: border-box;
  395. // flex: 1;
  396. // overflow-y: auto;
  397. }
  398. .bottom{
  399. width: 100%;
  400. background: #FFFFFF;
  401. box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(0,0,0,0.07);
  402. padding: 20rpx 40rpx 54rpx;
  403. box-sizing: border-box;
  404. position: fixed;
  405. left: 0;
  406. bottom: 0;
  407. z-index: 1000;
  408. &-left{
  409. image{
  410. width: 42rpx;
  411. height: 42rpx;
  412. }
  413. text{
  414. font-family: PingFangSC, PingFang SC;
  415. font-weight: 400;
  416. font-size: 24rpx;
  417. color: #536387;
  418. line-height: 40rpx;
  419. margin-top: 2rpx;
  420. }
  421. &-pre{
  422. &:last-child{
  423. margin-left: 40rpx;
  424. }
  425. }
  426. }
  427. &-right{
  428. width: 448rpx;
  429. height: 88rpx;
  430. background: linear-gradient( 90deg, #33A7A7 0%, #64BBBB 100%);;
  431. border-radius: 44rpx;
  432. font-family: PingFang-SC, PingFang-SC;
  433. font-weight: bold;
  434. font-size: 32rpx;
  435. color: #FFFFFF;
  436. line-height: 88rpx;
  437. text-align: center;
  438. letter-spacing: 2rpx;
  439. &.questionnaire{
  440. width: 383rpx;
  441. }
  442. }
  443. }
  444. }
  445. </style>