<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>饿了么开放平台</title>
  <script src="https://static11.elemecdn.com/eleme/napos.order.fe.melody/jssdk.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
  <h1></h1>
  <div class="w1000">
    <div class="userinfo">
      <p>当前登录用户ID为<span id="userId"></span></p>
      <p>当前店铺id为<span id="shopId"></span></p>
    </div>
    <div class="icon-box">
      <img src="https://fuss.alpha.elenet.me/0/71/ef76406ecd56a0cbac11a869eded1png.png" alt="">
      </br>
      智能配送助手
    </div>
    <dl>
      <dt class="abc">【智能配送助手】由【饿了么】进行开发</dt>
      <dd>
        饿了么商家开放平台作为一个服务集成平台，致力于满足商户的多样需求，提供合作伙伴更多的平台资源，服务商户接入，提供给商户更丰富的经营能力。同时，开放平台将通过提供一系列非业务性的，例如安全保障、服务监控告警、日志分析统计等解决方案，让开发者专注于业务系统的开发，降低接入者的开发成本。
      </dd>
    </dl>
  </div>

  <style>
    dt,dd{
      margin: 0;
    }
    .w1000{
      width: 1000px;
      margin: 0 auto;
      display: none
    }
    .userinfo{
      text-align: center;
    }
    .icon-box{
      text-align: center;
    }
    .icon-box img{
      width: 200px;
      height: 200px;
    }
  </style>
</body>
<script>
  function post(url, param) {
    return new Promise(function(resolve, reject) {
      var xhr = new XMLHttpRequest()
      xhr.open('POST',url, true)
      xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
          if (xhr.status === 200 || xhr.status === 1223) {
            console.log(xhr.responseText)
            // 这边双方约定返回json字符串
            resolve(JSON.parse(xhr.responseText))
          }
        }
      }
      xhr.setRequestHeader('content-type', 'application/json')
      xhr.send(JSON.stringify(param))
    })
  }

  window.onload = function() {
    // 如果不在客户端打开，提示需要在客户端打开
    // if (!eleme.userAgent.isClient) {
    //   document.querySelector('h1').innerHTML = '请在napos客户端打开'
    // } else {
      // Promise.all([
      //   eleme.getUserId(),
      //   eleme.getCurrentShopId()
      // ]).then(function (result) {
      //   post('/getInfo', {
      //     userId: result[0],
      //     shopId: result[1]
      //   }).then(result => {
      //     if (result.OAuthUrl) {
      //       location.href = result.OAuthUrl
      //     } else {
      //       var shopName = result.shopInfo.name
      //       var shopId = result.shopInfo.id
      //       var userId = result.userInfo.userId
      //       var text = '当前店铺为' + shopName + '\n' + 'shopId为' + shopId + '\n' + 'userId为' + userId
      //       document.querySelector('#result').innerHTML = text
      //     }
      //   })
      // })
    // }
    Promise.all([
      eleme.getUserId(),
      eleme.getCurrentShopId()
    ]).then(function (result) {
      post('/getInfo', {
        userId: result[0],
        shopId: result[1]
      }).then(result => {
        if (result.OAuthUrl) {
          location.href = result.OAuthUrl
        } else {
          var shopName = result.shopInfo.name
          var shopId = result.shopInfo.id
          var userId = result.userInfo.userId
          document.querySelector('#userId').innerHTML = userId
          document.querySelector('#shopId').innerHTML = shopId
          document.querySelector('.w100').style.display = 'block'
        }
      })
    })
  }
</script>
</html>