Commit 982f5295 authored by Damon's avatar Damon

feat: 答题分页数据对接

parent 559c4fe3
const UNI_APP = {
apiUrl: 'http://192.168.0.208:8760',
apiUrl: 'http://192.168.0.204:8760',
qn_base_url: 'https://qn-static.banshouhui.com/study/',
qn_asset_url: 'http://deudn.c.pp.cc/',
uUrl: 'http://rest-u.yunya.com/',
......
import { commonApiFun } from './common.js'
import { learnApiFun } from './learn.js'
const apiFun = function(vm) {
let commonApi = commonApiFun(vm)
let learnApi = learnApiFun(vm)
let apiUrl = {
...commonApi,
...learnApi
}
return apiUrl
}
......
let learnApiFun = function(vm) {
const globalUrl = process.uniEnv || {}
let ucenterApiUrl = globalUrl.uUrl
let getQuestion = async (params = {}) => await vm.$u.post('/rest/question/query/getQuestion', params)
let getQuestionTotal = async (params = {}) => await vm.$u.post('/rest/question/query/getQuestionTotal', params)
const learnApi = {
getQuestion,
getQuestionTotal
}
return learnApi
}
module.exports = {
learnApiFun,
}
......@@ -7,6 +7,7 @@ const install = (Vue, vm) => {
baseUrl: systemApiUrl,
header: {
'channel': 'Applets',
'tenant': 'weixiuhui',
'content-type': 'application/json; charset=utf-8',
},
loadingTime: 0,
......
......@@ -189,6 +189,7 @@ export default {
swiperList[current] = currentItem
swiperList[this.getLastSwiperChangeIndex(current)] = this.getLastSwiperNeedItem(currentItem, list)
swiperList[this.getNextSwiperChangeIndex(current)] = this.getNextSwiperNeedItem(currentItem, list)
console.log("swiperList", swiperList)
return swiperList;
},
/**
......
......@@ -11,11 +11,25 @@
<view class="u-flex contents" v-if="list.length > 0">
<view class="head" v-if="!daily">
<view class="u-flex tab" v-if="type !== 2">
<view :class="['tab-list', {'active': model === 1}]" @click="changeModel(1)">答题模式</view>
<view :class="['tab-list', {'active': model === 2}]" @click="changeModel(2)">学习模式</view>
<view
:class="['tab-list', { active: model === 1 }]"
@click="changeModel(1)"
>答题模式</view
>
<view
:class="['tab-list', { active: model === 2 }]"
@click="changeModel(2)"
>学习模式</view
>
</view>
<view class="u-flex count-down" v-if="type === 2">
<view>倒计时:<u-count-down :timestamp="timestamp" bg-color="" :show-hours="false"></u-count-down></view>
<view
>倒计时:<u-count-down
:timestamp="timestamp"
bg-color=""
:show-hours="false"
></u-count-down
></view>
</view>
</view>
<view class="swiper">
......@@ -36,11 +50,15 @@
<view class="u-flex">
<view class="c-list">
<u-icon name="grid" color="#5A6073" size="40"></u-icon>
<text>{{ currentIndex + 1 }}/{{ list.length }}</text>
<text>{{ currentIndex + 1 }}/{{ total }}</text>
</view>
</view>
<view class="u-flex">
<view class="c-list" v-if="type === 1 || type === 3" @click="collection">
<view
class="c-list"
v-if="type === 1 || type === 3"
@click="collection"
>
<u-icon name="star" color="#5A6073" size="40"></u-icon>
<text>收藏</text>
</view>
......@@ -56,7 +74,10 @@
</view>
</view>
<view class="u-flex default" v-if="list.length === 0 && [3, 4].includes(type)">
<view
class="u-flex default"
v-if="list.length === 0 && [3, 4].includes(type)"
>
<view>
<image :src="defaultImg" />
</view>
......@@ -81,6 +102,7 @@
import swiperLimitedLoad from '@/components/swiper-limited-load/index.vue'
import answerOperationTips from '@/components/answer-operation-tips/index.vue'
import answerCompletePop from '@/components/answer-complete-pop/index.vue'
import Utils from './utils.js'
const NO_PREV_PAGE = -1
const NO_NEXT_PAGE = -2
......@@ -96,7 +118,7 @@ export default {
visible: false,
show: false,
// 1 练习 or 每日一练 2 考试 3 收藏记录 4 错题记录
type: 1,
type: 1,
// 1 答题模式 2 学习模式
model: 1,
// 每日一练
......@@ -104,11 +126,24 @@ export default {
dailyCount: 0,
timestamp: 60 * 60,
list: [],
// key为currentPage,value为此页list数据
cachePageDataMap: null,
// 改变此值可控制swiper的位置
current: 0,
// 仅仅记录swiper的位置
currentIndex: 0,
swiperDuration: '0',
result: [],
completeType: 'pass',
// 分页数据
currentPage: 1,
pageSize: 10,
total: 0,
requesting: false,
// 科目ID
questionWarehouseId: null,
source: 0,
recordId: 0
}
},
computed: {
......@@ -122,145 +157,146 @@ export default {
return process.uniEnv.qn_base_url + name
},
},
created() {
onLoad(e) {
this.questionWarehouseId = e?.questionWarehouseId || 1414854649273384960
if (e?.source) this.source = e.source
if (e?.recordId) this.recordId = e.recordId
const params = {
questionWarehouseId: this.questionWarehouseId,
recordId: this.recordId,
source: this.source
}
this.$u.api.getQuestionTotal(params).then(res => {
// 列表总长度,一定要在第一次请求列表数据前明确该值
// 否则无法知道下一页是否还有数据
let total = parseInt(res)
this.total = total
// 初始化答题卡列表
Utils.initAnswerCardList(total, this)
this.cachePageDataMap = new Map()
// 上次答到了第几题
let index = this.lastIndex
this.refresh(index)
})
if (this.questionList.length === 0) {
this.show = true
}
const lists = [
{
type: 1,
title: '采用QS1电桥测量tanδ的接线方式一般有差接线及和接线。',
answerList: [
{
sort: 'A',
text: '正确',
},
{
sort: 'B',
text: '错误',
},
],
answer: 'A',
explain:
'极性电介质在外电场作用下,由偶极子分子的转向形成的极化称为偶极子式极化。',
index: 0,
rightKey: ['A'],
},
{
type: 2,
title:
'空气间隙的操作冲击击穿电压数值大小不仅与间隙距离、电极形状、电极极性有关,而且与操作冲击电压波的波前时间的长短有关。',
answerList: [
{
sort: 'A',
text: '正确',
},
{
sort: 'B',
text: '错误',
},
],
answer: 'B',
explain:
'极性电介质在外电场作用下,由偶极子分子的转向形成的极化称为偶极子式极化。',
index: 1,
rightKey: ['B'],
},
{
type: 3,
title: '西林电桥采用正接线及反接线时,其测试原理相同',
answerList: [
{
sort: 'A',
text: '影响',
},
{
sort: 'B',
text: '影响很小',
},
{
sort: 'C',
text: '影响很大',
},
{
sort: 'D',
text: '无影响',
},
],
answer: 'B,C',
explain:
'双臂电桥是在单臂电桥的基础上增加特殊机构,以消除测试时连接线和接线柱接触电阻对测量结果的影响。',
index: 2,
rightKey: ['B', 'C'],
},
{
type: 2,
title: '测试标题测试标题测试标题测试标题',
answerList: [
{
sort: 'A',
text: '是',
},
{
sort: 'B',
text: '否',
},
],
answer: 'B',
explain:
'测试内容测试内容测试内容测试内容测试内容测试内容',
index: 3,
rightKey: ['B'],
},
]
this.requestQuestionInfo(lists)
},
methods: {
requestQuestionInfo(lists) {
// 初始化setData后,需要调用swiper组件的init方法
this.list = lists
this.$nextTick(() => {
// 比如上次答到了第20题
this.$refs.swiper.init(0)
// 初始化后再把动画弄出来,否则初始的current不是0,界面会自动跳动到当前位置,体验不太好
this.swiperDuration = '250'
// 全局记一下list, 答题卡页暂时就直接用了
this.$u.vuex('questionList', lists)
})
},
swiperChange(e) {
let current = e.current
this.currentIndex = current
if (current == NO_PREV_PAGE) {
return uni.showToast({
title: '已经是第一题了',
icon: 'none',
})
}
if (current == NO_NEXT_PAGE) {
if (this.type === 2) {
this.$u.route({
url: 'pages/learn/source'
if (current == NO_PREV_PAGE || current == NO_NEXT_PAGE) {
if (this.requesting) {
uni.showToast({
title: '数据加载中',
icon: 'none',
})
} else {
uni.showModal({
title: '提示',
content: '已经是最后一题了',
success: (res) => {
if (res.confirm) {
this.$u.route({
url: '/pages/index/index',
type: 'tab'
})
}
},
return
}
if (current == NO_PREV_PAGE) {
return uni.showToast({
title: '已经是第一题了',
icon: 'none',
})
}
if (current == NO_NEXT_PAGE) {
if (this.type === 2) {
this.$u.route({
url: 'pages/learn/source',
})
} else {
uni.showModal({
title: '提示',
content: '已经是最后一题了',
success: (res) => {
if (res.confirm) {
this.$u.route({
url: '/pages/index/index',
type: 'tab',
})
}
},
})
}
}
}
this.loadMore(current)
},
loadMore(current) {
let list = this.list
if (this.requesting || current == 0 || current == this.total - 1) return
// 加载上一页
if (current == list[0].index) {
this.judgeLoadWay(list[0].currentPage - 1, current)
}
// 加载下一页
if (current == list[list.length - 1].index) {
this.judgeLoadWay(list[list.length - 1].currentPage + 1, current)
}
},
judgeLoadWay(currentPage, currentIndex) {
let cachePageDataMap = this.cachePageDataMap
if (cachePageDataMap.has(currentPage)) {
this.loadCacheOnePageInfo(currentPage, currentIndex)
return
}
this.requestOnePageInfo(currentPage, currentIndex)
},
loadCacheOnePageInfo(currentPage, currentIndex) {
let list = this.cachePageDataMap.get(currentPage)
this.handleOnePageInfo(list, currentIndex)
},
requestOnePageInfo(currentPage, currentIndex) {
this.requesting = true
Utils.request({
currentPage: currentPage,
size: this.pageSize,
onSuccess: (info) => {
this.requesting = false
this.cachePageDataMap.set(info.currentPage, info.questionList)
this.handleOnePageInfo(info.questionList, currentIndex)
},
onFailed: (msg) => {
this.requesting = false
},
},this)
},
handleOnePageInfo(list, currentIndex) {
let currentList = this.list
if (list == null || list.length == 0) return
if (currentList == null || currentList.length == 0) {
this.list = list
this.$nextTick(() => {
this.$refs.swiper.init(currentIndex)
})
return
}
if (list[0].currentPage == currentList[0].currentPage) return
// 看是需要将新的list往前插还是往后插
let resultList =
list[0].currentPage < currentList[0].currentPage
? list.concat(currentList)
: currentList.concat(list)
this.list = resultList
this.$nextTick(() => {
this.$refs.swiper.init(currentIndex)
})
},
changeModel(i) {
this.model = i
},
......@@ -270,9 +306,16 @@ export default {
this.$set(this.list[this.currentIndex], 'submitStatus', true)
this.$set(this.list[this.currentIndex], 'answerList', lists)
this.$set(this.list[this.currentIndex], 'result', status)
this.$set(this.list[this.currentIndex], 'errShow', this.type === 2 ? true : status === 'fail')
this.$u.vuex('questionList', this.list)
this.$set(
this.list[this.currentIndex],
'errShow',
this.type === 2 ? true : status === 'fail'
)
// 更新答题卡
this.questionList[this.currentIndex] = this.list[this.currentIndex]
this.$u.vuex('questionList', this.questionList)
this.$u.vuex('lastIndex', this.currentIndex + 1)
// 每日一练数量统计
if (this.type === 1 && this.daily) {
if (this.dailyCount < 3) this.dailyCount++
......@@ -282,19 +325,18 @@ export default {
return
}
}
// 如果回答错误或者是考试模式,自动进入下一题
if (status === 'pass' || this.type === 2) {
if (this.currentIndex + 1 > this.list.length - 1) {
return
if (this.currentIndex + 1 > this.total - 1) {
return
}
this.current = this.currentIndex + 1
}
},
// 交卷
handPapers() {
const fail = this.list.filter(v => v.result === 'fail').length // 错题数
const noStart = this.list.filter(v => !v.result).length // 未做题数
const fail = this.list.filter((v) => v.result === 'fail').length // 错题数
const noStart = this.list.filter((v) => !v.result).length // 未做题数
const fraction = 0 // 分数
this.result = [fail, noStart, fraction]
this.completeType = 'pass'
......@@ -308,13 +350,14 @@ export default {
} else {
// 提前交卷
this.$u.route({
url: 'pages/learn/source'
url: 'pages/learn/source',
})
}
},
// 弹框取消
cancel() {
if (this.completeType === 'daily') { // 查看记录
if (this.completeType === 'daily') {
// 查看记录
// this.$u.route('/pages/index/index')
} else {
this.visible = false
......@@ -322,14 +365,92 @@ export default {
},
// 收藏
collection() {
console.log("this.list[this.currentIndex]", this.list[this.currentIndex])
console.log("收藏")
console.log('this.list[this.currentIndex]', this.list[this.currentIndex])
console.log('收藏')
},
// 移除
remove() {
console.log("this.list[this.currentIndex]", this.list[this.currentIndex])
console.log("移除")
console.log('this.list[this.currentIndex]', this.list[this.currentIndex])
console.log('移除')
},
// 以下方法用于初始化数据
refresh(currentIndex) {
// 根据index和size计算得出数据在第几页
let currentPage = Utils.getInitcurrentPage(currentIndex, this.pageSize)
this.currentIndex = currentIndex
this.currentPage = currentPage
let { pageSize, total } = this
// 不确定我们需要请求几个列表,可能是1个,也可能是2个
// 比如一页10条,我们上次答题答到了第10题,正好在这个临界点上,我们还需要第11道题的数据
// 那么我们需要一起请求第一页和第二页的数据
let pageList = Utils.getInitPageList(
currentIndex,
pageSize,
currentPage,
total
)
this.judgeInitWay(pageList, currentIndex)
},
judgeInitWay(pageList, currentIndex) {
let cachePageDataMap = this.cachePageDataMap
let isNeedRequest = false
pageList.forEach((item) => {
if (!cachePageDataMap.has(item)) {
isNeedRequest = true
}
})
if (!isNeedRequest) {
this.loadCacheInitPageInfo(pageList, currentIndex)
return
}
this.requestInitPageInfo(pageList, currentIndex)
},
loadCacheInitPageInfo(pageList, currentIndex) {
let results = []
pageList.forEach((item) => {
let info = {}
info.currentPage = item
info.questionList = this.cachePageDataMap.get(item)
results.push(info)
})
this.handleInitInfo(results, currentIndex)
},
requestInitPageInfo(pageList, currentIndex) {
this.requesting = true
Utils.requestMulti({
pageList: pageList,
size: this.pageSize,
onSuccess: (results) => {
this.requesting = false
this.handleInitInfo(results, currentIndex)
},
onFailed: (msg) => {
this.requesting = false
},
},this)
},
handleInitInfo(results, currentIndex) {
let list = []
results.forEach((resultItem, index) => {
this.cachePageDataMap.set(
resultItem.currentPage,
resultItem.questionList
)
list = list.concat(resultItem.questionList)
})
this.list = list
this.$nextTick(() => {
this.$refs.swiper.init(currentIndex)
})
}
},
}
</script>
......@@ -349,31 +470,31 @@ export default {
width: 100%;
height: 90rpx;
.tab {
margin:10rpx auto;
border: 2rpx solid #2953FF;
margin: 10rpx auto;
border: 2rpx solid #2953ff;
border-radius: 10rpx;
width: 300rpx;
.tab-list {
flex: 1;
padding: 10rpx 25rpx;
font-size: 24rpx;
color: #191C27;
color: #191c27;
}
.active {
background: #2953FF;
background: #2953ff;
color: #fff;
}
}
.count-down {
color: #5A6073;
color: #5a6073;
font-size: 32rpx;
margin:10rpx auto;
margin: 10rpx auto;
width: 230rpx;
}
}
.swiper {
width: 100%;
flex: 1;
flex: 1;
}
.bottom {
width: 100%;
......@@ -389,13 +510,13 @@ export default {
}
}
}
.default {
flex-flow: column;
flex: 1;
justify-content: center;
.no-result {
color: #9EA5BB;
color: #9ea5bb;
font-size: 28rpx;
}
}
......
// 分页为如下格式:
// {
// "total": 300,
// "currentPage": 1,
// "list": []
// }
const TOTAL = 100
const lists = [
{
type: 1,
title: '采用QS1电桥测量tanδ的接线方式一般有差接线及和接线。',
answerList: [
{
sort: 'A',
text: '正确',
},
{
sort: 'B',
text: '错误',
},
],
answer: 'A',
explain:
'极性电介质在外电场作用下,由偶极子分子的转向形成的极化称为偶极子式极化。',
index: 0,
rightKey: ['A'],
},
{
type: 2,
title:
'空气间隙的操作冲击击穿电压数值大小不仅与间隙距离、电极形状、电极极性有关,而且与操作冲击电压波的波前时间的长短有关。',
answerList: [
{
sort: 'A',
text: '正确',
},
{
sort: 'B',
text: '错误',
},
],
answer: 'B',
explain:
'极性电介质在外电场作用下,由偶极子分子的转向形成的极化称为偶极子式极化。',
index: 1,
rightKey: ['B'],
},
{
type: 3,
title: '西林电桥采用正接线及反接线时,其测试原理相同',
answerList: [
{
sort: 'A',
text: '影响',
},
{
sort: 'B',
text: '影响很小',
},
{
sort: 'C',
text: '影响很大',
},
{
sort: 'D',
text: '无影响',
},
],
answer: 'B,C',
explain:
'双臂电桥是在单臂电桥的基础上增加特殊机构,以消除测试时连接线和接线柱接触电阻对测量结果的影响。',
index: 2,
rightKey: ['B', 'C'],
},
{
type: 2,
title: '测试标题测试标题测试标题测试标题',
answerList: [
{
sort: 'A',
text: '是',
},
{
sort: 'B',
text: '否',
},
],
answer: 'B',
explain: '测试内容测试内容测试内容测试内容测试内容测试内容',
index: 3,
rightKey: ['B'],
},
]
// 请求一页数据
function request({ currentPage, size, onSuccess, onFailed }, vm) {
const params = {
current: currentPage,
size: size,
model: {
questionWarehouseId: vm.questionWarehouseId,
recordId: vm.recordId,
source: vm.source
}
}
vm.$u.api.getQuestion(params).then(res => {
const list = res.records
let data = {}
data.total = res.total
data.currentPage = currentPage
let questionList = []
for (let i = 0; i < size; i++) {
const info = list[i]
const content = JSON.parse(info.content)
const answerList = content.choiceList.map(v => {
return {
sort: v.value,
text: v.label
}
})
let type
switch (info.echoMap.questionTypeId) {
case '单选题':
type = 1
break;
case '判断题':
type = 2
break;
case '多选题':
type = 3
break;
}
let item = {
index: i + (currentPage - 1) * size,
currentPage: currentPage,
total: data.total,
title: content.title,
answerList: answerList,
answer: info.answer,
rightKey: info.answer.split(','),
explain: info.analysis,
type: type
}
questionList.push(item)
}
data.questionList = questionList
onSuccess(data)
})
}
// 同时请求多页数据
function requestMulti({ pageList, size, onSuccess, onFailed }, vm) {
console.log('请求哪几页数据?', pageList)
uni.showLoading({
title: '加载中',
})
let promise = Promise.all(
pageList.map(function(pageNum) {
return new Promise(function(resolve, reject) {
request({
currentPage: pageNum,
size: size,
onSuccess: function(res) {
resolve(res)
},
onFailed: function(msg) {
reject(new Error('failed to request'))
},
},vm)
})
})
)
promise
.then(function(results) {
uni.hideLoading()
onSuccess(results)
})
.catch(function(err) {
uni.hideLoading()
onFailed(err)
})
}
// 根据初始index和每页item的数量获取当前是第几页
const getInitcurrentPage = function(index, size) {
let pageNum = parseInt((index + 1) / size)
let remain = (index + 1) % size
return remain == 0 ? pageNum : pageNum + 1
}
// 获取初始的pageList, 可能一页,可能两页
const getInitPageList = function(currentIndex, size, currentPage, total) {
if ((currentIndex + 1) % size == 1 && currentIndex != 0) {
return [currentPage - 1, currentPage]
}
if ((currentIndex + 1) % size == 0 && currentIndex != total - 1) {
return [currentPage, currentPage + 1]
}
return [currentPage]
}
function initAnswerCardList(total, vm) {
if (!vm.questionList) {
let list = []
for (let i = 0; i < total; i++) {
list.push({})
}
vm.$u.vuex('questionList', list)
}
}
module.exports = {
TOTAL: TOTAL,
request: request,
requestMulti: requestMulti,
getInitcurrentPage: getInitcurrentPage,
getInitPageList: getInitPageList,
initAnswerCardList: initAnswerCardList,
}
......@@ -12,7 +12,7 @@ try{
}
// 需要永久存储,且下次APP启动需要取出的,在state中的变量名
let saveStateKeys = ['userInfo', 'vuex_token', 'openId', 'searchHistory', 'questionList'];
let saveStateKeys = ['userInfo', 'vuex_token', 'openId', 'searchHistory', 'questionList', 'lastIndex'];
// 保存变量到本地存储中
const saveLifeData = function(key, value){
......@@ -37,6 +37,7 @@ const store = new Vuex.Store({
openId: lifeData.openId ? lifeData.openId : '',
searchHistory: lifeData.searchHistory ? lifeData.searchHistory : [], // 搜索历史记录
questionList: lifeData.questionList ? lifeData.questionList : [], // 练习列表
lastIndex: lifeData.lastIndex ? lifeData.lastIndex : 0, // 上次做到第几题
// 如果vuex_version无需保存到本地永久存储,无需lifeData.vuex_version方式
vuex_version: '1.0.0',
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment