game.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {
  2. init,
  3. authLogin,
  4. share,
  5. onShareAppMessage,
  6. reportRole,
  7. payment,
  8. getCurrentUserInfo
  9. } from "./dist/gameSdk.1.0.0.js"
  10. // 初始化skd
  11. (async () => {
  12. try {
  13. let data = {
  14. version: "1.0.1", // 游戏版本
  15. mini_program_id: "", //测试号id
  16. game_id: "", // 游戏id
  17. game_name: "测试游戏", // 游戏名称
  18. };
  19. // 静默授权
  20. const res = await init(data);
  21. console.log("init", res);
  22. tt.showToast({
  23. title: "进入游戏成功",
  24. });
  25. } catch (error) {
  26. }
  27. })();
  28. const handleLogin = async () => {
  29. const res = await authLogin()
  30. console.log("登录返回", res)
  31. }
  32. // 角色上报
  33. const handleRoleDataReport = async () => {
  34. const res = await reportRole({
  35. data_type: 1,
  36. server_id: 1,
  37. server_name: "测试服务器",
  38. role_id: "1",
  39. role_name: "测试角色",
  40. role_level: 1,
  41. })
  42. console.log("reportRole", res);
  43. }
  44. //主动分享
  45. const handleShareAppMessage = async () => {
  46. try {
  47. let info = {
  48. title: "分享测试",
  49. imageUrl: "https://xxxx.com/hd/img/632-2.jpg",
  50. ext: "&ceshi=linquan321",
  51. };
  52. await share(info);
  53. tt.showToast({
  54. title: "分享成功",
  55. });
  56. } catch (error) {
  57. console.log(error);
  58. }
  59. };
  60. //主动监听
  61. const handleOnShareAppMessage = () => {
  62. onShareAppMessage({
  63. title: "测试",
  64. imageUrl: "https://xxxx.com/hd/img/632-2.jpg",
  65. ext: "&ceshi=test",
  66. });
  67. };
  68. // 支付
  69. const handleMakePayment = async () => {
  70. let params = {
  71. amt: 1,
  72. cp_order_id: new Date().getTime(),
  73. server_id: 1,
  74. server_name: "测试1服",
  75. role_id: "10000111",
  76. role_name: "小林",
  77. role_level: 1,
  78. ext: "aaaaaaa",
  79. product_id: "-1",
  80. product_name: "钻石",
  81. };
  82. await payment(params)
  83. }
  84. // 获取当前用户信息
  85. const handleLoginUserInfo = async () => {
  86. const res = await getCurrentUserInfo()
  87. console.log("getCurrentUserInfo", res)
  88. }
  89. function newButtonMap(name, callback, x, y, w = 200, h = 30) {
  90. return {
  91. name: name,
  92. callback: callback,
  93. x: x,
  94. y: y,
  95. w: w,
  96. h: h,
  97. };
  98. }
  99. let buttonList = [
  100. newButtonMap("登录", handleLogin, 10, 60),
  101. newButtonMap("上报用户角色", handleRoleDataReport, 10, 110),
  102. newButtonMap("生成1元订单", handleMakePayment, 10, 160),
  103. newButtonMap("主动分享", handleShareAppMessage, 10, 260),
  104. newButtonMap("获取平台登陆用户信息", handleLoginUserInfo, 10, 210),
  105. newButtonMap("监听分享传参", handleOnShareAppMessage, 10, 310),
  106. ];
  107. const canvas = tt.createCanvas();
  108. const ctx = canvas.getContext("2d");
  109. ctx.fillStyle = "#ff0000";
  110. ctx.font = "14px Arial";
  111. ctx.strokeStyle = "#ff0000";
  112. for (let k in buttonList) {
  113. ctx.fillText(buttonList[k].name, 20, buttonList[k].y + 20);
  114. }
  115. tt.onTouchStart(function (a) {
  116. let cur = a.touches[0];
  117. for (let k in buttonList) {
  118. if (
  119. cur.clientX >= buttonList[k].x &&
  120. cur.clientX <= buttonList[k].x + buttonList[k].w &&
  121. cur.clientY >= buttonList[k].y &&
  122. cur.clientY <= buttonList[k].y + buttonList[k].h
  123. ) {
  124. buttonList[k].callback();
  125. }
  126. }
  127. });