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

const { regeneratorRuntime } = global
Array.prototype._indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
Array.prototype.remove = function(val) {
    var index = this._indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
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)
            })
        }
        try{
            let sms_code= await this.getClipboardData();
            wx.setClipboardData({
                data:"?"
            })
            console.log('SMS.code',sms_code);
            wx.reLaunch({
                url:'/pages/customer/index?sms_code='+sms_code
            });
        }catch (e){
            console.log('SMS.code.catch',e);
        }
    },
    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;
    },
    getClipboardData(){
        return new Promise((success,fail)=>{
            wx.getClipboardData({
                success:(res)=>{
                    let match=res.data.match(/#(.*)#/);
                    if(match && res.data.indexOf('一键订')>0){
                        success(match[1])
                    }else{
                        fail()
                    }
                },
                fail:()=>{
                    fail()
                }
            })
        })
    },
    onHide(){
        console.log('obHide');
        let time=new Date()/1000;
        wx.getStorageInfo({
            success:(res)=>{
                console.log(res.keys);
                if(res.keys.length>0){
                    res.keys.forEach(key=>{
                        wx.getStorage({
                            key:key,
                            success:(ca)=>{
                                //console.log('obHide',ca.data.timeout,time);
                                if(ca.data.timeout && ca.data.timeout<time && ca.data.timeout!=-1){
                                    wx.removeStorage({key:key})
                                }
                            }
                        })
                    })
                }
            },
        })
    }
})