Blame view

src/views/binding/binDing.vue 4.75 KB
94c1e6a3   liuqimichale   微信公众号 个人页面集合
1
2
3
4
5
6
7
8
9
10
  <template>
    <div>
  
      <mt-field label="手机号" placeholder="请输入正确的手机号" type="tel" v-model="phone" :attr="{ maxlength: 13 }"></mt-field>
  
      <mt-field label="验证码" v-model="codeText">
        <div class="getCode" @click="getCode">{{timeNum}}</div>
      </mt-field>
  
      <div style="margin-top: 34px" class="leftRightPadding">
6ea1f7ef   刘淇   个人中心
11
        <van-button type="info" block @click="bindPhoneHandle">绑定</van-button>
94c1e6a3   liuqimichale   微信公众号 个人页面集合
12
13
14
15
16
      </div>
    </div>
  </template>
  
  <script>
6ea1f7ef   刘淇   个人中心
17
18
  
  import { sendverificode, bindCustByOpenId, getOpenIdByCode, getTokenByOpenId } from "@/api/getUserIfo";
94c1e6a3   liuqimichale   微信公众号 个人页面集合
19
  export default {
6ea1f7ef   刘淇   个人中心
20
    name: "binDing",
94c1e6a3   liuqimichale   微信公众号 个人页面集合
21
22
    data() {
      return {
6ea1f7ef   刘淇   个人中心
23
24
25
        codeText: "",
        phone: "",
        timeText: "获取验证码",
94c1e6a3   liuqimichale   微信公众号 个人页面集合
26
        timeNum: 60,
6ea1f7ef   刘淇   个人中心
27
28
        webAppCode: ""
      };
94c1e6a3   liuqimichale   微信公众号 个人页面集合
29
    },
6ea1f7ef   刘淇   个人中心
30
31
32
    created() {
      this.timeNum = this.timeText;
      // this.webAppCode = this.getWxCode();  // 正式打开注释
94c1e6a3   liuqimichale   微信公众号 个人页面集合
33
34
    },
    methods: {
6ea1f7ef   刘淇   个人中心
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      getWxCode() {
        var appID = this.$utils.myVxAppId;
        var code = this.getUrlParam("code");
        var local = window.location.href;
        if (code == null || code === "") {
          window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appID + "&redirect_uri=" + encodeURIComponent(local) + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        } else {
          return code;
        }
      },
      getUrlParam(name) { // 获取url里面的参数
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        var r = window.location.search.substr(1).match(reg)
        if (r != null) return unescape(r[2])
        return null
      },
      getCode: function() {
        var _this = this;
94c1e6a3   liuqimichale   微信公众号 个人页面集合
53
        // this.timeText = this.timeNum+'S'
6ea1f7ef   刘淇   个人中心
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        const reg = /^1[3-9]\d{9}$/;
        if (reg.test(this.phone)) {
          _this.sendverificode();
          let Num = 60;
          var timer = setInterval(function() {
            _this.timeNum = Num-- + "s";
            if (Num == -1) {
              _this.timeNum = "获取验证码";
              clearInterval(timer);
            }
          }, 1000);
        } else {
          this.$toast("请输入正确手机号");
        }
      },
      sendverificode() {
        let jsondata = {
          phone: this.phone
        };
        jsondata.sign = this.$utils.signObject(jsondata);
        console.log("停车记录传参  " + JSON.stringify(jsondata));
        sendverificode(jsondata).then(response => {
          console.log(response);
          this.$toast("获取验证码成功");
        });
      },
      bindPhoneHandle() {
        const reg = /^1[3-9]\d{9}$/;
        if (reg.test(this.phone)) {
          this.getOpenIdByCode(); // 获取openid
  
        } else {
          this.$toast("请输入正确手机号");
        }
      },
      getOpenIdByCode() {  // 获取openid
  
        this.bindCustByOpenId() // 正式注释
  
  
  
        // 正式打开注释
  
        // let jsondata = {
        //   appId: this.$utils.myVxAppId,
        //   code: this.webAppCode
        // };
        // jsondata.sign = this.$utils.signObject(jsondata);
        // console.log("停车记录传参  " + JSON.stringify(jsondata));
        // getOpenIdByCode(jsondata).then(response => {
        //   if(response.code=='0'){
        //     this.bindCustByOpenId() // 获取token和用户手机号
        //   }
        // });
      },
      bindCustByOpenId(){ //
        let jsondata = {
          phoneNum: this.phone,
          verifyCode: this.codeText,
          // openid: this.$utils.openId  // 正式打开注释
          openid:'oWw3o5rY_bFsiT_nFd2CEQWGZfhs'  // 正式注释
        };
        jsondata.sign = this.$utils.signObject(jsondata);
        console.log("停车记录传参  " + JSON.stringify(jsondata));
        bindCustByOpenId(jsondata).then(response => {
          if(response.code=='0'){
            this.getTokenByOpenId() // 获取token和用户手机号
  
          }else if(response.code=='1017'||response.code==1017){
            this.$toast("已经绑定过");
          }else{
            this.$toast("绑定失败");
          }
        });
      },
      getTokenByOpenId(){  // 获取token和用户手机号
        let jsondata = {
          // openid: this.$utils.openId  // 正式打开注释
          openid:'oWw3o5rY_bFsiT_nFd2CEQWGZfhs'  // 正式注释
        };
        jsondata.sign = this.$utils.signObject(jsondata);
        console.log("停车记录传参  " + JSON.stringify(jsondata));
        getTokenByOpenId(jsondata).then(response => {
          if(response.code=='0'){
            this.$toast("亲,您已绑定成功!");
            localStorage.setItem('userToken',response.data.token)
            localStorage.setItem('userPhoneNum',response.data.phoneNum)
            this.$router.push({
                name: "selfNav"
              }
            );
94c1e6a3   liuqimichale   微信公众号 个人页面集合
145
          }
6ea1f7ef   刘淇   个人中心
146
        });
94c1e6a3   liuqimichale   微信公众号 个人页面集合
147
148
      }
    }
6ea1f7ef   刘淇   个人中心
149
  };
94c1e6a3   liuqimichale   微信公众号 个人页面集合
150
151
152
153
154
155
156
157
158
159
160
161
  </script>
  
  <style scoped lang="scss">
    .getCode {
      width: 100px;
      height: 48px;
      line-height: 48px;
      background: #ef4f4f;
      color: #fff;
      text-align: center;
    }
  </style>