// app.js
App({
    onLaunch() {

        // 版本更新
        const updateManager = wx.getUpdateManager()

        updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            if (res.hasUpdate) {
                //准备更新
                updateManager.onUpdateReady(function () {
                    wx.showModal({
                        title: '需求对对碰更新啦~',
                        content: '新版本已经准备好，请重启应用',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                updateManager.applyUpdate()
                            }
                        }
                    })
                })

                //更新失败
                updateManager.onUpdateFailed(function () {
                    wx.showModal({
                        title: '出错啦',
                        content: '更新失败，请检查您的网络状况',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                updateManager.applyUpdate()
                            }
                        }
                    })
                })
            }
        })



        // 登录
        wx.login({
            success: (res) => {
                if (res.code) {
                    //获取openid
                    wx.request({
                        url: this.mangerUrl + '/service/weixin/getWxOpenId',
                        data: {
                            appNum: 12,
                            js_code: res.code
                        },
                        success: (result) => {
                            console.log("微信LOGIN result")
                            if (!!result.data && result.data.openid) {
                                this.globalData.openid = result.data.openid;
                                this.globalData.appid = result.data.appid;
                                this.globalData.sessionKey = result.data.sessionKey;
                                // 获取本地用户信息
                                const userInfo = wx.getStorageSync('userInfo') || null;
                                const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null;
                                if(userInfo){
                                    this.globalData.userInfo = userInfo;
                                }
                                if(businessCommunicationCustomer){
                                    this.globalData.businessCommunicationCustomer = businessCommunicationCustomer;
                                    this.globalData.customerId = businessCommunicationCustomer.customerId;
                                }
                                console.log(`初始化拿到的缓存`,this.globalData)
                            }
                        }    
                    })
                } else {
                    wx.showToast({
                        title: '获取登录信息失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    globalData: {
        userInfo: null,
        // openid: "oBkT7vu1Q8l-xsbux_i1FNmRWuJ4",
        openid: "",
        appid: "",
        sessionKey: "",
        suiteCode: "",
        customerId: null,
        businessCommunicationCustomer:null,
        // websocket 是否已连接 用于底部tab收到新消息时消息有红点
        isOnSocketOpen:false,
    },
    // 跳转登录页
    goLogin(e) {
        wx.navigateTo({
            url: "/pages/login/login"
        })
    },
    // mangerUrl: 'http://test2.hhrchina.com',
    mangerUrl: "http://192.168.18.138",
    // mangerUrl: "https://www.hhrchina.com",
    // mangerUrlPath: "https://www.hhrchina.com",
    webSocketUrl:"192.168.18.138",
    // webSocketUrl:"test2.hhrchina.com",
})
