//app.js
App({
        onLaunch: function () {
                //自动更新版本
                const updateManager = wx.getUpdateManager()
                updateManager.onCheckForUpdate(function (res) {
                        // 请求完新版本信息的回调
                })

                updateManager.onUpdateReady(function () {
                        wx.showModal({
                                title: '更新提示',
                                content: '新版本已经准备好,是否重启应用?',
                                success: function (res) {
                                        if (res.confirm) {
                                                // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                                                updateManager.applyUpdate()
                                        }
                                }
                        })

                })

                updateManager.onUpdateFailed(function () {
                        // 新的版本下载失败
                        wx.showModal({
                                title: '更新提示',
                                content: '新版本下载失败',
                                showCancel: false
                        })
                })
        },
        changeToken() {
                let url = '/api/user/codeToToken',
                        t = this;
                wx.login({
                        success: function (res) {
                                t.post(url, {
                                        code: res.code
                                }).then((r) => {
                                        wx.setStorageSync('token', r.userInfo.token)
                                })
                        }
                })
        },

        upload(filetype, file) {
                var promise = new Promise((resolve, reject) => {
                        wx.showNavigationBarLoading()
                        wx.showLoading({
                                title: '上传中',
                        })
                        let url = 'https://anleguo.w.brotop.cn/portal/common/upload';
                        let head = {
                                'XX-Token': wx.getStorageSync('token'),
                                'XX-Device-Type': ''
                        }
                        let typename = {
                                filetype: filetype
                        }
                        wx.uploadFile({
                                url: url, //仅为示例,非真实的接口地址
                                filePath: file,
                                name: 'file',
                                header: {},
                                formData: typename,
                                success: function (res) {
                                        console.log('上传文件后', res)
                                        let temdata = JSON.parse(res.data);
                                        let urlobj = {
                                                url: temdata.data.preview_url
                                                //url: temdata.data.filepath
                                        }
                                        resolve(urlobj);
                                        wx.hideNavigationBarLoading();
                                        wx.hideLoading();
                                },
                                fail: function (res) {
                                        reject('网络出错');
                                        wx.hideNavigationBarLoading()
                                        wx.hideLoading()
                                },
                                complete: () => {
                                        wx.hideNavigationBarLoading()
                                        wx.hideLoading()
                                },
                        })
                });
                return promise;
        },

        post: function (url, data, headerParams, showLoad) {
                wx.showNavigationBarLoading()
                var promise = new Promise((resolve, reject) => {
                        //init
                        let that = this;
                        let postData = data;
                        let baseUrl = 'http://www.himrhi.com/api';
                        //网络请求
                        let header = {
                                'Content-Type': 'application/x-www-form-urlencoded',
                                'XX-Device-Type': 'wxapp',
                                'XX-Token': wx.getStorageSync("token") || ""

                        }
                        header = Object.assign(header, headerParams)
                        //网络请求 
             
                        wx.request({
                                url: baseUrl + url,
                                data: postData,
                                method: 'POST',
                                header: header,

                                success: function (res) { //返回取得的数据
                                        if (res.statusCode == '200') {
                                                resolve(res.data);
                                        } else if (res.statusCode == '201') {
                                                resolve(res.data);
                                        } else {
                                                reject(res)
                                                // wx.showModal({
                                                //         title: '提示',
                                                //         content: res.data.msg,
                                                //         showCancel: false
                                                // })
                                        }

                                     
                                        // setTimeout(function () {

                                        // wx.hideNavigationBarLoading()
                                        // }, 600)
                                },
                                fail: function (e) {
                                        reject('网络出错');
                                        // wx.hideLoading()
                                        wx.hideNavigationBarLoading()
                                },
                                complete: function () {
                                        wx.hideNavigationBarLoading()
                                        // wx.hideLoading()
                                },
                        })
                });
                return promise;
        },

        timeFormate(timestamp, timeType) {
                if (timestamp) {
                        var timeStamp = timestamp.length == 13 ? timestamp : timestamp * 1000
                        var date = new Date(timeStamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
                        var Y = date.getFullYear() + '-';
                        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                        var D = date.getDate() + ' ';
                        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours())+":";
                        var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
                        var s = date.getSeconds();
                        if (timeType == 'YYMMDD') {
                                return Y + M + D;
                        } else if (timeType == 'YYMMDDHHMM') {
                                return Y + M + D + " " + h + m;
                        } else {
                                return h + m;
                        }
                }
        },
        
        globalData: {
                user_id: null,
                baseUrl: 'http://www.himrhi.com/api',
                imgUrl: 'http://qiniu.himrhi.com/',
        }
})