/
test_wechatpy.py
234 lines (215 loc) · 7.44 KB
/
test_wechatpy.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify , render_template
from wechatpy import WeChatClient , parse_message
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException
from wechatpy.replies import TextReply , ImageReply
from toolkit import add_watermark
from db_ops import *
import urllib.request
import urllib.parse
token = 'xjj'
appId = 'wx0bf5a881e5a22886'
appSecret = 'a301e26f45585c26823d24be7a4b2fda'
application = Flask(__name__)
client = WeChatClient( appId , appSecret )
mini_app_id = 'wxc41ab2f0498e2894'
mini_app_secret = '3d0d8ef0085a0d05202536bacc70dd87'
def get_openid( jscode ) :
url = 'https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code'
return json.loads(
urllib.request.urlopen(
url.replace( 'APPID' , mini_app_id ).replace( 'SECRET' , mini_app_secret ).replace( 'JSCODE' , jscode )
).read( ).decode( 'utf-8' )
)['openid']
def get_access_token( ) :
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET'
return json.loads(
urllib.request.urlopen(
url.replace( 'APPID' , mini_app_id ).replace( 'APPSECRET' , mini_app_secret )
).read( ).decode( 'utf-8' )
)['access_token']
@application.route('/weixin' , methods=['GET','POST'])
def weixin( ) :
import hashlib , lxml , time , os , json
from lxml import etree
if request.method == 'GET' :
signature = request.values.get( 'signature' )
timestamp = request.values.get( 'timestamp' )
nonce = request.values.get( 'nonce' )
echostr = request.values.get( 'echostr' )
global token
try :
check_signature(
token ,
signature ,
timestamp ,
nonce
)
return echostr
except InvalidSignatureException :
return echostr + 'failed'
elif request.method == 'POST' :
str_xml = request.data
msg = parse_message( str_xml )
print( str_xml )
if msg.type == 'text' :
return TextReply(
content = 'u just say : ' + msg.content ,
message = msg
).render( )
elif msg.type == 'image' :
return ImageReply(
media_id = add_watermark(
msg.media_id ,
appId ,
appSecret
).get_media_id_with_watermark( ) ,
message = msg
).render( )
return 'wassup'
@application.route('/')
def hello( ) :
return render_template( 'test_pengyouquan.html' )
return 'wassup in index'
@application.route('/mini/test' , methods=['POST'])
def mini_test( ) :
#
# if openid exists, return user info
# if openid not exists , go to login
#
data = {
'result' : '' ,
'reason' : '' ,
}
if request.method == 'POST' :
url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN'
url = url.replace( 'ACCESS_TOKEN' , get_access_token( ) )
print( request.data )
request_to_send = urllib.request.Request(
url ,
urllib.parse.urlencode(
{
"touser": "oZqgd0RuyGvRb5zASsiw21SY8C9E",
"template_id": "M3zLARKl-kk6UDqqUq1rzewTApIAeb5MJAn58gZSuas",
"page": request.get_json( )['page'],
"form_id": request.get_json( )['form_id'],
"data": request.get_json( )['value']
}
).encode()
)
print( '-----------------------------------------' )
print( urllib.request.urlopen( request_to_send ).read( ).decode( ) )
print( '-----------------------------------------' )
return 'function mini_index return \'wassup\''
@application.route('/mini/location' , methods=['POST'])
def mini_location( ) :
if request.method == 'POST' :
print( request.data )
return 'function mini_location return \'wassup\''
@application.route('/mini/students_info' , methods=['POST'])
def mini_students_info( ) :
#
# if openid exists, return user info
# if openid not exists , go to login
#
data = {
'result' : '' ,
'reason' : '' ,
'data' : ''
}
if request.method == 'POST' :
openid = get_openid( request.get_json( )['jscode'] )
if db_check_openid( openid ) == False :
data['result'] = 'failed'
data['reason'] = 'openid not exists'
else :
data['result'] = 'success'
data['data'] = db_get_students_info_by_teacher_openid( openid )
return jsonify( data )
return 'function mini_students_info return \'wassup\''
@application.route('/mini/teacher' , methods=['POST'])
def mini_teacher( ) :
#
# if openid exists, return user info
# if openid not exists , go to login
#
data = {
'result' : '' ,
'reason' : '' ,
'data' : ''
}
if request.method == 'POST' :
openid = get_openid( request.get_json( )['jscode'] )
if db_check_openid( openid ) == False :
data['result'] = 'failed'
data['reason'] = 'openid not exists'
else :
data['result'] = 'success'
data['data'] = db_get_teacher_info( openid )
return jsonify( data )
return 'function mini_teacher return \'wassup\''
@application.route('/mini/index' , methods=['POST'])
def mini_index( ) :
#
# if openid exists, return user info
# if openid not exists , go to login
#
data = {
'result' : '' ,
'reason' : '' ,
'data' : ''
}
if request.method == 'POST' :
openid = get_openid( request.get_json( )['jscode'] )
if db_check_openid( openid ) == False :
data['result'] = 'failed'
data['reason'] = 'openid not exists'
else :
data['result'] = 'success'
data['data'] = db_get_wechat_info( openid )
return jsonify( data )
return 'function mini_index return \'wassup\''
@application.route( '/mini/login' , methods=['POST'] )
def mini_login( ) :
#
# username
# passwd
# wechat.openid
#
# if( check_user( username , passwd ) ) :
# add( username , passwd , wechatid )
# return True
# else :
# return False
data = {
'result' : '' , # 'success' 'failed'
'reason' : '' , # 'password_error' 'unkonw_user'
}
if request.method == 'POST' :
print( request.data )
rc = db_check_user_and_add_openid(
request.get_json( )['username'] ,
request.get_json( )['password'] ,
get_openid( request.get_json( )['jscode'] )
)
print( 'rc = %s' % rc )
if rc == 'success' :
data['result'] = 'success'
data['reason'] = 'success'
elif rc == 'password_error' :
data['result'] = 'failed'
data['reason'] = 'password_error'
elif rc == 'unknow_user' :
data['result'] = 'failed'
data['reason'] = 'unknow_user'
return jsonify( data )
return 'function mini_login returns'
if __name__ == '__main__':
exit( 0 )
application.run(
host = "10.135.22.157" ,
# host = '127.0.0.1' ,
port = 443 ,
ssl_context = 'adhoc'
)