Files
react-native-weibo-login/index.js
2018-06-08 16:29:41 +08:00

51 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NativeModules, NativeEventEmitter } from 'react-native';
const { WeiBo } = NativeModules
//创建原生监听器
const WeiBoNativeEventEmitter = new NativeEventEmitter(WeiBo)
/*
* Scope 是 OAuth2.0 授权机制中 authorize 接口的一个参数。通过 Scope平台将开放更多的微博
* 核心功能给开发者,同时也加强用户隐私保护,提升了用户体验,用户在新 OAuth2.0 授权页中有权利
* 选择赋予应用的功能。
* 目前 Scope 支持传入多个 Scope 权限,用逗号分隔。
*
* 有关哪些 OpenAPI 需要权限申请请查看http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
* 关于 Scope 概念及注意事项请查看http://open.weibo.com/wiki/Scope
* */
const defaultScope = "all"
// 默认 'https://api.weibo.com/oauth2/default.html'
// 必须和sina微博开放平台中应用高级设置中的redirectURI设置的一致不然会登录失败
const defaultRedirectURI = "https://api.weibo.com/oauth2/default.html"
function checkConfig(config) {
if(!config.redirectURI) {
config.redirectURI = defaultRedirectURI
}
if(!config.scope) {
config.scope = defaultScope
}
}
let weiBoAuthListener
const WeiBoLoginEventName = 'weibo_login'
export function login(config={}) {
return new Promise((resolve,reject)=>{
checkConfig(config)
//此回调只会返回SDK是否调用成功授权登录是否成功通过监听返回
WeiBo.login(config,(status)=>{
console.log(status)
});
weiBoAuthListener = WeiBoNativeEventEmitter.addListener(WeiBoLoginEventName,(data)=>{
weiBoAuthListener.remove()
if(data.errCode == 0){ //login success
resolve(data)
}else{ //login fail
reject(data)
}
})
})
}