questionnaireFill.vue 14 KB

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