questionnaireFill.vue 15 KB

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