index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <el-form-item prop="username">
  5. <el-input
  6. v-model="loginForm.username"
  7. type="text"
  8. name="username"
  9. auto-complete="off"
  10. placeholder="账号"
  11. tabindex="1"
  12. maxlength="20"
  13. @input="onUsernameChange"
  14. >
  15. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
  16. </el-input>
  17. </el-form-item>
  18. <el-form-item prop="password">
  19. <el-input
  20. v-model="loginForm.password"
  21. type="password"
  22. auto-complete="off"
  23. placeholder="密码"
  24. tabindex="2"
  25. maxlength="20"
  26. >
  27. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
  28. </el-input>
  29. </el-form-item>
  30. <el-form-item prop="verifyCode">
  31. <el-input
  32. v-model="loginForm.verifyCode"
  33. auto-complete="off"
  34. placeholder="4位验证码"
  35. style="width: 63%"
  36. tabindex="3"
  37. maxlength="4"
  38. >
  39. <svg-icon slot="prefix" icon-class="verifyCode" class="el-input__icon input-icon"/>
  40. </el-input>
  41. <div class="login-code">
  42. <img :src="captchaUrl" @click="updateCaptcha" class="login-code-img"/>
  43. </div>
  44. </el-form-item>
  45. <el-form-item prop="mailbox">
  46. <el-input
  47. v-model="loginForm.mailbox"
  48. auto-complete="off"
  49. placeholder="6位邮箱口令"
  50. style="width: 63%"
  51. tabindex="4"
  52. maxlength="6"
  53. >
  54. <svg-icon slot="prefix" icon-class="mailbox" class="el-input__icon input-icon"/>
  55. </el-input>
  56. <div class="mail-box">
  57. <el-button type="success" size="small" @click.prevent="sendMailCode" :disabled="isMailSend">
  58. {{ sendBtnText }}
  59. </el-button>
  60. </div>
  61. </el-form-item>
  62. <el-form-item style="width:100%;">
  63. <el-button
  64. :loading="loading"
  65. size="medium"
  66. type="primary"
  67. style="width:100%;"
  68. @click.native.prevent="handleLogin"
  69. >
  70. <span v-if="!loading">登 录</span>
  71. <span v-else>登 录 中...</span>
  72. </el-button>
  73. </el-form-item>
  74. </el-form>
  75. </div>
  76. </template>
  77. <script>
  78. export default {
  79. name: 'Login',
  80. data() {
  81. return {
  82. loginForm: {
  83. // 用户名
  84. username: '',
  85. // 密码
  86. password: '',
  87. // 验证码
  88. verifyCode: '',
  89. // 邮箱验证码
  90. mailbox: ''
  91. },
  92. verifyuuid: '',
  93. // 是否已经发送了验证码
  94. isMailSend: false,
  95. // 计时器对象
  96. timer: null,
  97. // 倒数60秒
  98. counter: 60,
  99. // 文本
  100. sendBtnText: '点击发送邮箱',
  101. captchaUrl: '/getVerifyCode',
  102. captchaText: '',
  103. loginRules: {
  104. /* username: [{ required: true, trigger: 'blur', validator: validateUsername }],
  105. password: [{ required: true, trigger: 'blur', validator: validatePassword }]*/
  106. username: [{required: true, trigger: 'blur', message: '请输入用户名'}],
  107. password: [{required: true, trigger: 'blur', message: '请输入密码'}],
  108. verifyCode: [{required: true, trigger: 'blur', message: '请输入验证码'}, {
  109. min: 4,
  110. max: 4,
  111. message: '请输入4位验证码',
  112. trigger: 'blur'
  113. }],
  114. mailbox: [{required: true, trigger: 'blur', message: '请输入邮箱口令'}, {
  115. min: 6,
  116. max: 6,
  117. message: '请输入6位邮箱口令',
  118. trigger: 'blur'
  119. }]
  120. },
  121. loading: false,
  122. redirect: undefined
  123. }
  124. },
  125. watch: {
  126. $route: {
  127. handler: function (route) {
  128. this.redirect = route.query && route.query.redirect
  129. },
  130. immediate: true
  131. }
  132. },
  133. mounted() {
  134. this.getCaptcha()
  135. },
  136. methods: {
  137. // 当账号变化时,重置发送按钮
  138. onUsernameChange() {
  139. this.reset()
  140. },
  141. /**
  142. * 重置倒计时
  143. */
  144. reset() {
  145. // 重置按钮可用
  146. this.isMailSend = false
  147. // 重置文本内容
  148. this.sendBtnText = '点击发送邮箱'
  149. if (this.timer) {
  150. // 存在计时器对象,则清除
  151. clearInterval(this.timer)
  152. // 重置秒数,防止下次混乱
  153. this.counter = 60
  154. // 计时器对象重置为空
  155. this.timer = null
  156. }
  157. },
  158. /**
  159. * 倒计时
  160. */
  161. countDown() {
  162. // 将setInterval()方法赋值给前面定义的timer计时器对象,等用clearInterval()方法时方便清空这个计时器对象
  163. this.timer = setInterval(() => {
  164. // 替换文本,用es6里面的``这个来创建字符串模板,让秒实时改变
  165. this.sendBtnText = `(${this.counter}秒)后重新发送`
  166. this.counter--
  167. if (this.counter < 0) {
  168. // 当计时小于零时,取消该计时器
  169. this.reset()
  170. }
  171. }, 1000)
  172. },
  173. /**
  174. * 发送邮箱验证码
  175. */
  176. sendMailCode() {
  177. // 判断账户是否已经输入
  178. if (!this.loginForm.username) {
  179. this.$message.error('请输入账号')
  180. return false
  181. }
  182. // 将按钮禁用,防止再次点击
  183. this.isMailSend = true
  184. // 开始倒计时,60s之后才能再次点击
  185. this.countDown() // 这里实现倒计时的功能,文章下面开始介绍
  186. const param = new URLSearchParams()
  187. param.append('username', this.loginForm.username)
  188. this.$axios.post(
  189. '/getMailCode', param,
  190. ).then((res) => {
  191. this.$message.success('邮件发送成功')
  192. })
  193. },
  194. // 获取验证码
  195. getCaptcha() {
  196. // this.$axios.get('/sysFile/getFileContent',{params:{"path":path,"name":name}}).then
  197. this.$axios.get('/getVerifyCode').then((res) => {
  198. this.verifyuuid = res.data.uuid
  199. this.captchaUrl = 'data:image/gif;base64,' + res.data.imgBase64;
  200. this.captchaText = res.data.captchaText
  201. })
  202. },
  203. updateCaptcha() {
  204. // 更新验证码
  205. this.getCaptcha()
  206. },
  207. handleLogin() {
  208. this.$refs.loginForm.validate(valid => {
  209. if (valid) {
  210. this.loading = true
  211. let verifycodetemp = this.loginForm.verifyCode
  212. if (this.captchaText.toLowerCase() != verifycodetemp.toLowerCase()) {
  213. this.$message.error('验证码录入错误!')
  214. this.updateCaptcha()
  215. this.loading = false
  216. return
  217. }
  218. const param = new URLSearchParams()
  219. param.append('username', this.loginForm.username)
  220. param.append('password', this.loginForm.password)
  221. param.append('code', this.loginForm.verifyCode)
  222. param.append('verifyuuid', this.verifyuuid)
  223. param.append('mailbox', this.loginForm.mailbox)
  224. this.$axios.post('/user/login', param).then((res) => {
  225. // sessionStorage.setItem('token', data)
  226. // document.cookie = "token=" + data;
  227. // document.cookie = "user=".concat(this.loginForm.username)
  228. sessionStorage.setItem('token', res.data)
  229. console.log('login user is :' + this.loginForm.username)
  230. this.$router.push('/')
  231. this.loading = false
  232. }).catch((error) => {
  233. // 登录失败刷新验证码
  234. this.updateCaptcha()
  235. this.loginForm.verifyCode = ''
  236. this.loginForm.mailbox = ''
  237. this.reset()
  238. this.loading = false
  239. })
  240. } else {
  241. console.log('error submit!!')
  242. return false
  243. }
  244. })
  245. }
  246. }
  247. }
  248. </script>
  249. <style rel="stylesheet/scss" lang="scss">
  250. .login {
  251. display: flex;
  252. justify-content: center;
  253. align-items: center;
  254. height: 100%;
  255. background-image: url("../../assets/images/login-background.jpg");
  256. background-size: cover;
  257. }
  258. .title {
  259. margin: 0px auto 30px auto;
  260. text-align: center;
  261. color: #707070;
  262. }
  263. .login-form {
  264. border-radius: 6px;
  265. background: #ffffff;
  266. width: 400px;
  267. padding: 25px 25px 5px 25px;
  268. .el-input {
  269. height: 38px;
  270. input {
  271. height: 38px;
  272. }
  273. }
  274. .input-icon {
  275. height: 39px;
  276. width: 14px;
  277. margin-left: 2px;
  278. }
  279. }
  280. .login-tip {
  281. font-size: 13px;
  282. text-align: center;
  283. color: #bfbfbf;
  284. }
  285. .login-code {
  286. display: flex;
  287. justify-content: center;
  288. align-items: center;
  289. width: 33%;
  290. height: 38px;
  291. float: right;
  292. img {
  293. cursor: pointer;
  294. vertical-align: middle;
  295. }
  296. }
  297. .el-login-footer {
  298. height: 40px;
  299. line-height: 40px;
  300. position: fixed;
  301. bottom: 0;
  302. width: 100%;
  303. text-align: center;
  304. color: #fff;
  305. font-family: Arial;
  306. font-size: 12px;
  307. letter-spacing: 1px;
  308. }
  309. .login-code-img {
  310. height: 38px;
  311. width: 105px;
  312. }
  313. .mail-box {
  314. display: flex;
  315. justify-content: center;
  316. align-items: center;
  317. width: 36%;
  318. height: 38px;
  319. float: right;
  320. img {
  321. cursor: pointer;
  322. vertical-align: middle;
  323. }
  324. }
  325. </style>