login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <div class="login">
  3. <!-- -->
  4. <el-form v-if="isFlag" ref="loginFirstRef" :model="loginForm" :rules="loginRules" class="login-form">
  5. <h3 class="title">中华人民共和国海事局</h3>
  6. <!-- 步骤一 -->
  7. <el-form-item prop="username">
  8. <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
  9. <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
  10. </el-input>
  11. </el-form-item>
  12. <el-form-item prop="password">
  13. <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
  14. @keyup.enter="handleAllLogin">
  15. <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
  16. </el-input>
  17. </el-form-item>
  18. <el-form-item prop="code" v-if="captchaEnabled">
  19. <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%"
  20. @keyup.enter="handleAllLogin">
  21. <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
  22. </el-input>
  23. <div class="login-code">
  24. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  25. </div>
  26. </el-form-item>
  27. <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">记住密码</el-checkbox>
  28. <el-form-item style="width: 100%">
  29. <el-button :loading="loading" size="large" type="primary" style="width: 100%"
  30. @click.prevent="handleAllLogin">
  31. <!-- <span v-if="!loading">登 录</span>
  32. <span v-else>登 录 中...</span> -->
  33. 登 录
  34. </el-button>
  35. <div style="float: right" v-if="register">
  36. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  37. </div>
  38. </el-form-item>
  39. </el-form>
  40. <!-- 步骤二 -->
  41. <el-form v-else ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
  42. <h3 class="title">中华人民共和国海事局</h3>
  43. <el-form-item prop="phone">
  44. <el-input v-model="loginForm.phone" type="text" size="large" auto-complete="off" placeholder="手机号"
  45. :disabled="true">
  46. <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
  47. </el-input>
  48. </el-form-item>
  49. <el-form-item style="width: 100%">
  50. <div class="flex">
  51. <el-input v-model="loginForm.code" class="mr10" type="text" size="large" auto-complete="off"
  52. placeholder="请输入验证码">
  53. <template #prefix><svg-icon icon-class="validCode"
  54. class="el-input__icon input-icon" /></template>
  55. </el-input>
  56. <el-button size="large" type="primary" style="width: 30%" @click.prevent="getVerificationCode">
  57. {{ countdownText }}
  58. </el-button>
  59. </div>
  60. </el-form-item>
  61. <el-form-item style="width: 100%">
  62. <el-button :loading="loading" size="large" type="primary" style="width: 100%"
  63. @click.prevent="handleEndLogin">
  64. <span v-if="!loading">登 录</span>
  65. <span v-else>登 录 中...</span>
  66. </el-button>
  67. </el-form-item>
  68. </el-form>
  69. <!-- 底部 -->
  70. <!-- <div class="el-login-footer">
  71. <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
  72. </div> -->
  73. </div>
  74. </template>
  75. <script setup lang="ts">
  76. import { getCodeImg, getPhoneCode, getPublicKey, loginFirst } from '@/api/login';
  77. import Cookies from 'js-cookie';
  78. import { encrypt, decrypt } from '@/utils/jsencrypt';
  79. import useUserStore from '@/store/modules/user';
  80. import { useRouter } from 'vue-router';
  81. import { FormInstance } from 'element-plus';
  82. import { ref } from 'vue';
  83. const userStore = useUserStore();
  84. const router = useRouter();
  85. const loginForm = ref<any>({
  86. username: '',
  87. password: '',
  88. rememberMe: false,
  89. code: '',
  90. uuid: '',
  91. phone: '',
  92. });
  93. const loginRules = {
  94. username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  95. password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
  96. code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
  97. };
  98. const codeUrl = ref('');
  99. const loading = ref(false);
  100. // 验证码开关
  101. const captchaEnabled = ref(true);
  102. // 注册开关
  103. const register = ref(false);
  104. const redirect = ref(undefined);
  105. const loginRef = ref<FormInstance>();
  106. const loginFirstRef = ref<FormInstance>();
  107. // -------------- 获取验证码 倒计时60秒 开始 ----------------
  108. const env = process.env.NODE_ENV;
  109. const isFlag = ref(true);
  110. let countdown = ref(60);
  111. const isCountingDown = ref(false);
  112. const countdownText = computed(() => {
  113. return isCountingDown.value ? `${countdown.value}s 后重新获取` : '获取验证码';
  114. });
  115. const getVerificationCode = async () => {
  116. if (!isCountingDown.value) {
  117. await getPhoneCode({ uuid: loginForm.value.uuid }).then((res: any) => {
  118. console.log(res);
  119. if (res.code === 200) {
  120. ElMessage.success('验证码发送成功');
  121. } else {
  122. ElMessage.error('验证码发送失败,请稍后重试');
  123. }
  124. });
  125. isCountingDown.value = true;
  126. countdown.value = 60;
  127. const intervalId = setInterval(() => {
  128. countdown.value--;
  129. if (countdown.value <= 0) {
  130. isCountingDown.value = false;
  131. clearInterval(intervalId);
  132. }
  133. }, 1000);
  134. }
  135. };
  136. const publicKey =
  137. 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
  138. 'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==';
  139. const privateKey =
  140. 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
  141. '7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
  142. 'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
  143. 'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
  144. 'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
  145. 'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
  146. 'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
  147. 'UP8iWi1Qw0Y=';
  148. // -------------- 获取验证码 倒计时60秒 end ----------------
  149. // 有下一步的登陆
  150. const handleNext = () => {
  151. loginFirstRef.value?.validate(valid => {
  152. if (valid) {
  153. loading.value = true;
  154. // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
  155. if (loginForm.value.rememberMe) {
  156. Cookies.set('username', loginForm.value.username, { expires: 30 });
  157. const enPwd = encrypt(loginForm.value.password, publicKey);
  158. // const enPwd = loginForm.value.password;
  159. if (enPwd) {
  160. Cookies.set('password', enPwd, { expires: 30 });
  161. }
  162. if (loginForm.value.rememberMe) {
  163. Cookies.set('rememberMe', String(loginForm.value.rememberMe), { expires: 30 });
  164. }
  165. } else {
  166. // 否则移除
  167. Cookies.remove('username');
  168. Cookies.remove('password');
  169. Cookies.remove('rememberMe');
  170. }
  171. // 调用action的登录方法
  172. userStore
  173. .phoneLogin(loginForm.value)
  174. .then((res: any) => {
  175. console.log(res);
  176. // router.push({ path: redirect.value || '/' });
  177. isFlag.value = false;
  178. loading.value = false;
  179. loginForm.value.code = '';
  180. loginForm.value.uuid = res.uuid;
  181. loginForm.value.phone = res.phone;
  182. })
  183. .catch(() => {
  184. loading.value = false;
  185. // 重新获取验证码
  186. if (captchaEnabled.value) {
  187. getCode();
  188. }
  189. });
  190. }
  191. });
  192. };
  193. // 有下一步的最后一步
  194. const handleEndLogin = () => {
  195. userStore
  196. .phoneLogin(loginForm.value)
  197. .then((res: any) => {
  198. if (res.token) {
  199. ElMessage.success('登陆成功');
  200. router.push({ path: redirect.value || '/' });
  201. } else {
  202. ElMessage.error('登陆失败');
  203. }
  204. loading.value = false;
  205. })
  206. .catch(() => {
  207. loading.value = false;
  208. });
  209. };
  210. // 无下一步的登陆
  211. function handleLogin() {
  212. loginFirstRef.value?.validate(valid => {
  213. if (valid) {
  214. loading.value = true;
  215. // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
  216. if (loginForm.value.rememberMe) {
  217. Cookies.set('username', loginForm.value.username, { expires: 30 });
  218. const enPwd = encrypt(loginForm.value.password, publicKey);
  219. if (enPwd) {
  220. Cookies.set('password', enPwd, { expires: 30 });
  221. }
  222. if (loginForm.value.rememberMe) {
  223. Cookies.set('rememberMe', String(loginForm.value.rememberMe), { expires: 30 });
  224. }
  225. } else {
  226. // 否则移除
  227. Cookies.remove('username');
  228. Cookies.remove('password');
  229. Cookies.remove('rememberMe');
  230. }
  231. // 调用action的登录方法
  232. userStore
  233. .login(loginForm.value)
  234. .then(() => {
  235. router.push({ path: redirect.value || '/' });
  236. })
  237. .catch(() => {
  238. loading.value = false;
  239. // 重新获取验证码
  240. if (captchaEnabled.value) {
  241. getCode();
  242. }
  243. });
  244. }
  245. });
  246. }
  247. // 登陆按钮
  248. const handleAllLogin = () => {
  249. // 本地
  250. // if (env === 'development') {
  251. // handleLogin();
  252. // } else {
  253. // handleNext();
  254. // }
  255. // handleNext();
  256. if (phoneCaptchaEnabled.value) {
  257. handleNext();
  258. } else {
  259. handleLogin();
  260. }
  261. };
  262. // 是否有手机号
  263. const phoneCaptchaEnabled = ref(false);
  264. function getCode() {
  265. getCodeImg().then((res: any) => {
  266. captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
  267. phoneCaptchaEnabled.value = res.phoneCaptchaEnabled;
  268. if (captchaEnabled.value) {
  269. codeUrl.value = 'data:image/gif;base64,' + res.img;
  270. loginForm.value.uuid = res.uuid;
  271. }
  272. });
  273. }
  274. function getCookie() {
  275. const username = Cookies.get('username');
  276. const password = Cookies.get('password');
  277. const rememberMe = Cookies.get('rememberMe');
  278. // let publicKey = '';
  279. // getPublicKey().then((res: any) => {
  280. // publicKey = res.publicKey;
  281. // });
  282. loginForm.value = {
  283. username: username === undefined ? loginForm.value.username : username,
  284. password: password === undefined ? loginForm.value.password : decrypt(password, privateKey) || '',
  285. // password: password === undefined ? loginForm.value.password : password || '',
  286. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  287. };
  288. }
  289. getCode();
  290. getCookie();
  291. </script>
  292. <style lang="scss" scoped>
  293. .login {
  294. display: flex;
  295. justify-content: center;
  296. align-items: center;
  297. height: 100%;
  298. background-image: url('../assets/images/login-background.jpg');
  299. background-size: cover;
  300. }
  301. .title {
  302. margin: 0px auto 30px auto;
  303. text-align: center;
  304. color: #707070;
  305. }
  306. .login-form {
  307. border-radius: 6px;
  308. background: #ffffff;
  309. width: 400px;
  310. padding: 25px 25px 5px 25px;
  311. .el-input {
  312. height: 40px;
  313. input {
  314. height: 40px;
  315. }
  316. }
  317. .input-icon {
  318. height: 39px;
  319. width: 14px;
  320. margin-left: 0px;
  321. }
  322. }
  323. .login-tip {
  324. font-size: 13px;
  325. text-align: center;
  326. color: #bfbfbf;
  327. }
  328. .login-code {
  329. width: 33%;
  330. height: 40px;
  331. float: right;
  332. img {
  333. cursor: pointer;
  334. vertical-align: middle;
  335. }
  336. }
  337. .el-login-footer {
  338. height: 40px;
  339. line-height: 40px;
  340. position: fixed;
  341. bottom: 0;
  342. width: 100%;
  343. text-align: center;
  344. color: #fff;
  345. font-family: Arial;
  346. font-size: 12px;
  347. letter-spacing: 1px;
  348. }
  349. .login-code-img {
  350. height: 40px;
  351. padding-left: 12px;
  352. }
  353. </style>