ReBindingMail.vue 4.2 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. import {login_url} from '../../config/index'
  24. export default {
  25. name: "ReBindingMail",
  26. data() {
  27. let isEmail = (rule, value, callback) => {
  28. if (!value) {
  29. callback();
  30. } else {
  31. const reg = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/;
  32. const email = reg.test(value);
  33. if (!email) {
  34. callback(new Error("请输入正确的邮箱格式,注意去掉前后的空格"));
  35. } else {
  36. callback();
  37. }
  38. }
  39. };
  40. return {
  41. active: 0,
  42. user: {},
  43. loading: false,
  44. emailBindingForm: {
  45. email: '',
  46. verifyCode: ''
  47. },
  48. hasVerifyCode: false,
  49. codeTime: 60,
  50. rules: {
  51. verifyCode: [
  52. {required: true, message: '请输入验证码', trigger: 'blur'},
  53. {min:6, max: 6, message: '验证码长度为6个字符', trigger: 'blur'}
  54. ],
  55. email: [
  56. {required: true, message: '请输入邮箱', trigger: 'blur'},
  57. { validator: isEmail, trigger: "blur" }
  58. ],
  59. },
  60. }
  61. },
  62. methods: {
  63. setUserInfo() {
  64. let userId = storageGet('user') && storageGet('user').userVO.id;
  65. Http.get(`/api/user/${userId}`).then(res => {
  66. this.user = res.userVO
  67. })
  68. },
  69. reBindingMail() {
  70. this.showLoading();
  71. let params = {
  72. "id": this.user.id,
  73. "email": this.emailBindingForm.email,
  74. "verifyCode": this.emailBindingForm.verifyCode
  75. };
  76. Http.put('/api/user/email', params).then((res) => {
  77. this.hideLoading();
  78. if (res.msg == "ERROR") {
  79. notify('error', '绑定失败:' + res.data);
  80. this.codeTime = 60;
  81. this.hasVerifyCode = false;
  82. } else {
  83. // notify('success', '绑定成功');
  84. window.alert('邮箱修改成功, 请重新登录');
  85. logout().then((res) => {
  86. window.location.href = login_url;
  87. // this.$router.push('/home')
  88. })
  89. }
  90. })
  91. },
  92. getVerifyCode() {
  93. let params = {
  94. id: this.user.id,
  95. email: this.emailBindingForm.email
  96. }
  97. Http.put('/api/verify/email', params).then((res) => {
  98. let _this = this;
  99. if (res.msg == "ERROR") {
  100. notify('error', '验证码获取失败:' + res.data);
  101. }else{
  102. this.hasVerifyCode = true;
  103. notify('success', res.data);
  104. let codeTimer = setInterval(function () {
  105. if (_this.codeTime > 0) {
  106. _this.codeTime--;
  107. } else {
  108. clearInterval(codeTimer);
  109. _this.hasVerifyCode = false;
  110. this.codeTime = 10;
  111. }
  112. }, 1000)
  113. }
  114. }).catch(err => {
  115. notify('error', '获取验证码失败:' + err.data);
  116. })
  117. },
  118. showLoading() {
  119. this.loading = true
  120. },
  121. hideLoading() {
  122. this.loading = false
  123. },
  124. },
  125. mounted() {
  126. this.setUserInfo();
  127. }
  128. }
  129. </script>
  130. <style scoped>
  131. </style>