app.js 3.44 KB
var uri_dc = require('utils/uri.js')
global.regeneratorRuntime = require('lib/regenerator/runtime-module')

const { regeneratorRuntime } = global
App({
    openid:"",
    system:{},
    shareTicket:"",
    onLaunch: async function (opt) {
        console.log('onLaunch',opt);
        if(opt.shareTicket){
            this.shareTicket=opt.shareTicket;
        }
        this.getSystemInfo();
        this.openid=await this.getOpenid();
        console.log('this.openid',this.openid);
    },
    onShow:async function(){
        if (wx.canIUse('getUpdateManager')) {
            const updateManager = wx.getUpdateManager();
            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                console.log('onCheckForUpdate',res)
            })

            updateManager.onUpdateReady(function (res) {
                console.log('onUpdateReady',res)
                updateManager.applyUpdate();
                wx.showToast({
                    title:'版本已更新',
                    duration:2000,
                    mask:true,
                    success:function(){

                    }
                })

            })
            updateManager.onUpdateFailed(function (res) {
                // 新的版本下载失败
                console.log('onUpdateFailed',res)
            })
        }
    },
    getSystemInfo(){
        this._getSystemInfo().then((res)=>{
            console.log('wx.getSystemInfo',res);
            this.system=res;
        }).catch(res=>{
            this.getSystemInfo();
        })
    },
    _getSystemInfo(){
        return new Promise((success,fail)=>{
            wx.getSystemInfo({
                success:(res)=>{
                    if(res.errMsg=='getSystemInfo:ok'){
                        success(res);
                    }else{
                        fail(res);
                    }
                },
                fail:(res)=>{
                    fail(res);
                }
            })
        })
    },
    async _login(){
        return new Promise((success,fail)=>{
            wx.login({
                success: (login_res)=>{
                    success(login_res);
                },
                fail:(res)=>{
                    fail(res);
                }
            })
        })
    },
    async _getLiteOpenidByCode(code){
        return new Promise((success,fail)=>{
            wx.request({
                url: uri_dc.HOST__LINE+'App.Reserve.GetLiteOpenid',
                method: 'POST',
                data: {code:code},
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                    let openid=res.data.data.openid;
                    wx.setStorage({key:'customer_openid',data:openid});
                    success(openid);
                },
                fail: function (res) {
                    console.log(res);
                    fail(res);
                }
            })
        })
    },
    async _getOpenid(){
        try{
            let login_res=await this._login();
            let openid=await this._getLiteOpenidByCode(login_res.code)
            return openid;
        }catch(e){
            return this._getOpenid();
        }
    },
    async getOpenid(){
        let openid=wx.getStorageSync('customer_openid');
        if(!openid){
            openid= await this._getOpenid();
        }
        return openid;
    },
})