UNPKG

autobots-lib

Version:

汽车人基础库

116 lines (88 loc) 2.7 kB
'use strict'; import React,{Component} from 'react'; import ReactNative from 'react-native'; import Native from '../native'; import Toast from './toast'; import {Buffer} from 'buffer'; import PropTypes from 'prop-types'; const { StyleSheet, requireNativeComponent, NativeModules, DeviceEventEmitter, Platform, View, Text, } = ReactNative; let openNative = function(data,callback) { let url = 'autobots://rn/dingding-share?'+data; Native.callNative(url,function(result,data){ if(callback) { callback(result,data); } }); } class Me extends Component { shareText(content,callback){ let type_64 = new Buffer("text").toString('base64'); let content_64 = new Buffer(content).toString('base64'); let data = "type="+type_64+"&content="+content_64; openNative(data,callback); } shareUrl(title,url,pic,content,callback) { let type_64 = new Buffer("url").toString('base64'); let title_64 = title && title!=""?new Buffer(title).toString('base64'):""; let url_64 = url&&url!=""?new Buffer(url).toString('base64'):""; let pic_64 = pic&&pic!=""?new Buffer(pic).toString('base64'):""; let content_64 = content&&content!=""?new Buffer(content).toString('base64'):""; let data = "type="+type_64+"&title="+title_64+"&url="+url_64+"&pic="+pic_64+"&content="+content_64; openNative(data,callback); } sharePic(pic,callback) { let type_64 = new Buffer("pic").toString('base64'); let pic_64 = new Buffer(pic).toString('base64'); let data = "type="+type_64+"&pic="+pic_64; openNative(data,callback); } componentWillMount() { var that = this; function cb_android(e:Event) { if(that.props.onCallBack) { that.props.onCallBack(JSON.parse(e.result)); } } function cb_ios(p) { if(that.props.onCallBack) { that.props.onCallBack(p); } } if(Platform.OS==='android') { this.sinListener = DeviceEventEmitter.addListener("react_event_dingding_result",cb_android); }else { this.sinListener = DeviceEventEmitter.addListener("react_event_dingding_result",cb_ios); } } componentWillUnmount() { if(this.sinListener!=null) { this.sinListener.remove(); this.sinListener = null; } } render() { return(<View/>); } } Me.propTypes= { onCallBack: PropTypes.func.isRequired, } module.exports = Me;