import requests
import json
import time
import random



headers = {'Content-Type': 'application/json'}
#测试环境
URL = 'http://reyunlog.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,
                "_imei":i,
                "_androidid":i,
                "_ip": IP,
                '_manufacturer': manufacturer,
                '_ryos': ryos,
                '_rydevicetype': rydevicetype,
                '_network': network,
                '_resolution': resolution,
                '_op': op,
                '_timestamp': current_time_13,
                '_gaid': i
            },
            '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)

        # 启动
    def startupEvent(self, i, IP, appkey):
        s = json.dumps({
            'what': 'startup',
            'appid': appkey,
            'context': {
                '_deviceid': i,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_timestamp': current_time_13,
                '_gaid': i
            },
            'when': when,
            'ds': ds,
            'who': 'lxx_test'
            })
        p = requests.post(url=URL, data=s, headers=headers, timeout=10)
        print("启动"+p.text)


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

            },
            '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("注册时间戳"+current_time_13)
        print("注册"+p.text)


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

            },
            '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)


    #付费

    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,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_timestamp': current_time_13,
                '_transactionid':transactionid,
                '_paymenttype': paymenttype,
                '_ryos': ryos,
                '_currencytype': currencytype,
                '_currencyamount': currencyamount,
                '_gaid': i

            },
            '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 payorder (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,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_timestamp': current_time_13,
                '_transactionid': transactionid,
                '_paymenttype': paymenttype,
                '_currencytype': currencytype,
                '_ryos': ryos,
                '_currencyamount': currencyamount,
                '_gaid': i

            },
            '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 ziding_event(self,i,IP,appkey,event):
        s = json.dumps({
            'what': event,
            'appid': appkey,
            'context': {
                '_deviceid': i,
                '_imei': i,
                '_androidid': i,
                '_ip': IP,
                '_ryos': ryos,
                #识别是否为自定义事件,默认传1
                '_isreyundefaultevent':'1',
                '_gaid': i
            },
            '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)