index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div class="login-wrapper">
  3. <div class="login-container">
  4. <div class="login-main">
  5. <van-tabs v-model:active="appStore.ownApp">
  6. <van-tab title="水利督查" name="1"></van-tab>
  7. <van-tab title="水利稽察" name="2"></van-tab>
  8. </van-tabs>
  9. <van-form @submit="handleLogin">
  10. <van-cell-group>
  11. <van-field
  12. v-model="form.phone"
  13. label="手机号码"
  14. placeholder="请输入手机号码"
  15. :rules="[{ required: true, message: '请填写手机号码' }]"
  16. />
  17. <van-field
  18. v-model="form.imgCode"
  19. label="图片验证码"
  20. placeholder="请输入验证码"
  21. :rules="[{ required: true, message: '请填写验证码' }]"
  22. >
  23. <template #button>
  24. <van-image
  25. width="100"
  26. height="40"
  27. :src="captchaImage"
  28. @click="refreshCaptcha"
  29. />
  30. </template>
  31. </van-field>
  32. <van-field
  33. v-model="form.code"
  34. label="短信验证码"
  35. placeholder="请输入短信验证码"
  36. :rules="[{ required: true, message: '请填写短信验证码' }]"
  37. >
  38. <template #button>
  39. <van-button
  40. size="small"
  41. type="primary"
  42. :disabled="countDown > 0"
  43. @click="sendSmsCode"
  44. >
  45. {{ countDown > 0 ? `${countDown}秒后重试` : "获取验证码" }}
  46. </van-button>
  47. </template>
  48. </van-field>
  49. </van-cell-group>
  50. <div style="margin: 16px">
  51. <van-button round block type="primary" native-type="submit">
  52. 登录
  53. </van-button>
  54. </div>
  55. </van-form>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script setup>
  61. import { nextTick, onBeforeUnmount, onMounted, ref } from "vue";
  62. import { showToast } from "vant";
  63. import { useAppStore } from "@/stores/app";
  64. import { useUserStore } from "@/stores/user";
  65. import { getCaptchaImage } from "@/api/login";
  66. import { useRouter } from "vue-router";
  67. import { sendMessage } from "@/api/login";
  68. const appStore = useAppStore();
  69. const userStore = useUserStore();
  70. const router = useRouter();
  71. // 表单数据
  72. const form = ref({
  73. phone: "",
  74. imgCode: "",
  75. imgCodeId: "",
  76. code: "",
  77. });
  78. // 图片验证码
  79. const captchaImage = ref(null);
  80. const imgcodeid = ref(null);
  81. const countDown = ref(0);
  82. let countTimer = null;
  83. function refreshCaptcha() {
  84. getCaptchaImage().then(async (res) => {
  85. await nextTick();
  86. imgcodeid.value = res.data.imgCodeId;
  87. captchaImage.value = "data:image/jpg;base64," + res.data.img;
  88. });
  89. }
  90. function loginFn() {
  91. if (window.cordova) {
  92. window.app.plugin.getUserInfo((res) => {
  93. let encrypt = new window.JSEncrypt();
  94. encrypt.setPrivateKey(window.keyUtil.getPrivateKey());
  95. let access_token = encrypt.decrypt(res.access_token); // access_token
  96. let refresh_token = encrypt.decrypt(res.refresh_token); // refresh_token
  97. let userId = encrypt.decrypt(res.userId); // 用户ID
  98. let userName = encrypt.decrypt(res.userName); // 用户姓名
  99. let mobile = encrypt.decrypt(res.mobile); // 手机号码
  100. form.value.phone = mobile;
  101. // form.value.phone = "13960760931";
  102. handleLogin();
  103. // let idCard = encrypt.decrypt(res.idCard) // 身份证号
  104. // let sex = encrypt.decrypt(res.sex) // 性别
  105. // let email = encrypt.decrypt(res.email) // 邮箱
  106. // let avatarUrl = encrypt.decrypt(res.avatarUrl) // 头像
  107. // let depId = encrypt.decrypt(res.depId) // 科室ID
  108. // let depName = encrypt.decrypt(res.depName) // 科室名称
  109. // let orgId = encrypt.decrypt(res.orgId) // 部门ID
  110. // let orgName = encrypt.decrypt(res.orgName) // 部门名称
  111. });
  112. } else {
  113. console.warn("Cordova 未加载,当前运行在浏览器环境");
  114. }
  115. }
  116. function sendSmsCode() {
  117. if (!form.value.phone) {
  118. showToast("请输入手机号码");
  119. return;
  120. }
  121. if (!form.value.imgCode) {
  122. showToast("请输入图片验证码");
  123. return;
  124. }
  125. // 倒计时
  126. sendMessage(form.value.phone, imgcodeid.value, form.value.imgCode).then(
  127. (res) => {
  128. if (!res.success) {
  129. showToast(res.message);
  130. }
  131. else{
  132. countDown.value = 60;
  133. countTimer = setInterval(() => {
  134. countDown.value--;
  135. if (countDown.value <= 0) {
  136. clearInterval(countTimer);
  137. }
  138. }, 1000);
  139. }
  140. }
  141. );
  142. }
  143. // 处理登录
  144. function handleLogin() {
  145. if (!form.value.phone || !form.value.imgCode || !form.value.code) {
  146. showToast("请填写完整信息");
  147. return;
  148. }
  149. userStore
  150. .Login(form.value.phone, form.value.imgCode, form.value.code)
  151. .then(() => {
  152. console.log(appStore.ownApp);
  153. if (appStore.ownApp === "1") {
  154. router.push("/inspectDC");
  155. } else {
  156. router.push("/");
  157. }
  158. });
  159. }
  160. onMounted(() => {
  161. refreshCaptcha();
  162. loginFn();
  163. });
  164. // 组件卸载时清除定时器
  165. onBeforeUnmount(() => {
  166. if (countTimer) clearInterval(countTimer);
  167. });
  168. </script>
  169. <style lang="scss" scoped>
  170. .login-wrapper {
  171. width: 100%;
  172. height: 100%;
  173. display: flex;
  174. justify-content: center;
  175. align-items: center;
  176. background-image: url("@/assets/images/login_background.png");
  177. .login-container {
  178. padding: 16px;
  179. .login-main {
  180. border-radius: 10px;
  181. background-color: #fff;
  182. padding-bottom: 10px;
  183. overflow: hidden;
  184. }
  185. }
  186. }
  187. </style>