ReBindingMail.vue 3.8 KB

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