关于钉钉扫码登录第三方网站

钉钉开放文档地址:https://open.dingtalk.com/document/isvapp-server/scan-qr-code-to-log-on-to-third-party-websites

<div v-if="curItem.type === 'DING_TALK'"
	class="px-2 py-2 text-caption d-flex justify-center align-center"
	style="width:270px;height:270px;margin: 0 auto;overflow:hidden;"
	id="ding_code"
	ref="ding_code">
</div>

data(){
	return {
		curItem:{}
	}
},
mounted(){
	if(this.curItem.type === 'Ding_TALK'){
		this.fetchStart()
	}	
},
methods:{
	fetchStart(){
		//调接口获取startRes数据
		this.loadJs(startRes)
	},
	async loadJs(startRes) {
      const dingJs = "//g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js";
      await new Promise((resolve, reject) => {
        // @see: https://stackoverflow.com/questions/8578617
        let scriptEle = document.createElement("script");
        scriptEle.type = "text/javascript";
        scriptEle.async = false;
        scriptEle.src = dingJs;
        scriptEle.onload = resolve;
        scriptEle.addEventListener("load", resolve);
        scriptEle.addEventListener("error", e => reject(e.error));
        document.head.appendChild(scriptEle);
      });
      
       this.dingLoginFn(
          startRes.content.appid,
          startRes.content.redirect_uri
        );
      }
    },
    dingLoginFn(appid, redirect_uri) {
      this.ding_appid = appid;
      this.ding_redirecturi = redirect_uri;
      return new window.DDLogin({
        id: "ding_code",
        goto: encodeURIComponent(
          `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${appid}&response_type=code&scope=snsapi_login&state=state&redirect_uri=${redirect_uri}`
        ),
        style: "border:none;background: none;",
        height: "330"
      });
    },
    // 钉钉事件
    dingEvent() {
      let handleMessage = function(event) {
        var origin = event.origin;
        if (origin == "https://login.dingtalk.com") {
          let loginTmpCode = event.data;
          if (loginTmpCode) {
            window.location.href = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.ding_appid}&response_type=code&scope=snsapi_login&state=state&redirect_uri=${this.ding_redirecturi}&loginTmpCode=${loginTmpCode}`;
          }
        }
      };
      if (typeof window.addEventListener != "undefined") {
        window.addEventListener("message", handleMessage, false);
      } else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onmessage", handleMessage);
      }
    },
},
watch:{
	curItem() {
      if (this.curItem.type === "DING_TALK") {
        this.dingEvent();
      }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值