@@ -10,6 +10,7 @@ from pywe_miniapp import get_session_info, get_session_key, get_userinfo, store_ |
||
| 10 | 10 |
from pywe_storage import RedisStorage |
| 11 | 11 |
|
| 12 | 12 |
from account.models import UserInfo |
| 13 |
+from live.models import RoomInfo, RoomUserInfo |
|
| 13 | 14 |
from utils.redis.connect import r |
| 14 | 15 |
|
| 15 | 16 |
|
@@ -63,6 +64,66 @@ def mini_login_api(request): |
||
| 63 | 64 |
|
| 64 | 65 |
return response(data=user.data) |
| 65 | 66 |
|
| 67 |
+@logit(res=True) |
|
| 68 |
+@transaction.atomic |
|
| 69 |
+def mini_live_login_api(request): |
|
| 70 |
+ share_openid = request.POST.get('code', '')
|
|
| 71 |
+ room_id = request.POST.get('room_id', '')
|
|
| 72 |
+ |
|
| 73 |
+ wxcfg = WECHAT.get('MINIAPP', {})
|
|
| 74 |
+ |
|
| 75 |
+ appid = wxcfg.get('appID')
|
|
| 76 |
+ secret = wxcfg.get('appsecret')
|
|
| 77 |
+ |
|
| 78 |
+ code = request.POST.get('code', '')
|
|
| 79 |
+ |
|
| 80 |
+ # // 正常返回的JSON数据包 |
|
| 81 |
+ # {
|
|
| 82 |
+ # "openid": "OPENID", |
|
| 83 |
+ # "session_key": "SESSIONKEY", |
|
| 84 |
+ # } |
|
| 85 |
+ # |
|
| 86 |
+ # // 满足UnionID返回条件时,返回的JSON数据包 |
|
| 87 |
+ # {
|
|
| 88 |
+ # "openid": "OPENID", |
|
| 89 |
+ # "session_key": "SESSIONKEY", |
|
| 90 |
+ # "unionid": "UNIONID" |
|
| 91 |
+ # } |
|
| 92 |
+ # // 错误时返回JSON数据包(示例为Code无效) |
|
| 93 |
+ # {
|
|
| 94 |
+ # "errcode": 40029, |
|
| 95 |
+ # "errmsg": "invalid code" |
|
| 96 |
+ # } |
|
| 97 |
+ session_info = get_session_info(appid=appid, secret=secret, code=code) |
|
| 98 |
+ logger.debug(session_info) |
|
| 99 |
+ session_key = session_info.get('session_key', '')
|
|
| 100 |
+ unionid = session_info.get('unionid', '')
|
|
| 101 |
+ openid = session_info.get('openid', '')
|
|
| 102 |
+ |
|
| 103 |
+ # Get or Create User |
|
| 104 |
+ user, _ = UserInfo.objects.select_for_update().get_or_create(openid=openid) |
|
| 105 |
+ |
|
| 106 |
+ # Set User Key's Value |
|
| 107 |
+ if unionid: |
|
| 108 |
+ user.unionid = unionid |
|
| 109 |
+ user.save() |
|
| 110 |
+ |
|
| 111 |
+ # Store SessionKey |
|
| 112 |
+ store_session_key(appid=appid, secret=secret, session_key=session_key, unid=user.user_id, storage=RedisStorage(r)) |
|
| 113 |
+ |
|
| 114 |
+ |
|
| 115 |
+ # Update live |
|
| 116 |
+ room = RoomInfo.objects.get(room_id=room_id) |
|
| 117 |
+ RoomUserInfo.create( |
|
| 118 |
+ room_id=room_id, |
|
| 119 |
+ share_openid=share_openid, |
|
| 120 |
+ user_id=user.user_id, |
|
| 121 |
+ openid=user.openid, |
|
| 122 |
+ anchor_id=room.anchor_id, |
|
| 123 |
+ ) |
|
| 124 |
+ |
|
| 125 |
+ return response(data=user.data) |
|
| 126 |
+ |
|
| 66 | 127 |
|
| 67 | 128 |
@logit |
| 68 | 129 |
@transaction.atomic |
@@ -158,6 +158,20 @@ class RoomGoodsInfo(BaseModelMixin): |
||
| 158 | 158 |
def __unicode__(self): |
| 159 | 159 |
return self.pk |
| 160 | 160 |
|
| 161 |
+class RoomUserInfo(BaseModelMixin): |
|
| 162 |
+ room_id = models.CharField(_('room_id'), max_length=32, help_text='房间唯一标识', db_index=True)
|
|
| 163 |
+ anchor_id = models.CharField(_('anchor_id'), max_length=32, blank=True, help_text='主播唯一标识')
|
|
| 164 |
+ user_id = models.CharField(_('anchor_id'), max_length=32, blank=True, help_text='用户唯一标识')
|
|
| 165 |
+ openid = models.CharField(_('openid'), max_length=32, blank=True, help_text='用户 openid')
|
|
| 166 |
+ share_openid = models.CharField(_('share_openid'), max_length=32, blank=True, help_text='分享用户 openid')
|
|
| 167 |
+ |
|
| 168 |
+ class Meta: |
|
| 169 |
+ verbose_name = _('直播间用户信息')
|
|
| 170 |
+ verbose_name_plural = _('直播间用户信息')
|
|
| 171 |
+ |
|
| 172 |
+ def __unicode__(self): |
|
| 173 |
+ return self.pk |
|
| 174 |
+ |
|
| 161 | 175 |
class RoomOrderInfo(BaseModelMixin): |
| 162 | 176 |
""" |
| 163 | 177 |
# Trade State of Wechat Query |