5a1302e03dde2f15e561f072b9058b7585697c8b.svn-base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div class="login">
  3. <!-- 手机号 -->
  4. <InputGroup
  5. v-model="phone"
  6. type="number"
  7. placeholder="手机号"
  8. :btn-title="btnTitle"
  9. :disabled="disabled"
  10. :error="errors.phone"
  11. @btnClick="getVerifyCode"
  12. />
  13. <!-- 输入验证码 -->
  14. <InputGroup
  15. v-model="verifyCode"
  16. type="number"
  17. placeholder="验证码"
  18. :error="errors.code"
  19. />
  20. <!-- 登录按钮 -->
  21. <div class="login_btn">
  22. <button :disabled="isClick" @click="handleLogin">登录</button>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import InputGroup from '@/components/InputGroup' // 引入封装的组件
  28. export default {
  29. name: 'Operation',
  30. components: { InputGroup },
  31. data() {
  32. return {
  33. phone: '', // 手机号
  34. verifyCode: '', // 验证码
  35. btnTitle: '获取验证码',
  36. disabled: false, // 是否可点击
  37. errors: {} // 验证提示信息
  38. }
  39. },
  40. computed: {
  41. // 手机号和验证码都不能为空
  42. isClick() {
  43. if (!this.phone || !this.verifyCode) {
  44. return true
  45. } else {
  46. return false
  47. }
  48. }
  49. },
  50. created() { },
  51. methods: {
  52. getVerifyCode() {
  53. // 获取验证码
  54. if (this.validatePhone()) {
  55. this.validateBtn()
  56. // 发送网络请求
  57. this.$axios.post('/api/posts/sms_send', { tpl_id: '', key: '', phone: this.phone }).then(res => {
  58. console.log(res)
  59. })
  60. }
  61. },
  62. validatePhone() {
  63. // 判断输入的手机号是否合法
  64. if (!this.phone) {
  65. this.errors = {
  66. phone: '手机号码不能为空'
  67. }
  68. // return false
  69. } else if (!/^1[345678]\d{9}$/.test(this.phone)) {
  70. this.errors = {
  71. phone: '请输入正确是手机号'
  72. }
  73. // return false
  74. } else {
  75. this.errors = {}
  76. return true
  77. }
  78. },
  79. validateBtn() {
  80. // 倒计时
  81. let time = 60
  82. const timer = setInterval(() => {
  83. if (time == 0) {
  84. clearInterval(timer)
  85. this.disabled = false
  86. this.btnTitle = '获取验证码'
  87. } else {
  88. this.btnTitle = time + '秒后重试'
  89. this.disabled = true
  90. time--
  91. }
  92. }, 1000)
  93. },
  94. handleLogin() {
  95. // 点击发送
  96. this.errors = {}
  97. this.$axios.post('http://192.168.1.84:8081/authdata/api/posts/sms_back', {
  98. phone: this.phone,
  99. code: this.verifyCode
  100. }).then(res => {
  101. console.log(res)
  102. localStorage.setItem('ele_login', true)
  103. this.$router.push('/')
  104. }).catch(error => {
  105. // 返回错误信息
  106. this.errors = {
  107. code: error.response.data.msg
  108. }
  109. })
  110. }
  111. }
  112. }
  113. </script>