questionnaireFill.vue 15 KB

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