App.js
3.7 KB
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Fragment, Component } from 'react';
import {
TouchableOpacity,
SafeAreaView,
ScrollView,
StyleSheet,
StatusBar,
Platform,
View,
Text,
Alert
} from 'react-native';
import {
Header,
Colors
} from 'react-native/Libraries/NewAppScreen';
import * as WeChat from 'react-native-wechat';
export default class App extends Component {
shareOptions = {
title: 'playground',
description: '微信分享测试',
thumbImage: 'https://i.loli.net/2019/09/03/62FauzAY37gsEXV.png',
type: 'news',
webpageUrl: 'https://github.com/little-snow-fox/react-native-wechat-lib'
}
constructor(props) {
super(props);
this.state = {
apiVersion: null,
isWXAppInstalled: false,
wxAppInstallUrl: null,
isWXAppSupportApi: false
}
}
handleOpenApp () {
if (this.state.isWXAppInstalled) {
return WeChat.openWXApp();
} else {
Alert.alert('没有安装微信,请安装之后重试');
}
}
handleShareToSession () {
if (this.state.isWXAppInstalled) {
WeChat.shareToSession(this.shareOptions).catch((error) => {
Alert.alert(error.message);
});
} else {
Alert.alert('没有安装微信,请安装之后重试');
}
}
handleShareToMoment () {
if (this.state.isWXAppInstalled) {
WeChat.shareToTimeline(this.shareOptions).catch((error) => {
Alert.alert(error.message);
});
} else {
Alert.alert('没有安装微信,请安装之后重试');
}
}
async componentDidMount() {
try {
WeChat.registerApp('your wexin AppID'); // Replace with your AppID
this.setState({
apiVersion: await WeChat.getApiVersion(),
wxAppInstallUrl: Platform.OS === 'ios' ? await WeChat.getWXAppInstallUrl(): null,
isWXAppSupportApi: await WeChat.isWXAppSupportApi(),
isWXAppInstalled: await WeChat.isWXAppInstalled()
});
} catch (e) {
console.error(e);
}
}
render() {
const { apiVersion } = this.state;
return (
<Fragment>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<Header />
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.highlight}>
ApiVersion: <Text>{ apiVersion }</Text>
</Text>
<TouchableOpacity style={styles.button} onPress={() => this.handleOpenApp()}>
<Text>打开微信</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={() => this.handleShareToSession()}>
<Text>分享至微信好友</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={() => this.handleShareToMoment()}>
<Text>分享至朋友圈</Text>
</TouchableOpacity>
</View>
</View>
</ScrollView>
</SafeAreaView>
</Fragment>
);
}
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter
},
body: {
backgroundColor: Colors.white
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24
},
highlight: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
textAlign: 'center'
},
button: {
flex: 1,
margin: 10,
borderWidth: 1,
borderRadius: 30,
paddingVertical: 12,
alignItems: 'center',
justifyContent: 'center',
borderColor: Colors.black
}
});