function sendMsgToApp(methodName, msg, callback) {
    const ua = window.navigator.userAgent
    const isIos = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    const isAndroid = ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1
    let jsb = new jsBridge()
    if (msg) {
        msg = isAndroid ? msg : JSON.stringify(msg)
    }
    if (jsb[methodName]) {
        return jsb[methodName](msg, callback)
    } else if (isIos) {
        return jsb.callHandler(methodName, msg, callback)
    } else if (isAndroid) {
        return window.dsBridge.call(methodName, msg, callback)
    }
}

function appGetToken(cb) {
    sendMsgToApp("getToken", '', cb)
}

// type: web web_title share login home shoppingCar
function appOpenNewPage(type, inviteCode = '', downLoadUrl = '') {
    sendMsgToApp("openNewPage", { type, inviteCode, downLoadUrl })
}

//  cb 0 成功  -1 错误  -3 取消
function appWxPay({ wxAppId:appId, mercId: merchantId, partnerid: partnerId, prePayId: prepayId, noncestr: nonceStr, timeStamp, packageValue = 'Sign=WXPay', sign }, cb) {
    sendMsgToApp("weChatPay", { appId, merchantId, partnerId, prepayId, nonceStr, timeStamp, packageValue, sign }, cb)
}

function appAliPay() {
    sendMsgToApp("aLiPay",)
}

function appShareGoods() {

}

function getVersionCode(callback) {
    sendMsgToApp("getVersionCode", "",function(e){
        callback(e)
    })
}

function updateApp() {
    sendMsgToApp("updateApp",)
}

function appTokenClear() {
    sendMsgToApp("toLogin",)
}

function appClosePage() {
    sendMsgToApp("close",)
}

function appLogout() {
    sendMsgToApp("logout",)
}



function appSignAuth() {
    return sendMsgToApp("signAuth", "")
}

function DownLoad(url) {
    return sendMsgToApp('DownLoad',url)
}
export const AppMsg = {
    appOpenNewPage,
    appGetToken,
    appWxPay,
    appAliPay,
    appShareGoods,
    getVersionCode,
    updateApp,
    appTokenClear,
    appClosePage,
    appLogout,

    appSignAuth,
    DownLoad
}
