<script>
//app.js
export default {
    data() {
        return {};
    },
    onLaunch: function () {
        let that = this;
        that.globalData.init();
        uni.login({
            success(res) {
                that.globalData.login(res.code);
            }
        });
    },
    globalData: {
        userInfo: null,
        site_name: '',
        imgUrl: '/img/',
        baseUrl: 'https://petsdemo.brofirst.cn/api/',

        post(url, data, showLoad) {
            uni.showNavigationBarLoading();
            var promise = new Promise((resolve, reject) => {
                let that = this; //网络请求

                uni.request({
                    url: this.baseUrl + url,
                    data: data,
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded',
                        token: uni.getStorageSync('token') || ''
                    },
                    success: function (res) {
                        //返回取得的数据
                        uni.hideNavigationBarLoading();

                        if (res.statusCode == '200') {
                            if (showLoad && res.data.code == 0) {
                                uni.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 300
                                });
                            }

                            resolve(res.data);
                        } else if (res.statusCode == '401') {
                            uni.showToast({
                                title: '您还没登录,请先登录~',
                                icon: 'none',
                                duration: 500,

                                success() {
                                    setTimeout(function () {
                                        uni.navigateTo({
                                            url: '/pages/index/start/start'
                                        });
                                    }, 500);
                                }
                            });
                        } else {
                            reject(res);
                        }
                    },
                    fail: function (e) {
                        uni.hideNavigationBarLoading();
                    }
                });
            });
            return promise;
        },

        post1(url, data, showLoad) {
            uni.showNavigationBarLoading();
            var promise = new Promise((resolve, reject) => {
                let that = this; //网络请求

                uni.request({
                    url: this.baseUrl + url,
                    data: data,
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded',
                        token: uni.getStorageSync('token') || ''
                    },
                    success: function (res) {
                        //返回取得的数据
                        console.log(res);
                        uni.hideNavigationBarLoading();

                        if (res.statusCode == '200') {
                            if (showLoad && res.data.code == 0) {
                                uni.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 300
                                });
                            }

                            resolve(res.data);
                        } else if (res.statusCode == '401') {
                            uni.showToast({
                                title: '您还没登录,请先登录~',
                                icon: 'none',
                                duration: 500,

                                success() {
                                    setTimeout(function () {
                                        uni.navigateTo({
                                            url: '/pages/index/start/start'
                                        });
                                    }, 500);
                                }
                            });
                        } else {
                            reject(res);
                        }
                    },
                    fail: function (e) {
                        uni.hideNavigationBarLoading();
                    }
                });
            });
            return promise;
        },

        //button登录
        buttonLogin(userInfo, callback) {
            let that = this;
            uni.login({
                success(res) {
                    let url = 'common/login';
                    let params = {
                        userInfo: userInfo,
                        code: res.code,
                        store_id: uni.getStorageSync('store_id') ? uni.getStorageSync('store_id') : 0
                    };
                    that.post(url, params, true).then((r1) => {
                        if (r1.code == 1) {
                            uni.setStorageSync('token', r1.data.userInfo.token);
                            callback();
                        }
                    });
                }
            });
        },

        //登录
        login(code) {
            let url = 'common/codeToToken';
            let params = {
                code: code
            };
            this.post(url, params, false).then((r) => {
                if (r.code == 1) {
                    console.log('尝试登录成功');
                    uni.setStorageSync('token', r.data.userInfo.token);
                    uni.setStorageSync('session_key', r.data.sessionKey.session_key);
                    uni.setStorageSync('openid', r.data.sessionKey.openid);
                } else {
                    uni.setStorageSync('session_key', r.data.sessionKey.session_key);
                    uni.setStorageSync('openid', r.data.sessionKey.openid);
                }
            });
        },

        //初始化
        init() {
            let that = this;
            this.post('common/init', {}, false).then((r) => {
                if (r.code == 1) {
                    uni.setStorageSync('initData', r.data);
                    that.globalData.site_name = r.data.site_name;
                    uni.setNavigationBarTitle({
                        title: r.data.site_name
                    });
                }
            });
        }
    }
};
</script>
<style>
/* 阿里巴巴矢量图标库 */
@font-face {
    font-family: 'iconfont'; /* project id 1595016 */
    src: url('//at.alicdn.com/t/font_1595016_m0i971ksx1e.eot');
    src: url('//at.alicdn.com/t/font_1595016_m0i971ksx1e.eot?#iefix') format('embedded-opentype'), url('//at.alicdn.com/t/font_1595016_m0i971ksx1e.woff2') format('woff2'),
        url('//at.alicdn.com/t/font_1595016_m0i971ksx1e.woff') format('woff'), url('//at.alicdn.com/t/font_1595016_m0i971ksx1e.ttf') format('truetype'),
        url('//at.alicdn.com/t/font_1595016_m0i971ksx1e.svg#iconfont') format('svg');
}

.iconfont {
    font-family: 'iconfont' !important;
    font-size: 16px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.icon-sousuo::after {
    content: '\e613';
}

page {
    width: 100%;
    height: 100%;
}

image {
    width: 100%;
    height: 100%;
}

::-webkit-scrollbar {
    display: none;
}

/* 灰色色块 */

.line {
    height: 16rpx;
    width: 100vw;
    background: rgba(245, 245, 245, 1);
    margin-left: -32rpx;
}

input,
textarea {
    background: transparent;
    border: 0 none;
    outline: 0 none;
}

/* 暂无样式 */

.noData {
    width: 100%;
    line-height: 80vh;
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: 400;
    color: #8c9198;
    text-align: center;
}

.wxParse-img {
    width: 100% !important;
    margin: 16rpx 0;
}

button {
    padding: 0;
    margin: 0;
    border: 0 none !important;
    display: flex;
    align-items: center;
    width: auto !important;
}

.border {
    border-bottom: 2rpx solid #eee;
}

.border.active {
    border: 0 none;
}
</style>