> 24
+
25
+    appid = wxcfg.get('appID')
26
+    secret = wxcfg.get('appsecret')
27
+
28
+    code = request.POST.get('code', '')
29
+
30
+    # // 正常返回的JSON数据包
31
+    # {
32
+    #     "openid": "OPENID",
33
+    #     "session_key": "SESSIONKEY",
34
+    # }
35
+    #
36
+    # // 满足UnionID返回条件时,返回的JSON数据包
37
+    # {
38
+    #     "openid": "OPENID",
39
+    #     "session_key": "SESSIONKEY",
40
+    #     "unionid": "UNIONID"
41
+    # }
42
+    # // 错误时返回JSON数据包(示例为Code无效)
43
+    # {
44
+    #     "errcode": 40029,
45
+    #     "errmsg": "invalid code"
46
+    # }
47
+    session_info = get_session_info(appid=appid, secret=secret, code=code)
48
+    logger.debug(session_info)
49
+    session_key = session_info.get('session_key', '')
50
+    unionid = session_info.get('unionid', '')
51
+    openid = session_info.get('openid', '')
52
+
53
+    # Get or Create User
54
+    user, _ = UserInfo.objects.select_for_update().get_or_create(openid=openid)
55
+
56
+    # Set User Key's Value
57
+    if unionid:
58
+        user.unionid = unionid
59
+    user.save()
60
+
61
+    # Store SessionKey
62
+    store_session_key(appid=appid, secret=secret, session_key=session_key, unid=user.user_id, storage=RedisStorage(r))
63
+
64
+    return response(data=user.data)
65
+
66
+
67
+@logit
68
+@transaction.atomic
69
+def get_userinfo_api(request):
70
+    wxcfg = WECHAT.get('MINIAPP', {})
71
+
72
+    appid = wxcfg.get('appID')
73
+    secret = wxcfg.get('appsecret')
74
+
75
+    user_id = request.POST.get('user_id', '')
76
+    encryptedData = request.POST.get('encryptedData', '')
77
+    iv = request.POST.get('iv', '')
78
+
79
+    try:
80
+        user = UserInfo.objects.get(user_id=user_id, status=True)
81
+    except UserInfo.DoesNotExist:
82
+        return response()
83
+
84
+    # {u'avatarUrl': u'http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0',
85
+    #  u'city': u'Guangzhou',
86
+    #  u'country': u'CN',
87
+    #  u'gender': 1,
88
+    #  u'language': u'zh_CN',
89
+    #  u'nickName': u'Band',
90
+    #  u'openId': u'oGZUI0egBJY1zhBYw2KhdUfwVJJE',
91
+    #  u'province': u'Guangdong',
92
+    #  u'unionId': u'ocMvos6NjeKLIBqg5Mr9QjxrP1FA',
93
+    #  u'watermark': {u'appid': u'wx4f4bc4dec97d474b', u'timestamp': 1477314187}}
94
+    session_key = get_session_key(appid=appid, secret=secret, unid=user_id, storage=RedisStorage(r))
95
+    # Get Userinfo
96
+    userinfo = get_userinfo(appid=appid, secret=secret, session_key=session_key, encryptedData=encryptedData, iv=iv)
97
+
98
+    # Set User Key's Value
99
+    user.unionid = userinfo.get('unionId', '')
100
+    user.openid = userinfo.get('openId', '')
101
+    user.nickname = userinfo.get('nickName', '')
102
+    user.sex = userinfo.get('gender', '')
103
+    user.avatar = userinfo.get('avatarUrl', '')
104
+    user.country = userinfo.get('country', '')
105
+    user.province = userinfo.get('province', '')
106
+    user.city = userinfo.get('city', '')
107
+    user.save()
108
+
109
+    return response(data=user.data)

+ 3 - 1
api/urls.py

@@ -2,10 +2,12 @@
2 2
 
3 3
 from django.urls import re_path
4 4
 
5
-from api import oauth_views
5
+from api import mini_views, oauth_views
6 6
 
7 7
 
8 8
 urlpatterns = [
9
+    re_path(r'^mini/login$', mini_views.mini_login_api, name='mini_login_api'),  # 小程序登录
10
+    re_path(r'^mini/userinfo$', mini_views.get_userinfo_api, name='get_userinfo_api'),  # 获取用户信息
9 11
 ]
10 12
 
11 13
 urlpatterns += [

+ 1 - 0
tiwen/settings.py

@@ -56,6 +56,7 @@ INSTALLED_APPS = [
56 56
     'django_we',
57 57
     'commands',
58 58
     'api',
59
+    'account',
59 60
 ]
60 61
 
61 62
 MIDDLEWARE = [

kodo - Gogs: Go Git Service

Nessuna descrizione

Brightcells: 05e046c8ca fix bug: errcode=48001 when scope=snsapi_base 10 anni fa
..
migrations 8f43551640 add api wx_jsapi_signature_api 10 anni fa
__init__.py 8f43551640 add api wx_jsapi_signature_api 10 anni fa
admin.py 8f43551640 add api wx_jsapi_signature_api 10 anni fa
models.py 8f43551640 add api wx_jsapi_signature_api 10 anni fa
tests.py 8f43551640 add api wx_jsapi_signature_api 10 anni fa
views.py 05e046c8ca fix bug: errcode=48001 when scope=snsapi_base 10 anni fa