ddlogin-react
ddlogin for react
Installation
$ npm install ddlogin-react --save
Example Usage
import React from 'react'
import DDlogin from 'ddlogin-react'
export default class Login extends React.Component{
constructor(props){
super(props)
this.state = {
APPID: 'dingoatk9pnaxugtzngq9l',
REDIRECT_URI: window.location.href
}
}
componentWillMount() {
const state = this.props.location && this.props.location.query.state;
if(state === 'dinglogin') {
const code = this.props.location.query.code;
}
}
componentDidMount () {
const handleMessage = (event) => {
const loginTempCode = event.data;
const origin = event.origin;
const url = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.state.APPID}&response_type=code&scope=snsapi_login&state=dinglogin&redirect_uri=${this.state.REDIRECT_URI}&loginTmpCode=${loginTempCode}`
if (origin === 'https://login.dingtalk.com') {
window.open(encodeURI(url), '_parent')
}
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', handleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', handleMessage);
}
}
render(){
const options = {
id: "login-container",
goto: `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.state.APPID}&response_type=code&scope=snsapi_login&state=dinglogin&redirect_uri=${this.state.REDIRECT_URI}`,
width: '350px',
height: '350px'
}
return (
<div id="login">
<h2>扫描登录</h2>
<DDlogin options={options}/>
</div>
)
}
}
Other
See more detail from Dingtalk Development Site
Licence
MIT License