|
8
|
+from django_logit import logit
|
|
|
9
|
+from django_response import response
|
|
|
10
|
+from ipaddr import client_ip
|
|
|
11
|
+from pyqywe_miniapp import get_userid
|
|
|
12
|
+from pywe_storage import RedisStorage
|
|
|
13
|
+from TimeConvert import TimeConvert as tc
|
|
|
14
|
+
|
|
|
15
|
+from account.models import UserInfo
|
|
|
16
|
+from statistic.models import RegisterStatisticInfo
|
|
|
17
|
+from utils.error.errno_utils import ProductBrandStatusCode
|
|
|
18
|
+from utils.redis.connect import r
|
|
|
19
|
+from utils.redis.rprofile import set_profile_info
|
|
|
20
|
+
|
|
|
21
|
+
|
|
|
22
|
+WECHAT = settings.WECHAT
|
|
|
23
|
+logger = logging.getLogger('logit')
|
|
|
24
|
+
|
|
|
25
|
+
|
|
|
26
|
+@logit(res=True)
|
|
|
27
|
+@transaction.atomic
|
|
|
28
|
+def qy_login_api(request):
|
|
|
29
|
+ brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
|
|
|
30
|
+ appId = request.POST.get('appId', 'QYMINIAPP')
|
|
|
31
|
+
|
|
|
32
|
+ if brand_id != settings.KODO_DEFAULT_BRAND_ID:
|
|
|
33
|
+ return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
|
|
|
34
|
+
|
|
|
35
|
+ wxcfg = WECHAT.get(appId, {})
|
|
|
36
|
+
|
|
|
37
|
+ appid = wxcfg.get('appID')
|
|
|
38
|
+ secret = wxcfg.get('appsecret')
|
|
|
39
|
+
|
|
|
40
|
+ code = request.POST.get('code', '')
|
|
|
41
|
+
|
|
|
42
|
+ userid = get_userid(appid=appid, secret=secret, code=code, storage=RedisStorage(r))
|
|
|
43
|
+
|
|
|
44
|
+ # Get or Create User
|
|
|
45
|
+ user, created = UserInfo.objects.select_for_update().get_or_create(appid=appid, userid=userid)
|
|
|
46
|
+
|
|
|
47
|
+ # Set User_id
|
|
|
48
|
+ if created:
|
|
|
49
|
+ user.user_id = CurtailUUID.uuid(UserInfo, 'user_id')
|
|
|
50
|
+ # 注册用户统计
|
|
|
51
|
+ rsi, _ = RegisterStatisticInfo.objects.select_for_update().get_or_create(
|
|
|
52
|
+ brand_id=brand_id,
|
|
|
53
|
+ ymd=int(tc.local_string(format='%Y%m%d')),
|
|
|
54
|
+ )
|
|
|
55
|
+ rsi.num += 1
|
|
|
56
|
+ rsi.save()
|
|
|
57
|
+
|
|
|
58
|
+ # Set User Key's Value
|
|
|
59
|
+ user.user_from = UserInfo.QYMINIAPP_USER
|
|
|
60
|
+ user.appid = appid
|
|
|
61
|
+ user.user_status = UserInfo.ACTIVATED
|
|
|
62
|
+ user.signup_ip = client_ip(request)
|
|
|
63
|
+ user.signup_at = tc.utc_datetime()
|
|
|
64
|
+ user.is_maintenance = userid in settings.QY_MAINTENANCE_USERID
|
|
|
65
|
+ user.save()
|
|
|
66
|
+
|
|
|
67
|
+ # Store Userinfo
|
|
|
68
|
+ set_profile_info(user)
|
|
|
69
|
+
|
|
|
70
|
+ return response(200, 'Mini App Login Success', u'微信小程序登录成功', user.brandata(brand_id=brand_id))
|