index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const store_user = require("../../store/user.js");
  4. const store_checkin = require("../../store/checkin.js");
  5. if (!Math) {
  6. NavBar();
  7. }
  8. const NavBar = () => "../components/NavBar/NavBar.js";
  9. const _sfc_main = {
  10. __name: "index",
  11. setup(__props) {
  12. const userStore = store_user.useUserStore();
  13. const checkinStore = store_checkin.useCheckinStore();
  14. const isTeacher = common_vendor.computed(() => userStore.userInfo.role === "TEACHER");
  15. common_vendor.onShow(() => {
  16. if (isTeacher.value) {
  17. common_vendor.index.redirectTo({
  18. url: "/pages/checkin/teacher/index"
  19. });
  20. }
  21. });
  22. const studentView = common_vendor.ref("main");
  23. const navBarRef = common_vendor.ref(null);
  24. const navHeight = common_vendor.ref(88);
  25. const navTitle = common_vendor.computed(() => {
  26. if (studentView.value === "numberInput")
  27. return "输入签到码";
  28. return "课堂签到";
  29. });
  30. const studentCode = common_vendor.ref(["", "", "", ""]);
  31. const isStudentCodeValid = common_vendor.computed(() => studentCode.value.every((c) => c !== "" && !isNaN(c)));
  32. const isAlreadyCheckedIn = common_vendor.computed(() => checkinStore.isStudentCheckedIn(userStore.userInfo.id));
  33. const studentGreeting = common_vendor.computed(() => {
  34. if (isAlreadyCheckedIn.value)
  35. return "你已成功签到,表现不错!👍";
  36. if (!checkinStore.session.isActive)
  37. return "签到还未开始,请稍作等待。";
  38. return `${userStore.userInfo.name || userStore.userInfo.englishName || "同学"}, 签到开始啦~`;
  39. });
  40. const studentButtonText = common_vendor.computed(() => {
  41. if (isAlreadyCheckedIn.value)
  42. return "已签到";
  43. if (!checkinStore.session.isActive)
  44. return "暂未设置";
  45. return checkinStore.session.method === "number" ? "数字签到" : "二维码签到";
  46. });
  47. const studentAction = () => {
  48. if (checkinStore.session.method === "number") {
  49. studentView.value = "numberInput";
  50. } else {
  51. common_vendor.index.scanCode({
  52. onlyFromCamera: true,
  53. success: (res) => {
  54. if (res.result === checkinStore.session.code) {
  55. const result = checkinStore.attemptCheckin({ id: userStore.userInfo.id, name: userStore.userInfo.name });
  56. common_vendor.index.showToast({ title: result.message, icon: result.success ? "success" : "none" });
  57. } else {
  58. common_vendor.index.showToast({ title: "无效的签到码", icon: "none" });
  59. }
  60. },
  61. fail: () => {
  62. common_vendor.index.showToast({ title: "取消扫码", icon: "none" });
  63. }
  64. });
  65. }
  66. };
  67. const submitNumberCode = () => {
  68. const code = studentCode.value.join("");
  69. const result = checkinStore.attemptCheckin({ id: userStore.userInfo.id, name: userStore.userInfo.name }, code);
  70. common_vendor.index.showToast({ title: result.message, icon: result.success ? "success" : "none" });
  71. if (result.success) {
  72. studentView.value = "main";
  73. }
  74. };
  75. const onBack = () => {
  76. if (studentView.value !== "main") {
  77. studentView.value = "main";
  78. }
  79. };
  80. const formattedCurrentTime = common_vendor.computed(() => {
  81. const now = /* @__PURE__ */ new Date();
  82. const year = now.getFullYear();
  83. const month = now.getMonth() + 1;
  84. const day = now.getDate();
  85. const hours = String(now.getHours()).padStart(2, "0");
  86. const minutes = String(now.getMinutes()).padStart(2, "0");
  87. return `${year}年${month}月${day}日 ${hours}:${minutes}`;
  88. });
  89. const contentWrapperStyle = common_vendor.computed(() => ({
  90. paddingTop: `${navHeight.value}px`,
  91. // 让内容区撑满剩余屏幕
  92. minHeight: `calc(100vh - ${navHeight.value}px)`
  93. }));
  94. common_vendor.watch(() => checkinStore.session.isActive, (newVal) => {
  95. if (!newVal && !isTeacher.value) {
  96. studentView.value = "main";
  97. }
  98. });
  99. common_vendor.onMounted(() => {
  100. common_vendor.watch(
  101. () => {
  102. var _a;
  103. return (_a = navBarRef.value) == null ? void 0 : _a.totalNavHeight;
  104. },
  105. (val) => {
  106. if (val)
  107. navHeight.value = val;
  108. },
  109. { immediate: true }
  110. );
  111. });
  112. return (_ctx, _cache) => {
  113. return common_vendor.e({
  114. a: !isTeacher.value
  115. }, !isTeacher.value ? common_vendor.e({
  116. b: common_vendor.sr(navBarRef, "1410bd6b-0", {
  117. "k": "navBarRef"
  118. }),
  119. c: common_vendor.o(onBack),
  120. d: common_vendor.p({
  121. title: navTitle.value,
  122. showBack: studentView.value !== "main"
  123. }),
  124. e: studentView.value === "main"
  125. }, studentView.value === "main" ? {
  126. f: common_vendor.t(studentGreeting.value),
  127. g: common_vendor.t(formattedCurrentTime.value),
  128. h: common_vendor.t(studentButtonText.value),
  129. i: !common_vendor.unref(checkinStore).session.isActive || isAlreadyCheckedIn.value,
  130. j: common_vendor.o(studentAction)
  131. } : studentView.value === "numberInput" ? {
  132. l: common_vendor.f(studentCode.value, (item, index, i0) => {
  133. return {
  134. a: index,
  135. b: studentCode.value[index],
  136. c: common_vendor.o(($event) => studentCode.value[index] = $event.detail.value, index)
  137. };
  138. }),
  139. m: !isStudentCodeValid.value,
  140. n: common_vendor.o(submitNumberCode)
  141. } : {}, {
  142. k: studentView.value === "numberInput",
  143. o: common_vendor.s(contentWrapperStyle.value)
  144. }) : {});
  145. };
  146. }
  147. };
  148. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-1410bd6b"]]);
  149. wx.createPage(MiniProgramPage);
  150. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/checkin/index.js.map