BindingMail.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div style="margin-top: 20px" v-loading="loading">
  3. <el-form label-width="80px" style="width:400px" :rules="rules" :model="mailBindingForm">
  4. <el-form-item label="邮箱号" prop="mail">
  5. <el-input v-model="mailBindingForm.mail"></el-input>
  6. </el-form-item>
  7. <el-form-item label="验证码" prop="verifyCode">
  8. <el-input placeholder="验证码内容" v-model="mailBindingForm.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="bindingEmail ">完成绑定</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: "BindingMail",
  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. mailBindingForm:{
  41. mail:'',
  42. verifyCode:''
  43. },
  44. loading: false,
  45. hasVerifyCode:false,
  46. codeTime:60,
  47. user:{},
  48. rules: {
  49. verifyCode: [
  50. {required: true, message: '请输入验证码', trigger: 'blur'},
  51. {min:6, max: 6, message: '验证码长度为6个字符', trigger: 'blur'}
  52. ],
  53. mail: [
  54. {required: true, message: '请输入邮箱', trigger: 'blur'},
  55. { validator: isEmail, trigger: "blur" }
  56. ],
  57. }
  58. }
  59. },
  60. methods:{
  61. setUserInfo() {
  62. let userId = storageGet('user')&&storageGet('user').userVO.id;
  63. Http.get(`/api/user/${userId}`).then(res=>{
  64. this.user = res.userVO
  65. })
  66. },
  67. getVerifyCode(){
  68. let params = {
  69. id:this.user.id,
  70. email:this.mailBindingForm.mail
  71. }
  72. Http.put('/api/verify/email',params).then((res)=>{
  73. if(res.msg == "ERROR"){
  74. notify('error', res.data);
  75. }else{
  76. notify('success', res.data);
  77. this.hasVerifyCode = true;
  78. let _this = this;
  79. let codeTimer = setInterval(function () {
  80. if(_this.codeTime > 0 ){
  81. _this.codeTime--;
  82. }else{
  83. clearInterval(codeTimer);
  84. _this.hasVerifyCode = false;
  85. this.codeTime = 10;
  86. }
  87. },1000)
  88. }
  89. }).catch(err=> {
  90. notify('error', '获取验证码失败:' + err.data);
  91. })
  92. },
  93. bindingEmail(){
  94. this.$refs['mailBindingForm'].validate(valid => {
  95. if (!valid) {
  96. this.showLoading();
  97. let params = {
  98. id:this.user.id,
  99. email:this.mailBindingForm.mail,
  100. verifyCode: this.mailBindingForm.verifyCode
  101. };
  102. Http.put('/api/user/email',params).then(res=>{
  103. this.hideLoading();
  104. if(res.msg == "ERROR"){
  105. notify('error', '绑定失败:' + res.data);
  106. this.codeTime = 60;
  107. this.hasVerifyCode = false;
  108. }else{
  109. notify('success', '绑定成功');
  110. this.$router.push({path:'/personal/mailBinding'});
  111. }
  112. }).catch(err=> {
  113. this.hideLoading();
  114. notify('error', err.data);
  115. })
  116. }
  117. })
  118. },
  119. showLoading() {
  120. this.loading = true
  121. },
  122. hideLoading() {
  123. this.loading = false
  124. },
  125. },
  126. mounted() {
  127. this.setUserInfo()
  128. }
  129. }
  130. </script>
  131. <style scoped>
  132. </style>