export default {
  init: function () {
    console.log('初始化百度地图脚本...')
    // Zfj5wQaAKiaRdc2zoHGorkGWg4oLuH3G
    const AK = 'E4GVGDUxWHpt3903bArFvIa54LkkSz1f'
    // Zfj5wQaAKiaRdc2zoHGorkGWg4oLuH3G E4GVGDUxWHpt3903bArFvIa54LkkSz1f 1XjLLEhZhQNUzd93EjU5nOGQ
    // eslint-disable-next-line camelcase
    const BMap_URL = 'https://api.map.baidu.com/api?v=3.0&ak=' + AK + '&callback=onBMapCallback'
    // const BMap_URL = 'https://api.map.baidu.com/api?v=2.0&ak=' + AK + '&s=1&callback=onBMapCallback'
    return new Promise((resolve, reject) => {
      // 如果已加载直接返回
      if (typeof BMap !== 'undefined') {
        // eslint-disable-next-line no-undef
        resolve(BMap)
        return true
      }
      // 百度地图异步加载回调处理
      window.onBMapCallback = function () {
        console.log('百度地图脚本初始化成功...')
        // eslint-disable-next-line no-undef
        resolve(BMap)
      }

      // 插入script脚本
      let scriptNode = document.createElement('script')
      scriptNode.setAttribute('type', 'text/javascript')
      scriptNode.setAttribute('src', BMap_URL)
      document.body.appendChild(scriptNode)
    })
  }
}
