DingTalk H5 micro application basic learning

DingTalk Development Documentation

1. Use the debugging tool - IDE:

1. Download the debugging tool first, and create a new enterprise internal application.
If you need administrator privileges, you can create an enterprise yourself.
(tips: Be sure to block the news of the company you created, otherwise there will be a lot of news)
2. Make sure that the node version you are using is above 16, otherwise the following error will be reported:
insert image description here
3. Start according to the tutorial , and the simulator here keeps running Do not open, file changes do not allow saving, so give up .
insert image description here

2. Use local development tools :

1. Execute the following command to install DingTalk-Design-CLI.

npm i dingtalk-design-cli@0.20.4 -g

insert image description here

2. Execute the following command to check whether DingTalk-Design-CLI has been successfully installed.

ding -v

insert image description here

3. Execute the following command to initialize the code template to the local.

ding  init -o h5Test -a h5 -t h5_jsapi_component_demo_vue -l javascript

insert image description here

4. Execute the following commands to start the development and debugging functions.

  • Execute the following command in the newly initialized code directory to start local development and debugging.
cd h5Test
ding dev web

After the command is executed successfully, the "H5 micro-application local development tool" will be opened on the local port 10003 by default, and the expected effect is as follows.
insert image description here
insert image description here

3. Use the DingTalk h5 micro-app to jump to a third-party website:

JSAPI debugging

1. Use uniapp to create a new H5 project

2. Install DingTalk JSAPI:

npm install dingtalk-jsapi --save

3. The main.js file introduces JSAPI:

import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载,也可按需进行加载

4. Write the code in the vue file:

<template>
	<view class="content">
		<view class="text-area">
			<text class="title" v-if="isPhone">仅支持在浏览器端访问</text>
		</view>
	</view>
</template>

<script>
	import * as dd from 'dingtalk-jsapi'; // 引入钉钉JSAPI
	export default {
    
    
		data() {
    
    
			return {
    
    
				isPhone: false // 当前的设备是移动端还是pc端
			}
		},
		onLoad() {
    
    
			var info = navigator.userAgent;
			//通过正则表达式的test方法判断是否包含“Mobile”字符串
			this.isPhone = /mobile/i.test(info);
			console.log("this.isPhone",this.isPhone)
			if(this.isPhone) {
    
    
				// 获取code码,并且将code发送给后端,让后端进行用户信息的获取
				this.init()
			}
		},
		methods: {
    
    
			init() {
    
    
				let that = this
				// 企业的corpid,由前端从URL中获取。
				let corpId = that.getCorpId('corpId');
				// 通过dd.env.platform检查当前是否在钉钉环境下
				if (dd.env.platform !== "notInDingTalk") {
    
    
					dd.ready(function() {
    
    
						// 获取免登授权码
					    dd.runtime.permission.requestAuthCode({
    
    
					        corpId, // 企业id
							success: (res) => {
    
    
							    const {
    
     code } = res; // 通过该免登授权码可以获取用户身份
								that.getAuthor(code)
							},
							fail: () => {
    
    },
							complete: () => {
    
    }
						});
							
					});
				}
			},
			// 从url中获取企业的corpId
			getCorpId(key) {
    
    
				let query = window.location.search.substring(1)
				let vars = query.split("&")
				for (let i=0;i<vars.length;i++) {
    
    
					let item = vars[i].split("=")
					if(item[0] == key) return item[1]
				}
				return false
			},
			// 获取用户的信息,然后完成跳转
			getAuthor(code) {
    
    
				console.log('拿到的code',code)
				dd.biz.util.openLink({
    
    
				    url:"https://ele.qre.com.cn/login.html#/user/login", // 要打开链接的地址
				    onSuccess : function(result) {
    
    
				        /**/
				    },
				    onFail : function(err) {
    
    }
				})
			}
		}
	}
</script>

<style>
	.content {
    
    
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.text-area {
    
    
		display: flex;
		justify-content: center;
	}

	.title {
    
    
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

5. Package it to the backend, deploy to the server, and jointly debug on the server.

Guess you like

Origin blog.csdn.net/Y1914960928/article/details/130709037