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