钉钉小程序入门3—钉钉扫码登录PC端网站
创始人
2024-03-20 07:18:18
0

第一部分、准备材料🌲

公网环境

老版钉钉扫码中必须要配置一个域名才可以调试,新版支持IP配置调了。我是手机打开热点,电脑连接热点进行调试的,比老版要方便了不少。

查看本机IP地址方法:
如果使用的Windows,执行命令:ipconfig

如果使用的Mac,执行命令:ifconfig en0

创建一个小程序(企业内应用即可)

这个小程序可以是H5微应用也可以是小程序,但需要是企业内应用。
将创建后的小程序appKey和appSecret

在这里插入图片描述

搭建SpringBoot项目

目录结构如下:
在这里插入图片描述

注意:resources中的目录结构和文件名一定要和我的一致,不能多不能少,否则会出现启动报错或者找不到文件的问题。

第二部分、环境配置⚙

SpringBoot项目pom.xml

这里我引入了VM模板用来放置扫码页与首页的html代码,引入DingTalk调用三方接口获取扫码用户的基本信息。


4.0.0org.springframework.bootspring-boot-starter-parent2.7.1 com.exampleSpringBoot-DDScan0.0.1-SNAPSHOTSpringBoot-DDScanDemo project for Spring Boot1.8org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestcom.aliyunalibaba-dingtalk-service-sdk1.1.1log4jlog4jcom.aliyundingtalk1.2.5com.alibaba.bootvelocity-spring-boot-starter1.0.4.RELEASEorg.springframework.bootspring-boot-maven-plugin

SpringBoot项目application.properties

spring.application.name=dd-scan
server.port=8080## 小程序AppID和AppSecret(此处填入自己复制的)
qr.appId=xxxx
qr.appSerret=xxxx# Velocity\u914D\u7F6E\uFF0C\u8BE6\u89C1 http://gitlab.alibaba-inc.com/middleware-container/pandora-boot/wikis/spring-boot-velocity
spring.velocity.resource-loader-path=classpath:/velocity/templates
spring.velocity.toolbox-config-location=/velocity/toolbox.xml
spring.velocity.layout-url=/velocity/layout/default.vm

钉钉开放平台-登录与分享配置回调域名

这里的回调地址就是扫码后跳转的地址。这里的地址与下面login.vm中的window.url 一定要一模一样,否则会扫码会弹出”回调域名配置错误“的提示。
在这里插入图片描述

钉钉开放平台-权限管理授权

选择:个人手机号信息、成员信息读权限权限即可
在这里插入图片描述

第三部分、代码📚

后端代码

SpringBootDdScanApplication.java

package com.example.springbootddscan;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringBootDdScanApplication {public static void main(String[] args) {SpringApplication.run(SpringBootDdScanApplication.class, args);}}

MainController.java

package com.example.springbootddscan.controller;import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders;
import com.aliyun.dingtalkcontact_1_0.models.GetUserResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;@Controller
@RequestMapping("/login")
public class MainController {@Value("${qr.appId}")private String qrAppId;@Value("${qr.appSerret}")private String qrAppSecret;@GetMapping("/toLoginPage")public ModelAndView toLoginPage() {return new ModelAndView("login");}@GetMapping("/scanLogin")public ModelAndView scanLogin(@RequestParam String authCode) throws Exception {//获取当前小程序的accesstokenGetUserTokenResponse userTokenResponse = getUserAccessToken(qrAppId, qrAppSecret, authCode, "authorization_code");//查询当前用户信息GetUserResponse me = getUserWithOptions(userTokenResponse.getBody().getAccessToken(), "me");//获取首页模板ModelAndView modelAndView = new ModelAndView("index");modelAndView.addObject("userid",me.getBody().getOpenId());modelAndView.addObject("userName",me.getBody().getNick());modelAndView.addObject("userPhone",me.getBody().getMobile());return modelAndView;}public GetUserResponse getUserWithOptions(String accessToken, String unionId) throws Exception {// 准备请求配置参数Config config = new Config();// 设置请求协议config.protocol = "https";// 设置请求区域config.regionId = "central";// 初始化账号Clientcom.aliyun.dingtalkcontact_1_0.Client client = new com.aliyun.dingtalkcontact_1_0.Client(config);GetUserHeaders getUserHeaders = new GetUserHeaders();getUserHeaders.xAcsDingtalkAccessToken = accessToken;return client.getUserWithOptions(unionId, getUserHeaders, new RuntimeOptions());}public GetUserTokenResponse getUserAccessToken(String suiteKey, String suiteSecret, String authCode, String grantType) throws Exception {// 准备请求配置参数Config config = new Config();// 设置请求协议config.protocol = "https";// 设置请求区域config.regionId = "central";// 初始化账号Clientcom.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest().setClientId(suiteKey).setClientSecret(suiteSecret).setCode(authCode).setGrantType(grantType);return client.getUserToken(getUserTokenRequest);}
}

前端代码

login.vm



钉钉扫码登录






index.vm

首页

userid: ${userid}

用户名: ${userName}

手机号码:${userPhone}

其他文件

toolbox.xml



default.vm

$!{screen_content}

第四部分、演示🍓

输入登录地址

http://xxx.xxx.xxx.xxx:8080/login/toLoginPage

这里记住一定要用IP,不能使用localhost或者127.0.0.1,否则即使二维码能出现,扫码完也不会有反应。
在这里插入图片描述

使用钉钉扫码

扫完码后钉钉会弹出一个授权页,点击同意即可。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RC5ajAZU-1670244714399)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d636a914a6144afc965d47a2aa50b409~tplv-k3u1fbpfcp-watermark.image?)]

钉钉回调地址进入首页

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-llcMLSRh-1670244714400)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bbbfd463657841febc7bed7e481ce3b5~tplv-k3u1fbpfcp-watermark.image?)]

第五部分、原理解释

钉钉文档链接:https://open.dingtalk.com/document/orgapp-server/tutorial-obtaining-user-personal-information

相关内容

热门资讯

汽车油箱结构是什么(汽车油箱结... 本篇文章极速百科给大家谈谈汽车油箱结构是什么,以及汽车油箱结构原理图解对应的知识点,希望对各位有所帮...
美国2年期国债收益率上涨15个... 原标题:美国2年期国债收益率上涨15个基点 美国2年期国债收益率上涨15个基...
嵌入式 ADC使用手册完整版 ... 嵌入式 ADC使用手册完整版 (188977万字)💜&#...
重大消息战皇大厅开挂是真的吗... 您好:战皇大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...
盘点十款牵手跑胡子为什么一直... 您好:牵手跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游...
senator香烟多少一盒(s... 今天给各位分享senator香烟多少一盒的知识,其中也会对sevebstars香烟进行解释,如果能碰...
终于懂了新荣耀斗牛真的有挂吗... 您好:新荣耀斗牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信8435338】很多玩家在这款游戏...
盘点十款明星麻将到底有没有挂... 您好:明星麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【5848499】很多玩家在这款游戏...
SAP PS 第9节 合并采购... SAP PS 第9节 合并采购申请、组合WBS之影响1 合并采购申请1.1 合并采购申请后台配置1....
总结文章“新道游棋牌有透视挂吗... 您好:新道游棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【7682267】很多玩家在这款游...