<script>
// app.js

/**
 * 新版备注：
 * 1. 两个流程 fare_type 0外卖 1自提  默认自提
 * 2. 有会员价
 * 3. 商品分规格, 有规格商品和无规格商品同时存在
 * 4. 获取应用实例 一律直接用getApp()
 * 5. 无主动退出
 * 6. 用户右上角的分享,只开首页和个人中心,其他页面都关闭
 * 7. 如需在接口成功时弹出返回信息,请单独加一下toast,避免与微信的loading冲突(wx.showLoading 与wx.showToast不能同时存在)
 * 8. 如需当接口连续调用,共用一个loading时,请传参nextInvoke,且主动控制何时关闭loading
 *
 * **/


export default {
    data() {
        return {
			
		};
    },
    onLaunch() {
        let that = this; // 拿code换openid(先放onLaunch试一段)

        // uni.login({
        //     success: function (res) {
        //         // console.log('获取wxcode',res)
        //         let params = {
        //             code: res.code
        //         };
        //         myPro
        //             .wxRequest('user/mini-auth', 'POST', params)
        //             .then((res) => {
        //                 let result = res.result; // 有token 是老用户

        //                 if (result.token) {
        //                     that.globalData.token = result.token;
        //                     uni.setStorage({
        //                         key: 'token',
        //                         data: result.token
        //                     });
        //                     that.globalData.is_member = result.user.is_member; // 会员 0否 1是

        //                     that.globalData.userInfo = {
        //                         nickname: result.user.nickname,
        //                         thumb: result.user.thumb,
        //                         phone: result.user.phone
        //                     };

        //                     if (result.user.store) {
        //                         that.globalData.role = 1; // 有店铺,即为门店
        //                     } // console.log('用户',that.globalData.userInfo)
        //                     // 拿用户信息(供首页使用)
        //                 } else {
        //                     // 新用户
        //                     that.globalData.openid = result.openid;
        //                     that.globalData.session_key = result.session_key;
        //                 }
        //             })
        //             .catch((err) => {
        //                 console.log('报错信息', err);
        //                 uni.showToast({
        //                     title: err,
        //                     icon: 'none'
        //                 });
        //             });
        //     },
        //     fail: function (res) {
        //         uni.showToast({
        //             title: '微信未授权成功，请在设置中允许使用用户信息',
        //             icon: 'none'
        //         });
        //     }
        // });
    },
    onShow() {
        // console.log('onShow');
        let that = this; // 设备信息

        uni.getSystemInfo({
            success(res) {
                // 获取系统信息
                const systemInfo = uni.getSystemInfoSync(); // 胶囊按钮位置信息

                const menuButtonInfo = uni.getMenuButtonBoundingClientRect(); // 胶囊位置(初始位置)

                that.globalData.customTop = menuButtonInfo.top; // 胶囊位置(中间位置)

                that.globalData.customCenter = menuButtonInfo.top + menuButtonInfo.height / 2; // 导航栏高度 = 状态栏到胶囊的间距（胶囊距上距离-状态栏高度） * 2 + 胶囊高度 + 状态栏高度

                that.globalData.setBgHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
            }
        }); // 定位
        // that.getLocal();
    },
    globalData: {
        // 测试 domain api imgUrl
        // 测试appid: wx3fa706377424c2c9
        // domain: "http://singlestore.shanmuzhi.com",
        //     api: "http://singlestore.shanmuzhi.com/",
        // imgUrl: "http://singlestore.shanmuzhi.com",
        

        // 正式 wx4ee6b1b6aa3d1403
        // domain: 'https://user.jinyutaoxiaoya.com/',
        // api: 'https://user.jinyutaoxiaoya.com/',
        // imgUrl: 'https://jinyu-txy.oss-cn-beijing.aliyuncs.com',
        location_auth: null,

        // 地理位置授权标识 1同意 -1拒绝
        lat: 0,

        // 衡水市火车站 37.743955
        lng: 0,

        // 衡水市火车站 115.691485
        token: uni.getStorageSync('token') ? uni.getStorageSync('token') : '',

        // ceshitoken
        openid: null,

        session_key: null,
        role: null,

        // 角色 1为门店  null
        userInfo: null,

        customTop: null,

        // 胶囊位置 顶部
        customCenter: null,

        // 胶囊位置 中间
        setBgHeight: null,

        // 导航栏高度
        logoTu: '/static/statics/img/logo.png',

        is_member: 0,

        // 是否为会员 1是 0否
        store_id: 1,

        // 单店(约定id是1)
        // 声明一个对象用于存储请求个数
        loadingCount: 0,

        // 验证登录
        verifyLogin(callBack) {
            const that = this;

            if (!callBack) {
                callBack = function () {};
            }

            if (!that.globalData.token) {
                let pages = getCurrentPages();

                if (pages.length > 0) {
                    uni.setStorage({
                        key: 'loginCallBackPage',
                        data: that.pageBackUrl(),
                        success: function () {
                            uni.reLaunch({
                                url: '/pages/login/index/index'
                            });
                        }
                    });
                }

                callBack(false);
            } else {
                callBack(true);
            }
        },

        // 回跳地址
        pageBackUrl: function () {
            let pages = getCurrentPages();
            let currentPage = pages[pages.length - 1];
            let url = currentPage.route;
            let options = JSON.stringify(currentPage.options);
            options = options.replace(/[\{\}\"]/g, '');
            options = options.replace(/\:/g, '=');
            options = options.replace(/\,/g, '&');
            url = url + '?' + options; // console.log('回跳地址',url)

            return url;
        },

        // 定位授权
        getLocal: function (callback) {
            var that = this;

            if (callback == undefined) {
                callback = function () {
                    // console.log('定位授权::::默认回调函数')
                };
            }

            uni.getSetting({
                success(res) {
                    if (!res.authSetting['scope.userLocation']) {
                        // 未授权时，先授权
                        uni.authorize({
                            scope: 'scope.userLocation',

                            success(auth) {
                                console.log('auth:::::', auth); // 授权成功

                                if (auth.errMsg == 'authorize:ok') {
                                    // 用户授权成功
                                    that.location_auth = 1; // 执行获取地理位置

                                    that.getLocation(callback);
                                }
                            },

                            fail(auth) {
                                console.log('auth:::::', auth);
                                callback(); // 用户拒绝,添加标识

                                that.location_auth = -1;
                            }
                        });
                    } else {
                        // 已授权，执行获取地理位置
                        that.location_auth = 1;
                        that.getLocation(callback);
                    }
                },

                fail(res) {
                    that.location_auth = -1;
                    callback();
                    console.log('设备信息调取失败', res);
                }
            });
        },

        // 执行获取地理位置
        getLocation(callback) {
            let that = this; // 用户已经同意小程序使用定位

            uni.getLocation({
                type: 'wgs84',

                success(res) {
                    console.log('定位成功', res);
                    that.globalData.lat = res.latitude;
                    that.globalData.lng = res.longitude; // console.log('地理位置：', res)
                    // 执行回调函数

                    setTimeout(() => {
                        callback();
                    }, 0);
                },

                fail(res) {
                    console.log('定位失败', res);
                    setTimeout(() => {
                        callback();
                    }, 0);
                }
            });
        }
    }
};
</script>
<style>
/**app.wxss**/
view {
    box-sizing: border-box;
}
.slh_s {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}
.slh {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
/* 商品 */
.goods_list .goods_item {
    display: flex;
    align-items: center;
    margin-top: 30rpx;
}
.goods_list .goods_item .goods_tu {
    width: 200rpx;
    height: 200rpx;
    border-radius: 5rpx;
}
.goods_list .goods_item .goods_info {
    padding-left: 16rpx;
    font-size: 24rpx;
    color: rgba(8, 8, 8, 0.54);
    flex: 1;
}
.goods_list .goods_item .goods_title {
    font-size: 30rpx;
    color: #080808;
    font-weight: bold;
}
.goods_list .goods_item .goods_labels {
    display: flex;
    flex-wrap: wrap;
}
.goods_list .goods_item .goods_labels .label {
    padding: 6rpx;
    background: #f6f6f6;
    margin: 10rpx 14rpx 0 0;
}
.goods_list .goods_item .goods_desc {
    font-size: 24rpx;
    color: rgba(8, 8, 8, 0.54);
    margin: 20rpx 0;
}
.goods_list .goods_item .goods_specs {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.goods_list .goods_item .goods_specs .price {
    font-size: 34rpx;
    color: #080808;
}
.goods_list .goods_item .goods_specs .unit {
    font-size: 22rpx;
}
.goods_list .goods_item .goods_specs .btn_specs {
    width: 120rpx;
    height: 45rpx;
    background: #295c56;
    border-radius: 23rpx;
    font-size: 26rpx;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
}
/* 订单商品 */
.order_goods {
    padding: 0 0 20rpx;
}
.order_goods .goods_item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx 0;
}
.order_goods .goods_item .goods_item_l {
    width: 530rpx;
    display: flex;
}
.order_goods .goods_item .goods_tu {
    width: 138rpx;
    height: 138rpx;
    border-radius: 5rpx;
}
.order_goods .goods_item .goods_info {
    padding-left: 20rpx;
    font-size: 24rpx;
    color: rgba(8, 8, 8, 0.54);
    width: 320rpx;
}
.order_goods .goods_item .goods_title {
    font-size: 30rpx;
    color: #080808;
    font-weight: bold;
}
.order_goods .goods_item .goods_labels {
    display: flex;
    flex-wrap: wrap;
}
.order_goods .goods_item .goods_labels .label {
    padding: 6rpx;
    background: #f6f6f6;
    margin: 10rpx 14rpx 0 0;
}
.order_goods .goods_item .goods_item_r {
    text-align: right;
}
.order_goods .goods_item .goods_price {
    font-size: 34rpx;
    color: #080808;
}
.order_goods .goods_item .goods_price .unit {
    font-size: 22rpx;
}
.order_goods .goods_item .goods_num {
    font-size: 26rpx;
    padding-top: 20rpx;
    color: rgba(51, 51, 51, 0.54);
}
/* tabs */
.cate_list {
    width: 750rpx;
    height: 100rpx;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
}
.cate_list .van-tab {
    font-size: 32rpx;
    font-weight: 500;
}
/* 券 */
.coupon_list {
    padding: 30rpx 0;
    background: #f6f6f6;
}
.coupon_list .van-cell {
    padding: 0 !important;
    background: unset !important;
}
.coupon_item {
    width: 690rpx;
    margin: 30rpx auto 0;
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    align-items: center;
}
.coupon_item:first-child {
    margin-top: 0;
}
.coupon_item .item_l {
    width: 196rpx;
    height: 168rpx;
    font-size: 28rpx;
    color: rgba(255, 255, 255, 0.78);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.coupon_item .item_l .price {
    font-size: 60rpx;
    color: #ffffff;
}
.coupon_item .item_l .price .unit {
    font-size: 30rpx;
}
.coupon_item .item_r {
    flex: 1;
    height: 168rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 30rpx;
    font-size: 26rpx;
    color: rgba(51, 51, 51, 0.54);
}
.coupon_item .item_r .item_r_l {
    text-align: left;
}
.coupon_item .quan_name {
    font-size: 34rpx;
    color: #333333;
}
.coupon_item .item_status {
    width: 130rpx;
    height: 50rpx;
    background: #295c56;
    border-radius: 5rpx;
    font-size: 26rpx;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
}
.coupon_item.unable .item_status {
    background: transparent;
    color: rgba(0, 0, 0, 0.54);
}
.coupon_item.unable .item_l {
    color: rgba(255, 255, 255, 0.78);
}
.coupon_item.unable .item_l .price {
    color: #ffffff;
}
/* 全局loading */
.custom_loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
/* 充值卡 */
.card_item_w {
    padding: 20rpx 0;
    border-bottom: 1rpx dashed rgba(0, 0, 0, 0.12);
}
.card_item_w:last-child {
    border: 0 none;
}
.card_item {
    border: 1px solid #295c56;
    border-radius: 5rpx;
    padding: 30rpx 0;
    /* display: flex;
	align-items: center;
	justify-content: center; */
    color: #295c56;
    font-size: 24rpx;
}
.card_item .card_item_col {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30rpx;
    font-weight: 500;
}
.card_item .card_item_col.desc {
    color: rgba(14, 14, 14, 0.54);
    padding-top: 4rpx;
}
.card_item .card_item_l,
.card_item .card_item_r {
    display: flex;
    align-items: baseline;
    justify-content: center;
}
.card_item .card_price {
    padding: 0 20rpx 0 10rpx;
    font-size: 42rpx;
}
.card_item .card_price .unit {
    font-size: 27rpx;
}
.card_item .card_send {
    width: 44rpx;
    height: 44rpx;
    margin: 0 10rpx;
    background: #295c56;
    border-radius: 50%;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
}
.card_item .card_yuan {
    padding-left: 16rpx;
    font-size: 36rpx;
    font-weight: bold;
}
.card_item .card_times {
    font-size: 27rpx;
    padding: 0 14rpx;
}
.card_item .coupons_nums {
    font-size: 36rpx;
    font-weight: bold;
}
.card_item.active {
    background: #295c56;
    color: #fff;
}
.card_item.active .card_send {
    background: #ffffff;
    color: #295c56;
}
.card_item.active .card_item_col.desc {
    color: rgba(255, 255, 255, 0.8);
}
/* 授权按钮 */
.custom_dialog .van-button {
    width: 100% !important;
}
/* 提示确认框 */
.single_btn .van-button {
    width: 100%;
}
/* 步进器 */
.stepper .step_input {
    /* height: 24rpx; */
    height: 42rpx;
    line-height: 24rpx;
    padding-top: 0;
    font-size: 26rpx;
    color: rgba(0, 0, 0, 1);
    background: none;
}
.stepper .plus,
.stepper .minus {
    border-radius: 50%;
    overflow: hidden;
    vertical-align: bottom;
    width: 42rpx;
    height: 42rpx;
}
.stepper .plus {
    color: #ffffff;
    border: 1px solid #295c56;
    background: #295c56;
}
.stepper .minus {
    border: 1px solid #295c56;
    color: rgba(0, 0, 0, 1);
    background: #fff;
}
</style>
