PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    uniapp实现钉钉扫码登录

    作者:admin来源:网络浏览:时间:2021-12-30 12:59:20我要评论
    导读:由于uniapp暂无钉钉授权登录所以本文将钉钉扫码登录作为网页嵌入uniapp,最终实现钉钉扫码登录app1. 用H5调起钉钉扫码登录钉钉在网页端的...
    由于uniapp暂无钉钉授权登录所以本文将钉钉扫码登录作为网页嵌入uniapp,最终实现钉钉扫码登录app

    1. 用H5调起钉钉扫码登录

    钉钉在网页端的扫码登录可参考钉钉文档:扫码登录第三方网站 - 钉钉开放平台 (dingtalk.com)

     

    1. // 钉钉扫码登录 
    2.     dingLoginFn() { 
    3.       let dingData = { 
    4.         appid: OUT_LINK_CONFIG.dingAppid, 
    5.         state: "STATE"
    6.         url: encodeURIComponent('登录后的回调地址:可以是你的H5的一个页面地址(href)'// 这个地址御用钉钉扫码确认后的路由重定向(会携带扫码获取的code值) 
    7.       }; 
    8.       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}`; 
    9.       let goto = encodeURIComponent(oauth); 
    10.       DDLogin({ 
    11.         id: "loginContainer"//这里需要你在自己的页面定义一个HTML标签并设置id,例如<div id="login_container"></div>或<span id="login_container"></span> 
    12.         gotogoto
    13.         style: "border:none;background-color:#FFFFFF;"
    14.         width: "268" 
    15.       }); 
    16.       let handleMessage = (event) => { 
    17.         // 判断是否来自ddLogin扫码事件。 
    18.         if (event.origin == "https://login.dingtalk.com" && event.data) { 
    19.           console.log("loginTmpCode", event.data); 
    20.           window.location.href = `${oauth}&loginTmpCode=${event.data}`; // 获取到loginTmpCode后就可以在这里构造跳转链接进行跳转了 
    21.         } 
    22.       }; 
    23.       if (typeof window.addEventListener != "undefined") { 
    24.         window.addEventListener("message", handleMessage, false); 
    25.       } else if (typeof window.attachEvent != "undefined") { 
    26.         window.attachEvent("onmessage", handleMessage); 
    27.       } 
    28.     } 

    2. 用于路由重定向的地址最好不要是调起钉钉二维码的网页地址(步骤1的地址),因为在uniapp中如果两个地址一样会导致回传code到登录的过程再次展示一下二维码页面才跳转到登录成功界面。路由重定向页面(本文采用Vue构建),想要在H5中使用uni的API,需要在public/index.html中引入uni的jdk

    1. <!DOCTYPE html> 
    2. <html lang=""
    3.   <head> 
    4.     <meta charset="utf-8"
    5.     <meta http-equiv="X-UA-Compatible" content="IE=edge"
    6.     <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> 
    7.     <!-- 引入钉钉扫码登录的JDK --> 
    8.     <script src="https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script> 
    9.     <title></title> 
    10.   </head> 
    11.   <body> 
    12.     <noscript> 
    13.       <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> 
    14.     </noscript> 
    15.     <div id="app"></div> 
    16.     <!-- built files will be auto injected --> 
    17.   </body>  <!-- 引入uni API的JDK 注意:一定要在body后引入 在head中引入可能获取失败 --> 
    18.   <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script> 
    19. </html> 

    重定向页面:

     

    1. <template> 
    2.   <div></div> 
    3. </template> 
    4. <script> 
    5. export default { 
    6.   name: "LoginCallback"
    7.   mounted() {  // 在mounted生命周期监听钉钉重定向后携带的参数并回传uniapp 
    8.     document.addEventListener("plusready", () => { 
    9.       this.$nextTick(() => { 
    10.         // 待触发 `UniAppJSBridgeReady` 事件后,即可调用 uni 的 API。如果不是一打开页面就调用 可以不用这个监听 
    11.         document.addEventListener("UniAppJSBridgeReady", () => { 
    12.           // this.$toast("location.search:::", JSON.stringify(location.search)); 
    13.           if (location.search.includes("?code")) { 
    14.             console.log("getCode:::"); 
    15.             let code = location.search.split("?")[1].split("&")[0].split("=")[1];       // 这里获取code后通过uni API 跳转回uniapp的页面并携带参数(uniapp中在onLoad中获取) 也可以通过uniapp提供的 uni.postMessage({data: {code}}) 去传递(注意:通过postMessage传的参数在uniap中获取的data是一个数组) 
    16.             uni.webView.navigateTo({ 
    17.               url: `/pages/login/login_webview?code=${code}` 
    18.             }); 
    19.           } 
    20.         }); 
    21.       }); 
    22.     }); 
    23.   } 
    24. }; 
    25. </script> 

    3. uniapp中可以使用webview去承载钉钉扫码的网页,并接收钉钉扫码后获取的code参数 

     

    1. <template> 
    2.     <view> 
    3.         <web-view :src="url"></web-view> 
    4.     </view> 
    5. </template> 
    6.  
    7. <script> 
    8.     import { dingLogin } from '@/api/login' 
    9.     import { setToken } from "@/utils/auth" 
    10.     export default { 
    11.         name: "LoginWebview"
    12.         data() { 
    13.             return { 
    14.                 url: 'http://xxxxxxx/dd_login' // 这里的 url 就是步骤1中写的钉钉扫码网页地址 
    15.             } 
    16.         }, 
    17.         onLoad(options) {              // 这里是扫码后回传的参数code 用于登录 
    18.             if (options.code) { 
    19.                 this.login(options.code) 
    20.             } 
    21.         }, 
    22.         methods: { 
    23.             async login(code) { 
    24.                 uni.showLoading() 
    25.                 try { 
    26.                     const res = await dingLogin(code) 
    27.                     if (res.data.code === 200) { 
    28.                         setToken(res.data.token) 
    29.                         uni.reLaunch({ 
    30.                             url: '/pages/home/index' 
    31.                         }) 
    32.                     } 
    33.                     uni.hideLoading() 
    34.                 } catch (e) { 
    35.                     console.log('登录失败', e) 
    36.                     uni.hideLoading() 
    37.                 } 
    38.             } 
    39.         } 
    40.     } 
    41. </script> 








     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-6654-1.html
    相关热词搜索: uniapp钉钉扫码登录