liangjin
2021-04-01 a734e1028dbc6e91a12ee03b1eeba27cdc94cd26
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
<!--
 * @Author: 小明丶
 * @Date: 2019-09-26 15:50:59
 * @LastEditors: 小明丶
 * @LastEditTime: 2019-09-26 15:50:59
 * @Description: 
 -->
<template>
    <div class="open-huabei-page h-100-g">
        <x-header :title="'开通' + this.$route.query.title" :left-options="{backText:'',preventGoBack:true}"
                  @on-click-back="pageGoBack"></x-header>
        <div class="tip">请用支付宝客户端,扫一扫</div>
        <div class="qrcode-url" v-if="qrCodeUrl">
            <img :src="qrCodeUrl" alt="">
        </div>
        <f-space></f-space>
        <f-button @on-click="handleNextStep"
                  :disabled="buttonText === '等待授权'">{{ buttonText }}</f-button>
    </div>
</template>
 
<script>
    import QRCode from 'qrcode'
 
    export default {
        name: 'huaBeiOpen',
        data() {
            return {
                isClickNextBtn: false, // 是否点击下一步btn的状态
                alAccountTypeList: [],
                qrCodeUrl: '', // 二维码图片地址
                timeId: '', // 轮询时间id
                buttonText: '等待授权' // 按钮文字
            };
        },
        methods: {
            // 点击返回
            pageGoBack () {
                this.$router.push({
                    path: '/main/productManagement'
                });
            },
            // 检查授权
            checkAuth (isNext) {
                // authStatus: 0 未授权   1授权---用户授权后才能执行下一步
                this.$api.checkAuth({}).then((res) => {
                    if (res.body.authStatus === 1) {
                        this.buttonText = '下一步';
                        clearTimeout(this.timeId);
                        if (isNext) {
                            this.$router.push({
                                path: '/huabei/alipay',
                                query: {
                                    typeId: this.$route.query.typeId, // 产品typeId
                                }
                            });
                        }
                    } else if(res.body.authStatus === 2){
                        // 3秒钟,轮询一次
                        this.$notify_success('请重新进入此页面');
                    }else {
                        // 3秒钟,轮询一次
                        this.timeId = setTimeout(() => {
                            this.checkAuth();
                        }, 3000);
                    }
                }, (error) => {
                    clearTimeout(this.timeId);
                });
            },
            // 获取列表
            init () {
                this.$api.hbOpenInit({}).then((res) => {
                    // 这个类型列表(缓存在初始化列表, 等待上传图片的是,支付宝类型)
                    let alAccountTypeList = res.body.alAccountTypeList || [];
                    sessionStorage.setItem('huabei_accountTypeList', JSON.stringify(alAccountTypeList));
                    // 1-图片,2-路径
                    if (res.body.urlType === 1) {
                        this.qrCodeUrl = res.body.qrCodeUrl;
                    } else if (res.body.urlType === 2) {
                        // 生成图片
                        QRCode.toDataURL(res.body.qrCodeUrl)
                            .then(url => {
                                this.qrCodeUrl = url;
                            })
                            .catch(err => {
                                console.error(err)
                            });
                    }
 
                });
                // 进入这个页面,就进行授权
                this.checkAuth(false);
            },
            // 点击下一步
            handleNextStep () {
                this.checkAuth(true);
            }
        },
        beforeRouteLeave(to,form,next){
            clearTimeout(this.timeId);
            this.qrCodeUrl = null;
            next();
        },
        // destroyed() {
        //     clearTimeout(this.timeId);
        //     this.qrCodeUrl = null;
        // },
        created() {
            this.qrCodeUrl = '';
            this.timeId =  '';
            this.buttonText = '等待授权';
            this.isClickNextBtn = false;
            this.init();
        }
    };
</script>
 
<style lang="less">
    .open-huabei-page {
        padding-top: 54px;
        background-color: #f1f1f1;
 
        .tip {
            color: #999;
            text-align: center;
            font-size: 14px;
            line-height: 1.6;
            padding: 14px 0;
        }
        .qrcode-url {
            text-align: center;
            img {
                width: 60%;
                height: auto;
                border: 5px solid @color-border-theme;;
            }
        }
    }
</style>