<html>
  <head>
    <title>Dingtalk Login</title>
    <script src="//g.alicdn.com/ilw/ding/0.9.2/scripts/dingtalk.js"></script>
  </head>
  <body>
    <div id="notify"></div>
    <script>
      var next_url = '';
      if(typeof window.URLSearchParams != 'undefined'){
          next_url = new URLSearchParams(location.search).get("next") || '';
      }
      function notify(msg){
          document.getElementById("notify").innerHTML = msg;
      }
      dd.ready(function(){
          dd.runtime.permission.requestAuthCode({
            corpId: "{{ dingtalk_data.corpId }}",
            onSuccess: function(result){
              dd.biz.util.openLink({
                url: location.protocol
                     + "//" + location.host
                     + "/pauli/auth/web/dingtalk/success?code="
                     + result.code
                     + "&next="
                     + encodeURIComponent(next_url)});
            },
            onFail: function(err){
                notify("permission error: " + JSON.stringify(err));
            }
          });
      });

      dd.error(function(err){
          notify("global error: " + JSON.stringify(err));
      });

      dd.config({
        agentId: "{{ dingtalk_data.agentId }}",
        corpId: "{{ dingtalk_data.corpId }}",
        timeStamp: "{{ dingtalk_data.timeStamp }}",
        nonceStr: "{{ dingtalk_data.nonceStr }}",
        signature: "{{ dingtalk_data.signature }}",
        jsApiList: ['runtime.info', 'runtime.permission.requestAuthCode']
      });

    </script>
  </body>
</html>
