questionnaireFill.vue 16 KB

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