BindingMobile.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div style="margin-top: 20px" v-loading="loading">
  3. <el-form label-width="80px" style="width:400px" :rules="rules" :model="phoneBindingForm">
  4. <el-form-item label="手机号码" prop="phone">
  5. <el-input v-model="phoneBindingForm.phone"></el-input>
  6. </el-form-item>
  7. <el-form-item label="验证码" prop="verifyCode">
  8. <el-input placeholder="验证码内容" v-model="phoneBindingForm.verifyCode" class="input-with-select">
  9. <el-button slot="append" @click="getVerifyCode" :disabled="hasVerifyCode">{{hasVerifyCode ? codeTime :
  10. '获取验证码'}}
  11. </el-button>
  12. </el-input>
  13. <span v-if="hasVerifyCode">验证码已发送到您手机上</span>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button type="primary" plain @click="bindingMobile">完成绑定</el-button>
  17. </el-form-item>
  18. </el-form>
  19. </div>
  20. </template>
  21. <script>
  22. import Http from '@/js/http.js'
  23. import {storageGet} from '@/js/index'
  24. import {notify} from '@/constants/index'
  25. export default {
  26. name: "BindingMobile",
  27. data() {
  28. return {
  29. phoneBindingForm: {
  30. phone: '',
  31. verifyCode: ''
  32. },
  33. loading: false,
  34. hasVerifyCode: false,
  35. codeTime: 60,
  36. user: {},
  37. rules: {
  38. verifyCode: [
  39. {required: true, message: '请输入验证码', trigger: 'blur'},
  40. {min: 6, max: 6, message: '验证码长度为6个字符', trigger: 'blur'}
  41. ],
  42. phone: [
  43. {required: true, message: '请输入手机号', trigger: 'blur'},
  44. {min: 11, max: 11, message: '手机号不合法', trigger: 'blur'}
  45. ],
  46. }
  47. }
  48. },
  49. methods: {
  50. setUserInfo() {
  51. let userId = storageGet('user') && storageGet('user').userVO.id;
  52. Http.get(`/api/user/${userId}`).then(res => {
  53. this.user = res.userVO
  54. })
  55. },
  56. getVerifyCode() {
  57. let params = {
  58. id: this.user.id,
  59. mobile: this.phoneBindingForm.phone
  60. }
  61. Http.put('/api/verify/mobile', params).then((res) => {
  62. if (res.msg == "ERROR") {
  63. notify('error', res.data);
  64. } else {
  65. notify('success', res.data);
  66. this.hasVerifyCode = true;
  67. let _this = this;
  68. let codeTimer = setInterval(function () {
  69. if (_this.codeTime > 0) {
  70. _this.codeTime--;
  71. } else {
  72. clearInterval(codeTimer);
  73. _this.hasVerifyCode = false;
  74. this.codeTime = 10;
  75. }
  76. }, 1000)
  77. }
  78. }).catch(err => {
  79. notify('error', '绑定手机失败:' + err.data);
  80. })
  81. },
  82. bindingMobile() {
  83. let params = {
  84. id: this.user.id,
  85. mobile: this.phoneBindingForm.phone,
  86. verifyCode: this.phoneBindingForm.verifyCode
  87. };
  88. Http.put('/api/user/mobile', params).then(res => {
  89. if (res.msg == "ERROR") {
  90. notify('error', '绑定失败:' + res.data);
  91. this.active = 1;
  92. this.phoneBindingForm.phone = '';
  93. this.phoneBindingForm.verifyCode = '';
  94. this.codeTime = 60;
  95. this.hasVerifyCode = false;
  96. } else {
  97. notify('success', '绑定成功');
  98. this.$router.push({path: '/personal/phoneBinding'});
  99. }
  100. }).catch(err => {
  101. notify('error', err.data);
  102. })
  103. },
  104. showLoading() {
  105. this.loading = true
  106. },
  107. hideLoading() {
  108. this.loading = false
  109. },
  110. },
  111. mounted() {
  112. this.setUserInfo()
  113. }
  114. }
  115. </script>
  116. <style scoped>
  117. </style>