questionnaireFill.vue 15 KB

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