-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (148 loc) · 3.95 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react'
import {
Linking,
Platform,
} from 'react-native'
const launchURL = (url) => {
Linking.canOpenURL(url).then(supported => {
if (!supported) {
console.log('Can\'t handle url: ' + url)
} else {
return Linking.openURL(url)
}
}).catch(err => console.error('An unexpected error happened', err))
}
const isCorrectType = (expected, actual) => {
return Object.prototype.toString.call(actual).slice(8, -1) === expected
}
const getValidArgumentsFromArray = (array, type) => {
var validValues = []
array.forEach((value) => {
if (isCorrectType(type, value)) {
validValues.push(value)
}
})
return validValues
}
const LinkingWrap = {
phonecall(phoneNumber, prompt) {
if (arguments.length !== 2) {
console.log('you must supply exactly 2 arguments')
return
}
if (!isCorrectType('String', phoneNumber)) {
console.log('the phone number must be provided as a String value')
return
}
if (!isCorrectType('Boolean', prompt)) {
console.log('the prompt parameter must be a Boolean')
return
}
let url
if (Platform.OS !== 'android') {
url = prompt ? 'telprompt:' : 'tel:'
} else {
url = 'tel:'
}
url += phoneNumber
launchURL(url)
},
/**
* (description)
* 发送邮件
* @param to 字符串或多个字符串的数组
* @param cc 字符串或多个字符串的数组
* @param bcc 字符串或多个字符串的数组
* @param subject 字符串
* @param body 字符串
*/
email(to, cc, bcc, subject, body) {
let url = 'mailto:'
let argLength = arguments.length
switch (argLength) {
case 0:
launchURL(url)
return
case 5:
break
default:
console.log('you must supply either 0 or 5 arguments. You supplied ' + argLength)
return
}
let valueAdded = false
if (isCorrectType('Array', arguments[0])) {
const validAddresses = getValidArgumentsFromArray(arguments[0], 'String')
if (validAddresses.length > 0) {
url += validAddresses.join(',')
}
}
url += '?'
if (isCorrectType('Array', arguments[1])) {
const validAddresses = getValidArgumentsFromArray(arguments[1], 'String')
if (validAddresses.length > 0) {
valueAdded = true
url += 'cc=' + validAddresses.join(',')
}
}
if (isCorrectType('Array', arguments[2])) {
if (valueAdded) {
url += '&'
}
const validAddresses = getValidArgumentsFromArray(arguments[2], 'String')
if (validAddresses.length > 0) {
valueAdded = true
url += 'bcc=' + validAddresses.join(',')
}
}
if (isCorrectType('String', arguments[3])) {
if (valueAdded) {
url += '&'
}
valueAdded = true
url += 'subject=' + arguments[3]
}
if (isCorrectType('String', arguments[4])) {
if (valueAdded) {
url += '&'
}
url += 'body=' + arguments[4]
}
url = encodeURI(url)
launchURL(url)
},
/**
* (description)
* 发送短信
* @param phoneNumber 号码
*/
text(phoneNumber) {
if (arguments.length > 1) {
console.log('you supplied too many arguments. You can either supply 0 or 1')
return
}
let url = 'sms:'
if (arguments.length !== 0) {
if (isCorrectType('String', phoneNumber)) {
url += phoneNumber
} else {
console.log('the phone number should be provided as a string. It was provided as '
+ Object.prototype.toString.call(phoneNumber).slice(8, -1)
+ ',ignoring the value provided')
}
}
launchURL(url)
},
web(address) {
if (!address) {
console.log('Missing address argument')
return
}
if (!isCorrectType('String', address)) {
console.log('address was not provided as a string, it was provided as '
+ Object.prototype.toString.call(address).slice(8, -1))
return
}
launchURL(address)
},
}
export default LinkingWrap