questionnaireFill.vue 14 KB

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