codelogin.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.phone"
  12. auto-complete="off"
  13. :placeholder="$t('login.phone')">
  14. <i slot="prefix"
  15. class="icon-shouji"/>
  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="$t('login.code')">
  24. <i slot="prefix"
  25. class="icon-yanzhengma"
  26. style="margin-top:6px;"/>
  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">{{$t('login.submit')}}</el-button>
  39. </el-form-item>
  40. </el-form>
  41. </template>
  42. <script>
  43. import { isvalidatemobile } from "@/util/validate";
  44. import { mapGetters } from "vuex";
  45. export default {
  46. name: "codelogin",
  47. data() {
  48. const validatePhone = (rule, value, callback) => {
  49. if (isvalidatemobile(value)[0]) {
  50. callback(new Error(isvalidatemobile(value)[1]));
  51. } else {
  52. callback();
  53. }
  54. };
  55. const validateCode = (rule, value, callback) => {
  56. if (value.length !== 4) {
  57. callback(new Error("请输入4位数的验证码"));
  58. } else {
  59. callback();
  60. }
  61. };
  62. return {
  63. msgText: "",
  64. msgTime: "",
  65. msgKey: false,
  66. loginForm: {
  67. phone: "",
  68. code: ""
  69. },
  70. loginRules: {
  71. phone: [{ required: true, trigger: "blur", validator: validatePhone }],
  72. code: [{ required: true, trigger: "blur", validator: validateCode }]
  73. }
  74. };
  75. },
  76. created() {
  77. this.msgText = this.config.MSGINIT;
  78. this.msgTime = this.config.MSGTIME;
  79. },
  80. mounted() {},
  81. computed: {
  82. ...mapGetters(["tagWel"]),
  83. config() {
  84. return {
  85. MSGINIT: this.$t("login.msgText"),
  86. MSGSCUCCESS: this.$t("login.msgSuccess"),
  87. MSGTIME: 60
  88. };
  89. }
  90. },
  91. props: [],
  92. methods: {
  93. handleSend() {
  94. if (this.msgKey) return;
  95. this.msgText = this.msgTime + this.config.MSGSCUCCESS;
  96. this.msgKey = true;
  97. const time = setInterval(() => {
  98. this.msgTime--;
  99. this.msgText = this.msgTime + this.config.MSGSCUCCESS;
  100. if (this.msgTime === 0) {
  101. this.msgTime = this.config.MSGTIME;
  102. this.msgText = this.config.MSGINIT;
  103. this.msgKey = false;
  104. clearInterval(time);
  105. }
  106. }, 1000);
  107. },
  108. handleLogin() {
  109. this.$refs.loginForm.validate(valid => {
  110. if (valid) {
  111. this.$store.dispatch("LoginByPhone", this.loginForm).then(() => {
  112. this.$router.push({ path: this.tagWel.value });
  113. });
  114. }
  115. });
  116. }
  117. }
  118. };
  119. </script>
  120. <style>
  121. .msg-text {
  122. display: block;
  123. width: 60px;
  124. font-size: 12px;
  125. text-align: center;
  126. cursor: pointer;
  127. }
  128. .msg-text.display {
  129. color: #ccc;
  130. }
  131. </style>