game.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.0.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. try {
  84. await payment(params)
  85. } catch (error) {
  86. console.log('payment cancel or fail', error)
  87. }
  88. }
  89. // 获取当前用户信息
  90. const handleLoginUserInfo = async () => {
  91. const res = await getCurrentUserInfo()
  92. console.log("getCurrentUserInfo", res)
  93. }
  94. // 文本内容安全检测
  95. const handleSecCheckText = async () => {
  96. const res = await safeTextCheck({ content: "文本内容", scene: 1 })
  97. console.log(res)
  98. }
  99. function newButtonMap(name, callback, x, y, w = 200, h = 30) {
  100. return {
  101. name: name,
  102. callback: callback,
  103. x: x,
  104. y: y,
  105. w: w,
  106. h: h,
  107. };
  108. }
  109. let buttonList = [
  110. newButtonMap("登录", handleLogin, 10, 60),
  111. newButtonMap("上报用户角色", handleRoleDataReport, 10, 110),
  112. newButtonMap("生成1元订单", handleMakePayment, 10, 160),
  113. newButtonMap("主动分享", handleShareAppMessage, 10, 260),
  114. newButtonMap("获取平台登陆用户信息", handleLoginUserInfo, 10, 210),
  115. newButtonMap("监听分享传参", handleOnShareAppMessage, 10, 310),
  116. newButtonMap("文本内容安全检测", handleSecCheckText, 10, 360),
  117. ];
  118. const canvas = wx.createCanvas();
  119. const ctx = canvas.getContext("2d");
  120. ctx.fillStyle = "#ff0000";
  121. ctx.font = "14px Arial";
  122. ctx.strokeStyle = "#ff0000";
  123. for (let k in buttonList) {
  124. ctx.fillText(buttonList[k].name, 20, buttonList[k].y + 20);
  125. }
  126. wx.onTouchStart(function (a) {
  127. let cur = a.touches[0];
  128. for (let k in buttonList) {
  129. if (
  130. cur.clientX >= buttonList[k].x &&
  131. cur.clientX <= buttonList[k].x + buttonList[k].w &&
  132. cur.clientY >= buttonList[k].y &&
  133. cur.clientY <= buttonList[k].y + buttonList[k].h
  134. ) {
  135. buttonList[k].callback();
  136. }
  137. }
  138. });