questionnaireFill.vue 14 KB

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