-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
97 lines (91 loc) · 3.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict'
import {NativeModules,Platform,CameraRoll} from 'react-native'
import RNFS from 'react-native-fs';
const Md5 = require('md5');
const shareLocal = NativeModules.RNShareLocal
export function shareMessage(option){
if(option instanceof Object && option.length == undefined){
if(Platform.OS === 'ios'){
return shareLocal.message(option.text,option.image,option.excluded,(result)=>{
if(option.callback)option.callback(result);
});
}else if (Platform.OS === 'android') {
return shareLocal.message(option.winTitle,option.subject,option.text,option.component,(error)=>{
if(option.callback)option.callback(error);
});
}
}
}
export function shareLink(option){
if(option instanceof Object && option.length == undefined){
if(Platform.OS === 'ios'){
return shareLocal.link(option.title,option.link,option.icon,option.excluded,(result)=>{
if(option.callback)option.callback(result);
});
}else if (Platform.OS === 'android') {
return shareLocal.link(option.winTitle,option.subject,option.url,option.component,(error)=>{
if(option.callback)option.callback(error);
});
}
}
}
export function sharePictures(option){
if(option instanceof Object && option.length == undefined){
if(Platform.OS === 'ios'){
return shareLocal.pictures(option.imagesUrl,option.excluded,(result)=>{
if(option.callback)option.callback(result);
});
}else if (Platform.OS === 'android') {
downloadImage(option.imagesUrl).then(imagesFile=>{
return shareLocal.pictures(option.winTitle,option.subject,option.text,imagesFile,option.component,(error)=>{
if(option.callback)option.callback(error);
});
});
}
}
}
function getImageBase64 (url) {
return new Promise((RES, REJ) => {
fetch(url).then(r => r.blob()).then(blob => {
const reader = new FileReader();
reader.onload = (e) => {
const data = e.target.result;
RES(data.split('base64,')[1]);
}
reader.readAsDataURL(blob);
}).catch(REJ);
})
}
export function downloadImage(imagesUrl){
if (Platform.OS === 'android') {
const storeLocation = `${RNFS.ExternalCachesDirectoryPath}`;
return new Promise((resolve, reject)=>{
var downFiles=[];
var downNum=0;
for(let key in imagesUrl){
let pathName = Md5(imagesUrl[key]) + ".jpg";
let downloadDest = `${storeLocation}/${pathName}`;
if(imagesUrl[key].substring(0,4) == 'file'){
RNFS.moveFile(imagesUrl[key],downloadDest).then((result)=>{
downFiles[key]="file://" + downloadDest;
downNum = downNum + 1;
if(downFiles.length == imagesUrl.length)resolve(downFiles);
})
}else{
RNFS.exists(downloadDest).then(exists=>{
if(!exists){
RNFS.downloadFile({fromUrl:imagesUrl[key],toFile:downloadDest}).promise.then(res => {
downFiles[key]="file://" + downloadDest;
downNum = downNum + 1;
if(downNum == imagesUrl.length)resolve(downFiles);
});
}else{
downFiles[key]="file://" + downloadDest;
if(downFiles.length == imagesUrl.length)resolve(downFiles);
}
})
}
}
});
}
}