//app.js
App({
    onShow() {
        console.log('app-onshow');
        const self = this;
        // 查看是否授权
        wx.getSetting({
            success(res) {
                if (res.authSetting['scope.userInfo']) {
                    console.log('授权过到首页');
                    self.globalData.isLogin = true;
                    // wx.redirectTo({
                        // url: '/pages/index/index', //跳转到授权页面
                    // });
                } else {
                    console.log('授权失效到授权页');
                    self.globalData.isLogin = false;
                    wx.navigateTo({
                        url: '/pages/my/my', //跳转到授权页面
                    })
                }
            }
        })
    },
    onLaunch: function () {
        console.log('app-onLaunch');
        // 展示本地存储能力
        // var logs = wx.getStorageSync('logs') || []
        // logs.unshift(Date.now())
        // wx.setStorageSync('logs', logs)


        // 获取用户信息
    },
    getLogin() {
        const self = this;

        return new Promise(function(resolve, reject) {
            wx.getUserInfo({
                success: function (user) {
                    // console.log(user);
                    // 登录
                    let url = '/portal/Login/getToken';
                    wx.login({
                        success: function (res) {
                            if(self.loginCallback) {
                                console.log('onlaunch-callback');
                                self.loginCallback(user)
                            }
                            console.log('获取code', res);
                            self.post(url, {
                                code: res.code,
                                user_nickname: user.userInfo.nickName,
                                avatar: user.userInfo.avatarUrl
                            }, {}).then((ret) => {
                                console.log('获取token', ret);
                                wx.setStorageSync('token', ret.data.token);
                                self.globalData.userInfo = user.userInfo;
                                resolve(ret.data);
                                setTimeout(function () {
                                    wx.navigateBack({delta:1})
                                },200)
                            })
                        },
                        fail: function (res) {
                        },
                        complete: function (res) {
                        },
                    });
                }
            });

        })
    },
    post: function (url, data, headerParams) {
        /**
         * 自定义post函数,返回Promise
         * +-------------------
         * @param {String}      url 接口网址
         * @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
         * +-------------------
         * @return {Promise}    promise 返回promise供后续操作
         */
        // wx.showLoading({
        //   title: '加载中',
        // })
        wx.showNavigationBarLoading()
        // wx.showToast({
        //     title: '加载中',
        //     icon:'none',
        // })
        var promise = new Promise((resolve, reject) => {
            //init
            let that = this;
            let postData = data;
            let baseUrl = 'http://wmatchrd.com/api';
            //网络请求
            let header = {
                'content-type': 'application/x-www-form-urlencoded'
            }
            header = Object.assign(header, headerParams)
            wx.request({
                url: baseUrl + url,
                data: postData,
                method: 'POST',
                header: header,
                success: function (res) {//返回取得的数据
                    // console.log('返回取得的数据res', res);
                    if(res.data.code == 1){
                        resolve(res.data);
                    }else if (res.data.code == '20000') {
                        console.log('20000');
                        resolve(res.data.data);
                    } else if (res.data.data.code == '10001') {  //未获取授权
                        console.log('10001');
                        wx.showModal({
                            title: '提示',
                            content: res.data.data.msg,
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    // wx.removeStorageSync('token');
                                    wx.navigateTo({
                                        url: '/pages/index/index',
                                    })
                                }
                            }
                        });
                        resolve(res.data.data);
                    } else if (res.data.data.code == '30000') {
                        resolve(res.data);
                    } else if (res.data.data.code == '40001') {//返回错误提示信息
                        console.log(555);
                        wx.showModal({
                            title: '提示',
                            content: res.data.data.msg,
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    // wx.removeStorageSync('token');
                                    // wx.navigateTo({
                                    //     url: '/pages/start/start',
                                    // })
                                }
                            }
                        });
                        resolve(res.data.msg);
                    } else if (res.data.code == '40000') {
                        console.log(333);
                        // wx.showModal({
                        //     title: '提示',
                        //     content: res.data.data.msg,
                        //     showCancel: false,
                        //     success: function (res) { }
                        // });
                        resolve(res.data.data);
                    } else if (res.data.data.code == '40106') {
                        console.log('重复绑定');
                        resolve(res.data.data);
                    } else if (res.data.data.code == '40107') {
                        console.log('手机号已注册');
                        resolve(res.data.data);
                    } else if (res.data.data.code == '40111') {
                        console.log('新旧密码不能相同');
                        resolve(res.data.data);
                    } else {
                        console.log('code=0');
                        console.log(res.data);
                        wx.showModal({
                            title: '提示',
                            content: res.data.message,
                            showCancel: false,
                        })
                        resolve(res.data.data);
                        reject(res.data)
                    }
                    setTimeout(function () {
                        // wx.hideLoading()
                        // wx.hideToast()
                        wx.hideNavigationBarLoading()
                    }, 500)
                },
                error: function (e) {
                    reject('网络出错');
                    // wx.hideLoading()
                    wx.hideNavigationBarLoading()
                }
            })
        });
        return promise;
    },
    //获取当前日期
    nowDate() {
        let date = new Date();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        // let hours = date.getHours();
        // let minutes = date.getMinutes();
        let Y = date.getFullYear() + '-' + (month > 9 ? month : ('0' + month)) + '-' + (day > 9 ? day : ('0' + day));
        // let H = (hours>9?hours:('0'+hours)) + ':' +(minutes>9?minutes:('0'+minutes));
        let time = [];
        time.push(Y);
        // let new_time = time.join(' ');
        return time;
    },
    //获取截止时间的前几个小时
    nextTime(over_time,hour_num) {
        let over = new Date(over_time); //转成标准时间
        let month = over.getMonth() + 1;
        let day = over.getDate();
        let hours = over.getHours();//获取截止时间的小时
        let minutes = over.getMinutes();
        let Y = over.getFullYear() + '.' + (month > 9 ? month : ('0' + month)) + '.' + (day > 9 ? day : ('0' + day));
        let H = (hours > 9 ? hours - hour_num : ('0' + hours - hour_num)) + ':' + (minutes > 9 ? minutes : ('0' + minutes));
        let time = [];
        console.log('Y,H,month', Y, H,month);
        time.push(Y,H);
        let new_time = time.join(' ');
        return new_time;
    },
    globalData: {
        userInfo: null,
        isLogin:null
    }
});