codelogin.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <el-form class="login-form"
  3. status-icon
  4. :rules="loginRules"
  5. ref="loginForm"
  6. :model="loginForm"
  7. label-width="0">
  8. <el-form-item prop="phone">
  9. <el-input size="small"
  10. @keyup.enter.native="handleLogin"
  11. v-model="loginForm.mobile"
  12. auto-complete="off"
  13. placeholder="请输入手机号码">
  14. <i slot="prefix"
  15. class="icon-shouji"></i>
  16. </el-input>
  17. </el-form-item>
  18. <el-form-item prop="code">
  19. <el-input size="small"
  20. @keyup.enter.native="handleLogin"
  21. v-model="loginForm.code"
  22. auto-complete="off"
  23. placeholder="请输入验证码">
  24. <i slot="prefix"
  25. class="icon-yanzhengma"
  26. style="margin-top:6px;"></i>
  27. <template slot="append">
  28. <span @click="handleSend"
  29. class="msg-text"
  30. :class="[{display:msgKey}]">{{msgText}}</span>
  31. </template>
  32. </el-input>
  33. </el-form-item>
  34. <el-form-item>
  35. <el-button size="small"
  36. type="primary"
  37. @click.native.prevent="handleLogin"
  38. class="login-submit">登录</el-button>
  39. </el-form-item>
  40. </el-form>
  41. </template>
  42. <script>
  43. const MSGINIT = "发送验证码",
  44. MSGSCUCCESS = "${time}秒后重发",
  45. MSGTIME = 60;
  46. import { isvalidatemobile } from "@/util/validate";
  47. import { mapGetters } from "vuex";
  48. import request from '@/router/axios'
  49. export default {
  50. name: "codelogin",
  51. data () {
  52. const validatePhone = (rule, value, callback) => {
  53. if (isvalidatemobile(value)[0]) {
  54. callback(new Error(isvalidatemobile(value)[1]));
  55. } else {
  56. callback();
  57. }
  58. };
  59. const validateCode = (rule, value, callback) => {
  60. if (value.length != 4) {
  61. callback(new Error("请输入4位数的验证码"));
  62. } else {
  63. callback();
  64. }
  65. };
  66. return {
  67. msgText: MSGINIT,
  68. msgTime: MSGTIME,
  69. msgKey: false,
  70. loginForm: {
  71. mobile: '',
  72. code: ''
  73. },
  74. loginRules: {
  75. mobile: [{ required: true, trigger: 'blur', validator: validatePhone }],
  76. code: [{ required: true, trigger: 'blur', validator: validateCode }]
  77. }
  78. };
  79. },
  80. created () { },
  81. mounted () { },
  82. computed: {
  83. ...mapGetters(["tagWel"])
  84. },
  85. props: [],
  86. methods: {
  87. handleSend() {
  88. if (this.msgKey) return
  89. request({
  90. url: '/admin/mobile/' + this.loginForm.mobile,
  91. method: 'get'
  92. }).then(response => {
  93. console.log(response.data.data)
  94. if (response.data.data) {
  95. this.$message.success('验证码发送成功')
  96. } else {
  97. this.$message.error(response.data.msg)
  98. }
  99. })
  100. this.msgText = MSGSCUCCESS.replace('${time}', this.msgTime)
  101. this.msgKey = true
  102. const time = setInterval(() => {
  103. this.msgTime--
  104. this.msgText = MSGSCUCCESS.replace('${time}', this.msgTime)
  105. if (this.msgTime == 0) {
  106. this.msgTime = MSGTIME
  107. this.msgText = MSGINIT
  108. this.msgKey = false
  109. clearInterval(time)
  110. }
  111. }, 1000)
  112. },
  113. handleLogin () {
  114. this.$refs.loginForm.validate(valid => {
  115. if (valid) {
  116. this.$store.dispatch("LoginByPhone", this.loginForm).then(() => {
  117. this.$router.push({ path: this.tagWel.value });
  118. });
  119. }
  120. });
  121. }
  122. }
  123. };
  124. </script>
  125. <style>
  126. .msg-text {
  127. display: block;
  128. width: 60px;
  129. font-size: 12px;
  130. text-align: center;
  131. cursor: pointer;
  132. }
  133. .msg-text.display {
  134. color: #ccc;
  135. }
  136. </style>