ttztsdk.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { sys } from "cc";
  2. export class ttZtSdk {
  3. private static SaveKey = "ttzt_ext"
  4. private static ReportURL = "https://api.zt.y73s.cn/api/v1/conversion"
  5. private static extend: { [k: string]: any } = {};
  6. public static get openid() { return this.extend && this.extend.openid }
  7. public static get clickid() { return this.extend && this.extend.clickid }
  8. public static reportActive() {
  9. if (typeof window.tt === 'undefined' || !tt) {
  10. console.log("#### ttZtSdk #### 非字节平台不初始化")
  11. return;
  12. }
  13. console.log("#### ttZtSdk #### 初始化")
  14. // 加载历史记录
  15. let savedata = sys.localStorage.getItem(this.SaveKey);
  16. console.log("#### ttZtSdk #### 读取旧数据", savedata)
  17. if (savedata) {
  18. this.extend = JSON.parse(savedata)
  19. }
  20. // 如果已经激活, 不重复上报
  21. if (ttZtSdk.openid) {
  22. console.log("#### ttZtSdk #### 激活已经上报,不重复上报, openid:", ttZtSdk.openid, "clickid:", ttZtSdk.clickid)
  23. return;
  24. }
  25. tt.login({
  26. force: true,
  27. success: (res: any) => {
  28. console.debug(`#### ttZtSdk #### login 调用成功code:${res.code} a_code:${res.anonymousCode}`);
  29. let options = tt.getLaunchOptionsSync();
  30. let queryStr = JSON.stringify(options.query)
  31. let appid = options.extra.appId || options.query.appId
  32. console.debug("#### ttZtSdk #### appid:", appid, "query:", queryStr);
  33. tt.request({
  34. url: ttZtSdk.ReportURL,
  35. data: {
  36. code: res.code,
  37. anonvmous_code: res.anonymousCode,
  38. appid: appid, // 请求来源:appid
  39. active_data: queryStr, // 激活相关数据 json字符串
  40. },
  41. header: { "content-type": "application/json" },
  42. method: "POST",
  43. success: (res: any) => {
  44. console.log("#### ttZtSdk #### 数据上报成功", res.data)
  45. if (res.data.code == 0) {
  46. ttZtSdk.extend = {}
  47. ttZtSdk.extend.openid = res.data.data.openid;
  48. ttZtSdk.extend.clickid = res.data.data.clickid;
  49. let extStr = JSON.stringify(ttZtSdk.extend)
  50. console.log("#### ttZtSdk #### ext:", extStr)
  51. sys.localStorage.setItem(this.SaveKey, extStr)
  52. }
  53. },
  54. fail: (res: any) => {
  55. console.log("#### ttZtSdk #### 数据上报失败", res.errMsg)
  56. },
  57. })
  58. },
  59. fail: (res: any) => {
  60. console.debug(`#### ttZtSdk #### login调用失败`, res);
  61. },
  62. });
  63. }
  64. }