questionnaireFill.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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">{{title||''}}</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>{{currentIndex + 1}}</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">
  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 v-if="currentQuestion">
  27. <view class="l_item" :id="'question-' + currentIndex">
  28. <question-item :item="currentQuestion" :index="currentIndex" @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" v-if="turnType==='questionnaire'" @click="showTeamUser">
  40. <image :src="imgBase+'questionnaire_users.png'"></image>
  41. <text>团队人员</text>
  42. </view>
  43. <view class="question-nav adfacjb">
  44. <view class="question-nav-btn secondary" :class="{disabled: currentIndex === 0 || isSubmitting}" @click="previousQuestion">上一题</view>
  45. <view class="question-nav-btn primary" :class="{disabled: isSubmitting}" @click="nextQuestion">
  46. {{currentIndex === list.length - 1 ? (isSubmitting ? '提交中...' : '提交问卷') : '下一题'}}
  47. </view>
  48. </view>
  49. </view>
  50. <cus-notice :show="noticeShow" @close="noticeShow=false" @handleKnow="noticeShow=false"></cus-notice>
  51. <cus-team-info :show="teamShow" :teamInfo="teamInfo" @close="teamShow=false"></cus-team-info>
  52. <cus-team-user :show="userShow" :canEdit="false" :list="teamUserList" @close="userShow=false"></cus-team-user>
  53. </view>
  54. </template>
  55. <script>
  56. import QuestionItem from '@/components/QuestionItem/index3.vue'
  57. import CusNotice from '@/components/CusNotice/index2.vue'
  58. import CusTeamInfo from '@/components/CusTeamInfo/index.vue'
  59. import CusTeamUser from '@/components/CusTeamUser/index.vue'
  60. const { validateFutureExpectation, normalizeFutureExpectationOptions } = require('@/utils/perillScore.js')
  61. export default {
  62. components:{ CusNotice, QuestionItem, CusTeamInfo, CusTeamUser },
  63. data(){
  64. return {
  65. title:'',
  66. type:'',
  67. turnType:'',
  68. questionnaireId:'',
  69. noticeShow:false,
  70. teamShow:false,
  71. userShow:false,
  72. teamUserList:[],
  73. teamQuestionnaireId: '',
  74. list: [],
  75. currentIndex: 0,
  76. questionnaire: null,
  77. isLoading: true,
  78. isSubmitting: false,
  79. scrollTop: 0, // scroll-view 的滚动位置
  80. oldScrollTop: 0, // 辅助值,确保即使滚动到相同位置也能触发
  81. userAnswerTemp: [],
  82. teamId:'',
  83. teamInfo:{},
  84. teamScaleData:[],
  85. teamLevelData:[],
  86. answerCount:0,
  87. categoryData:[]
  88. }
  89. },
  90. onLoad(options) {
  91. this.title = this.decodeTitle(options.title).replace(/PERILL团队发展动态评估/g, 'PERILL倍力团队六维评估');
  92. this.type = options.type;
  93. this.turnType = options.turnType;
  94. this.teamQuestionnaireId = options.teamQuestionnaireId;
  95. this.teamId = options.teamId;
  96. this.getList();
  97. },
  98. computed: {
  99. currentQuestion() {
  100. return this.list[this.currentIndex] || null;
  101. }
  102. },
  103. methods:{
  104. decodeTitle(title) {
  105. let decoded = String(title || '')
  106. for (let i = 0; i < 5; i += 1) {
  107. try {
  108. const next = decodeURIComponent(decoded)
  109. if (next === decoded) break
  110. decoded = next
  111. } catch (error) {
  112. break
  113. }
  114. }
  115. return decoded || 'PERILL倍力团队六维评估'
  116. },
  117. handleAnswerChange(e) {
  118. const item = this.list[e.index];
  119. if (item) {
  120. // 检查这道题在更新前是否已经回答完整
  121. const wasAnswered = item.answer.filter(a => a?.value).length === this.userAnswerTemp.length;
  122. let answer = JSON.parse(JSON.stringify(item.answer));
  123. answer.forEach(a => {
  124. a.value = a.assessmentMethod == e.assessmentMethod ? e.value : a.value
  125. });
  126. this.$set(item, 'answer', answer);
  127. const userAnswer = JSON.parse(JSON.stringify(item.userAnswer));
  128. userAnswer.forEach(method => {
  129. if (method.assessmentMethod != e.assessmentMethod) return;
  130. method.answer = e.value;
  131. method.questionOption.forEach(option => {
  132. option.userAnswer = option.questionOption === e.value;
  133. });
  134. });
  135. this.$set(item, 'userAnswer', userAnswer);
  136. // 检查这道题在更新后是否回答完整
  137. const isAnsweredNow = item.answer.filter(a => a?.value).length === this.userAnswerTemp.length;
  138. // 只有当题目从“未答完”变为“已答完”时,才增加计数
  139. if (!wasAnswered && isAnsweredNow) {
  140. this.answerCount++;
  141. }
  142. if (item.warn) {
  143. this.$set(item, 'warn', false);
  144. }
  145. }
  146. this.$nextTick(() => {
  147. this.setQuestionnaireCache();
  148. });
  149. },
  150. getList() {
  151. let questionnaire = null;
  152. try {
  153. const cacheStr = uni.getStorageSync('questionnaire');
  154. if (cacheStr) {
  155. questionnaire = JSON.parse(cacheStr);
  156. }
  157. } catch (e) {
  158. console.error('解析问卷缓存失败:', e);
  159. uni.removeStorageSync('questionnaire');
  160. }
  161. this.isLoading = true;
  162. this.$api
  163. .get('/core/team/member/answer/listByUser/' + this.teamQuestionnaireId)
  164. .then((res) => {
  165. if (res.data.code !== 0) return this.$showToast(res.data.msg);
  166. if(res.data.data.length===0) return this.$showToast('暂无问卷');
  167. const answerMap = new Map();
  168. if (questionnaire && this.teamQuestionnaireId == questionnaire.key) {
  169. questionnaire.list.forEach((q) => answerMap.set(q.id, q.answer));
  170. }
  171. const questionItems = res.data.data.map(item => Object.assign({}, item, {
  172. userAnswer: normalizeFutureExpectationOptions(item.userAnswer)
  173. }));
  174. this.userAnswerTemp = questionItems[0].userAnswer.map(u=>{
  175. return {
  176. assessmentMethod:u.assessmentMethod,
  177. value:''
  178. }
  179. })
  180. let uaTemp = JSON.parse(JSON.stringify(this.userAnswerTemp))
  181. this.list = questionItems.map(item => {
  182. let _a = answerMap.get(item.id);
  183. return {
  184. ...item,
  185. answer:_a?uaTemp.map(u=>{
  186. return {
  187. assessmentMethod:u.assessmentMethod,
  188. value:_a.find(a=>a.assessmentMethod==u.assessmentMethod)?.value||''
  189. }
  190. }):this.userAnswerTemp,
  191. warn: false
  192. }
  193. });
  194. this.list.forEach(l=>{
  195. let _a = answerMap.get(l.id);
  196. l.userAnswer.forEach(u=>{
  197. u.answer = _a?(_a.find(a=>a.assessmentMethod==u.assessmentMethod)?.value||''):''
  198. })
  199. })
  200. this.answerCount = this.list.filter(l => l?.answer.filter(a => a?.value).length == this.userAnswerTemp.length).length;
  201. const firstUnansweredIndex = this.list.findIndex(l => l.answer.filter(a => a?.value).length !== this.userAnswerTemp.length)
  202. this.currentIndex = firstUnansweredIndex === -1 ? 0 : firstUnansweredIndex;
  203. this.isLoading = false;
  204. })
  205. .catch(() => {
  206. this.isLoading = false;
  207. this.$showToast('获取异常,请稍后重试');
  208. })
  209. .finally(() => {
  210. this.isLoading = false;
  211. });
  212. },
  213. scrollToQuestion(index) {
  214. const query = uni.createSelectorQuery().in(this);
  215. const targetId = `#question-${index}`;
  216. query.select(targetId).boundingClientRect();
  217. query.select('.list').scrollOffset(); // 获取 scroll-view 的滚动信息
  218. query.select('.list').boundingClientRect(); // 获取 scroll-view 自身的位置信息
  219. query.exec((res) => {
  220. // 增加安全校验
  221. if (!res || !res[0] || !res[1] || !res[2]) {
  222. return;
  223. }
  224. // res[0]: 目标元素 (#question-N) 的位置信息
  225. const itemRect = res[0];
  226. // res[1]: 滚动容器 (.list) 的滚动信息
  227. const scrollViewScroll = res[1];
  228. // res[2]: 滚动容器 (.list) 自身的位置信息
  229. const scrollViewRect = res[2];
  230. // 计算目标滚动位置 = 当前滚动距离 + (目标元素顶部 - 滚动容器顶部) - 预留间距
  231. const targetPosition = scrollViewScroll.scrollTop + itemRect.top - scrollViewRect.top - 10; // 减10px作为顶部留白
  232. // 通过先设置为旧值,再在 nextTick 中设置为新值的方式,确保滚动能够触发
  233. this.scrollTop = this.oldScrollTop;
  234. this.$nextTick(() => {
  235. this.scrollTop = targetPosition;
  236. this.oldScrollTop = targetPosition;
  237. });
  238. });
  239. },
  240. async getUserCategoryData(){
  241. return new Promise((resolve,reject)=>{
  242. this.$api.get('/getListByType/UserCategory').then(({data:res})=>{
  243. if(res.code!==0) return this.$showToast(res.msg)
  244. this.categoryData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  245. resolve()
  246. })
  247. })
  248. },
  249. async showTeamUser(){
  250. await this.getUserCategoryData()
  251. this.$api.get(`/core/member/listByQueTeamId/${this.teamQuestionnaireId}`).then(({data:res})=>{
  252. if(res.code!==0) return this.$showToast(res.msg)
  253. this.teamUserList = res.data;
  254. this.teamUserList.forEach(l=>{
  255. l.emailTM = this.$reg.desensitizeContent(l.email);
  256. l.mobileTM = this.$reg.desensitizeContent(l.mobile,2);
  257. l.categoryName = this.categoryData.find(c=>c.id==l.category)?.name;
  258. })
  259. this.userShow = true
  260. })
  261. },
  262. previousQuestion() {
  263. if (this.currentIndex === 0 || this.isSubmitting) return;
  264. this.currentIndex--;
  265. this.scrollTop = 0;
  266. },
  267. nextQuestion() {
  268. if (this.isSubmitting || !this.currentQuestion) return;
  269. const current = this.currentQuestion;
  270. const answerError = current.answer.filter(a => a?.value).length !== this.userAnswerTemp.length;
  271. this.$set(current, 'warn', answerError);
  272. if (answerError) {
  273. this.$showToast(`第 ${this.currentIndex + 1} 项未选择完整答案,请选择。`);
  274. return;
  275. }
  276. const expectationError = validateFutureExpectation(current.userAnswer);
  277. this.$set(current, 'warn', !!expectationError);
  278. if (expectationError) {
  279. this.$showToast('一年后期待分不得低于现状评分,请注意!');
  280. return;
  281. }
  282. if (this.currentIndex < this.list.length - 1) {
  283. this.currentIndex++;
  284. this.scrollTop = 0;
  285. return;
  286. }
  287. this.confirmSubmit();
  288. },
  289. confirmSubmit() {
  290. if (this.isSubmitting) return;
  291. let firstUnansweredIndex = -1;
  292. this.list.forEach((l, i) => {
  293. const isAnswered = l.answer.filter(a => a?.value).length==this.userAnswerTemp.length?true:false;
  294. this.$set(l, 'warn', !isAnswered);
  295. if (!isAnswered && firstUnansweredIndex === -1) {
  296. firstUnansweredIndex = i;
  297. }
  298. });
  299. if (firstUnansweredIndex > -1) {
  300. this.currentIndex = firstUnansweredIndex;
  301. this.scrollTop = 0;
  302. this.$showToast(`第 ${firstUnansweredIndex + 1} 项未选择完整答案,请选择。`);
  303. return; // 终止提交
  304. }
  305. let firstInvalidExpectationIndex = -1;
  306. this.list.forEach((l, i) => {
  307. const error = validateFutureExpectation(l.userAnswer);
  308. this.$set(l, 'warn', !!error);
  309. if (error && firstInvalidExpectationIndex === -1) firstInvalidExpectationIndex = i;
  310. });
  311. if (firstInvalidExpectationIndex > -1) {
  312. this.currentIndex = firstInvalidExpectationIndex;
  313. this.scrollTop = 0;
  314. this.$showToast('一年后期待分不得低于现状评分,请注意!');
  315. return;
  316. }
  317. const submitList = this.list.map((l) => {
  318. let {answer,userAnswer,warn,...other} = JSON.parse(JSON.stringify(l));
  319. userAnswer.forEach(u=>{
  320. u.questionOption.forEach(q=>{
  321. q.userAnswer=answer.find(a=>a.assessmentMethod==u.assessmentMethod)?.value==q.questionOption;
  322. })
  323. })
  324. return {
  325. ...other,
  326. isAnswer: '1',
  327. userAnswer
  328. };
  329. });
  330. let that = this;
  331. uni.showModal({
  332. title:'温馨提示',
  333. content:'是否确认提交问卷?提交后不可修改。',
  334. success: (res) => {
  335. if(res.confirm){
  336. that.isSubmitting = true;
  337. that.$api
  338. .post('/core/team/member/answer/submit', submitList)
  339. .then((res) => {
  340. if (res.data.code !== 0) {
  341. that.isSubmitting = false;
  342. return that.$showToast(res.data.msg);
  343. }
  344. uni.removeStorageSync('questionnaire');
  345. let url = '/pagesPublish/questionnaireResult'
  346. if(that.type == 2) url = '/pagesPublish/submitResult'
  347. uni.navigateTo({ url })
  348. })
  349. .catch((err) => {
  350. that.$showToast('网络异常,请稍后重试');
  351. })
  352. .finally(() => {
  353. that.isSubmitting = false;
  354. });
  355. }
  356. }
  357. })
  358. },
  359. setQuestionnaireCache() {
  360. const answeredList = this.list.filter((l) => l.answer.filter(a => a?.value).length>0).map((l) => ({ id: l.id, answer: l.answer }));
  361. if (answeredList.length === 0) {
  362. uni.removeStorageSync('questionnaire');
  363. return;
  364. }
  365. const qinfo = {
  366. key: this.teamQuestionnaireId,
  367. list: answeredList
  368. };
  369. uni.setStorageSync('questionnaire', JSON.stringify(qinfo));
  370. },
  371. async getTeamScaleData(){
  372. return new Promise((resolve,reject)=>{
  373. this.$api.get('/getListByType/team_scale').then(({data:res})=>{
  374. if(res.code!==0) return this.$showToast(res.msg)
  375. this.teamScaleData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  376. resolve()
  377. })
  378. })
  379. },
  380. async getTeamHierarchyData(){
  381. return new Promise((resolve,reject)=>{
  382. this.$api.get('/getListByType/team_hierarchy').then(({data:res})=>{
  383. if(res.code!==0) return this.$showToast(res.msg)
  384. this.teamLevelData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  385. resolve()
  386. })
  387. })
  388. },
  389. async showTeamInfo(){
  390. await this.getTeamScaleData()
  391. await this.getTeamHierarchyData()
  392. this.$api.get(`/core/user/team/${this.teamId}`).then(({data:res})=>{
  393. if(res.code!==0) return this.$showToast(res.msg)
  394. this.teamInfo = res.data;
  395. this.teamInfo.functionsName = res.data.functions.map(f=>f.functionName).join('、');
  396. this.teamInfo.organizationsName = res.data.organizations.map(f=>f.orgName).join('、');
  397. this.teamInfo.address = res.data.provinceName+res.data.cityName;
  398. this.teamInfo.scaleName = this.teamScaleData.find(d=>d.id==res.data.scale)?this.teamScaleData.find(d=>d.id==res.data.scale).name:'';
  399. this.teamInfo.hierarchyName = this.teamLevelData.find(d=>d.id==res.data.hierarchy)?this.teamLevelData.find(d=>d.id==res.data.hierarchy).name:'';
  400. this.teamShow = true;
  401. })
  402. }
  403. }
  404. }
  405. </script>
  406. <style scoped lang="scss">
  407. .question-nav {
  408. flex: 1;
  409. gap: 20rpx;
  410. .question-nav-btn {
  411. flex: 1;
  412. height: 88rpx;
  413. border-radius: 44rpx;
  414. line-height: 88rpx;
  415. text-align: center;
  416. font-size: 30rpx;
  417. font-weight: bold;
  418. &.primary {
  419. background: linear-gradient(90deg, #33A7A7 0%, #64BBBB 100%);
  420. color: #FFFFFF;
  421. }
  422. &.secondary {
  423. background: rgba(25, 156, 156, 0.06);
  424. border: 2rpx solid #33A7A7;
  425. color: #009191;
  426. }
  427. &.disabled {
  428. opacity: .5;
  429. }
  430. }
  431. }
  432. </style>
  433. <style scoped lang="scss">
  434. .loading-container {
  435. width: 100%;
  436. height: 100%;
  437. }
  438. .default_page{
  439. padding: 0 0 40rpx;
  440. background: #FFFFFF;
  441. box-sizing: border-box;
  442. height: 100vh;
  443. overflow: hidden;
  444. display: flex;
  445. flex-direction: column;
  446. .top{
  447. padding: 30rpx 24rpx 20rpx;
  448. &-title{
  449. font-family: PingFang-SC, PingFang-SC;
  450. font-weight: bold;
  451. font-size: 40rpx;
  452. color: #002846;
  453. line-height: 51rpx;
  454. }
  455. &-progress{
  456. margin-top: 25rpx;
  457. &-text{
  458. font-family: PingFangSC, PingFang SC;
  459. font-weight: 400;
  460. font-size: 30rpx;
  461. color: #002846;
  462. line-height: 30rpx;
  463. }
  464. &-box{
  465. flex: 1;
  466. width: 100%;
  467. height: 14rpx;
  468. margin: 0 20rpx;
  469. background: #F0F2F8;
  470. border-radius: 8rpx;
  471. position: relative;
  472. &-current{
  473. height: 14rpx;
  474. background: #FFD750;
  475. border-radius: 8rpx;
  476. position: absolute;
  477. left: 0;
  478. top: 0;
  479. }
  480. }
  481. &-num{
  482. font-family: PingFangSC, PingFang SC;
  483. font-weight: 400;
  484. font-size: 26rpx;
  485. color: #667E90;
  486. line-height: 26rpx;
  487. span{
  488. font-size: 36rpx;
  489. color: #199C9C;
  490. line-height: 36rpx;
  491. }
  492. }
  493. }
  494. }
  495. .memo{
  496. margin: -20rpx 0 30rpx 24rpx;
  497. text{
  498. font-family: PingFangSC, PingFang SC;
  499. font-weight: 400;
  500. font-size: 24rpx;
  501. color: #536387;
  502. line-height: 30rpx;
  503. }
  504. image{
  505. width: 24rpx;
  506. height: 24rpx;
  507. margin-left: 9rpx;
  508. }
  509. }
  510. .list{
  511. width: 100%;
  512. padding: 0 24rpx 20rpx;
  513. box-sizing: border-box;
  514. flex: 1;
  515. overflow-y: auto;
  516. }
  517. .bottom{
  518. width: 100%;
  519. background: #FFFFFF;
  520. box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(0,0,0,0.07);
  521. padding: 20rpx 40rpx 24rpx;
  522. box-sizing: border-box;
  523. &-left{
  524. image{
  525. width: 42rpx;
  526. height: 42rpx;
  527. }
  528. text{
  529. font-family: PingFangSC, PingFang SC;
  530. font-weight: 400;
  531. font-size: 24rpx;
  532. color: #536387;
  533. line-height: 40rpx;
  534. margin-top: 2rpx;
  535. }
  536. &-pre{
  537. &:last-child{
  538. margin-left: 40rpx;
  539. }
  540. }
  541. }
  542. &-right{
  543. width: 448rpx;
  544. height: 88rpx;
  545. background: linear-gradient( 90deg, #33A7A7 0%, #64BBBB 100%);;
  546. border-radius: 44rpx;
  547. font-family: PingFang-SC, PingFang-SC;
  548. font-weight: bold;
  549. font-size: 32rpx;
  550. color: #FFFFFF;
  551. line-height: 88rpx;
  552. text-align: center;
  553. letter-spacing: 2rpx;
  554. &.questionnaire{
  555. margin-left: 40rpx;
  556. flex: 1;
  557. }
  558. }
  559. }
  560. }
  561. </style>