el="diff-97a78423441d3f41fc33ade043e0e207690f2caeR88">88
+
|
|
|
89
|
+ # // 正常返回的JSON数据包
|
|
|
90
|
+ # {
|
|
|
91
|
+ # "openid": "OPENID",
|
|
|
92
|
+ # "session_key": "SESSIONKEY",
|
|
|
93
|
+ # }
|
|
|
94
|
+ #
|
|
|
95
|
+ # // 满足UnionID返回条件时,返回的JSON数据包
|
|
|
96
|
+ # {
|
|
|
97
|
+ # "openid": "OPENID",
|
|
|
98
|
+ # "session_key": "SESSIONKEY",
|
|
|
99
|
+ # "unionid": "UNIONID"
|
|
|
100
|
+ # }
|
|
|
101
|
+ # // 错误时返回JSON数据包(示例为Code无效)
|
|
|
102
|
+ # {
|
|
|
103
|
+ # "errcode": 40029,
|
|
|
104
|
+ # "errmsg": "invalid code"
|
|
|
105
|
+ # }
|
|
|
106
|
+ session_info = get_session_info(appid=appid, secret=secret, code=code)
|
|
|
107
|
+ logger.debug(session_info)
|
|
|
108
|
+ session_key = session_info.get('session_key', '')
|
|
|
109
|
+ unionid = session_info.get('unionid', '')
|
|
|
110
|
+ openid = session_info.get('openid', '')
|
|
|
111
|
+
|
|
|
112
|
+ # Get or Create User
|
|
|
113
|
+ user, created = UserInfo.objects.select_for_update().get_or_create(openid_miniapp=openid)
|
|
|
114
|
+
|
|
|
115
|
+ # Set User Key's Value
|
|
|
116
|
+ if unionid:
|
|
|
117
|
+ user.unionid = unionid
|
|
|
118
|
+
|
|
|
119
|
+ user.user_status = UserInfo.ACTIVATED
|
|
|
120
|
+ user.save()
|
|
|
121
|
+
|
|
|
122
|
+ # Store SessionKey
|
|
|
123
|
+ store_session_key(appid=appid, secret=secret, session_key=session_key, unid=user.user_id, storage=RedisStorage(r))
|
|
|
124
|
+ # Just for compatible because of store session_key has changed
|
|
|
125
|
+ store_session_key(appid=appid, secret=secret, session_key=session_key, unid='', storage=RedisStorage(r))
|
|
|
126
|
+
|
|
|
127
|
+ return response(200, 'Mini App Login Success', u'微信小程序登录成功', user.brandata(brand_id=brand_id))
|
|
|
128
|
+
|
|
|
129
|
+
|
|
|
130
|
+@logit
|
|
|
131
|
+@transaction.atomic
|
|
|
132
|
+def get_userinfo_api2(request):
|
|
|
133
|
+ brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
|
|
|
134
|
+ user_id = request.POST.get('user_id', '')
|
|
|
135
|
+ appId = request.POST.get('appId', 'MINIAPP')
|
|
|
136
|
+
|
|
|
137
|
+ wxcfg = WECHAT.get(appId, {})
|
|
|
138
|
+
|
|
|
139
|
+ appid = wxcfg.get('appID')
|
|
|
140
|
+ secret = wxcfg.get('appsecret')
|
|
|
141
|
+
|
|
|
142
|
+ encryptedData = request.POST.get('encryptedData', '')
|
|
|
143
|
+ iv = request.POST.get('iv', '')
|
|
|
144
|
+
|
|
|
145
|
+ try:
|
|
|
146
|
+ user = UserInfo.objects.select_for_update().get(user_id=user_id, status=True)
|
|
|
147
|
+ except UserInfo.DoesNotExist:
|
|
|
148
|
+ return response()
|
|
|
149
|
+
|
|
|
150
|
+ # {u'avatarUrl': u'http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0',
|
|
|
151
|
+ # u'city': u'Guangzhou',
|
|
|
152
|
+ # u'country': u'CN',
|
|
|
153
|
+ # u'gender': 1,
|
|
|
154
|
+ # u'language': u'zh_CN',
|
|
|
155
|
+ # u'nickName': u'Band',
|
|
|
156
|
+ # u'openId': u'oGZUI0egBJY1zhBYw2KhdUfwVJJE',
|
|
|
157
|
+ # u'province': u'Guangdong',
|
|
|
158
|
+ # u'unionId': u'ocMvos6NjeKLIBqg5Mr9QjxrP1FA',
|
|
|
159
|
+ # u'watermark': {u'appid': u'wx4f4bc4dec97d474b', u'timestamp': 1477314187}}
|
|
|
160
|
+ session_key = get_session_key(appid=appid, secret=secret, unid=user_id, storage=RedisStorage(r))
|
|
|
161
|
+ # Get Userinfo
|
|
|
162
|
+ userinfo = get_userinfo(appid=appid, secret=secret, session_key=session_key, encryptedData=encryptedData, iv=iv)
|
|
|
163
|
+
|
|
|
164
|
+ # Set User Key's Value
|
|
|
165
|
+ user.unionid = userinfo.get('unionId', '')
|
|
|
166
|
+ user.openid_miniapp = userinfo.get('openId', '')
|
|
|
167
|
+ user.sex = userinfo.get('gender', '')
|
|
|
168
|
+ user.nickname = userinfo.get('nickName', '')
|
|
|
169
|
+ user.avatar = userinfo.get('avatarUrl', '')
|
|
|
170
|
+ user.country = userinfo.get('country', '')
|
|
|
171
|
+ user.province = userinfo.get('province', '')
|
|
|
172
|
+ user.city = userinfo.get('city', '')
|
|
|
173
|
+ user.save()
|
|
|
174
|
+
|
|
|
175
|
+ return response(200, 'Mini App Get Userinfo Success', u'微信小程序获取用户信息成功', user.brandata(brand_id=brand_id))
|
|
|
|
@@ -2,7 +2,8 @@
|
|
2
|
2
|
|
|
3
|
3
|
from django.conf.urls import url
|
|
4
|
4
|
|
|
5
|
|
-from api import branch_campus_admin_views, branch_campus_views, course_field_views, course_views, oauth_views, wx_views
|
|
|
5
|
+from api import (branch_campus_admin_views, branch_campus_views, course_field_views, course_views, mini_views,
|
|
|
6
|
+ oauth_views, wx_views)
|
|
6
|
7
|
|
|
7
|
8
|
|
|
8
|
9
|
urlpatterns = [
|
|
|
|
@@ -42,3 +43,10 @@ urlpatterns += [
|
|
42
|
43
|
url(r'^course/field/list$', course_field_views.get_course_field_list, name='get_course_field_list'),
|
|
43
|
44
|
url(r'^course/field/delete$', course_field_views.delete_course_field, name='delete_course_field'),
|
|
44
|
45
|
]
|
|
|
46
|
+
|
|
|
47
|
+# Mini App
|
|
|
48
|
+urlpatterns = [
|
|
|
49
|
+ url(r'^mini/userinfo$', mini_views.get_userinfo_api, name='get_userinfo_api'), # 获取用户信息
|
|
|
50
|
+ url(r'^mini/login$', mini_views.mini_login_api, name='mini_login_api'), # 小程序登录
|
|
|
51
|
+ url(r'^mini/userinfo2$', mini_views.get_userinfo_api2, name='get_userinfo_api2'), # 获取用户信息
|
|
|
52
|
+]
|
|
|
|
@@ -56,6 +56,7 @@ INSTALLED_APPS = [
|
|
56
|
56
|
'django_we',
|
|
57
|
57
|
'commands',
|
|
58
|
58
|
'api',
|
|
|
59
|
+ 'account',
|
|
59
|
60
|
'registration',
|
|
60
|
61
|
]
|
|
61
|
62
|
|