Miniapp login & getUserInfo

Brightcells vor 6 Jahren
Ursprung
Commit
cef6755ce5
4 geänderte Dateien mit 133 neuen Zeilen und 4 gelöschten Zeilen
  1. 5 1
      account/models.py
  2. 2 0
      api/urls.py
  3. 125 2
      miniapp/views.py
  4. 1 1
      requirements_pywe.txt

+ 5 - 1
account/models.py

@@ -374,6 +374,10 @@ class UserInfo(BaseModelMixin, LensmanTypeBoolMixin):
374 374
         }
375 375
 
376 376
     def brandata(self, brand_id=None):
377
+        if self.unionid:
378
+            saleclerk = SaleclerkInfo.objects.filter(brand_id=brand_id, unionid=self.unionid, status=True).exists()
379
+        else:
380
+            saleclerk = False
377 381
         return {
378 382
             'user_id': self.user_id,
379 383
             'name': self.name,
@@ -383,7 +387,7 @@ class UserInfo(BaseModelMixin, LensmanTypeBoolMixin):
383 387
             'phone': self.phone,
384 388
             # TODO: Diff for Brands
385 389
             'has_membercard': self.has_membercard,
386
-            'saleclerk': SaleclerkInfo.objects.filter(brand_id=brand_id, unionid=self.unionid, status=True).exists(),
390
+            'saleclerk': saleclerk,
387 391
         }
388 392
 
389 393
 

+ 2 - 0
api/urls.py

@@ -180,6 +180,8 @@ urlpatterns += [
180 180
 # Mini App
181 181
 urlpatterns += [
182 182
     url(r'^mini/userinfo$', mini_views.get_userinfo_api, name='get_userinfo_api'),  # 获取用户信息
183
+    url(r'^mini/login$', mini_views.mini_login_api, name='mini_login_api'),  # 小程序登录
184
+    url(r'^mini/userinfo2$', mini_views.get_userinfo_api2, name='get_userinfo_api2'),  # 获取用户信息
183 185
 ]
184 186
 
185 187
 urlpatterns += [

+ 125 - 2
miniapp/views.py

@@ -7,13 +7,13 @@ from django_logit import logit
7 7
 from django_response import response
8 8
 from ipaddr import client_ip
9 9
 from pywe_membercard import get_miniapp_extraData
10
-from pywe_miniapp import get_session_key, get_userinfo, store_session_key
10
+from pywe_miniapp import get_session_info, get_session_key, get_userinfo, store_session_key
11 11
 from pywe_storage import RedisStorage
12 12
 from TimeConvert import TimeConvert as tc
13 13
 
14 14
 from account.models import UserInfo
15 15
 from statistic.models import RegisterStatisticInfo
16
-from utils.error.errno_utils import ProductBrandStatusCode
16
+from utils.error.errno_utils import ProductBrandStatusCode, UserStatusCode
17 17
 from utils.redis.connect import r
18 18
 from utils.redis.rprofile import set_profile_info
19 19
 
@@ -83,6 +83,75 @@ def get_userinfo_api(request):
83 83
 
84 84
     # Store Userinfo
85 85
     set_profile_info(user)
86
+
87
+    # Store SessionKey
88
+    store_session_key(appid=appid, secret=secret, session_key=session_key, unid=user.user_id, storage=RedisStorage(r))
89
+
90
+    return response(200, 'Mini App Login Success', u'微信小程序登录成功', user.brandata(brand_id=brand_id))
91
+
92
+
93
+@logit
94
+@transaction.atomic
95
+def mini_login_api(request):
96
+    brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
97
+
98
+    if brand_id != settings.KODO_DEFAULT_BRAND_ID:
99
+        return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
100
+
101
+    wxcfg = WECHAT.get('MINIAPP', {})
102
+
103
+    appid = wxcfg.get('appID')
104
+    secret = wxcfg.get('appsecret')
105
+
106
+    code = request.POST.get('code', '')
107
+
108
+    # // 正常返回的JSON数据包
109
+    # {
110
+    #     "openid": "OPENID",
111
+    #     "session_key": "SESSIONKEY",
112
+    # }
113
+    #
114
+    # // 满足UnionID返回条件时,返回的JSON数据包
115
+    # {
116
+    #     "openid": "OPENID",
117
+    #     "session_key": "SESSIONKEY",
118
+    #     "unionid": "UNIONID"
119
+    # }
120
+    # // 错误时返回JSON数据包(示例为Code无效)
121
+    # {
122
+    #     "errcode": 40029,
123
+    #     "errmsg": "invalid code"
124
+    # }
125
+    session_info = get_session_info(appid=appid, secret=secret, code=code)
126
+    session_key = session_info.get('session_key', '')
127
+    unionid = session_info.get('unionid', '')
128
+    openid = session_info.get('openid', '')
129
+
130
+    # Get or Create User
131
+    user, created = UserInfo.objects.select_for_update().get_or_create(openid_miniapp=openid)
132
+
133
+    # Set User_id
134
+    if created:
135
+        user.user_id = CurtailUUID.uuid(UserInfo, 'user_id')
136
+        # 注册用户统计
137
+        rsi, _ = RegisterStatisticInfo.objects.select_for_update().get_or_create(
138
+            brand_id=brand_id,
139
+            ymd=int(tc.local_string(format='%Y%m%d')),
140
+        )
141
+        rsi.num += 1
142
+        rsi.save()
143
+
144
+    # Set User Key's Value
145
+    user.user_from = UserInfo.MINIAPP_USER
146
+    user.unionid = unionid
147
+    user.user_status = UserInfo.ACTIVATED
148
+    user.signup_ip = client_ip(request)
149
+    user.signup_at = tc.utc_datetime()
150
+    user.save()
151
+
152
+    # Store Userinfo
153
+    set_profile_info(user)
154
+
86 155
     # Store SessionKey
87 156
     store_session_key(appid=appid, secret=secret, session_key=session_key, unid=user.user_id, storage=RedisStorage(r))
88 157
 
@@ -91,6 +160,60 @@ def get_userinfo_api(request):
91 160
 
92 161
 @logit
93 162
 @transaction.atomic
163
+def get_userinfo_api2(request):
164
+    brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
165
+    user_id = request.POST.get('user_id', '')
166
+
167
+    if brand_id != settings.KODO_DEFAULT_BRAND_ID:
168
+        return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
169
+
170
+    wxcfg = WECHAT.get('MINIAPP', {})
171
+
172
+    appid = wxcfg.get('appID')
173
+    secret = wxcfg.get('appsecret')
174
+
175
+    code = request.POST.get('code', '')
176
+    encryptedData = request.POST.get('encryptedData', '')
177
+    iv = request.POST.get('iv', '')
178
+
179
+    try:
180
+        user = UserInfo.objects.get(user_id=user_id, status=True)
181
+    except UserInfo.DoesNotExist:
182
+        return response(UserStatusCode.USER_NOT_FOUND)
183
+
184
+    # {u'avatarUrl': u'http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0',
185
+    #  u'city': u'Guangzhou',
186
+    #  u'country': u'CN',
187
+    #  u'gender': 1,
188
+    #  u'language': u'zh_CN',
189
+    #  u'nickName': u'Band',
190
+    #  u'openId': u'oGZUI0egBJY1zhBYw2KhdUfwVJJE',
191
+    #  u'province': u'Guangdong',
192
+    #  u'unionId': u'ocMvos6NjeKLIBqg5Mr9QjxrP1FA',
193
+    #  u'watermark': {u'appid': u'wx4f4bc4dec97d474b', u'timestamp': 1477314187}}
194
+    session_key = get_session_key(appid=appid, secret=secret, code=code, unid=user_id, storage=RedisStorage(r))
195
+    # Get Userinfo
196
+    userinfo = get_userinfo(appid=appid, secret=secret, code=code, session_key=session_key, encryptedData=encryptedData, iv=iv)
197
+
198
+    # Set User Key's Value
199
+    user.unionid = userinfo.get('unionId', '')
200
+    user.openid_miniapp = userinfo.get('openId', '')
201
+    user.sex = userinfo.get('gender', '')
202
+    user.nickname = userinfo.get('nickName', '')
203
+    user.avatar = userinfo.get('avatarUrl', '')
204
+    user.country = userinfo.get('country', '')
205
+    user.province = userinfo.get('province', '')
206
+    user.city = userinfo.get('city', '')
207
+    user.save()
208
+
209
+    # Store Userinfo
210
+    set_profile_info(user)
211
+
212
+    return response(200, 'Mini App Get Userinfo Success', u'微信小程序获取用户信息成功', user.brandata(brand_id=brand_id))
213
+
214
+
215
+@logit
216
+@transaction.atomic
94 217
 def membercard_extradata(request):
95 218
     wxcfg = WECHAT.get('JSAPI', {})
96 219
 

+ 1 - 1
requirements_pywe.txt

@@ -1,6 +1,6 @@
1 1
 pywe-jssdk==1.1.0
2 2
 pywe-membercard==1.0.0
3
-pywe-miniapp==1.1.3
3
+pywe-miniapp==1.1.4
4 4
 pywe-oauth==1.0.6
5 5
 pywe-pay==1.0.12
6 6
 pywe-pay-notify==1.0.4