questionnaireFill.vue 16 KB

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