12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
- @AUTHOR: ASJRD018
- @NAME: AnsjerFormal
- @software: PyCharm
- @DATE: 2018/12/5 9:30
- @Version: python3.6
- @MODIFY DECORD:ansjer dev
- @file: cloudstorage.py
- @Contact: chanjunkai@163.com
- """
- import logging
- import os
- import time
- from urllib.parse import quote, parse_qs, unquote
- import apns2
- import boto3
- import botocore
- import jpush
- import paypalrestsdk
- from boto3.session import Session
- from botocore import client
- from django.db import transaction
- from django.db.models import Q, F, Sum
- from django.http import HttpResponseRedirect, HttpResponse
- from django.views.generic.base import View
- from pyfcm import FCMNotification
- from Ansjer.config import PAYPAL_CRD, \
- SERVER_DOMAIN_SSL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APNS_MODE, APNS_CONFIG, BASE_DIR, \
- JPUSH_CONFIG, FCM_CONFIG, DETECT_PUSH_DOMAINS
- from Model.models import Device_Info, Order_Model, ExperienceAiModel, Pay_Type, CDKcontextModel, UidPushModel, \
- AiStoreMeal, AiService, UidSetModel, Ai_Push_Info
- from Object.AliPayObject import AliPayObject
- from Object.ETkObject import ETkObject
- from Object.RedisObject import RedisObject
- from Object.ResponseObject import ResponseObject
- from Object.TokenObject import TokenObject
- from Object.WechatPayObject import WechatPayObject
- from Service.CommonService import CommonService
- from Service.ModelService import ModelService
- # AI服务
- class AiView(View):
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.GET, request, operation)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.POST, request, operation)
- def validation(self, request_dict, request, operation):
- response = ResponseObject()
- if operation is None:
- return response.json(444, 'error path')
- elif operation == 'doPayPalCallBack': # paypal支付回调
- return self.do_pay_by_paypal_callback(request_dict, response)
- elif operation == 'doAlipayCallBack': # 支付宝支付回调
- return self.do_alipay_callback(request_dict, response)
- elif operation == 'doWechatCallBack': # 微信支付回调
- return self.do_wechat_callback(request, response)
- else:
- token = request_dict.get('token', None)
- # 设备主键uid
- tko = TokenObject(token)
- response.lang = tko.lang
- if tko.code != 0:
- return response.json(tko.code)
- userID = tko.userID
- if operation == 'createpayorder': # 创建支付订单
- return self.do_create_pay_order(request_dict, request, userID, response)
- elif operation == 'changeaistatus': # 修改AI开关状态
- return self.do_change_ai_status(userID, request_dict, response)
- elif operation == 'getAiStatus': # 获取AI开关状态
- return self.getAiStatus(userID, request_dict, response)
- elif operation == 'commoditylist': # 获取AI套餐列表
- return self.do_commodity_list(request_dict, response)
- elif operation == 'queryInfo': # 查询消息列表
- return self.queryInfo(userID, request_dict, response)
- elif operation == 'readInfo': # 消息已读
- return self.readInfo(userID, request_dict, response)
- elif operation == 'deleteInfo': # 删除消息
- return self.deleteInfo(userID, request_dict, response)
- elif operation == 'queryorderlist': # 查询订单
- return self.do_querylist(userID, request_dict, response)
- elif operation == 'getUsingPackage': # 获取当前使用的ai套餐
- return self.getUsingPackage(request_dict, userID, response)
- elif operation == 'experienceOrder': # 体验AI套餐
- return self.experience_order(request_dict, userID, response)
- else:
- return response.json(414)
- def do_change_ai_status(self, userID, request_dict, response):
- token_val = request_dict.get('token_val', None)
- appBundleId = request_dict.get('appBundleId', None)
- app_type = request_dict.get('app_type', None)
- push_type = request_dict.get('push_type', None)
- status = request_dict.get('status', None)
- m_code = request_dict.get('m_code', None)
- uid = request_dict.get('uid', None)
- lang = request_dict.get('lang', 'en')
- tz = request_dict.get('tz', '0')
- detect_group = request_dict.get('detect_group', None)
- interval = request_dict.get('interval', None)
- if not status:
- return response.json(444, 'status')
- # 关闭推送
- if not all([appBundleId, app_type, token_val, uid, m_code]):
- return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')
- # 如果传空上来,就默认为0
- tz = 0 if tz == '' else tz.replace("GMT", "")
- # 判断推送类型对应key是否存在
- if push_type == '0':
- if appBundleId not in APNS_CONFIG.keys():
- return response.json(904)
- elif push_type == '1':
- if appBundleId not in FCM_CONFIG.keys():
- return response.json(904)
- elif push_type == '2':
- if appBundleId not in JPUSH_CONFIG.keys():
- return response.json(904)
- else:
- return response.json(173)
- try:
- ai_service_qs = AiService.objects.filter(uid=uid, use_status=1)
- if not ai_service_qs.exists():
- return response.json(10053)
- nowTime = int(time.time())
- endTime = ai_service_qs.values('endTime')[0]['endTime']
- if nowTime > endTime:
- return response.json(10054)
- # 查询设备是否属于该用户
- device_info_qs = Device_Info.objects.filter(userID_id=userID, UID=uid)
- if not device_info_qs.exists():
- return response.json(14)
- status = int(status)
- nowTime = int(time.time())
- uid_set_qs = UidSetModel.objects.filter(uid=uid)
- if uid_set_qs.exists():
- uid_set_id = uid_set_qs[0].id
- interval = uid_set_qs[0].new_detect_interval if not interval else interval
- qs_data = {
- 'updTime': nowTime,
- }
- if interval:
- qs_data['detect_interval'] = int(interval)
- qs_data['detect_group'] = detect_group if detect_group else ''
- uid_set_qs.update(**qs_data)
- else:
- qs_data = {
- 'uid': uid,
- 'addTime': nowTime,
- 'updTime': nowTime,
- }
- if interval:
- qs_data['detect_interval'] = int(interval)
- qs_data['detect_group'] = detect_group if detect_group else ''
- # 添加设备配置
- uid_set_qs = UidSetModel.objects.create(**qs_data)
- uid_set_id = uid_set_qs.id
- qs_data['detect_status'] = status # ai开关状态
- ai_service_qs.update(**qs_data)
- thing_name = CommonService.query_serial_with_uid(uid) # 存在序列号则为使用序列号作为物品名
- topic_name = 'ansjer/generic/{}'.format(thing_name)
- if status == 0: # 关闭
- # mqtt通知设备关闭AI识别功能
- msg = {'commandType': 'AIDisable'}
- req_success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
- if not req_success:
- return response.json(10044)
- return response.json(0)
- elif status == 1: # 开启
- uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
- if uid_push_qs.exists():
- uid_push_update_dict = {
- 'appBundleId': appBundleId,
- 'app_type': app_type,
- 'push_type': push_type,
- 'token_val': token_val,
- 'updTime': nowTime,
- 'lang': lang,
- 'tz': tz
- }
- uid_push_qs.update(**uid_push_update_dict)
- else:
- uid_push_create_dict = {
- 'uid_set_id': uid_set_id,
- 'userID_id': userID,
- 'appBundleId': appBundleId,
- 'app_type': app_type,
- 'push_type': push_type,
- 'token_val': token_val,
- 'm_code': m_code,
- 'addTime': nowTime,
- 'updTime': nowTime,
- 'lang': lang,
- 'tz': tz
- }
- # 绑定设备推送
- UidPushModel.objects.create(**uid_push_create_dict)
- etkObj = ETkObject(etk='')
- etk = etkObj.encrypt(uid)
- aiIdentificationUrl = "{DETECT_PUSH_DOMAIN}AiService/identification".format(
- DETECT_PUSH_DOMAIN=DETECT_PUSH_DOMAINS)
- # mqtt通知设备开启AI识别功能
- msg = {
- 'commandType': 'AIEnable',
- 'payload': {
- 'etk': etk,
- 'endTime': endTime,
- 'aiIdentificationUrl': aiIdentificationUrl,
- }
- },
- req_success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
- if not req_success:
- return response.json(10044)
- return response.json(0, {'aiIdentificationUrl': aiIdentificationUrl, 'endTime': endTime, 'etk': etk})
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- def getAiStatus(self, userID, request_dict, response):
- uid = request_dict.get('uid', None)
- if not uid:
- return response.json(444)
- try:
- ai_server_qs = AiService.objects.filter(uid=uid, use_status=1).values('detect_status', 'detect_group')
- if not ai_server_qs.exists():
- return response.json(173)
- res = {
- 'detect_status': ai_server_qs[0]['detect_status'],
- 'detect_group': ai_server_qs[0]['detect_group'],
- }
- return response.json(0, {'data': res})
- except Exception as e:
- return response.json(500, repr(e))
- # 获取AI套餐列表
- @staticmethod
- def do_commodity_list(request_dict, response):
- uid = request_dict.get('uid', None)
- lang = request_dict.get('lang', 'en')
- try:
- # DVR/NVR设备暂不返回套餐列表
- device_info_qs = Device_Info.objects.filter(Q(UID=uid), Q(Type__lte=4) | Q(Type=10001))
- if device_info_qs.exists():
- return response.json(0)
- # 没免费体验过的设备只返回体验套餐数据,体验过的不再返回
- exc_ai_qs = ExperienceAiModel.objects.filter(uid=uid, experience_type=0)
- if exc_ai_qs.exists():
- ai_meal_qs = AiStoreMeal.objects.filter(~Q(pay_type=10))
- else:
- ai_meal_qs = AiStoreMeal.objects.filter(pay_type=10)
- # 查询套餐数据
- ai_meal_qs = ai_meal_qs.filter(is_show=1, lang__lang=lang). \
- annotate(ai_meal_id=F('id'), title=F('lang__title'), content=F('lang__content')). \
- values("ai_meal_id", "title", "content", "price", "effective_day", "currency", "virtual_price",
- "symbol")
- if not ai_meal_qs.exists():
- return response.json(0)
- # 查询每种套餐的所有支付方式
- ai_meal_list = list(ai_meal_qs)
- for ai_meal in ai_meal_list:
- pay_type_qs = Pay_Type.objects.filter(aistoremeal=ai_meal['ai_meal_id']).values('id', 'payment')
- ai_meal['pay_type'] = list(pay_type_qs)
- result = {
- 'meals': ai_meal_list,
- }
- return response.json(0, result)
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- def do_querylist(self, userID, request_dict, response):
- page = request_dict.get('page', None)
- line = request_dict.get('line', None)
- uid = request_dict.get('uid', None)
- lang = request_dict.get('lang', 'en')
- if not page or not line:
- return response.json(444, 'page,line')
- try:
- page = int(page)
- line = int(line)
- omqs = Order_Model.objects.filter(userID_id=userID, status=1, order_type=1, ai_rank__lang__lang=lang)
- # 筛选指定设备id的订单
- if uid:
- omqs.filter(UID=uid)
- if not omqs.exists():
- return response.json(173)
- count = omqs.count()
- omqs = omqs.annotate(rank__title=F('ai_rank__lang__title'), rank__content=F('ai_rank__lang__content'),
- rank__day=F('ai_rank__effective_day'), rank__price=F('ai_rank__price'),
- rank__expire=F('ai_rank__effective_day'), rank__id=F('ai_rank_id'),
- rank__currency=F('ai_rank__currency'))
- order_ql = omqs[(page - 1) * line:page * line].values("orderID", "UID", "channel", "desc", "price",
- "currency",
- "addTime",
- "updTime", "paypal", "rank__day", "payType",
- "rank__price", "status",
- "rank__content", "rank__title", "rank__currency",
- "rank__expire", "ai_rank_id")
- order_list = list(order_ql)
- data = []
- nowTime = int(time.time())
- # 这里需要进行优化
- uid_list = []
- for od in order_list:
- uid_list.append(od['UID'])
- didqs = Device_Info.objects.filter(userID_id=userID, UID__in=uid_list).values('id', 'UID', 'Type')
- for d in order_list:
- if d['status'] == 0:
- if d['addTime'] + 3600 < nowTime:
- d['status'] = 3
- for did in didqs:
- if d['UID'] == did['UID']:
- d['did'] = did['id']
- d['Type'] = did['Type']
- data.append(d)
- # d['rank__lang__content'] = '月' if lang == 'cn' else 'month'
- return response.json(0, {'data': data, 'count': count})
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- # 获取当前使用的ai套餐
- def getUsingPackage(self, request_dict, userID, response):
- uid = request_dict.get('uid', None)
- lang = request_dict.get('lang', 'en')
- # dv_qs = Device_Info.objects.filter(userID_id=userID, UID=uid, isShare=False, isExist=1, vodPrimaryUserID=userID)
- # if not dv_qs.exists():
- # return response.json(12)
- try:
- ai_service_qs = AiService.objects.filter(uid=uid, use_status=1, orders__ai_rank__lang__lang=lang)
- if not ai_service_qs.exists():
- return response.json(0, [])
- # 计算套餐过期时间
- sum_end_time = AiService.objects.filter(Q(uid=uid), ~Q(use_status=2)).aggregate(Sum('endTime'))[
- 'endTime__sum']
- ai_service_qs = ai_service_qs.order_by('addTime').annotate(
- bucket__content=F('orders__ai_rank__lang__title')). \
- values('uid', 'use_status', 'bucket__content')
- ai_service_data = ai_service_qs[0]
- # 如果存在序列号返回完整序列号
- device_info_qs = Device_Info.objects.filter(UID=uid).values('serial_number', 'Type')
- serial_number = device_info_qs[0]['serial_number']
- device_type = device_info_qs[0]['Type']
- if serial_number:
- ai_service_data['serial_number'] = CommonService.get_full_serial_number(uid, serial_number, device_type)
- ai_service_data['endTime'] = sum_end_time
- return response.json(0, [ai_service_data])
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- # 体验AI套餐
- @staticmethod
- def experience_order(request_dict, userID, response):
- uid = request_dict.get('uid', None)
- channel = request_dict.get('channel', None)
- pay_type = int(request_dict.get('pay_type', None))
- rank = request_dict.get('rank', None)
- cdk = request_dict.get('cdk', None)
- lang = request_dict.get('lang', 'en')
- # 使用redis设置唯一key加锁
- redisObj = RedisObject()
- redis_key = uid + 'do_experience_ai_order'
- isLock = redisObj.CONN.setnx(redis_key, 1)
- redisObj.CONN.expire(redis_key, 60)
- if not isLock:
- return response.json(5)
- try:
- if pay_type == 10: # 判断是否已体验过套餐
- exc_ai_qs = ExperienceAiModel.objects.filter(uid=uid, experience_type=0)
- if exc_ai_qs.exists():
- return response.json(5)
- if cdk is not None and pay_type == 11:
- cdk_qs = CDKcontextModel.objects.filter(cdk=cdk).values('is_activate', 'rank__id', 'rank__commodity_code')
- if not cdk_qs.exists():
- return response.json(10040)
- if cdk_qs[0]['is_activate'] == 1:
- return response.json(10039)
- rank = cdk_qs[0]['rank__id']
- if uid is None or channel is None or pay_type is None or rank is None:
- redisObj.del_data(key=redis_key)
- return response.json(444)
- # 判断是否为主用户操作
- device_info_qs = Device_Info.objects.filter(Q(UID=uid) & ~Q(vodPrimaryUserID='')).values('vodPrimaryUserID')
- if device_info_qs.exists():
- if device_info_qs[0]['vodPrimaryUserID'] != userID:
- if pay_type == 10:
- return response.json(10035)
- if pay_type == 11:
- return response.json(10036)
- dv_qs = Device_Info.objects.filter(userID_id=userID, UID=uid, isShare=False, isExist=1)
- if not dv_qs.exists():
- return response.json(12)
- orderID = CommonService.createOrderID()
- nowTime = int(time.time())
- ai_store_meal_qs = AiStoreMeal.objects.filter(id=rank, lang__lang=lang, is_show=1). \
- values('lang__content', 'price', 'currency', 'effective_day')
- if not ai_store_meal_qs.exists():
- return response.json(173)
- effective_day = ai_store_meal_qs[0]['effective_day']
- endTime = nowTime + effective_day * 24 * 60 * 60 # 套餐结束时间
- with transaction.atomic():
- # 订单表创建数据
- Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID,
- desc=ai_store_meal_qs[0]['lang__content'], payType=pay_type, payTime=nowTime,
- price=ai_store_meal_qs[0]['price'], currency=ai_store_meal_qs[0]['currency'],
- addTime=nowTime, updTime=nowTime, pay_url='AI体验',
- rank_id=1, ai_rank_id=rank, status=1)
- # ai服务表创建数据
- AiService.objects.create(uid=uid, channel=channel, orders_id=orderID, detect_status=1, endTime=endTime,
- addTime=nowTime, updTime=nowTime, use_status=1)
- if pay_type == 10:
- ExperienceAiModel.objects.create(
- experience_type=0,
- uid=uid,
- do_time=nowTime
- )
- elif pay_type == 11:
- CDKcontextModel.objects.filter(cdk=cdk).update(is_activate=1, order=orderID)
- redisObj.del_data(key=redis_key)
- pay_ok_url = "{}cloudstorage/payOK?paytype={}&lang={}".format(SERVER_DOMAIN_SSL, pay_type, lang)
- return response.json(0, pay_ok_url)
- except Exception as e:
- print(e)
- redisObj.del_data(key=redis_key)
- return response.json(474)
- def do_create_pay_order(self, request_dict, request, userID, response):
- uid = request_dict.get('uid', None)
- channel = request_dict.get('channel', None)
- pay_type = int(request_dict.get('pay_type', 1))
- rank = request_dict.get('rank', None)
- lang = request_dict.get('lang', 'en')
- if not uid or not channel or not pay_type or not rank:
- return response.json(444)
- try:
- # 判断是否为主用户
- # dv_qs = Device_Info.objects.filter(userID_id=userID, UID=uid, isShare=False, isExist=1).values(
- # 'vodPrimaryUserID',
- # 'vodPrimaryMaster')
- # if not dv_qs.exists():
- # return response.json(12)
- # dvq = Device_Info.objects.filter(UID=uid)
- # dvq = dvq.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID')
- # if dvq.exists():
- # if dvq[0]['vodPrimaryUserID'] != userID:
- # return response.json(10033)
- # 获取ai套餐数据
- ai_sm_qs = AiStoreMeal.objects.filter(id=rank, pay_type=pay_type, is_show=1, lang__lang=lang). \
- values('lang__title', 'lang__content', 'currency', 'price')
- if not ai_sm_qs.exists():
- return response.json(173)
- title = ai_sm_qs[0]['lang__title']
- content = ai_sm_qs[0]['lang__content']
- currency = ai_sm_qs[0]['currency']
- price = ai_sm_qs[0]['price']
- nowTime = int(time.time())
- orderID = CommonService.createOrderID()
- price = round(float(price), 2)
- order_dict = {
- 'orderID': orderID,
- 'UID': uid,
- 'channel': channel,
- 'userID_id': userID,
- 'desc': content,
- 'payType': pay_type,
- 'payTime': nowTime,
- 'price': price,
- 'currency': currency,
- 'addTime': nowTime,
- 'updTime': nowTime,
- 'ai_rank_id': rank,
- 'rank_id': 1,
- 'order_type': 1,
- }
- if pay_type == 1: # PayPal支付
- order_dict['paymentID'], order_dict['pay_url'] = self.create_paypal_payment(lang, orderID, price,
- currency, content, response)
- res_data = {'redirectUrl': order_dict['pay_url'], 'orderID': orderID}
- elif pay_type == 2: # 支付宝
- order_dict['pay_url'] = self.create_alipay_payment(lang, orderID, price, title, content, response)
- res_data = {'redirectUrl': order_dict['pay_url'], 'orderID': orderID}
- elif pay_type == 3: # 微信支付
- ip = CommonService.get_ip_address(request)
- order_dict['pay_url'], sign_params = self.create_wechat_payment(lang, orderID, price, ip, content,
- response)
- res_data = {'redirectUrl': order_dict['pay_url'], 'orderID': orderID, 'result': sign_params}
- else:
- return response.json(444, {'param': 'pay_type'})
- Order_Model.objects.create(**order_dict)
- return response.json(0, res_data)
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- @staticmethod
- def create_paypal_payment(lang, orderID, price, currency, content, response):
- cancel_url = CommonService.get_payment_status_url(lang, 'fail')
- call_sub_url = "{}AiService/doPayPalCallBack?orderID={}&lang={}".format(SERVER_DOMAIN_SSL, orderID, lang)
- paypalrestsdk.configure(PAYPAL_CRD)
- payment = paypalrestsdk.Payment({
- "intent": "sale",
- "payer": {"payment_method": "paypal"},
- "redirect_urls": {"return_url": call_sub_url, "cancel_url": cancel_url},
- "transactions": [{
- "item_list": {"items": [
- {"name": "Cloud video", "sku": "1", "price": price, "currency": "USD", "quantity": 1}]},
- "amount": {"total": price, "currency": currency},
- "description": content}]})
- if not payment.create(): # 创建失败
- return response.json(10, payment.error)
- paymentID = payment['id'] # 获取paymentID
- for link in payment.links:
- if link.rel == "approval_url":
- pay_url = str(link.href)
- return paymentID, pay_url
- return response.json(10, 'create_ai_order_failed')
- @staticmethod
- def create_alipay_payment(lang, orderID, price, title, content, response):
- aliPayObj = AliPayObject()
- alipay = aliPayObj.conf()
- subject = title + content
- order_string = alipay.api_alipay_trade_wap_pay(
- out_trade_no=orderID,
- total_amount=price,
- subject=subject,
- return_url="{}web/paid2/success.html".format(SERVER_DOMAIN_SSL),
- notify_url="{}AiService/doAlipayCallBack".format(SERVER_DOMAIN_SSL),
- quit_url="{}web/paid2/fail.html".format(SERVER_DOMAIN_SSL),
- passback_params=quote("lang=" + lang)
- )
- if not order_string:
- return response.json(10, '生成订单错误.')
- return aliPayObj.alipay_prefix + order_string
- @staticmethod
- def create_wechat_payment(lang, orderID, price, ip, content, response):
- pay = WechatPayObject()
- pay_url = "{}AiService/doWechatCallBack".format(SERVER_DOMAIN_SSL)
- # 统一调用接口
- pay.get_parameter(orderID, content, float(price) * 100, ip, pay_url, quote("lang=" + lang))
- sign_params = pay.re_finall(orderid=orderID)
- if not sign_params:
- return response.json(10, '生成订单错误.')
- return pay_url, sign_params
- def do_pay_by_paypal_callback(self, request_dict, response):
- logger = logging.getLogger('info')
- logger.info('AI订单---paypal支付回调')
- paymentId = request_dict.get('paymentId', None)
- PayerID = request_dict.get('PayerID', None)
- orderID = request_dict.get('orderID', None)
- lang = request_dict.get('lang', 'en')
- if not orderID:
- pay_failed_url = CommonService.get_payment_status_url(lang, 'fail')
- return HttpResponseRedirect(pay_failed_url)
- logger.info("paymentID: {}, payerID: {}".format(paymentId, PayerID))
- # redis加锁,防止订单重复
- redisObj = RedisObject()
- isLock = redisObj.CONN.setnx(orderID + 'do_notify', 1)
- redisObj.CONN.expire(orderID + 'do_notify', 60)
- if not isLock:
- return response.json(5)
- try:
- order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
- if not order_qs.exists():
- return response.json(173)
- paypalrestsdk.configure(PAYPAL_CRD)
- payment = paypalrestsdk.Payment.find(paymentId)
- logger.info("payment: {}".format(payment))
- payer = payment.execute({"payer_id": PayerID})
- logger.info('payres: {}'.format(payer))
- if not payer:
- pay_failed_url = CommonService.get_payment_status_url(lang, 'fail')
- redisObj.del_data(key=orderID + 'do_notify')
- return HttpResponseRedirect(pay_failed_url)
- return self.payment_success(orderID, lang, order_qs, redisObj)
- except Exception as e:
- logger.info('AI订单paypal支付回调异常:{}'.format(repr(e)))
- order_qs.update(status=10)
- pay_failed_url = CommonService.get_payment_status_url(lang, 'fail')
- redisObj.del_data(key=orderID + 'do_notify')
- return HttpResponseRedirect(pay_failed_url)
- def do_alipay_callback(self, request_dict, response): # 支付宝支付回调
- logger = logging.getLogger('info')
- logger.info('AI订单---支付宝支付回调')
- try:
- data = request_dict.dict()
- passback_params = data['passback_params']
- parmap = dict([(k, v[0]) for k, v in parse_qs(unquote(passback_params)).items()])
- lang = parmap['lang']
- signature = data['sign']
- data.pop('sign')
- orderID = data['out_trade_no']
- # redis加锁,防止订单重复
- redisObj = RedisObject()
- isLock = redisObj.CONN.setnx(orderID + 'do_notify', 1)
- redisObj.CONN.expire(orderID + 'do_notify', 60)
- if not isLock:
- return response.json(5)
- order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
- if not order_qs.exists():
- return response.json(173)
- aliPayObj = AliPayObject()
- alipay = aliPayObj.conf()
- success = alipay.verify(data, signature)
- if not success or data["trade_status"] not in ("TRADE_SUCCESS", "TRADE_FINISHED"):
- return response.json(0, signature)
- return self.payment_success(orderID, lang, order_qs, redisObj)
- except Exception as e:
- logger.info('AI订单支付宝支付回调异常:{}'.format(repr(e)))
- order_qs.update(status=10)
- redisObj.del_data(key=orderID + 'do_notify')
- pay_failed_url = CommonService.get_payment_status_url(lang, 'fail')
- redisObj.del_data(key=orderID + 'do_notify')
- return HttpResponseRedirect(pay_failed_url)
- def do_wechat_callback(self, request, response): # 微信支付回调
- logger = logging.getLogger('info')
- logger.info('AI订单---微信支付回调')
- try:
- pay = WechatPayObject()
- data = pay.weixinpay_call_back(request.body)
- attach = data["attach"]
- parmap = dict([(k, v[0]) for k, v in parse_qs(unquote(attach)).items()])
- lang = parmap['lang']
- trade_status = data['result_code'] # 业务结果 SUCCESS/FAIL
- orderID = data['out_trade_no'] # 商户订单号
- order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
- if not order_qs.exists():
- return response.json(173)
- if trade_status != 'SUCCESS':
- order_qs.update(status=10)
- return HttpResponse(pay.xml_to_dict({'return_code': 'FAIL'}))
- check_sign = pay.get_notifypay(data)
- if not check_sign:
- return HttpResponse(pay.xml_to_dict({'return_code': 'FAIL', 'return_msg': '签名失败'}))
- # redis加锁,防止订单重复
- redisObj = RedisObject()
- isLock = redisObj.CONN.setnx(orderID + 'do_notify', 1)
- redisObj.CONN.expire(orderID + 'do_notify', 60)
- if not isLock:
- return response.json(5)
- return self.payment_success(self, orderID, lang, order_qs, redisObj, True)
- except Exception as e:
- order_qs.update(status=10)
- redisObj.del_data(key=orderID + 'do_notify')
- return HttpResponse(pay.xml_to_dict({'return_code': 'FAIL', 'return_msg': repr(e)}))
- def payment_success(self, orderID, lang, order_qs, redisObj, is_wechat_pay=False):
- nowTime = int(time.time())
- order_list = order_qs.values("UID", "channel", "commodity_code", "ai_rank__effective_day", "isSelectDiscounts",
- "userID__userID", "userID__username", "coupon_id")
- userid = order_list[0]['userID__userID']
- username = order_list[0]['userID__username']
- UID = order_list[0]['UID']
- channel = order_list[0]['channel']
- effective_day = order_list[0]['ai_rank__effective_day']
- ai_service_qs = AiService.objects.filter(Q(uid=UID), Q(channel=channel), Q(use_status=1))
- ai_service_dict = {'orders_id': orderID,
- 'uid': UID,
- 'channel': channel,
- 'detect_status': 1,
- 'addTime': nowTime,
- 'updTime': nowTime,
- }
- if ai_service_qs.exists(): # 有正在使用的套餐,套餐结束时间保存为套餐有效期
- ai_service_dict['endTime'] = effective_day * 24 * 60 * 60
- else:
- ai_service_dict['use_status'] = 1
- ai_service_dict['endTime'] = nowTime + effective_day * 24 * 60 * 60
- with transaction.atomic():
- # 更新设备主用户
- Device_Info.objects.filter(UID=UID, vodPrimaryUserID='', vodPrimaryMaster=''). \
- update(vodPrimaryUserID=userid, vodPrimaryMaster=username)
- # 更新订单数据,返回支付成功url
- order_qs.update(status=1, updTime=nowTime)
- # 创建AiService数据
- AiService.objects.create(**ai_service_dict)
- pay_success_url = CommonService.get_payment_status_url(lang, 'success')
- redisObj.del_data(key=orderID + 'do_notify')
- if is_wechat_pay:
- return HttpResponse("<xml>\
- <return_code><![CDATA[SUCCESS]]></return_code>\
- <return_msg><![CDATA[OK]]></return_msg>\
- </xml>")
- else:
- return HttpResponseRedirect(pay_success_url)
- ## 检查是否有符合条件的标签,并且返回标签坐标位置信息
- def labelsCoords(self, user_detect_group, rekognition_res, image_size, n_time):
- logger = logging.getLogger('info')
- labels = rekognition_res['Labels']
- label_name = []
- logger.info('--------识别到的标签-------')
- logger.info(labels)
- labels_type = {
- '1': ['Person', 'Human'], # 人
- '2': ['Dog', 'Pet', 'Canine', 'Animal', 'Puppy'], # 动物
- '3': ['Car', 'Vehicle', 'Transportation', 'Automobile'] # 车
- }
- # 找出识别的所有标签
- for label in labels:
- label_name.append(label['Name'])
- for Parents in label['Parents']:
- label_name.append(Parents['Name'])
- # 删除用户没有选择的ai识别类型, 并且得出最终识别结果
- user_detect_list = user_detect_group.split(',')
- user_detect_list = [i.strip() for i in user_detect_list]
- label_result_list = []
- new_labels_type = labels_type
- for key, label_type in labels_type.items():
- if key in user_detect_list:
- new_labels_type[key] = label_type
- for label in label_type:
- if label in label_name:
- label_result_list.append(label)
- # 找出标签边框线位置信息
- boundingBoxList = []
- for label in labels:
- if label['Name'] in label_result_list:
- for boundingBox in label['Instances']:
- boundingBoxList.append(boundingBox['BoundingBox'])
- # 找出边框位置信息对应的单图位置并重新计算位置比
- merge_image_height = image_size['height']
- merge_image_width = image_size['width']
- single_height = merge_image_height // image_size['num']
- new_bounding_box_dict = {}
- new_bounding_box_dict[n_time + '_0'] = []
- new_bounding_box_dict[n_time + '_1'] = []
- new_bounding_box_dict[n_time + '_2'] = []
- new_bounding_box_dict[n_time + '_3'] = []
- for k, val in enumerate(boundingBoxList):
- boundingBoxTop = merge_image_height * val['Top']
- # 找出当前边框属于哪张图片范围
- boxDict = {}
- for i in range(image_size['num']):
- min = i * single_height # 第n张图
- max = (i + 1) * single_height
- if boundingBoxTop >= min and boundingBoxTop <= max:
- # print("属于第{i}张图".format(i=i+1))
- boxDict['Width'] = val['Width']
- boxDict['Height'] = merge_image_height * val['Height'] / single_height
- boxDict['Top'] = ((merge_image_height * val['Top']) - (
- i * single_height)) / single_height # 减去前i张图片的高度
- boxDict['Left'] = val['Left']
- boxDict['picName'] = "{n_time}_{i}".format(n_time=n_time, i=i)
- # new_bounding_box_list.append(boxDict)
- # new_bounding_box_list.append(boxDict)
- new_bounding_box_dict["{n_time}_{i}".format(n_time=n_time, i=i)].append(boxDict)
- # exit(new_bounding_box_list)
- user_labels_list = list(new_labels_type.keys())
- user_labels_list.sort()
- return {'label_type': user_labels_list, 'label_list': label_result_list,
- 'new_bounding_box_dict': new_bounding_box_dict}
- def upload_s3(self, file_path, upload_path):
- try:
- aws_key = "AKIA2E67UIMD45Y3HL53" # 【你的 aws_access_key】
- aws_secret = "ckYLg4Lo9ZXJIcJEAKkzf2rWvs8Xth1FCjqiAqUw" # 【你的 aws_secret_key】
- session = Session(aws_access_key_id=aws_key,
- aws_secret_access_key=aws_secret,
- region_name="us-east-1")
- s3 = session.resource("s3")
- # client = session.client("s3")
- bucket = "foreignpush" # 【你 bucket 的名字】 # 首先需要保.证 s3 上已经存在该存储桶,否则报错
- upload_data = open(file_path, "rb")
- # upload_key = "test"
- s3.Bucket(bucket).put_object(Key=upload_path, Body=upload_data)
- return True
- except Exception as e:
- print(repr(e))
- return False
- def get_msg_title(self, appBundleId, nickname):
- package_title_config = {
- 'com.ansjer.customizedd_a': 'DVS',
- 'com.ansjer.zccloud_a': 'ZosiSmart',
- 'com.ansjer.zccloud_ab': '周视',
- 'com.ansjer.adcloud_a': 'ADCloud',
- 'com.ansjer.adcloud_ab': 'ADCloud',
- 'com.ansjer.accloud_a': 'ACCloud',
- 'com.ansjer.loocamccloud_a': 'Loocam',
- 'com.ansjer.loocamdcloud_a': 'Anlapus',
- 'com.ansjer.customizedb_a': 'COCOONHD',
- 'com.ansjer.customizeda_a': 'Guardian365',
- 'com.ansjer.customizedc_a': 'PatrolSecure',
- }
- if appBundleId in package_title_config.keys():
- return package_title_config[appBundleId] + '(' + nickname + ')'
- else:
- return nickname
- def get_msg_text(self, channel, n_time, lang, tz, label_list):
- n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
- if lang == 'cn':
- msg = '摄像头AI识别到了{}'.format(label_list)
- send_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
- else:
- msg = 'Camera AI recognizes{}'.format(label_list)
- send_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
- return send_text
- def do_jpush(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
- app_key = JPUSH_CONFIG[appBundleId]['Key']
- master_secret = JPUSH_CONFIG[appBundleId]['Secret']
- # 此处换成各自的app_key和master_secre
- _jpush = jpush.JPush(app_key, master_secret)
- push = _jpush.create_push()
- push.audience = jpush.registration_id(token_val)
- push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
- "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
- android = jpush.android(alert=msg_text, priority=1, style=1, alert_type=7,
- big_text=msg_text, title=msg_title,
- extras=push_data)
- push.notification = jpush.notification(android=android)
- push.platform = jpush.all_
- res = push.send()
- print(res)
- return res.status_code
- def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
- try:
- serverKey = FCM_CONFIG[appBundleId]
- push_service = FCMNotification(api_key=serverKey)
- data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
- "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
- result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
- message_body=msg_text, data_message=data,
- extra_kwargs={
- 'default_vibrate_timings': True,
- 'default_sound': True,
- 'default_light_settings': True
- })
- print('fcm push ing')
- print(result)
- return result
- except Exception as e:
- return 'serverKey abnormal'
- def do_apns(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
- logger = logging.getLogger('info')
- logger.info("进来do_apns函数了")
- logger.info(token_val)
- logger.info(APNS_MODE)
- logger.info(os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
- try:
- cli = apns2.APNSClient(mode=APNS_MODE,
- client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
- push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
- "received_at": n_time, "sound": "", "uid": uid, "zpush": "1", "channel": channel}
- alert = apns2.PayloadAlert(body=msg_text, title=msg_title)
- payload = apns2.Payload(alert=alert, custom=push_data, sound="default")
- n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
- res = cli.push(n=n, device_token=token_val, topic=appBundleId)
- if res.status_code == 200:
- return res.status_code
- else:
- logger.info('apns push fail')
- logger.info(res.reason)
- return res.status_code
- except (ValueError, ArithmeticError):
- return 'The program has a numeric format exception, one of the arithmetic exceptions'
- except Exception as e:
- print(repr(e))
- logger.info(repr(e))
- return repr(e)
- def queryInfo(self, userID, request_dict, response):
- page = int(request_dict.get('page', None))
- line = int(request_dict.get('line', None))
- if not page or not line:
- return response.json(444, 'page,line')
- startTime = request_dict.get('startTime', None)
- endTime = request_dict.get('endTime', None)
- eventType = request_dict.get('eventType', None)
- now_time = int(time.time())
- seven_days_ago = now_time - 7 * 24 * 3600 # 查询7天内的数据
- qs = Ai_Push_Info.objects.filter(userID_id=userID, eventTime__gt=seven_days_ago).order_by('-eventTime')
- if startTime and endTime:
- qs = qs.filter(eventTime__range=(startTime, endTime))
- # if eventType:
- # qs = qs.filter(eventType__contains=eventType)
- uids = request_dict.get('uids', None)
- if uids:
- uid_list = uids.split(',')
- qs = qs.filter(devUid__in=uid_list)
- dvqs = Device_Info.objects.filter(UID__in=uid_list, userID_id=userID).values('UID', 'Type', 'NickName')
- uid_type_dict = {}
- for dv in dvqs:
- uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
- else:
- dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
- uid_type_dict = {}
- for dv in dvqs:
- uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
- if not qs.exists():
- return response.json(0, {'datas': [], 'count': 0})
- count = qs.count()
- qs = qs.values('id', 'devUid', 'devNickName', 'Channel', 'eventType', 'status', 'alarm', 'eventTime',
- 'receiveTime', 'is_st', 'addTime', 'storage_location')
- qs = qs[(page - 1) * line:page * line]
- res = []
- aws_s3_client = boto3.client(
- 's3',
- aws_access_key_id=AWS_ACCESS_KEY_ID[0],
- aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
- config=botocore.client.Config(signature_version='s3v4'),
- region_name='cn-northwest-1'
- )
- for p in qs:
- p['eventType'] = int(p['eventType'][0])
- devUid = p['devUid']
- eventTime = p['eventTime']
- channel = p['Channel']
- storage_location = p['storage_location']
- if p['is_st'] == 1:
- s3_img_cover = '{uid}/{channel}/cover{time}.jpg'.format(uid=devUid, channel=channel, time=eventTime)
- s3_img_desc = '{uid}/{channel}/desc{time}.jpg'.format(uid=devUid, channel=channel, time=eventTime)
- response_url_cover = aws_s3_client.generate_presigned_url('get_object',
- ExpiresIn=300,
- Params={
- 'Bucket': 'aipush', 'Key': s3_img_cover
- },
- )
- response_url_desc = aws_s3_client.generate_presigned_url('get_object',
- ExpiresIn=300,
- Params={
- 'Bucket': 'aipush', 'Key': s3_img_desc
- },
- )
- p['img'] = response_url_cover
- p['img_list'] = [response_url_desc]
- elif p['is_st'] == 2:
- pass
- elif p['is_st'] == 3:
- # 列表装载回放时间戳标记
- p['img_list'] = []
- for i in range(4):
- thumbspng = '{uid}/{channel}/{time}_{st}.jpg'.format(uid=devUid, channel=p['Channel'],
- time=eventTime, st=i)
- response_url = aws_s3_client.generate_presigned_url('get_object',
- ExpiresIn=300,
- Params={
- 'Bucket': 'aipush', 'Key': thumbspng
- },
- )
- p['img_list'].append(response_url)
- if devUid in uid_type_dict.keys():
- p['uid_type'] = uid_type_dict[devUid]['type']
- p['devNickName'] = uid_type_dict[devUid]['NickName']
- else:
- p['uid_type'] = ''
- res.append(p)
- return response.json(0, {'datas': res, 'count': count})
- def readInfo(self, userID, request_dict, response):
- is_update_all = request_dict.get('is_update_all', 0)
- try:
- if int(is_update_all) == 1: # 全部已读
- is_update = Ai_Push_Info.objects.filter(userID_id=userID).update(status=1)
- return response.json(0, {'update_count': is_update})
- else:
- id_list = request_dict.get('id_list', None)
- if not id_list:
- request_dict.getlist('id_list[]', None) # 获取IOS数组传参
- logger = logging.getLogger('info')
- logger.info('已读ai消息id_list:{}'.format(id_list))
- if not id_list:
- return response.json(444)
- id_list = eval(id_list) # 字符串转列表
- param_flag = CommonService.get_param_flag(data=id_list)
- if not param_flag:
- return response.json(444)
- count = 0
- for id in id_list:
- ai_push_qs = Ai_Push_Info.objects.filter(id=int(id))
- if ai_push_qs.exists():
- own_dev = ModelService.check_own_device(userID, ai_push_qs[0].devUid)
- if own_dev:
- count += 1
- ai_push_qs.update(status=1)
- return response.json(0, {'update_success': count})
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- def deleteInfo(self, userID, request_dict, response):
- id_list = request_dict.get('id_list', None)
- if not id_list:
- request_dict.getlist('id_list[]', None) # 获取IOS数组传参
- logger = logging.getLogger('info')
- logger.info('删除ai消息id_list:{}'.format(id_list))
- if not id_list:
- return response.json(444)
- try:
- id_list = eval(id_list) # 字符串转列表
- param_flag = CommonService.get_param_flag(data=id_list)
- if not param_flag:
- return response.json(444)
- for id in id_list:
- ai_push_qs = Ai_Push_Info.objects.filter(id=id)
- if ai_push_qs.exists():
- own_dev = ModelService.check_own_device(userID, ai_push_qs[0].devUid)
- if own_dev:
- ai_push_qs.delete()
- return response.json(0)
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
|