ReBindingMail.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 {logout, 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. window.alert('邮箱修改成功, 请重新登录');
  84. logout().then((res) => {
  85. window.location.href = process.env.LOGIN_URL;
  86. // this.$router.push('/home')
  87. })
  88. }
  89. })
  90. },
  91. getVerifyCode() {
  92. let params = {
  93. id: this.user.id,
  94. email: this.emailBindingForm.email
  95. }
  96. Http.put('/api/verify/email', params).then((res) => {
  97. this.hasVerifyCode = true;
  98. let _this = this;
  99. if (res.msg == "ERROR") {
  100. notify('error', '验证码获取失败:' + res.data);
  101. }else{
  102. notify('success', res.data);
  103. let codeTimer = setInterval(function () {
  104. if (_this.codeTime > 0) {
  105. _this.codeTime--;
  106. } else {
  107. clearInterval(codeTimer);
  108. _this.hasVerifyCode = false;
  109. this.codeTime = 10;
  110. }
  111. }, 1000)
  112. }
  113. }).catch(err => {
  114. notify('error', '获取验证码失败:' + err.data);
  115. })
  116. },
  117. showLoading() {
  118. this.loading = true
  119. },
  120. hideLoading() {
  121. this.loading = false
  122. },
  123. },
  124. mounted() {
  125. this.setUserInfo();
  126. }
  127. }
  128. </script>
  129. <style scoped>
  130. </style>