index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <template>
  2. <view class="container">
  3. <image mode="" style="width: 100%; height: 336rpx;" src="../../static/images/zxks.jpeg">
  4. <view class='mainBox'>
  5. <view class="mainTop">
  6. 考试概要
  7. </view>
  8. <view class="ksBox">
  9. <image mode="" class="imgStyle" style="width: 100%; height: 100%;"
  10. src="../../static/images/gkbj.png">
  11. <text class="fontTitle">{{htmlData.examName}}</text>
  12. <text class="fontStyle">出题人</text>
  13. <text class="fontSize">{{htmlData.createBy}}</text>
  14. <view class="ksMain">
  15. <view class="boxStyle">
  16. <view class="fontColor">
  17. {{htmlData.questionsNum||0}}题
  18. </view>
  19. <view class="fontStyle">
  20. <uni-icons type="calendar" size="20"></uni-icons>
  21. 题目总数
  22. </view>
  23. </view>
  24. <view class="boxStyle">
  25. <view class="fontColor">
  26. {{htmlData.scoreNum||0}}分
  27. </view>
  28. <view class="fontStyle">
  29. <uni-icons type="star" size="20"></uni-icons>
  30. 题目总分
  31. </view>
  32. </view>
  33. <view class="boxStyle">
  34. <view class="fontColor">
  35. {{htmlData.paperPeriod||0}}分钟
  36. </view>
  37. <view class="fontStyle">
  38. <uni-icons type="wallet" size="20"></uni-icons>
  39. 考试时长
  40. </view>
  41. </view>
  42. </view>
  43. <button type="primary" class="buttonStyle" @click="open">开始答题</button>
  44. </view>
  45. </view>
  46. <view class='mainBox'>
  47. <view class="mainTop">
  48. 考试说明
  49. </view>
  50. <view class="ksBox fontStyle">
  51. <text class="smPadding">
  52. {{htmlData.remark||'无'}}
  53. </text>
  54. </view>
  55. </view>
  56. <view class="phb" @click="toRanking">
  57. <uni-icons type="flag" size="20" color="#FFFFFF"></uni-icons>
  58. 排行榜
  59. </view>
  60. <view class="ckcj" @click="lookGrade" v-if="isUser">
  61. <!-- v-if="uni.getStorageSync("user")" -->
  62. <uni-icons type="flag" size="20" color="#FFFFFF"></uni-icons>
  63. 查看成绩
  64. </view>
  65. <view class="outLogin" @click="outLogin" v-if="isUser&&isH5">
  66. <!-- v-if="uni.getStorageSync("user")" -->
  67. <uni-icons type="arrow-left" size="20" color="#FFFFFF"></uni-icons>
  68. 退出
  69. </view>
  70. <uni-popup ref="popup" type="center">
  71. <view class="popupBox">
  72. <view class="testTitle">
  73. <view class="hr"></view>
  74. <view class="testText">本场考试</view>
  75. <view class="hr"></view>
  76. </view>
  77. <view class="ksMain">
  78. <view class="boxStyle">
  79. <view class="fontColor">
  80. {{htmlData.questionsNum}}题
  81. </view>
  82. <view class="fontStyle">
  83. 题目总数
  84. </view>
  85. </view>
  86. <view class="boxStyle">
  87. <view class="fontColor">
  88. {{htmlData.scoreNum}}分
  89. </view>
  90. <view class="fontStyle">
  91. 题目总分
  92. </view>
  93. </view>
  94. <view class="boxStyle">
  95. <view class="fontColor">
  96. {{htmlData.paperPeriod}}分钟
  97. </view>
  98. <view class="fontStyle">
  99. 考试时长
  100. </view>
  101. </view>
  102. </view>
  103. <view class="testTitle">
  104. <view class="hr"></view>
  105. <view class="testText">登录考试</view>
  106. <view class="hr"></view>
  107. </view>
  108. <uni-forms ref="dynamicForm" :rules="dynamicRules" :modelValue="dynamicFormData" class="formStyle">
  109. <template v-for="(item,index) in dynamicLists">
  110. <uni-forms-item :label="item.label" required :rules="changeRules(item)" :key="item.name"
  111. v-if="item.required==true" :name="item.name">
  112. <view class="form-item">
  113. <uni-easyinput v-model="dynamicFormData[item.name]"
  114. @input="setValue(item.name,dynamicFormData[item.name])"
  115. :placeholder="'请输入'+item.label" />
  116. </view>
  117. </uni-forms-item>
  118. <uni-forms-item :label="item.label" :rules="changeRules(item)" :key="item.name" v-else
  119. :name="item.name">
  120. <view class="form-item">
  121. <uni-easyinput v-model="dynamicFormData[item.name]"
  122. :placeholder="'请输入'+item.label" />
  123. </view>
  124. </uni-forms-item>
  125. </template>
  126. </uni-forms>
  127. <button type="primary" class="buttonStyle" @click="login('dynamicForm')">登录考试</button>
  128. </view>
  129. </uni-popup>
  130. </view>
  131. </template>
  132. <script>
  133. import {
  134. getExam,
  135. phoneLogin,
  136. checkOpenId,
  137. getStuOpenId,
  138. startExam,
  139. } from "../../utils/url.js";
  140. import openId from "../../utils/getOpenId.js";
  141. export default {
  142. data() {
  143. return {
  144. // 数据源
  145. dynamicFormData: {
  146. loginName: '',
  147. password: '',
  148. },
  149. // 动态表单数据
  150. dynamicLists: [{
  151. label: '账号',
  152. id: 0,
  153. name: 'loginName',
  154. required: true
  155. },
  156. {
  157. label: '密码',
  158. id: 1,
  159. name: 'password',
  160. required: true
  161. }
  162. ],
  163. // 规则
  164. dynamicRules: {
  165. email: {
  166. rules: []
  167. }
  168. },
  169. //页面数据
  170. htmlData: {
  171. // 考试名 examName
  172. // 出题人 createBy
  173. // 题目总数 questionsNum
  174. // 题目总分 scoreNum
  175. // 考试时长 paperPeriod
  176. // 考试说明 remark
  177. // 考试类型 0选人 1自主填报
  178. },
  179. paper: null,
  180. examId: null,
  181. isPaper: false,
  182. isUser: false,
  183. isH5: false,
  184. }
  185. },
  186. methods: {
  187. handUser() {
  188. console.log(uni.getStorageSync("user"), 'uni.getStorageSync("user")')
  189. if (uni.getStorageSync("user")) {
  190. return true
  191. } else {
  192. return false
  193. }
  194. },
  195. setValue(name, value) {
  196. // 设置表单某项对应得值来触发表单校验
  197. // 接受两个参数,第一个参数为表单域的 name ,第二个参数为表单域的值
  198. this.$refs.dynamicForm.setValue(name, value)
  199. },
  200. async getPaper(id) {
  201. uni.showLoading({
  202. title: '加载中'
  203. });
  204. let data = await getExam(id);
  205. uni.hideLoading();
  206. this.changeForm(data.data);
  207. uni.setStorageSync("examData", data.data);
  208. },
  209. changeForm(data) {
  210. this.htmlData = data;
  211. if (this.htmlData.examType == "0") {
  212. this.dynamicLists = [{
  213. label: '账号',
  214. id: 0,
  215. name: 'loginName',
  216. required: true
  217. },
  218. {
  219. label: '密码',
  220. id: 1,
  221. name: 'password',
  222. required: true
  223. }
  224. ]
  225. } else {
  226. this.dynamicLists = [{
  227. label: '姓名',
  228. id: 0,
  229. name: 'studentName',
  230. required: true
  231. },
  232. {
  233. label: '账号',
  234. id: 1,
  235. name: 'loginName',
  236. required: true
  237. },
  238. {
  239. label: '密码',
  240. id: 2,
  241. name: 'password',
  242. required: true
  243. }
  244. ]
  245. }
  246. },
  247. async phoneLogin(res) {
  248. uni.showLoading({
  249. title: '加载中'
  250. });
  251. console.log(res, 'res');
  252. let data = await phoneLogin(res);
  253. console.log(data, 'data');
  254. uni.hideLoading();
  255. uni.setStorageSync("token", data.data.token.access_token);
  256. uni.setStorageSync("user", res);
  257. //卷子是否存在
  258. // console.log(uni.getStorageSync("examFlg"),'uni.getStorageSync("examFlg")');
  259. // console.log(uni.getStorageSync("examData").id,'uni.getStorageSync("examData").id');
  260. // console.log(uni.getStorageSync("oldExamId"),'uni.getStorageSync("oldExamId")');
  261. // console.log(uni.getStorageSync("user").loginName,'uni.getStorageSync("user").loginName');
  262. // console.log(uni.getStorageSync("oldLoginName"),'uni.getStorageSync("oldLoginName")');
  263. // console.log(uni.getStorageSync("paper"),'uni.getStorageSync("paper")');
  264. let resd = uni.getStorageSync("examFlg") == 1 && uni.getStorageSync("examData").id == uni
  265. .getStorageSync("oldExamId") && uni.getStorageSync("user").loginName == uni.getStorageSync(
  266. "oldLoginName") && uni.getStorageSync("paper")
  267. if (this.isPaper || resd) {
  268. uni.showModal({
  269. title: '提醒',
  270. content: '还有未答完的试卷!',
  271. showCancel: false,
  272. success: function(res) {
  273. if (res.confirm) {
  274. uni.navigateTo({
  275. url: '/pages/test/test?paper=true'
  276. });
  277. // alert('用户点击确认');
  278. } else if (res.cancel) {
  279. // alert('用户点击取消');
  280. }
  281. }
  282. })
  283. } else {
  284. let res = uni.getStorageSync("user");
  285. console.log(res, 'res');
  286. let parmar = await this.getExam(uni.getStorageSync("examData").id, res.loginName, res.password);
  287. console.log(parmar, 'parmar');
  288. uni.navigateTo({
  289. url: '/pages/test/test'
  290. });
  291. }
  292. },
  293. async open() {
  294. // if (this.isPaper) {
  295. // uni.showModal({
  296. // title: '提醒',
  297. // content: '还有未答完的试卷!',
  298. // showCancel: false,
  299. // success: function(res) {
  300. // if (res.confirm) {
  301. // uni.navigateTo({
  302. // url: '/pages/test/test'
  303. // });
  304. // // alert('用户点击确认');
  305. // } else if (res.cancel) {
  306. // // alert('用户点击取消');
  307. // }
  308. // }
  309. // })
  310. // } else
  311. console.log(uni.getStorageSync("token"), 'uni.getStorageSync("token")')
  312. if (uni.getStorageSync("user")) {
  313. let res = uni.getStorageSync("user");
  314. let exam=uni.getStorageSync("examData")
  315. //考试类型不一样需要登录
  316. if(exam.examType!=uni.getStorageSync("oldExamType"))
  317. {
  318. this.$refs.popup.open();
  319. }else{
  320. this.phoneLogin({...res,examId:exam.id});
  321. }
  322. // let resd = uni.getStorageSync("examFlg") == 1 && uni.getStorageSync("examData").id == uni
  323. // .getStorageSync("oldExamId") && uni.getStorageSync("user").loginName == uni.getStorageSync(
  324. // "oldLoginName") && uni.getStorageSync("paper")
  325. // if (this.isPaper || resd) {
  326. // uni.showModal({
  327. // title: '提醒',
  328. // content: '还有未答完的试卷!',
  329. // showCancel: false,
  330. // success: function(res) {
  331. // if (res.confirm) {
  332. // uni.navigateTo({
  333. // url: '/pages/test/test?paper=true'
  334. // });
  335. // // alert('用户点击确认');
  336. // } else if (res.cancel) {
  337. // // alert('用户点击取消');
  338. // }
  339. // }
  340. // })
  341. // } else {
  342. // let res = uni.getStorageSync("user");
  343. // let parmar = await this.getExam(uni.getStorageSync("examData").id, res.loginName, res
  344. // .password);
  345. // uni.navigateTo({
  346. // url: '/pages/test/test'
  347. // });
  348. // }
  349. } else {
  350. this.$refs.popup.open();
  351. }
  352. },
  353. async getExam(examId, loginName, loginPassword) {
  354. uni.showLoading({
  355. title: '加载中'
  356. });
  357. let data = await startExam(examId, loginName, loginPassword);
  358. uni.hideLoading();
  359. uni.setStorageSync("paper", data.data);
  360. },
  361. login(ref) {
  362. this.$refs[ref].validate().then(res => {
  363. this.phoneLogin({
  364. ...res,
  365. examId: this.htmlData.id,
  366. openId: uni.getStorageSync("openId") ? uni.getStorageSync("openId") : null
  367. });
  368. this.$refs.popup.close()
  369. }).catch(err => {
  370. console.log('表单错误信息:', err);
  371. })
  372. },
  373. changeRules(item) {
  374. return [{
  375. 'required': item.required,
  376. errorMessage: item.label + '必填'
  377. }, ]
  378. },
  379. toRanking() {
  380. uni.navigateTo({
  381. url: '/pages/ranking/ranking?examId=' + this.examId
  382. });
  383. },
  384. lookGrade() {
  385. uni.navigateTo({
  386. url: '/pages/performance/performance'
  387. });
  388. },
  389. async checkOpenId(examId, openId) {
  390. uni.showLoading({
  391. title: '加载中'
  392. });
  393. let result = await checkOpenId(examId, openId);
  394. uni.hideLoading();
  395. console.log(result, 'result');
  396. if (result.data.id) {
  397. this.paper = result.data;
  398. uni.setStorageSync("paper", this.paper);
  399. //存在卷,并且卷还在进行中
  400. if (parseInt(result.data.examNum) > 0 && result.data.examFlg == '1') {
  401. this.isPaper = true;
  402. this.phoneLogin({
  403. loginName: result.data.loginName,
  404. password: result.data.loginPassword,
  405. examId: this.examId,
  406. openId: uni.getStorageSync("openId") ? uni.getStorageSync("openId") : null
  407. });
  408. } else {
  409. let user = {
  410. loginName: result.data.loginName,
  411. password: result.data.loginPassword,
  412. examId: this.examId,
  413. openId: uni.getStorageSync("openId") ? uni.getStorageSync("openId") : null
  414. }
  415. uni.setStorageSync("user", user);
  416. }
  417. } else {
  418. uni.setStorageSync("token", "");
  419. }
  420. },
  421. getParam(path, name) {
  422. var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
  423. if (reg.test(path))
  424. return unescape(RegExp.$2.replace(/\+/g, " "));
  425. return "";
  426. },
  427. outLogin() {
  428. uni.showModal({
  429. title: '提示',
  430. content: '确定退出当前用户吗?',
  431. success: function(res) {
  432. if (res.confirm) {
  433. uni.setStorageSync("token", "");
  434. uni.setStorageSync("user", "");
  435. this.isUser = false;
  436. this.isH5 = false;
  437. //H5页面不是响应式的
  438. location.reload();
  439. // uni.showToast({
  440. // title: '退出成功',
  441. // icon: 'none',
  442. // duration: 5000
  443. // })
  444. } else if (res.cancel) {
  445. console.log('用户点击取消');
  446. }
  447. }
  448. });
  449. }
  450. },
  451. async onShow() {
  452. //// #ifdef H5
  453. // openId.getOpenId(() => {
  454. // let dt = window.localStorage.getItem("openId");
  455. // console.log(dt,'dt');
  456. // });
  457. //// #endif
  458. if (uni.getStorageSync("user")) {
  459. this.isUser = true;
  460. } else {
  461. this.isUser = false;
  462. }
  463. console.log(this.examId, 'this.examId');
  464. console.log(!this.examId, 'this.examId');
  465. console.log(uni.getStorageSync("user"), 'uni.getStorageSync("user")');
  466. if (!this.examId && uni.getStorageSync("examData")) {
  467. // console.log(uni.getStorageSync("examData"),'uni.getStorageSync("examData")');
  468. this.examId = uni.getStorageSync("examData").id;
  469. }
  470. await this.getPaper(this.examId);
  471. console.log(this.examId, 'this.examId');
  472. let openId = uni.getStorageSync("openId");
  473. if (openId) {
  474. this.checkOpenId(this.examId, openId)
  475. //有id代表就是有卷子
  476. }
  477. },
  478. async onLoad(options) {
  479. // #ifdef MP-WEIXIN
  480. if (options.q) {
  481. const q = decodeURIComponent(options.q); // 获取到二维码原始链接内容
  482. const scancode_time = parseInt(options.scancode_time); // 获取用户扫码时间 UNIX 时间戳
  483. console.log(q, 'q');
  484. console.log(scancode_time, 'scancode_time');
  485. let str = q
  486. let index = str.lastIndexOf("\/");
  487. str = str.substring(index + 1, str.length);
  488. console.log('我是获取到的考试id', str);
  489. this.examId = str;
  490. }
  491. uni.login({
  492. provider: 'weixin',
  493. success: async (res) => {
  494. uni.showLoading({
  495. title: '加载中'
  496. });
  497. let data = await getStuOpenId({
  498. jsCode: res.code
  499. });
  500. uni.hideLoading();
  501. uni.setStorageSync("openId", data.msg);
  502. }
  503. })
  504. // #endif
  505. // #ifdef H5
  506. // uni.setStorageSync("token", "");
  507. // console.log('我是清除token');
  508. // uni.setStorageSync("user", "");
  509. // uni.setStorageSync("examData", "");
  510. let local = location.href;
  511. let payment_id = this.getParam(local, "id");
  512. this.examId = parseInt(payment_id);
  513. this.isH5 = true;
  514. // #endif
  515. //获取url中的参数
  516. // let id = 57;
  517. // this.examId = id;
  518. }
  519. }
  520. </script>
  521. <style scoped>
  522. .container {}
  523. .mainBox {
  524. width: 90%;
  525. /* height: 600rpx; */
  526. margin: 20rpx auto;
  527. position: relative;
  528. border-radius: 10rpx;
  529. display: flex;
  530. flex-direction: column;
  531. box-shadow: 0 0.05333rem 0.10667rem rgb(0 0 0 / 12%), 0 0 0.16rem rgb(0 0 0 / 4%);
  532. }
  533. .imgStyle {
  534. position: absolute;
  535. width: 100%;
  536. height: 75%;
  537. bottom: 0;
  538. }
  539. .mainTop {
  540. height: 80rpx;
  541. border-radius: 10rpx 10rpx 0 0;
  542. line-height: 80rpx;
  543. text-align: center;
  544. color: #fff;
  545. font-size: 32rpx;
  546. background-color: rgb(245, 166, 35);
  547. }
  548. .container {
  549. background: #fff;
  550. width: 100%;
  551. /* height: 100%; */
  552. }
  553. .ksBox {
  554. position: relative;
  555. width: 100%;
  556. height: 100%;
  557. display: flex;
  558. flex-direction: column;
  559. text-align: center;
  560. }
  561. .ksMain {
  562. /* height: 140rpx; */
  563. display: flex;
  564. /* margin-top: 20rpx; */
  565. }
  566. .fontStyle {
  567. color: #909399;
  568. font-size: 28rpx;
  569. }
  570. .boxStyle {
  571. /* margin-left: 55rpx;
  572. margin-top: 10rpx; */
  573. text-align: center;
  574. margin-top: 10rpx;
  575. flex: 1;
  576. }
  577. .fontColor {
  578. color: rgb(245, 166, 35);
  579. font-size: 28rpx;
  580. margin-bottom: 10rpx;
  581. }
  582. .fontSize {
  583. font-size: 28rpx;
  584. }
  585. .fontTitle {
  586. font-size: 30rpx;
  587. margin-top: 35rpx;
  588. margin-bottom: 35rpx;
  589. }
  590. /* 小程序端需要添加!important,要不颜色是默认绿色 */
  591. .buttonStyle {
  592. width: 85%;
  593. margin-left: 7%;
  594. margin-top: 30rpx;
  595. margin-bottom: 70rpx;
  596. background-color: rgb(245, 166, 35) !important;
  597. border-radius: 44rpx;
  598. }
  599. .smPadding {
  600. margin: 20rpx;
  601. line-height: 40rpx;
  602. text-align: left;
  603. }
  604. .phb {
  605. position: fixed;
  606. right: 0;
  607. bottom: 20%;
  608. display: flex;
  609. background-color: rgb(245, 166, 35);
  610. padding: 10rpx 10rpx 10rpx 30rpx;
  611. border-radius: 30rpx 0 0 30rpx;
  612. font-size: 25rpx;
  613. color: #fff;
  614. }
  615. .ckcj {
  616. position: fixed;
  617. right: 0;
  618. bottom: 14%;
  619. display: flex;
  620. background-color: rgb(245, 166, 35);
  621. padding: 10rpx 10rpx 10rpx 30rpx;
  622. border-radius: 30rpx 0 0 30rpx;
  623. font-size: 25rpx;
  624. color: #fff;
  625. }
  626. .outLogin {
  627. position: fixed;
  628. right: 0;
  629. bottom: 8%;
  630. display: flex;
  631. background-color: rgb(245, 166, 35);
  632. padding: 10rpx 10rpx 10rpx 30rpx;
  633. border-radius: 30rpx 0 0 30rpx;
  634. font-size: 25rpx;
  635. color: #fff;
  636. }
  637. .popupBox {
  638. background-color: #f7f8fa;
  639. /* width: 680rpx;
  640. height: 800rpx; */
  641. border-radius: 12rpx;
  642. display: flex;
  643. flex-direction: column;
  644. padding: 50rpx 0;
  645. }
  646. .testTitle {
  647. display: flex;
  648. justify-content: center;
  649. }
  650. .hr {
  651. background-color: #dcdfe6;
  652. width: 200rpx;
  653. height: 2rpx;
  654. margin: 20rpx;
  655. }
  656. .testText {
  657. color: #303133;
  658. font-weight: 700;
  659. }
  660. .formStyle {
  661. padding: 0 40rpx;
  662. margin-top: 40rpx;
  663. }
  664. </style>