login.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
  4. <h3 class="title">怀化高新区企业信用分级分类服务平台</h3>
  5. <el-form-item prop="username">
  6. <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
  7. <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
  8. </el-input>
  9. </el-form-item>
  10. <el-form-item prop="password">
  11. <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
  12. @keyup.enter="handleLogin">
  13. <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
  14. </el-input>
  15. </el-form-item>
  16. <el-form-item prop="code" v-if="captchaOnOff">
  17. <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%"
  18. @keyup.enter="handleLogin">
  19. <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
  20. </el-input>
  21. <div class="login-code">
  22. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  23. </div>
  24. </el-form-item>
  25. <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">记住密码</el-checkbox>
  26. <el-form-item style="width: 100%">
  27. <el-button :loading="loading" size="large" type="primary" style="width: 100%"
  28. @click.prevent="handleLogin">
  29. <span v-if="!loading">登 录</span>
  30. <span v-else>登 录 中...</span>
  31. </el-button>
  32. <div style="float: right" v-if="register">
  33. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  34. </div>
  35. </el-form-item>
  36. </el-form>
  37. <!-- 底部 -->
  38. <!-- <div class="el-login-footer">
  39. <span>Copyright © 2018 湖南顶壹信息技术有限公司 湘ICP备 18016393号-1</span>
  40. </div> -->
  41. </div>
  42. </template>
  43. <script setup>
  44. import {
  45. getCodeImg
  46. } from '@/api/login'
  47. import Cookies from 'js-cookie'
  48. import {
  49. encrypt,
  50. decrypt
  51. } from '@/utils/jsencrypt'
  52. const store = useStore()
  53. const router = useRouter()
  54. const {
  55. proxy
  56. } = getCurrentInstance()
  57. const loginForm = ref({
  58. username: '',
  59. password: '',
  60. rememberMe: false,
  61. code: '',
  62. uuid: ''
  63. })
  64. const loginRules = {
  65. username: [{
  66. required: true,
  67. trigger: 'blur',
  68. message: '请输入您的账号'
  69. }],
  70. password: [{
  71. required: true,
  72. trigger: 'blur',
  73. message: '请输入您的密码'
  74. }],
  75. code: [{
  76. required: true,
  77. trigger: 'change',
  78. message: '请输入验证码'
  79. }]
  80. }
  81. const codeUrl = ref('')
  82. const loading = ref(false)
  83. // 验证码开关
  84. const captchaOnOff = ref(true)
  85. // 注册开关
  86. const register = ref(true)
  87. const redirect = ref(undefined)
  88. function handleLogin() {
  89. proxy.$refs.loginRef.validate((valid) => {
  90. if (valid) {
  91. loading.value = true
  92. // 勾选了需要记住密码设置在cookie中设置记住用户名和命名
  93. if (loginForm.value.rememberMe) {
  94. Cookies.set('username', loginForm.value.username, {
  95. expires: 30
  96. })
  97. Cookies.set('password', encrypt(loginForm.value.password), {
  98. expires: 30
  99. })
  100. Cookies.set('rememberMe', loginForm.value.rememberMe, {
  101. expires: 30
  102. })
  103. } else {
  104. // 否则移除
  105. Cookies.remove('username')
  106. Cookies.remove('password')
  107. Cookies.remove('rememberMe')
  108. }
  109. // 调用action的登录方法
  110. store
  111. .dispatch('Login', loginForm.value)
  112. .then(() => {
  113. localStorage.setItem('username', loginForm.value.username)
  114. router.push({
  115. path: '/index'
  116. })
  117. })
  118. .catch(() => {
  119. loading.value = false
  120. loginForm.value.code = ''
  121. // 重新获取验证码
  122. if (captchaOnOff.value) {
  123. getCode()
  124. }
  125. })
  126. }
  127. })
  128. }
  129. function getCode() {
  130. getCodeImg().then((res) => {
  131. captchaOnOff.value =
  132. res.captchaOnOff === undefined ? true : res.captchaOnOff
  133. if (captchaOnOff.value) {
  134. codeUrl.value = 'data:image/gif;base64,' + res.img
  135. loginForm.value.uuid = res.uuid
  136. }
  137. })
  138. }
  139. function getCookie() {
  140. const username = Cookies.get('username')
  141. const password = Cookies.get('password')
  142. const rememberMe = Cookies.get('rememberMe')
  143. loginForm.value = {
  144. username: username === undefined ? loginForm.value.username : username,
  145. password: password === undefined ? loginForm.value.password : decrypt(password),
  146. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  147. }
  148. }
  149. getCode()
  150. getCookie()
  151. </script>
  152. <style lang="scss" scoped>
  153. .login {
  154. display: flex;
  155. justify-content: center;
  156. align-items: center;
  157. height: 100%;
  158. background-image: url('../assets/images/login-bg.jpg');
  159. background-size: cover;
  160. }
  161. .title {
  162. margin: 0px auto 30px auto;
  163. text-align: center;
  164. color: #707070;
  165. }
  166. .login-form {
  167. border-radius: 6px;
  168. background: #ffffff;
  169. width: 400px;
  170. padding: 25px 25px 5px 25px;
  171. .el-input {
  172. height: 40px;
  173. input {
  174. height: 40px;
  175. }
  176. }
  177. .input-icon {
  178. height: 39px;
  179. width: 14px;
  180. margin-left: 0px;
  181. }
  182. }
  183. .login-tip {
  184. font-size: 13px;
  185. text-align: center;
  186. color: #bfbfbf;
  187. }
  188. .login-code {
  189. width: 33%;
  190. height: 40px;
  191. float: right;
  192. img {
  193. cursor: pointer;
  194. vertical-align: middle;
  195. }
  196. }
  197. .el-login-footer {
  198. height: 40px;
  199. line-height: 40px;
  200. position: fixed;
  201. bottom: 0;
  202. width: 100%;
  203. text-align: center;
  204. color: #fff;
  205. font-family: Arial;
  206. font-size: 12px;
  207. letter-spacing: 1px;
  208. }
  209. .login-code-img {
  210. height: 40px;
  211. padding-left: 12px;
  212. }
  213. </style>