liangjin
2021-04-01 17b4a933b86881641eb65e6ffcdc3d253cb3e7e1
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!--
 * @Author: 小明丶
 * @Date: 2020-11-09 09:43:37
 * @LastEditors: 小明丶
 * @LastEditTime: 2021-01-07 10:34:23
 * @Description: 微信扫码登录页面
-->
<template>
  <div class="login-by-wx">
    <div class="height-44" v-if="showContent">登录</div>
    <div class="info-box" v-if="showContent">
      <van-field
        v-model="phoneNum"
        type="tel"
        clearable
        maxlength="11"
        left-icon="phone-o left-icon"
        placeholder="请输入手机号码"
      />
      <van-field
        v-model="code"
        type="number"
        clearable
        maxlength="4"
        left-icon="envelop-o left-icon"
        input-align="left"
        placeholder="请输入短信验证码"
      >
        <template #button>
          <van-button color="#896EDB" style="width:60px" size="mini" @click="getCode" :disabled="isDisabled" plain type="info">{{btnText}}</van-button>
        </template>
      </van-field>
      <van-button
        round
        type="info"
        color="#896EDB"
        style="width: 100%; margin-top: 150px"
        @click="loginhanld"
        >登录</van-button
      >
    </div>
    <div v-if="!showContent"></div>
  </div>
</template>
<script>
import LoginBox from "../../components/common/h-login-box.vue";
import { GoWeChat } from "../../utils/wechat";
import { mapActions } from 'vuex';
import Vue from 'vue';
import { Toast } from 'vant';
 
Vue.use(Toast);
export default {
  components: {
    LoginBox,
  },
  data() {
    return {
      btnText: "获取验证码",
      phoneNum: "", //电话
      code: "", //验证码
      openId: "", //微信openID
      showContent: false, //显示内容或者输入框
      urlCode: "", //授权后回调链接上截取的code值
      timer: "", //定时器
      isDisabled:false,
    };
  },
  created() {
 
    if(!sessionStorage.sib_wx_merId || typeof sessionStorage.sib_wx_merId == 'undefined'){
      sessionStorage.sib_wx_merId = location.href.split('?')[1].split('=')[1]
    }
    clearTimeout(this.timer);
    this.urlCode = this.getUrlCode("code");
    console.log(this.urlCode);
    this.init();
  },
  methods: {
    ...mapActions(['loginByOpenId']),
    /**
     * @description:初始化授权
     * @returns void
     * **/
    init() {
      this.openId = localStorage.sib_wx_openId;
      console.log(this.openId)
      this.showContent = false;
      // 模拟数据本地调试
      // this.openId = "oJbj21YwUatdNaYf3iEPwwGtWuUg";
      //this.urlCode = "021qXn000q9GCK1xU7300xMP1p0qXn0A";
      if (
        (!this.openId && !this.urlCode) ||
        (typeof this.openId == "undefined" && !this.urlCode)
      ) {
        console.log(111);
        let url = location.host;
        // url = "t.finlean.com";
        var str = encodeURIComponent(`https://${url}/sib_wx_scorea/#/loginByWx`);
        var fn = new GoWeChat("wx594ab33e0466eccf", str);
        fn.gocontract();
      } else {
        if(!this.openId && this.urlCode){
          this.$api.getWeChatUserInfo({
            code:this.urlCode
          }).then(res=>{
            this.openId = res.body
 
            this.showContent = true;
          })
        }
        else if(this.openId && !this.urlCode){
          this.$api.checkOpenIdIsBind({
            wechatOpenId:this.openId,
            merId:sessionStorage.sib_wx_merId
          }).then(res=>{
              this.showContent = true;
              if(res.body.isBind === 1){
                  localStorage.sib_wx_mblNo = res.body.mblNo
                  localStorage.sib_wx_userId = res.body.userId
                  this.loginByOpenId({
                      vm: this,
                      loginType:2,
                      verCode:this.code,
                      mblNo:res.body.mblNo,
                      merId:sessionStorage.sib_wx_merId,
                      wechatOpenId:this.openId,
                      userId:res.body.userId
                  })
                  // this.$router.push('/mine/toggle-identity?title=选择门店')
              }
          })
        }
 
 
      }
    },
    /**
     * @description:截取code值
     * @returns void
     * **/
    getUrlCode(name) {
      return (
        decodeURIComponent(
          (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
            location.href
          ) || [, ""])[1].replace(/\+/g, "%20")
        ) || null
      );
    },
    /**
     * @description:倒计时
     * @param {number} 秒数
     * @return void
     * **/
    timeOut(time) {
      time = time - 1;
      if (time === 0) {
        clearTimeout(this.timer);
        this.btnText = '重新获取'
        this.isDisabled = false
      } else {
        this.isDisabled = true
        this.timer = setTimeout(() => {
          this.btnText = `${time}S`;
          this.timeOut(time);
        }, 1000);
      }
    },
    loginhanld(){
      let v = this.$tool;
      if(v.checkValEmpty(this.phoneNum)){
        Toast.fail('请输入手机号');
        return;
      }
      if(!v.checkPhone(this.phoneNum)){
        Toast.fail('请输入姓名');
        return;
      }
      if(v.checkValEmpty(this.code)){
        Toast.fail('请输入验证码');
        return;
      }
      this.loginByOpenId({
          vm: this,
          loginType:1,
          verCode:this.code,
          mblNo:this.phoneNum,
          merId:sessionStorage.sib_wx_merId,
          wechatOpenId:this.openId
      })
 
      // this.$api.loginByOpenId({
      //   loginType:1,
      //   verCode:this.code,
      //   mblNo:this.phoneNum,
      //   merId:sessionStorage.sib_wx_merId,
      //   wechatOpenId:this.openId
      // }).then(res=>{
      //   localStorage.sib_wx_mblNo = res.body.mblNo
      //   localStorage.sib_wx_userId = res.body.userId
      //   commit(SET_SESSION_ID, 'beef1616-fc2a-43a3-8c27-deb7837bde06');
      //   this.$router.push('/mine/toggle-identity?title=选择门店')
      // })
    },
    getCode(){
      this.$api.userSendMsg({
        verCodeType:2,
        mblNo:this.phoneNum
      }).then(res=>{
        this.timeOut(60)
      })
    }
  },
};
</script>
<style lang="less" scoped>
.login-by-wx {
  & {
    background-color: #fff;
    min-height: 100vh;
  }
  .height-44 {
    .lh(44px);
    .flex(center, center);
    font-size: @font-20;
    color: @c-333;
  }
  .info-box {
    & {
      width: 100%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
      padding: 0 24px;
      box-sizing: border-box;
    }
    .btn {
      width: 100px;
      border: 0;
      outline: none;
      background: #fff;
      position: relative;
    }
    .btn::before {
      content: "";
      position: absolute;
      height: 15px;
      border: solid 1px #ebedf0;
      left: 0px;
    }
  }
}
</style>