questionnaireFill.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. 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. })
  167. .catch(() => {
  168. this.isLoading = false;
  169. this.$showToast('获取异常,请稍后重试');
  170. })
  171. .finally(() => {
  172. this.isLoading = false;
  173. });
  174. },
  175. scrollToQuestion(index) {
  176. const query = uni.createSelectorQuery().in(this);
  177. const targetId = `#question-${index}`;
  178. query.select(targetId).boundingClientRect();
  179. query.select('.list').scrollOffset(); // 获取 scroll-view 的滚动信息
  180. query.select('.list').boundingClientRect(); // 获取 scroll-view 自身的位置信息
  181. query.exec((res) => {
  182. // 增加安全校验
  183. if (!res || !res[0] || !res[1] || !res[2]) {
  184. return;
  185. }
  186. // res[0]: 目标元素 (#question-N) 的位置信息
  187. const itemRect = res[0];
  188. // res[1]: 滚动容器 (.list) 的滚动信息
  189. const scrollViewScroll = res[1];
  190. // res[2]: 滚动容器 (.list) 自身的位置信息
  191. const scrollViewRect = res[2];
  192. // 计算目标滚动位置 = 当前滚动距离 + (目标元素顶部 - 滚动容器顶部) - 预留间距
  193. const targetPosition = scrollViewScroll.scrollTop + itemRect.top - scrollViewRect.top - 10; // 减10px作为顶部留白
  194. // 通过先设置为旧值,再在 nextTick 中设置为新值的方式,确保滚动能够触发
  195. this.scrollTop = this.oldScrollTop;
  196. this.$nextTick(() => {
  197. this.scrollTop = targetPosition;
  198. this.oldScrollTop = targetPosition;
  199. });
  200. });
  201. },
  202. confirmSubmit() {
  203. if (this.isSubmitting) return;
  204. let firstUnansweredIndex = -1;
  205. this.list.forEach((l, i) => {
  206. const isAnswered = l.answer.filter(a => a?.value).length==this.userAnswerTemp.length?true:false;
  207. this.$set(l, 'warn', !isAnswered);
  208. if (!isAnswered && firstUnansweredIndex === -1) {
  209. firstUnansweredIndex = i;
  210. }
  211. });
  212. if (firstUnansweredIndex > -1) {
  213. uni.showModal({
  214. title: '提示',
  215. content: `第 ${firstUnansweredIndex + 1} 项未选择答案,请选择。`,
  216. showCancel: false,
  217. success: (res) => {
  218. if (res.confirm) {
  219. // 调用新的滚动方法
  220. this.scrollToQuestion(firstUnansweredIndex);
  221. }
  222. }
  223. });
  224. return; // 终止提交
  225. }
  226. const submitList = this.list.map((l) => {
  227. let {answer,userAnswer,warn,...other} = JSON.parse(JSON.stringify(l));
  228. userAnswer.forEach(u=>{
  229. u.questionOption.forEach(q=>{
  230. q.userAnswer=answer.find(a=>a.assessmentMethod==u.assessmentMethod)?.value==q.questionOption;
  231. })
  232. })
  233. return {
  234. ...other,
  235. isAnswer: '1',
  236. userAnswer
  237. };
  238. });
  239. this.isSubmitting = true;
  240. this.$api
  241. .post('/core/team/member/answer/submit', submitList)
  242. .then((res) => {
  243. if (res.data.code !== 0) {
  244. this.isSubmitting = false;
  245. return this.$showToast(res.data.msg);
  246. }
  247. uni.removeStorageSync('questionnaire');
  248. let url = '/pagesPublish/questionnaireResult'
  249. if(this.type == 2) url = '/pagesPublish/submitResult'
  250. uni.navigateTo({ url })
  251. })
  252. .catch((err) => {
  253. this.$showToast('网络异常,请稍后重试');
  254. })
  255. .finally(() => {
  256. this.isSubmitting = false;
  257. });
  258. },
  259. setQuestionnaireCache() {
  260. const answeredList = this.list.filter((l) => l.answer.filter(a => a?.value).length>0).map((l) => ({ id: l.id, answer: l.answer }));
  261. if (answeredList.length === 0) {
  262. uni.removeStorageSync('questionnaire');
  263. return;
  264. }
  265. const qinfo = {
  266. key: this.teamQuestionnaireId,
  267. list: answeredList
  268. };
  269. uni.setStorageSync('questionnaire', JSON.stringify(qinfo));
  270. },
  271. async getTeamScaleData(){
  272. return new Promise((resolve,reject)=>{
  273. this.$api.get('/getListByType/team_scale').then(({data:res})=>{
  274. if(res.code!==0) return this.$showToast(res.msg)
  275. this.teamScaleData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  276. resolve()
  277. })
  278. })
  279. },
  280. async getTeamHierarchyData(){
  281. return new Promise((resolve,reject)=>{
  282. this.$api.get('/getListByType/team_hierarchy').then(({data:res})=>{
  283. if(res.code!==0) return this.$showToast(res.msg)
  284. this.teamLevelData = res.data.map(d=>({name:d.dictLabel,id:d.dictValue}))
  285. resolve()
  286. })
  287. })
  288. },
  289. async showTeamInfo(){
  290. await this.getTeamScaleData()
  291. await this.getTeamHierarchyData()
  292. this.$api.get(`/core/user/team/${this.teamId}`).then(({data:res})=>{
  293. if(res.code!==0) return this.$showToast(res.msg)
  294. this.teamInfo = res.data;
  295. this.teamInfo.functionsName = res.data.functions.map(f=>f.functionName).join('、');
  296. this.teamInfo.organizationsName = res.data.organizations.map(f=>f.orgName).join('、');
  297. this.teamInfo.address = res.data.provinceName+res.data.cityName;
  298. this.teamInfo.scaleName = this.teamScaleData.find(d=>d.id==res.data.scale).name;
  299. this.teamInfo.hierarchyName = this.teamLevelData.find(d=>d.id==res.data.hierarchy).name;
  300. this.teamShow = true;
  301. })
  302. }
  303. }
  304. }
  305. </script>
  306. <style scoped lang="scss">
  307. .loading-container {
  308. width: 100%;
  309. height: 100%;
  310. }
  311. .default_page{
  312. padding: 0 0 192rpx;
  313. background: #FFFFFF;
  314. box-sizing: border-box;
  315. height: 100vh;
  316. overflow: hidden;
  317. display: flex;
  318. flex-direction: column;
  319. .top{
  320. padding: 40rpx 24rpx 50rpx;
  321. &-title{
  322. font-family: PingFang-SC, PingFang-SC;
  323. font-weight: bold;
  324. font-size: 40rpx;
  325. color: #002846;
  326. line-height: 51rpx;
  327. }
  328. &-progress{
  329. margin-top: 35rpx;
  330. &-text{
  331. font-family: PingFangSC, PingFang SC;
  332. font-weight: 400;
  333. font-size: 30rpx;
  334. color: #002846;
  335. line-height: 30rpx;
  336. }
  337. &-box{
  338. flex: 1;
  339. width: 100%;
  340. height: 14rpx;
  341. margin: 0 20rpx;
  342. background: #F0F2F8;
  343. border-radius: 8rpx;
  344. position: relative;
  345. &-current{
  346. height: 14rpx;
  347. background: #FFD750;
  348. border-radius: 8rpx;
  349. position: absolute;
  350. left: 0;
  351. top: 0;
  352. }
  353. }
  354. &-num{
  355. font-family: PingFangSC, PingFang SC;
  356. font-weight: 400;
  357. font-size: 26rpx;
  358. color: #667E90;
  359. line-height: 26rpx;
  360. span{
  361. font-size: 36rpx;
  362. color: #199C9C;
  363. line-height: 36rpx;
  364. }
  365. }
  366. }
  367. }
  368. .memo{
  369. margin: -20rpx 0 50rpx 24rpx;
  370. text{
  371. font-family: PingFangSC, PingFang SC;
  372. font-weight: 400;
  373. font-size: 24rpx;
  374. color: #536387;
  375. line-height: 30rpx;
  376. }
  377. image{
  378. width: 24rpx;
  379. height: 24rpx;
  380. margin-left: 9rpx;
  381. }
  382. }
  383. .list{
  384. width: 100%;
  385. padding: 0 24rpx;
  386. box-sizing: border-box;
  387. // flex: 1;
  388. // overflow-y: auto;
  389. }
  390. .bottom{
  391. width: 100%;
  392. background: #FFFFFF;
  393. box-shadow: 0rpx -2rpx 6rpx 0rpx rgba(0,0,0,0.07);
  394. padding: 20rpx 40rpx 54rpx;
  395. box-sizing: border-box;
  396. position: fixed;
  397. left: 0;
  398. bottom: 0;
  399. z-index: 1000;
  400. &-left{
  401. image{
  402. width: 42rpx;
  403. height: 42rpx;
  404. }
  405. text{
  406. font-family: PingFangSC, PingFang SC;
  407. font-weight: 400;
  408. font-size: 24rpx;
  409. color: #536387;
  410. line-height: 40rpx;
  411. margin-top: 2rpx;
  412. }
  413. &-pre{
  414. &:last-child{
  415. margin-left: 40rpx;
  416. }
  417. }
  418. }
  419. &-right{
  420. width: 448rpx;
  421. height: 88rpx;
  422. background: linear-gradient( 90deg, #33A7A7 0%, #64BBBB 100%);;
  423. border-radius: 44rpx;
  424. font-family: PingFang-SC, PingFang-SC;
  425. font-weight: bold;
  426. font-size: 32rpx;
  427. color: #FFFFFF;
  428. line-height: 88rpx;
  429. text-align: center;
  430. letter-spacing: 2rpx;
  431. &.questionnaire{
  432. width: 383rpx;
  433. }
  434. }
  435. }
  436. }
  437. </style>