uniapp實現釘釘掃碼登錄


由於uniapp暫無釘釘授權登錄所以本文將釘釘掃碼登錄作為網頁嵌入uniapp,最終實現釘釘掃碼登錄app

1. 用H5調起釘釘掃碼登錄

釘釘在網頁端的掃碼登錄可參考釘釘文檔:掃碼登錄第三方網站 - 釘釘開放平台 (dingtalk.com)

// 釘釘掃碼登錄
    dingLoginFn() {
      let dingData = {
        appid: OUT_LINK_CONFIG.dingAppid,
        state: "STATE",
        url: encodeURIComponent('登錄后的回調地址:可以是你的H5的一個頁面地址(href)') // 這個地址御用釘釘掃碼確認后的路由重定向(會攜帶掃碼獲取的code值)
      };
      let oauth = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${dingData.appid}&response_type=code&scope=snsapi_login&state=${dingData.state}&redirect_uri=${dingData.url}`;
      let goto = encodeURIComponent(oauth);
      DDLogin({
        id: "loginContainer", //這里需要你在自己的頁面定義一個HTML標簽並設置id,例如<div id="login_container"></div>或<span id="login_container"></span>
        goto: goto,
        style: "border:none;background-color:#FFFFFF;",
        width: "268"
      });
      let handleMessage = (event) => {
        // 判斷是否來自ddLogin掃碼事件。
        if (event.origin == "https://login.dingtalk.com" && event.data) {
          console.log("loginTmpCode", event.data);
          window.location.href = `${oauth}&loginTmpCode=${event.data}`; // 獲取到loginTmpCode后就可以在這里構造跳轉鏈接進行跳轉了
        }
      };
      if (typeof window.addEventListener != "undefined") {
        window.addEventListener("message", handleMessage, false);
      } else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onmessage", handleMessage);
      }
    }

2. 用於路由重定向的地址最好不要是調起釘釘二維碼的網頁地址(步驟1的地址),因為在uniapp中如果兩個地址一樣會導致回傳code到登錄的過程再次展示一下二維碼頁面才跳轉到登錄成功界面。路由重定向頁面(本文采用Vue構建),想要在H5中使用uni的API,需要在public/index.html中引入uni的jdk

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <!-- 引入釘釘掃碼登錄的JDK -->
    <script src="https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>
    <title></title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
  <!-- 引入uni API的JDK 注意:一定要在body后引入 在head中引入可能獲取失敗 --> <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script> </html>

重定向頁面:

<template>
  <div></div>
</template>
<script>
export default {
  name: "LoginCallback",
  mounted() {
  // 在mounted生命周期監聽釘釘重定向后攜帶的參數並回傳uniapp document.addEventListener("plusready", () => { this.$nextTick(() => { // 待觸發 `UniAppJSBridgeReady` 事件后,即可調用 uni 的 API。如果不是一打開頁面就調用 可以不用這個監聽 document.addEventListener("UniAppJSBridgeReady", () => { // this.$toast("location.search:::", JSON.stringify(location.search)); if (location.search.includes("?code")) { console.log("getCode:::"); let code = location.search.split("?")[1].split("&")[0].split("=")[1];
       // 這里獲取code后通過uni API 跳轉回uniapp的頁面並攜帶參數(uniapp中在onLoad中獲取) 也可以通過uniapp提供的 uni.postMessage({data: {code}}) 去傳遞(注意:通過postMessage傳的參數在uniap中獲取的data是一個數組) uni.webView.navigateTo({ url: `/pages/login/login_webview?code=${code}` }); } }); }); }); } }; </script>

3. uniapp中可以使用webview去承載釘釘掃碼的網頁,並接收釘釘掃碼后獲取的code參數 

<template>
	<view>
		<web-view :src="url"></web-view>
	</view>
</template>
<script>
	import { dingLogin } from '@/api/login'
	import { setToken } from "@/utils/auth"
	export default {
		name: "LoginWebview",
		data() {
			return {
				url: 'http://xxxxxxx/dd_login' // 這里的 url 就是步驟1中寫的釘釘掃碼網頁地址
			}
		},
		onLoad(options) {
              // 這里是掃碼后回傳的參數code 用於登錄 if (options.code) { this.login(options.code) } }, methods: { async login(code) { uni.showLoading() try { const res = await dingLogin(code) if (res.data.code === 200) { setToken(res.data.token) uni.reLaunch({ url: '/pages/home/index' }) } uni.hideLoading() } catch (e) { console.log('登錄失敗', e) uni.hideLoading() } } } } </script>

好了,大功告成!!!

 

如果有需要從釘釘掃碼的頁面返回uniapp切換登錄方式的操作,可以通過uni的API跳回uniapp

uni.webView.navigateTo({
    url: "/pages/login/index"
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM