{"size": 2090, "ext": "js", "max_stars_repo_path": "src/core.js", "max_stars_repo_name": "lemix-project/lemix-js-sdk", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/core.js", "max_issues_repo_name": "lemix-project/lemix-js-sdk", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/core.js", "max_forks_repo_name": "lemix-project/lemix-js-sdk", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.8288288288, "max_line_length": 101, "alphanum_fraction": 0.5961722488}
let _platform = ''
let _callbackObj = new Object()

const __android__ = 'android'
const __ios__ = 'ios'

/**
 * 设置当前平台（通常由平台调用）
 * @param platform 平台名称：android / ios
 */
let setPlatform = (platform) => {
    _platform = platform
}

/**
 * 获取当前设置的所属平台
 * @returns {string} 平台名称：android / ios
 */
let getPlatform = () => {
    return _platform
}

/**
 * 调用平台入口函数
 * @param type
 * @param parameters
 */
let sendMessage = (type, parameters, isSync) => {
    let data = JSON.stringify({
        type: type,
        params: parameters,
        sync: isSync
    })
    if (getPlatform() === __android__) {
        // android消息发送
        return window.__js_android__.message(data)
    }
    else if (getPlatform() === __ios__) {
        // ios消息发送7
        let result = window.prompt(data)
        return result
    }
}

/**
 * 保存callback函数
 * @param uuid
 * @param fn
 */
let saveCallback = (fn) => {
    let uuid = createUUID()
    _callbackObj[uuid] = fn
    return uuid
}

/**
 * 读取callback函数
 * @param uuid
 * @returns {*}
 */
let loadCallback = function (uuid) {
    let arr = Array.prototype.slice.call(arguments)
    arr.splice(0, 1);
    return _callbackObj[uuid].apply(window, arr)
}

/**
 * 销毁uuid和callback映射关系
 * @param uuid
 */
let distoryCallback = (uuid) => {
    delete _callbackObj[uuid]
}

/**
 * create uuid
 */
let createUUID = (function (uuidRegEx, uuidReplacer) {
    return function () {
        return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(uuidRegEx, uuidReplacer).toUpperCase();
    };
})(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0,
        v = c == "x" ? r : (r & 3 | 8);
    return v.toString(16);
})

/**
 * 统一处理config，把config中的回调函数传入saveCallback
 * @param config
 * @returns {*}
 */
let handleConfig = (config) => {
    for (let key in config) {
        if (typeof config[key] === "function") {
            config[key] = saveCallback(config[key])
        }
    }
    return config
}

export default {
    setPlatform,
    getPlatform,
    sendMessage,
    saveCallback,
    loadCallback,
    distoryCallback,
    createUUID,
    handleConfig
}