forked from yorkie/react-native-wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (164 loc) · 5.64 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use strict";
import { DeviceEventEmitter, NativeModules } from 'react-native';
import promisify from 'es6-promisify';
import { EventEmitter } from 'events';
let isAppRegistered = false;
const {WeChat} = NativeModules;
// Event emitter to dispatch request and response from WeChat.
const emitter = new EventEmitter();
DeviceEventEmitter.addListener('WeChat_Resp', resp => {
emitter.emit(resp.type, resp);
});
// Used only with promisify. Transform callback to promise result.
function translateError(err, result) {
if (!err) {
return this.resolve(result);
}
if (typeof err === 'string') {
return this.reject(new Error(err));
}
this.reject(err);
}
function wrapRegisterApp(nativeFunc) {
if (!nativeFunc) {
return undefined;
}
const promisified = promisify(nativeFunc, translateError);
return (...args) => {
if (isAppRegistered) {
return Promise.reject(new Error('App is already registered.'));
}
isAppRegistered = true;
return promisified(...args);
};
}
function wrapApi(nativeFunc) {
if (!nativeFunc) {
return undefined;
}
const promisified = promisify(nativeFunc, translateError);
return (...args) => {
if (!isAppRegistered) {
return Promise.reject(new Error('registerApp required.'));
}
return promisified(...args);
};
}
/**
* `addListener` inherits from `events` module
* @method addListener
* @param {String} eventName - the event name
* @param {Function} trigger - the function when event is fired
*/
export const addListener = emitter.addListener.bind(emitter);
/**
* `once` inherits from `events` module
* @method once
* @param {String} eventName - the event name
* @param {Function} trigger - the function when event is fired
*/
export const once = emitter.once.bind(emitter);
/**
* `removeAllListeners` inherits from `events` module
* @method removeAllListeners
* @param {String} eventName - the event name
*/
export const removeAllListeners = emitter.removeAllListeners.bind(emitter);
/**
* @method registerApp
* @param {String} appid - the app id
* @return {Promise}
*/
export const registerApp = wrapRegisterApp(WeChat.registerApp);
/**
* @method registerAppWithDescription
* @param {String} appid - the app id
* @param {String} appdesc - the app description
* @return {Promise}
*/
export const registerAppWithDescription = wrapRegisterApp(WeChat.registerAppWithDescription);
/**
* Return if the wechat app is installed in the device.
* @method isWXAppInstalled
* @return {Promise}
*/
export const isWXAppInstalled = wrapApi(WeChat.isWXAppInstalled);
/**
* Return if the wechat application supports the api
* @method isWXAppSupportApi
* @return {Promise}
*/
export const isWXAppSupportApi = wrapApi(WeChat.isWXAppSupportApi);
/**
* Get the wechat app installed url
* @method getWXAppInstallUrl
* @return {String} the wechat app installed url
*/
export const getWXAppInstallUrl = wrapApi(WeChat.getWXAppInstallUrl);
/**
* Get the wechat api version
* @method getApiVersion
* @return {String} the api version string
*/
export const getApiVersion = wrapApi(WeChat.getApiVersion);
/**
* Open wechat app
* @method openWXApp
* @return {Promise}
*/
export const openWXApp = wrapApi(WeChat.openWXApp);
// wrap the APIs
const nativeShareToTimeline = wrapApi(WeChat.shareToTimeline);
const nativeShareToSession = wrapApi(WeChat.shareToSession);
const nativeSendAuthRequest = wrapApi(WeChat.sendAuthRequest);
/**
* @method sendAuthRequest
* @param {Array} scopes - the scopes for authentication.
* @return {Promise}
*/
export function sendAuthRequest(scopes, state) {
return new Promise((resolve, reject) => {
WeChat.sendAuthRequest(scopes, state,() => {});
// TODO(Yorkie): should we use once to instead?
emitter.on('SendAuth.Resp', (resp) => {
const result = resp.errCode;
if (result === 0) {
resolve(resp.code);
} else {
reject(result);
}
});
});
}
/**
* Share something to timeline/moments/朋友圈
* @method shareToTimeline
* @param {Object} data
* @param {String} data.thumbImage - Thumb image of the message, which can be a uri or a resource id.
* @param {String} data.type - Type of this message. Could be {news|text|imageUrl|imageFile|imageResource|video|audio|file}
* @param {String} data.webpageUrl - Required if type equals news. The webpage link to share.
* @param {String} data.imageUrl - Provide a remote image if type equals image.
* @param {String} data.videoUrl - Provide a remote video if type equals video.
* @param {String} data.musicUrl - Provide a remote music if type equals audio.
* @param {String} data.filePath - Provide a local file if type equals file.
* @param {String} data.fileExtension - Provide the file type if type equals file.
*/
export function shareToTimeline(data) {
return nativeShareToTimeline(data);
}
/**
* Share something to a friend or group
* @method shareToSession
* @param {Object} data
* @param {String} data.thumbImage - Thumb image of the message, which can be a uri or a resource id.
* @param {String} data.type - Type of this message. Could be {news|text|imageUrl|imageFile|imageResource|video|audio|file}
* @param {String} data.webpageUrl - Required if type equals news. The webpage link to share.
* @param {String} data.imageUrl - Provide a remote image if type equals image.
* @param {String} data.videoUrl - Provide a remote video if type equals video.
* @param {String} data.musicUrl - Provide a remote music if type equals audio.
* @param {String} data.filePath - Provide a local file if type equals file.
* @param {String} data.fileExtension - Provide the file type if type equals file.
*/
export function shareToSession(data) {
return nativeShareToSession(data);
}