index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {
  2. examUserExamStart,
  3. examUserExamCommitmentConfirm,
  4. examUserCoursewareLearningRestart,
  5. examUserExamAttemptFinished,
  6. } from '@/pages_safetyEducationExamination/api/index.js'
  7. /**
  8. * 考试入口统一处理
  9. * @param {Object} item 考试任务数据
  10. * @param {Object} callbacks { openCommitmentDialog } —— 需要显示承诺书时的回调
  11. */
  12. export async function goToExamPage(item, callbacks = {}) {
  13. if (item.conditionType == 1) {
  14. if (item.examStudy && item.courseId) {
  15. // 学习课程 — 等待后续逻辑
  16. uni.navigateTo({
  17. url: `/pages_safetyEducationExamination/views/webViewPage/courseLearning?courseId=${item.courseId}&examId=${item.examId}`,
  18. });
  19. } else {
  20. if (item.knowledgePointIds && item.knowledgePointIds.length > 0) {
  21. const ids = Array.isArray(item.knowledgePointIds)
  22. ? item.knowledgePointIds.join(',')
  23. : item.knowledgePointIds;
  24. uni.navigateTo({
  25. url: `/pages_safetyEducationExamination/views/studyHourTask/index?knowledgePointId=${ids}`,
  26. });
  27. } else {
  28. uni.navigateTo({ url: '/pages_safetyEducationExamination/views/study/index' });
  29. }
  30. }
  31. } else if (item.conditionType == 2) {
  32. // 学习课程
  33. uni.navigateTo({
  34. url: `/pages_safetyEducationExamination/views/webViewPage/courseLearning?courseId=${item.courseId}&examId=${item.examId}`,
  35. });
  36. } else if (item.conditionType == 3) {
  37. console.log('item',item)
  38. const pages = getCurrentPages();
  39. const currentPage = pages[pages.length - 1];
  40. const currentRoute = currentPage.route;
  41. if (item.mockExamId) {
  42. // const examData = encodeURIComponent(JSON.stringify(item));
  43. // uni.navigateTo({
  44. // url: `/pages_safetyEducationExamination/views/exam/mockExamPlay?examId=${item.examId}&attemptId=${item.mockExamId}`,
  45. // });
  46. let obj = {
  47. examId: item.examId,
  48. attemptId: item.mockExamId,
  49. };
  50. console.log('goToExamPage-obj',obj)
  51. examUserExamAttemptFinishedPlay(2,obj);
  52. } else {
  53. let obj = {
  54. examId: item.examId,
  55. };
  56. examUserExamStartPlay(2,obj);
  57. // try {
  58. // const { data } = await examUserExamStart({ examId: item.examId, examScene: 2 });
  59. // if (data.code === 200 && data.data && data.data.attemptId) {
  60. // const filled = { ...item, attemptId: data.data.attemptId };
  61. // const examData = encodeURIComponent(JSON.stringify(filled));
  62. // uni.navigateTo({
  63. // url: `/pages_safetyEducationExamination/views/exam/mockExamPlay?examId=${item.examId}&attemptId=${data.data.attemptId}`,
  64. // });
  65. // } else {
  66. // uni.showToast({ title: data.message || '获取模拟试卷失败', icon: 'none' });
  67. // }
  68. // } catch (e) {
  69. // console.error('获取模拟试卷失败', e);
  70. // uni.showToast({ title: '获取模拟试卷失败', icon: 'none' });
  71. // }
  72. }
  73. } else if (item.conditionType == 4) {
  74. await resolveExamEntry(item, callbacks);
  75. }
  76. }
  77. /**
  78. * 判断是否需要承诺书,并决定后续跳转
  79. * @param {Object} item 考试任务数据
  80. * @param {Object} callbacks { openCommitmentDialog(item) }
  81. */
  82. export async function resolveExamEntry(item, callbacks = {}) {
  83. if (item.commitmentEnabled == 1 && !item.commitmentConfirmed) {
  84. // 需要展示承诺书弹窗,交由调用方组件处理
  85. if (typeof callbacks.openCommitmentDialog === 'function') {
  86. callbacks.openCommitmentDialog(item);
  87. }
  88. } else {
  89. // 不需要承诺书,直接进入考试
  90. // await startFormalExam(item);
  91. if(item.attemptId){
  92. console.log('resolveExamEntry-item',item)
  93. examUserExamAttemptFinishedPlay(1,item);
  94. }else{
  95. examUserExamStartPlay(1,item);
  96. }
  97. }
  98. }
  99. /**
  100. * 确认承诺书并进入考试(由组件点击"确认"按钮时调用)
  101. * @param {Object} preData 当前承诺书对应的考试数据
  102. * @param {Function} onClose 承诺确认成功后关闭弹窗的回调
  103. */
  104. export async function confirmCommitmentAndStart(preData, onClose) {
  105. let attemptId = preData.attemptId;
  106. if (!attemptId) {
  107. try {
  108. const { data } = await examUserExamStart({ examId: preData.examId, examScene: 1 });
  109. if (data.code === 200 && data.data && data.data.attemptId) {
  110. attemptId = data.data.attemptId;
  111. } else {
  112. uni.showToast({ title: data.message || '开始考试失败', icon: 'none' });
  113. return;
  114. }
  115. } catch (e) {
  116. console.error('开始考试失败', e);
  117. uni.showToast({ title: '开始考试失败', icon: 'none' });
  118. return;
  119. }
  120. }
  121. try {
  122. const { data } = await examUserExamCommitmentConfirm({ attemptId });
  123. if (data.code === 200) {
  124. if (typeof onClose === 'function') {
  125. onClose();
  126. } else {
  127. // navigateToExamPlay(preData.examId, attemptId, preData.examKind);
  128. let obj = {
  129. examId: preData.examId,
  130. attemptId: attemptId,
  131. type: '1',
  132. examKind:preData.examKind,
  133. attemptLimit:preData.attemptLimit,
  134. attemptCount:preData.attemptCount,
  135. }
  136. console.log('confirmCommitmentAndStart-obj',obj)
  137. examUserExamAttemptFinishedPlay(1,obj);
  138. }
  139. } else {
  140. uni.showToast({ title: data.message || '确认失败', icon: 'none' });
  141. }
  142. } catch (e) {
  143. console.error('确认承诺书失败', e);
  144. uni.showToast({ title: '确认失败', icon: 'none' });
  145. }
  146. }
  147. /**
  148. * 发起或续考正式考试
  149. * @param {Object} item 考试任务数据(含 examId / attemptId / examKind)
  150. */
  151. export async function startFormalExam(item) {
  152. let attemptId = item.attemptId;
  153. if (!attemptId) {
  154. try {
  155. const { data } = await examUserExamStart({ examId: item.examId, examScene: 1 });
  156. if (data.code !== 200 || !data.data || !data.data.attemptId) {
  157. uni.showToast({ title: data.message || '开始考试失败', icon: 'none' });
  158. return;
  159. }
  160. attemptId = data.data.attemptId;
  161. } catch (e) {
  162. console.error('开始考试失败', e);
  163. uni.showToast({ title: '开始考试失败', icon: 'none' });
  164. return;
  165. }
  166. }
  167. navigateToExamPlay(item.examId, attemptId, item.examKind);
  168. }
  169. /**
  170. * 跳转到答题页
  171. */
  172. export function navigateToExamPlay(examId, attemptId, examKind) {
  173. uni.navigateTo({
  174. url: `/pages_safetyEducationExamination/views/exam/onlineExamPlay?examId=${encodeURIComponent(examId)}&attemptId=${encodeURIComponent(attemptId)}&examKind=${encodeURIComponent(examKind || '')}`,
  175. });
  176. }
  177. /**
  178. * 学习课件
  179. */
  180. export function goLearning(item){
  181. if(item.learnStatus == 2){
  182. uni.showModal({
  183. title: '提示',
  184. content: `该课件已学完,是否重置进度重新学习?`,
  185. success: async (res) => {
  186. if (res.confirm) {
  187. await examUserCoursewareLearningRestartFunction(item.coursewareId);
  188. }
  189. }
  190. });
  191. }else{
  192. uni.navigateTo({
  193. url: `/pages_safetyEducationExamination/views/webViewPage/coursewareLearning?coursewareId=${item.coursewareId}`,
  194. });
  195. }
  196. }
  197. /**
  198. * 查询课件是否已学过
  199. */
  200. export async function examUserCoursewareLearningRestartFunction(coursewareId ) {
  201. try {
  202. const { data } = await examUserCoursewareLearningRestart({coursewareId:coursewareId});
  203. if (data.code === 200) {
  204. uni.navigateTo({
  205. url: `/pages_safetyEducationExamination/views/webViewPage/coursewareLearning?coursewareId=${coursewareId}`,
  206. });
  207. }
  208. } catch (e) { console.error('重新开始学习课件失败', e); }
  209. }
  210. //二次确认考试是否已结束
  211. export async function examUserExamAttemptFinishedPlay(type,item) {
  212. console.log('调用了')
  213. try {
  214. console.log('item',item)
  215. const { data } = await examUserExamAttemptFinished({ attemptId:item.attemptId });
  216. if (data.code == 200) {
  217. if(data.data){
  218. //已结束
  219. if(type == 1){
  220. //在线考试
  221. if(item.attemptLimit == 0 || item.attemptCount < item.attemptLimit){
  222. //获取新试卷
  223. examUserExamStartPlay(type,item)
  224. }else{
  225. //无可用次数 提示并刷新数据列表
  226. uni.showToast({ title: '当前考试已结束', icon: 'none' });
  227. checkData();
  228. }
  229. }else if(type == 2){
  230. //模拟考试-获取新试卷
  231. examUserExamStartPlay(type,item)
  232. }
  233. }else{
  234. //未结束
  235. confirmTitle(type,item);
  236. }
  237. }
  238. } catch (e) { console.error('二次确认考试是否已结束失败', e);}
  239. }
  240. //获取新试卷
  241. export async function examUserExamStartPlay(type,item) {
  242. try {
  243. const { data } = await examUserExamStart({ examId: item.examId, examScene: type });
  244. if (data.code === 200 && data.data && data.data.attemptId) {
  245. let obj = {
  246. examId: item.examId,
  247. attemptId: data.data.attemptId,
  248. };
  249. if(type == 1){
  250. obj.type = 1;
  251. obj.examKind = item.examKind;
  252. }
  253. confirmTitle(type,obj);
  254. } else {
  255. uni.showToast({ title: data.message || '获取试卷失败', icon: 'none' });
  256. }
  257. } catch (e) {
  258. console.error('获取试卷失败', e);
  259. uni.showToast({ title: '获取试卷失败', icon: 'none' });
  260. }
  261. }
  262. //弹窗确认
  263. export async function confirmTitle(type,item) {
  264. if(type == 1){
  265. uni.showModal({
  266. title: '进入考试页面',
  267. content: `考试期间未完成答题请勿离开页面,离开后倒计时仍会继续,超时将自动交卷并记录成绩,扣除 1 次考试机会。`,
  268. confirmText:'确定进入',
  269. success: async (res) => {
  270. if (res.confirm) {
  271. uni.navigateTo({
  272. url: `/pages_safetyEducationExamination/views/exam/onlineExamPlay?examId=${encodeURIComponent(item.examId)}&attemptId=${encodeURIComponent(item.attemptId)}&examKind=${encodeURIComponent(item.examKind || '')}`,
  273. });
  274. }
  275. }
  276. });
  277. }else if(type == 2){
  278. uni.navigateTo({
  279. url: `/pages_safetyEducationExamination/views/exam/mockExamPlay?examId=${item.examId}&attemptId=${item.attemptId}`,
  280. });
  281. }
  282. }
  283. //监听数据刷新事件
  284. export function checkData(data) {
  285. uni.$emit('onDataCheckSuccess', data)
  286. }