game.js 3.3 KB

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