import requests
import json
import time
import random

headers = {'Content-Type': 'application/json'}
# 测试环境
URL = 'http://52.80.137.106:8080/receive/tkio/event'

# 线上环境
# URL = 'http://tlog.trackingio.com/receive/tkio/event'
# URL = 'http://log.trackingio.com/receive/tkio/event'

# 时间和日期
when = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
# 日期
ds = time.strftime('%Y-%m-%d', time.localtime(time.time()))

# 访问时常,以s计时
duration = '5'
# 订单id
transactionid = '34232323'
# 支付类型   支付宝(alipay),银联(unionpay),微信支付(weixinpay),易宝支付(yeepay)
paymenttype = 'alipay'
# 货币类型
currencytype = 'CNY'
# 支付金额
currencyamount = '45'
# 是否热云默认,标记为自定义事件
isreyundefaultevent = ''


class allEvent:
    # 打开小程序
    def openxiaochengxu(self, i, IP, cid, surl, appid):
        s = json.dumps({
            "what": "launch",
            "appid": appid,
            "context": {
                "_cid": cid,
                "_ip": IP,
                "_deviceid": i,
                "_openid": i,
                "_campaignid": surl,
                "_sceneid": "unknown",
                "_ry_adgroup_id": "111",
                "_ry_adplan_id": "111",
                "_ry_adcreative_id": "111",
                # "advertiser_id": "aaa",
                # "ad_id": "aaa",
                # "creative_id": "aaa",
                "_apptype": "wx"
            },
            "where": "launch",
            "when": when,
            "ds": ds,
            "who": "lxx-test"
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)
        print(p.url)
        print(i)

        # pv

    def pviaochengxu(self, i, IP, cid, surl, appid):
        s = json.dumps({
            "what": "pageview",
            "appid": appid,
            "context": {
                "_cid": cid,
                "_ip": IP,
                "_deviceid": i,
                "_openid": i,
                "_campaignid": surl,
                "_sceneid": "unknown",
                "_apptype": "wx"
            },
            "where": "pageview",
            "when": when,
            "ds": ds,
            "who": "lxx-test"
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)

    # hide
    def hidechengxu(self, i, IP, cid, surl, appid):
        s = json.dumps({
            "what": "hide",
            "appid": appid,
            "context": {
                "_cid": cid,
                "_ip": IP,
                "_deviceid": i,
                "_openid": i,
                "_campaignid": surl,
                "_sceneid": "unknown",
                "_apptype": "wx",
                "_num_pages": "5",
                "_duration": "10"
            },
            "where": "startup",
            "when": when,
            "ds": ds,
            "who": "lxx-test"
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)

    # 注册
    def regedchengxu(self, i, IP, cid, surl, appid):
        s = json.dumps({
            "what": "reged",
            "appid": appid,
            "context": {
                "_cid": cid,
                "_ip": IP,
                "_deviceid": i,
                "_openid": i,
                "_campaignid": surl,
                "_sceneid": "unknown",
                "_apptype": "wx",
                "_num_pages": "5",
                "_duration": "10"
            },
            "where": "reged",
            "when": when,
            "ds": ds,
            "who": "lxx-test"
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)

        # 付费
    def paymentchengxu(self, i, IP, cid, surl, appid):
        transactionid = ''.join(str(random.choice(range(10))) for _ in range(19))
        s = json.dumps({
            "what": "payment",
            "appid": appid,
            "context": {
                "_cid": cid,
                "_ip": IP,
                "_deviceid": i,
                "_openid": i,
                "_campaignid": surl,
                "_sceneid": "unknown",
                "_apptype": "wx",
                "_num_pages": "5",
                '_transactionid': transactionid,
                '_paymenttype': paymenttype,
                '_currencyamount': '90',
                "_currencytype":currencytype,
                "_duration": "10"
            },
            "where": "payment",
            "when": when,
            "ds": ds,
            "who": "lxx-test"
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)


     # loggedin
    def loggedinchengxu(self, i, IP, cid, surl, appid):
        s = json.dumps({
            "what": "loggedin",
            "appid": appid,
            "context": {
                "_cid": cid,
                "_ip": IP,
                "_deviceid": i,
                "_openid": i,
                "_campaignid": surl,
                "_sceneid": "unknown",
                "_apptype": "wx",
                "_num_pages": "5",
                "_duration": "10"
            },
            "where": "loggedin",
            "when": when,
            "ds": ds,
            "who": "lxx-test"
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)


     # 自定义事件
    def eventchengxu(self, i, IP, cid, surl, appid):
        s = json.dumps({
            "what": "event_10",
            "appid": appid,
            "context": {
                "_cid": cid,
                "_ip": IP,
                "_deviceid": i,
                "_openid": i,
                "_campaignid": surl,
                "_sceneid": "unknown",
                "_apptype": "wx",
                "_num_pages": "5",
                '_isreyundefaultevent': '1',
                "_duration": "10"
            },
            "where": "event_10",
            "when": when,
            "ds": ds,
            "who": "lxx-test"
        })
        p = requests.post(url=URL, data=s, headers=headers, timeout=5)
        print(p.text)