杨磊
4 天以前 ef37c59e055a990ce247b265b27d3fcef430a243
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
  <div>
    <div class="iconBox" v-if="state == 'loading'">
      <i class="el-icon-loading" style="color: #666"></i>
      <p>正在绑定微信...</p>
    </div>
    <div class="iconBox" v-if="state == 'success'">
      <i class="el-icon-success" style="color: #67c23a"></i>
      <p>绑定成功!</p>
      <p class="backBox">
        即将返回({{ time }}s)<el-button
          type="primary"
          size="mini"
          @click="goBack"
          >立即返回</el-button
        >
      </p>
    </div>
    <div class="iconBox" v-if="state == 'error'">
      <i class="el-icon-error" style="color: #f56c6c"></i>
      <p>绑定失败,该微信已被绑定!</p>
      <p class="backBox">
        即将返回({{ time }}s)<el-button
          type="primary"
          size="mini"
          @click="goBack"
          >立即返回</el-button
        >
      </p>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      state: "loading",
      time: 5
    };
  },
  created() {
    var url = window.location.href;
    if (url.indexOf("WeChatScanningCodeBind") > -1) {
      var querys = url.substring(url.indexOf("?") + 1).split("&");
      var result = {};
      for (var i = 0; i < querys.length; i++) {
        var temp = querys[i].split("=");
        if (temp.length < 2) {
          result[temp[0]] = "";
        } else {
          result[temp[0]] = temp[1];
        }
      }
      if (result && result.code) {
        this.MG.identity
          .bindingWeChat({
            code: result.code
          })
          .then((res) => {
            if (res) {
              this.state = "success";
            } else {
              this.state = "error";
            }
            this.setTime();
          });
      }
    }
    this.setTime();
  },
  methods: {
    setTime() {
      if (this.timer) clearInterval(this.timer);
      this.time = 5;
      this.timer = setInterval(() => {
        this.time--;
        if (this.time == 0) {
          this.goBack();
        }
      }, 1000);
    },
    goBack() {
      if (this.state == "success") {
        this.$router.push({
          path: "/personalCenter"
        });
      }
      if (this.state == "error") {
        this.$router.push({
          path: "/bindWeChat"
        });
      }
    }
  },
  beforeRouteLeave(to, form, next) {
    if (this.timer) clearInterval(this.timer);
    next();
  }
};
</script>
<style lang="less" scoped>
.iconBox {
  padding-top: 100px;
  text-align: center;
  i {
    font-size: 60px;
    margin-bottom: 30px;
  }
  p {
    font-size: 24px;
    margin-bottom: 50px;
  }
  .backBox {
    font-size: 14px;
  }
}
</style>