| @@ -0,0 +1,24 @@ | ||
| 1 | +# -*- coding: utf-8 -*- | |
| 2 | + | |
| 3 | +from __future__ import division | |
| 4 | + | |
| 5 | +import json | |
| 6 | + | |
| 7 | +from django_logit import logit | |
| 8 | +from django_response import response | |
| 9 | + | |
| 10 | +from account.models import UserInfo | |
| 11 | +from utils.redis.connect import r | |
| 12 | +from utils.redis.rkeys import MEMBERCARD_USERINFO_LIST | |
| 13 | + | |
| 14 | + | |
| 15 | +@logit | |
| 16 | +def phone(request): | |
| 17 | + users = UserInfo.objects.filter(phone='').exclude(memberusercardcode='') | |
| 18 | + | |
| 19 | + p = r.pipeline() | |
| 20 | + for u in users: | |
| 21 | + p.rpush(MEMBERCARD_USERINFO_LIST, json.dumps(u.cardata)) | |
| 22 | + p.execute() | |
| 23 | + | |
| 24 | + return response() | 
| @@ -6,7 +6,7 @@ from django_file_upload import views as file_views | ||
| 6 | 6 | from account import tourguide_views | 
| 7 | 7 | from account import views as account_views | 
| 8 | 8 | from api import (admin_views, clerk_views, distributor_views, encrypt_views, mch_views, member_views, model_views, | 
| 9 | - operator_views, sr_views, staff_views, log_views) | |
| 9 | + operator_views, refresh_views, sr_views, staff_views, log_views) | |
| 10 | 10 | from box import views as box_views | 
| 11 | 11 | from geo import views as geo_views | 
| 12 | 12 | from group import (groupuser_views, lensman_views, tourguidegroup_views, tourguidegroupadmin_views, | 
| @@ -304,7 +304,10 @@ urlpatterns += [ | ||
| 304 | 304 | url(r'^admin/statistic/distributor$', admin_views.statistic_distributor, name='statistic_distributor'), | 
| 305 | 305 |  | 
| 306 | 306 | url(r'^admin/statistic/deep/analyze$', admin_views.statistic_deep_analyze, name='statistic_deep_analyze'), | 
| 307 | +] | |
| 307 | 308 |  | 
| 309 | +urlpatterns += [ | |
| 310 | + url(r'^refresh/phone$', refresh_views.phone, name='phone'), | |
| 308 | 311 | ] | 
| 309 | 312 |  | 
| 310 | 313 | urlpatterns += [ | 
| @@ -0,0 +1,76 @@ | ||
| 1 | +# -*- coding: utf-8 -*- | |
| 2 | + | |
| 3 | +import logging | |
| 4 | + | |
| 5 | +from django.conf import settings | |
| 6 | +from django.db import transaction | |
| 7 | +from django_six import CompatibilityBaseCommand, close_old_connections | |
| 8 | +from pywe_membercard import get_userinfo | |
| 9 | +from pywe_storage import RedisStorage | |
| 10 | + | |
| 11 | +from account.models import UserInfo | |
| 12 | +from utils.redis.connect import r | |
| 13 | +from utils.redis.rkeys import MEMBERCARD_USERINFO_LIST | |
| 14 | + | |
| 15 | + | |
| 16 | +WECHAT = settings.WECHAT | |
| 17 | + | |
| 18 | + | |
| 19 | +logger = logging.getLogger('console') | |
| 20 | + | |
| 21 | + | |
| 22 | +def get_phone(fields): | |
| 23 | + for field in fields: | |
| 24 | +        name = field.get('name', '') | |
| 25 | + if name == 'USER_FORM_INFO_FLAG_MOBILE': | |
| 26 | +            return field.get('value', '') | |
| 27 | + return '' | |
| 28 | + | |
| 29 | + | |
| 30 | +class Command(CompatibilityBaseCommand): | |
| 31 | + def handle(self, *args, **options): | |
| 32 | + | |
| 33 | +        logger.info('MemberCard userinfo is dealing') | |
| 34 | + | |
| 35 | + while True: | |
| 36 | +            # r.rpushjson('MEMBERCARD_USERINFO_LIST', { | |
| 37 | + # 'brand_id': 'brand_id', | |
| 38 | + # 'card_id': 'card_id', | |
| 39 | + # 'code': 'code', | |
| 40 | + # }) | |
| 41 | + k, v = r.blpopjson(MEMBERCARD_USERINFO_LIST, 60) | |
| 42 | + if not v: | |
| 43 | + continue | |
| 44 | + | |
| 45 | + logger.info(v) | |
| 46 | + | |
| 47 | +            brand_id, card_id, code = v.get('brand_id', ''), v.get('card_id', ''), v.get('code', '') | |
| 48 | + | |
| 49 | + if not (card_id and code): | |
| 50 | + continue | |
| 51 | + | |
| 52 | +            wxcfg = WECHAT.get('{}:JSAPI'.format(brand_id), {}) | |
| 53 | + | |
| 54 | +            appid = wxcfg.get('appID') | |
| 55 | +            secret = wxcfg.get('appsecret') | |
| 56 | + | |
| 57 | + userinfo = get_userinfo(card_id, code, appid=appid, secret=secret, storage=RedisStorage(r)) | |
| 58 | + | |
| 59 | + logger.info(userinfo) | |
| 60 | + | |
| 61 | +            userinfo = userinfo.get('user_info', {}) | |
| 62 | + if not userinfo: | |
| 63 | + continue | |
| 64 | + | |
| 65 | +            common_field_list = userinfo.get('common_field_list', []) | |
| 66 | + phone = get_phone(common_field_list) | |
| 67 | + | |
| 68 | + if not phone: | |
| 69 | + continue | |
| 70 | + | |
| 71 | + close_old_connections() | |
| 72 | + | |
| 73 | + with transaction.atomic(): | |
| 74 | + UserInfo.objects.select_for_update().filter(membercardid=card_id, memberusercardcode=code).update(phone=phone) | |
| 75 | + | |
| 76 | + close_old_connections() |