<template>
    <view class="container">
        <uni-load-more :status="status" :contentText="contentText" @clickLoadMore="loadMore"></uni-load-more>
    </view>
</template>

<script>
    import {
        stringIsNotEmpty
    } from '../../common/empty'
    export default {
        data() {
            return {
                status: "loading",
                contentText: {
                    contentdown: "获取信息失败，点击重试",
                    contentrefresh: "获取信息中，请稍后",
                    contentnomore: "没有更多数据了"
                },
                // 中间件数据，不用改
                middleware: null
            }
        },
        onLoad(options) {

            // 不用改
            if (options != undefined && options.data != undefined) {
                let shareData;
                try {
                    // 跳转携带数据
                    shareData = JSON.parse(options.data)
                } catch (e) {
                    shareData = JSON.parse(decodeURIComponent(options.data))
                }
                this.middleware = shareData
            }

            this.getWxCode()
        },
        methods: {
            loadMore(e) {
                if ('more' === e.detail.status) {
                    // 重新获取
                    this.getWxCode()
                }
            },
            getWxCode() {

                this.status = "loading"
                let _this = this;


                wx.login({
                    success: function(res) {
                        uni.request({
                            // 组装请求地址
                            url: getApp().globalData.wxSilentLoginURL + "?wxCode=" + res.code,
                            method: "GET",
                            header: {
                                'content-type': "application/x-www-form-urlencoded"
                            },
                            data: {
                                "wxCode": res.code
                            },
                            success: res => {
                                if (res.statusCode == 200) {
                                    if (!res.data.success) {
                                        if (res.data.code == "110") {
                                            // 需要绑定手机号
                                            uni.reLaunch({
                                                url: '/pages/index/BindPhone?openid=' +
                                                    res.data.data.wxMpOpenid
                                            })
                                        } else {
                                            _this.status = 'more'
                                            // _this.contentText.contentdown = res.data.msg+"，点击重试"
                                        }
                                    } else {
                                        // 成功后跳转主页
                                        getApp().globalData.sid = res.data.data.sid
                                        getApp().globalData.token = res.data.data.token
                                        getApp().globalData.isNewUser = res.data.data.isNewUser
                                        getApp().globalData.isPurchase = res.data.data
                                            .isPurchase
                                        getApp().globalData.mobile = res.data.data.mobile
                                        uni.setStorageSync("satoken", res.data.data.token)
                                        console.log("人员sid", res.data.data.sid);
                                        console.log("token", res.data.data.token);
                                        console.log("mobile", res.data.data.mobile);


                                        console.log("middleware>>>>>>>>>>>>", _this.middleware);

                                        if (_this.middleware != null) {
                                            _this.middleware.params.customerSid = res.data.data
                                                .sid

                                            // 走中间件的逻辑
                                            if ('share' == _this.middleware.functionName) {
                                                uni.reLaunch({
                                                    url: _this.middleware.url
                                                });
                                            } else if ('bindCard' == _this.middleware
                                                .functionName) {

                                                uni.reLaunch({
                                                    url: _this.middleware.url
                                                });

                                                // _this.$api.pickUpCardBind(_this.middleware.params)
                                                // 	.then((resp) => {
                                                // 		uni.reLaunch({
                                                // 			url: _this.middleware.url
                                                // 		});
                                                // 	}).catch(e => {
                                                // 		uni.showModal({
                                                // 			title: '提示',
                                                // 			content: e.msg,
                                                // 			showCancel: false,
                                                // 			success: function(res) {
                                                // 				uni.switchTab({
                                                // 					url: '/pages/home/pickUpCard'
                                                // 				})
                                                // 			}
                                                // 		});
                                                // 	})

                                            } else {
                                                uni.showModal({
                                                    title: '提示',
                                                    content: '版本过低',
                                                    showCancel: false,
                                                    success: function(res) {
                                                        uni.switchTab({
                                                            url: '/pages/home/myCellar'
                                                        })
                                                    }
                                                });
                                            }

                                        } else {
                                            uni.switchTab({
                                                // url: '/pages/home/pickUpCard',
                                                // url: '/pages/home/myHome',
                                                url: '/pages/home/myCellar',
                                            })
                                        }

                                        if (_this.stringIsNotEmpty(_this.shareUrl)) {

                                        } else {

                                        }
                                    }
                                } else {
                                    _this.status = 'more'
                                }
                            },
                            fail: (err) => {
                                _this.status = 'more'
                            }
                        });
                    },
                    fail: function(res) {
                        _this.status = 'more'
                    }
                });

            }
        }
    }
</script>

<style>
    .container {
        width: 100%;
        height: 90vh;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
    }

    uni-page-body,
    page {
        width: 100%;
        height: 100%;
        height: calc(100% + var(--window-bottom); );
    }
</style>