import requests
import json
import time
import random



headers = {'Content-Type': 'application/json'}

URL = 'http://log.reyun.com/receive/tkio/event'
#时间戳
current_time_13 = int(round(time.time() * 1000))
#时间和日期
when = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#日期
ds = time.strftime('%Y-%m-%d', time.localtime(time.time()))
#设备品牌
manufacturer = 'vivo'
#设备系统
ryos = 'android'
#设备类型
rydevicetype = 'vivox9'
#网络类型
network='wifi'
#分辨率
resolution='1344*750'
#运行商
op='中国移动'
# 支付类型   支付宝(alipay),银联(unionpay),微信支付(weixinpay),易宝支付(yeepay)
paymenttype = 'alipay'
#货币类型
currencytype = 'CNY'
#支付的货币金额
currencyamount = '45'

class allEvent:
    # 激活
    def installUpload(self,i,IP,appkey):
        s = json.dumps({
            "what":'install',
            "appid":appkey,
            "context":{
                "_cmpaignid":'default',
                "_deviceid":i,
                "_idfa":i,
                "_ip":IP,
                '_manufacturer': manufacturer,
                '_ryos': ryos,
                '_rydevicetype': rydevicetype,
                '_network': network,
                '_resolution': resolution,
                '_op':op,
                #'_gaid': i,
                '_timestamp':current_time_13
            },
            'when':time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
            'ds':ds,
            'who':'lxx_test'

        })
        p = requests.post(url=URL,data=s,headers=headers,timeout=5)
        print(p.text)


    #注册
    def registerEvent(self,i,IP,appkey):
        s = json.dumps({
            'what':'register',
            'appid':appkey,
            'context':{
                '_deviceid':i,
                "_idfa":i,
                '_ip':IP,
                '_timestamp': current_time_13

            },
            'when':when,
            'ds':ds,
            'who':'lxx_test'
        })
        p = requests.post(url=URL,data=s,headers=headers,timeout=5)
        print(p.text)


    #登陆
    def loginEvent(self,i,IP,appkey):
        s = json.dumps({
            'what': 'loggedin',
            'appid': appkey,
            'context': {
                '_deviceid': i,
                "_idfa":i,
                '_ip': IP,
                '_timestamp': current_time_13

            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)


    #付费

    def payEvent(self,i,IP,appkey):
        #订单号
        transactionid = ''.join(str(random.choice(range(10))) for _ in range(19))
        s = json.dumps({
            'what': 'payment',
            'appid': appkey,
            'context': {
                '_deviceid': i,
                "_idfa":i,
                '_ip': IP,
                '_timestamp': current_time_13,
                '_transactionid':transactionid,
                '_paymenttype': paymenttype,
                '_currencytype': currencytype,
                '_currencyamount': currencyamount,

            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)

        # 订单
    def orderEvent(self, i, IP, appkey):
            # 订单号
            transactionid = ''.join(str(random.choice(range(10))) for _ in range(19))
            s = json.dumps({
                'what': 'order',
                'appid': appkey,
                'context': {
                    '_deviceid': i,
                    "_idfa":i,
                    '_ip': IP,
                    '_timestamp': current_time_13,
                    '_transactionid': transactionid,
                    '_paymenttype': paymenttype,
                    '_currencytype': currencytype,
                    '_currencyamount': currencyamount,

                },
                'when': when,
                'ds': ds,
                'who': 'lxx_test'
            })
            p = requests.post(url=URL, data=s, headers=headers, timeout=5)
            print(p.text)



     #自定义事件
    def ziding_event(self,i,IP,appkey,event):
        s = json.dumps({
            'what': event,
            'appid': appkey,
            'context': {
                '_deviceid': i,
                "_idfa":i,
                '_ip': IP,
                #识别是否为自定义事件,默认传1
                '_isreyundefaultevent':'1'


            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)

        # 启动
        def startupEvent(self, i, IP, appkey):
            s = json.dumps({
                'what': 'startup',
                'appid': appkey,
                'context': {
                    '_deviceid': i,
                    '_idfa': i,
                    '_ip': IP,
                    '_timestamp': current_time_13

                },
                'when': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
                'ds': ds,
                'who': 'lxx_test'
            })
            p = requests.post(url=URL, data=s, headers=headers, timeout=10)
            print(p.text)