|
@@ -1,11 +1,16 @@
|
|
# Create your views here.
|
|
# Create your views here.
|
|
|
|
+import hashlib
|
|
|
|
+import time
|
|
|
|
+import uuid
|
|
|
|
+
|
|
|
|
+import requests
|
|
from rest_framework.views import APIView
|
|
from rest_framework.views import APIView
|
|
from rest_framework.viewsets import ModelViewSet
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
|
|
-from background.Object import AmazonS3Util
|
|
|
|
|
|
+from background.Object import AmazonS3Util, RedisObject, TokenObject
|
|
from background.serializers import ProductInfoSerializer, VideoInSerializer, QuickStartInfoSerializer, \
|
|
from background.serializers import ProductInfoSerializer, VideoInSerializer, QuickStartInfoSerializer, \
|
|
UpgradeFirmwareInfoSerializer
|
|
UpgradeFirmwareInfoSerializer
|
|
-from background.models import ProductInfo, VideoInfo, QuickStartInfo, UpgradeFirmwareInfo
|
|
|
|
|
|
+from background.models import ProductInfo, VideoInfo, QuickStartInfo, UpgradeFirmwareInfo, WechatUserInfo
|
|
from rest_framework.response import Response
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
|
|
@@ -54,3 +59,142 @@ class GetUploadUrlView(APIView):
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return Response(
|
|
return Response(
|
|
{'code': 500, 'result': {'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))}})
|
|
{'code': 500, 'result': {'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))}})
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class WechatLoginView(APIView):
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ return self.validation(request.GET, operation, request)
|
|
|
|
+
|
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ return self.validation(request.POST, operation, request)
|
|
|
|
+
|
|
|
|
+ def validation(self, request_dict, operation, request):
|
|
|
|
+ if operation == 'get-state': # 获取state值
|
|
|
|
+ return self.get_state()
|
|
|
|
+ elif operation == 'get-user': # 获取用户
|
|
|
|
+ return self.get_user(request_dict)
|
|
|
|
+ else:
|
|
|
|
+ return Response({'code': 414, 'result': {'error_msg': '请求路径有误'}})
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def get_state():
|
|
|
|
+ """
|
|
|
|
+ 获取state
|
|
|
|
+ @return: response
|
|
|
|
+ """
|
|
|
|
+ nwo_time = time.time()
|
|
|
|
+ redis_obj = RedisObject()
|
|
|
|
+ try:
|
|
|
|
+ state = hashlib.md5((str(uuid.uuid1()) + str(nwo_time)).encode('utf-8')).hexdigest()
|
|
|
|
+ state_statue = redis_obj.set_ex_data(state, 0, 300) # redis记录state
|
|
|
|
+ if state_statue:
|
|
|
|
+ return Response({'code': 0, 'result': {'state': state}})
|
|
|
|
+ else:
|
|
|
|
+ return Response({'code': 120, 'result': {'error_msg': '获取status失败'}})
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return Response(
|
|
|
|
+ {'code': 500, 'result': {'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))}})
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def get_user(cls, request_dict):
|
|
|
|
+ """
|
|
|
|
+ 获取用户信息
|
|
|
|
+ @request_dict code:唯一标识
|
|
|
|
+ @request_dict state:唯一标识
|
|
|
|
+ return:
|
|
|
|
+ """
|
|
|
|
+ code = request_dict.get('code', None)
|
|
|
|
+ state = request_dict.get('state', None)
|
|
|
|
+ if not all([code, state]):
|
|
|
|
+ return Response({'code': 444, 'result': {'error_msg': '缺少参数'}})
|
|
|
|
+ # 验证state,获取token
|
|
|
|
+ access_token = cls.get_access_token(state)
|
|
|
|
+ if not access_token:
|
|
|
|
+ return Response({'code': 120, 'result': {'error_msg': '获取token失败'}})
|
|
|
|
+ data = {
|
|
|
|
+ 'access_token': access_token,
|
|
|
|
+ 'code': code
|
|
|
|
+ }
|
|
|
|
+ try:
|
|
|
|
+ # 获取用户id
|
|
|
|
+ get_userid_url = 'https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo'
|
|
|
|
+ response = requests.get(get_userid_url, params=data)
|
|
|
|
+ data = response.json()
|
|
|
|
+ user_id = data.get("userid")
|
|
|
|
+ if user_id:
|
|
|
|
+ user_name = cls.add_or_update_user_info(access_token, user_id)
|
|
|
|
+ if not user_name:
|
|
|
|
+ return Response({'code': 120, 'result': {'error_msg': '登录失败'}})
|
|
|
|
+ token_obj = TokenObject()
|
|
|
|
+ token = token_obj.generate(data={'userID': user_id, 'user': user_name})
|
|
|
|
+ return Response({'code': 0, 'result': {'token': token}})
|
|
|
|
+ else:
|
|
|
|
+ errcode = data.get('errcode')
|
|
|
|
+ errmsg = data.get('errmsg')
|
|
|
|
+ return Response({'code': errcode, 'result': {'error_msg': errmsg}})
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return Response(
|
|
|
|
+ {'code': 500, 'result': {'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))}})
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def get_access_token(cls, state):
|
|
|
|
+ redis_client = RedisObject()
|
|
|
|
+ state_val = redis_client.get_data(state)
|
|
|
|
+ # 验证state
|
|
|
|
+ if state_val:
|
|
|
|
+ redis_client.del_data(state)
|
|
|
|
+ else:
|
|
|
|
+ return False
|
|
|
|
+ access_token = redis_client.get_data(key='enterprise_wechat_access_token')
|
|
|
|
+ if access_token:
|
|
|
|
+ return access_token
|
|
|
|
+ data = {
|
|
|
|
+ 'corpid': 'ww467ec1685e8262e6',
|
|
|
|
+ 'corpsecret': 'IeUoaQ-0hEhEduCQq1zyfVXjfeZpMsThK1nklszRzUY'
|
|
|
|
+ }
|
|
|
|
+ # 获取access_token,redis中不存在该值时重新请求获取access_token(有效时长两小时)
|
|
|
|
+ token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
|
|
|
|
+ token_response = requests.get(token_url, params=data)
|
|
|
|
+ data = token_response.json()
|
|
|
|
+ if data.get('errcode') == 0:
|
|
|
|
+ access_token = data['access_token']
|
|
|
|
+ redis_client.set_data('enterprise_wechat_access_token', access_token)
|
|
|
|
+ return access_token
|
|
|
|
+ else:
|
|
|
|
+ return False
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def add_or_update_user_info(cls, access_token, user_id):
|
|
|
|
+ now_time = int(time.time())
|
|
|
|
+ data = {
|
|
|
|
+ 'access_token': access_token,
|
|
|
|
+ 'userid': user_id
|
|
|
|
+ }
|
|
|
|
+ # 获取用户信息
|
|
|
|
+ token_url = 'https://qyapi.weixin.qq.com/cgi-bin/user/get'
|
|
|
|
+ response = requests.get(token_url, params=data)
|
|
|
|
+ data = response.json()
|
|
|
|
+ wechat_user_qs = WechatUserInfo.objects.filter(user_id=user_id)
|
|
|
|
+ if data.get('errcode') == 0:
|
|
|
|
+ url_data = {
|
|
|
|
+ 'name': data.get('name'),
|
|
|
|
+ 'position': data.get('position'),
|
|
|
|
+ 'status': data.get('status'),
|
|
|
|
+ 'add_time': now_time,
|
|
|
|
+ 'upd_time': now_time
|
|
|
|
+ }
|
|
|
|
+ if not wechat_user_qs.exists():
|
|
|
|
+ url_data['user_id'] = user_id
|
|
|
|
+ WechatUserInfo.objects.create(**url_data)
|
|
|
|
+ else:
|
|
|
|
+ wechat_user_qs.update(url_data)
|
|
|
|
+ if data.get('status') == 1:
|
|
|
|
+ return data.get('name')
|
|
|
|
+ else:
|
|
|
|
+ return False
|
|
|
|
+ else:
|
|
|
|
+ return False
|