questionnaireFill.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. let that = this;
  247. uni.showModal({
  248. title:'温馨提示',
  249. content:'是否确认提交问卷?提交后不可修改。',
  250. success: (res) => {
  251. if(res.confirm){
  252. that.isSubmitting = true;
  253. that.$api
  254. .post('/core/team/member/answer/submit', submitList)
  255. .then((res) => {
  256. if (res.data.code !== 0) {
  257. that.isSubmitting = false;
  258. return that.$showToast(res.data.msg);
  259. }
  260. uni.removeStorageSync('questionnaire');
  261. let url = '/pagesPublish/questionnaireResult'
  262. if(that.type == 2) url = '/pagesPublish/submitResult'
  263. uni.navigateTo({ url })
  264. })
  265. .catch((err) => {
  266. that.$showToast('网络异常,请稍后重试');
  267. })
  268. .finally(() => {
  269. that.isSubmitting = false;
  270. });
  271. }
  272. }
  273. })
  274. },
  275. setQuestionnaireCache() {
  276. const answeredList = this.list.filter((l) => l.answer.filter(a => a?.value).length>0).map((l) => ({ id: l.id, answer: l.answer }));
  277. if (answeredList.length === 0) {
  278. uni.removeStorageSync('questionnaire');
  279. return;
  280. }
  281. const qinfo = {
  282. key: this.teamQuestionnaireId,
  283. list: answeredList
  284. };
  285. uni.setStorageSync('questionnaire', JSON.stringify(qinfo));
  286. },
  287. async getTeamScaleData(){
  288. return new Promise((resolve,reject)=>{
  289. this.$api.get('/getListByType/team_scale').then(({data:res})=>{
  290. if(res.code!==0) return this.$showToast(res.msg)
  291. this.teamScaleData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  292. resolve()
  293. })
  294. })
  295. },
  296. async getTeamHierarchyData(){
  297. return new Promise((resolve,reject)=>{
  298. this.$api.get('/getListByType/team_hierarchy').then(({data:res})=>{
  299. if(res.code!==0) return this.$showToast(res.msg)
  300. this.teamLevelData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  301. resolve()
  302. })
  303. })
  304. },
  305. async showTeamInfo(){
  306. await this.getTeamScaleData()
  307. await this.getTeamHierarchyData()
  308. this.$api.get(`/core/user/team/${this.teamId}`).then(({data:res})=>{
  309. if(res.code!==0) return this.$showToast(res.msg)
  310. this.teamInfo = res.data;
  311. this.teamInfo.functionsName = res.data.functions.map(f=>f.functionName).join('、');
  312. this.teamInfo.organizationsName = res.data.organizations.map(f=>f.orgName).join('、');
  313. this.teamInfo.address = res.data.provinceName+res.data.cityName;
  314. this.teamInfo.scaleName = this.teamScaleData.find(d=>d.id==res.data.scale).name;
  315. this.teamInfo.hierarchyName = this.teamLevelData.find(d=>d.id==res.data.hierarchy).name;
  316. this.teamShow = true;
  317. })
  318. }
  319. }
  320. }
  321. </script>
  322. <style scoped lang="scss">
  323. .loading-container {
  324. width: 100%;
  325. height: 100%;
  326. }
  327. .default_page{
  328. padding: 0 0 192rpx;
  329. background: #FFFFFF;
  330. box-sizing: border-box;
  331. height: 100vh;
  332. overflow: hidden;
  333. display: flex;
  334. flex-direction: column;
  335. .top{
  336. padding: 40rpx 24rpx 50rpx;
  337. &-title{
  338. font-family: PingFang-SC, PingFang-SC;
  339. font-weight: bold;
  340. font-size: 40rpx;
  341. color: #002846;
  342. line-height: 51rpx;
  343. }
  344. &-progress{
  345. margin-top: 35rpx;
  346. &-text{
  347. font-family: PingFangSC, PingFang SC;
  348. font-weight: 400;
  349. font-size: 30rpx;
  350. color: #002846;
  351. line-height: 30rpx;
  352. }
  353. &-box{
  354. flex: 1;
  355. width: 100%;
  356. height: 14rpx;
  357. margin: 0 20rpx;
  358. background: #F0F2F8;
  359. border-radius: 8rpx;
  360. position: relative;
  361. &-current{
  362. height: 14rpx;
  363. background: #FFD750;
  364. border-radius: 8rpx;
  365. position: absolute;
  366. left: 0;
  367. top: 0;
  368. }
  369. }
  370. &-num{
  371. font-family: PingFangSC, PingFang SC;
  372. font-weight: 400;
  373. font-size: 26rpx;
  374. color: #667E90;
  375. line-height: 26rpx;
  376. span{
  377. font-size: 36rpx;
  378. color: #199C9C;
  379. line-height: 36rpx;
  380. }
  381. }
  382. }
  383. }
  384. .memo{
  385. margin: -20rpx 0 50rpx 24rpx;
  386. text{
  387. font-family: PingFangSC, PingFang SC;
  388. font-weight: 400;
  389. font-size: 24rpx;
  390. color: #536387;
  391. line-height: 30rpx;
  392. }
  393. image{
  394. width: 24rpx;
  395. height: 24rpx;
  396. margin-left: 9rpx;
  397. }
  398. }
  399. .list{
  400. width: 100%;
  401. padding: 0 24rpx 20rpx;
  402. box-sizing: border-box;
  403. // flex: 1;
  404. // overflow-y: auto;
  405. }
  406. .bottom{
  407. width: 100%;
  408. background: #FFFFFF;
  409. box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(0,0,0,0.07);
  410. padding: 20rpx 40rpx 54rpx;
  411. box-sizing: border-box;
  412. position: fixed;
  413. left: 0;
  414. bottom: 0;
  415. z-index: 1000;
  416. &-left{
  417. image{
  418. width: 42rpx;
  419. height: 42rpx;
  420. }
  421. text{
  422. font-family: PingFangSC, PingFang SC;
  423. font-weight: 400;
  424. font-size: 24rpx;
  425. color: #536387;
  426. line-height: 40rpx;
  427. margin-top: 2rpx;
  428. }
  429. &-pre{
  430. &:last-child{
  431. margin-left: 40rpx;
  432. }
  433. }
  434. }
  435. &-right{
  436. width: 448rpx;
  437. height: 88rpx;
  438. background: linear-gradient( 90deg, #33A7A7 0%, #64BBBB 100%);;
  439. border-radius: 44rpx;
  440. font-family: PingFang-SC, PingFang-SC;
  441. font-weight: bold;
  442. font-size: 32rpx;
  443. color: #FFFFFF;
  444. line-height: 88rpx;
  445. text-align: center;
  446. letter-spacing: 2rpx;
  447. &.questionnaire{
  448. width: 383rpx;
  449. }
  450. }
  451. }
  452. }
  453. </style>