game.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import {
  2. init,
  3. loginUserInfo,
  4. login,
  5. dataReport,
  6. makePayment,
  7. shareAppMessage,
  8. addShortcut,
  9. checkShortcut,
  10. onShareAppMessage,
  11. navigateToScene,
  12. openCustomerServiceConversation,
  13. requestFeedSubscribe,
  14. checkFeedSubscribeStatus,
  15. reportScene,
  16. getFeedLaunchParams
  17. } from "index";
  18. // from './dist/dyMiniGameSdk.1.0.0.js'
  19. // 初始化skd
  20. (async () => {
  21. try {
  22. let data = {
  23. app_version: "1.0.0",
  24. config_id: "tta0dbd6663eb8fad902",
  25. game_name: "暮影战神",
  26. };
  27. // 静默授权
  28. const res = await init(data);
  29. console.log("初始化")
  30. console.log(res);
  31. tt.showToast({
  32. title: "进入游戏成功",
  33. });
  34. } catch (error) {
  35. console.log(error);
  36. }
  37. })();
  38. // 下单
  39. const handleMakePayment = async () => {
  40. try {
  41. let info = {
  42. amt: 1,
  43. server_id: 1,
  44. server_name: "测试1服",
  45. role_id: "10000111",
  46. role_name: "小林",
  47. role_level: 1,
  48. ext: "aaaaaaa",
  49. product_id: "001",
  50. product_name: "测试商品",
  51. };
  52. makePayment(info)
  53. .then((res) => {
  54. console.log("支付调用结果:", res);
  55. })
  56. .catch((error) => {
  57. console.log(error);
  58. });
  59. } catch (error) {
  60. console.log("支付调用结果error:", error);
  61. }
  62. };
  63. //角色上报按钮
  64. const handleDataReport = async () => {
  65. try {
  66. let info = {
  67. data_type: 2,
  68. server_id: 1,
  69. server_name: "测试1服",
  70. role_id: "10000111",
  71. role_name: "小林",
  72. role_level: 1,
  73. role_currency: 1000,
  74. };
  75. const res = await dataReport(info);
  76. tt.showToast({
  77. title: "角色上报成功",
  78. });
  79. console.log("角色上报")
  80. console.log(res);
  81. } catch (error) {
  82. console.log(error);
  83. }
  84. };
  85. /**
  86. * 2025年05月06日
  87. * 新增登陆方法
  88. */
  89. const handleLogin = async ()=>{
  90. try {
  91. const res = await login();
  92. console.log("登录")
  93. console.log(res);
  94. tt.showToast({
  95. title: "获取成功",
  96. });
  97. } catch (error) {
  98. console.log(error);
  99. }
  100. }
  101. //获取平台登录用户信息
  102. const handleLoginUserInfo = async () => {
  103. try {
  104. const res = await loginUserInfo();
  105. console.log(res);
  106. tt.showToast({
  107. title: "获取成功",
  108. });
  109. } catch (error) {
  110. console.log(error);
  111. }
  112. };
  113. //主动分享
  114. const handleShareAppMessage = async () => {
  115. try {
  116. let info = {
  117. title: "分享测试",
  118. imageUrl: "https://app.hainanruiyu.com/hd/img/632-2.jpg",
  119. ext: "&ceshi=linquan321",
  120. channel: "",
  121. templateId: "",
  122. };
  123. await shareAppMessage(info);
  124. tt.showToast({
  125. title: "分享成功",
  126. });
  127. } catch (error) {
  128. console.log(error);
  129. }
  130. };
  131. //主动监听
  132. const handleOnShareAppMessage = () => {
  133. onShareAppMessage({
  134. title: "测试",
  135. imageUrl: "https://app.hainanruiyu.com/hd/img/632-2.jpg",
  136. ext: "&ceshi=test",
  137. });
  138. };
  139. // 将小游戏快捷方式添加到手机桌面上
  140. const handleAddShortcut = async () => {
  141. checkShortcut()
  142. .then((res) => {
  143. console.log("+++++++++++++++++++++++checkShortcut_res", res);
  144. })
  145. .catch((error) => {
  146. console.log(error);
  147. });
  148. };
  149. // 侧边栏能力
  150. const handleNavigateToScene = async () => {
  151. navigateToScene()
  152. .then((res) => {
  153. console.log("navigateToScene then res:", res);
  154. })
  155. .catch((error) => {
  156. console.log(error);
  157. });
  158. };
  159. // IM客服能力
  160. const handleService = async () => {
  161. openCustomerServiceConversation()
  162. .then((res) => {
  163. console.log("openCustomerServiceConversation then res:", res);
  164. })
  165. .catch((error) => {
  166. console.log(error);
  167. });
  168. };
  169. // 请求推荐流直出订阅
  170. const handleRequestFeedSubscribe = async () => {
  171. requestFeedSubscribe({ scene: 3})
  172. .then((res) => {
  173. console.log("cp调用【请求推荐流直出订阅】res: ", res);
  174. })
  175. .catch((error) => {
  176. console.log(error);
  177. });
  178. }
  179. // 检查推荐流直出订阅状态
  180. const handleCheckFeedSubscribeStatus = async () => {
  181. checkFeedSubscribeStatus({ scene: 3 })
  182. .then((res) => {
  183. console.log("checkFeedSubscribeStatus then res:", res);
  184. })
  185. .catch((error) => {
  186. console.log(error);
  187. });
  188. }
  189. reportScene()
  190. getFeedLaunchParams().then(res=>{
  191. console.log("CP调用启动参数res:", res)
  192. })
  193. // =================== 按钮功能 UI
  194. function newButtonMap(name, callback, x, y, w = 200, h = 30) {
  195. return {
  196. name: name,
  197. callback: callback,
  198. x: x,
  199. y: y,
  200. w: w,
  201. h: h,
  202. };
  203. }
  204. let buttonList = [
  205. newButtonMap("登陆", handleLogin, 10, 150),
  206. newButtonMap("获取平台登陆用户信息", handleLoginUserInfo, 10, 200),
  207. newButtonMap("上报用户角色", handleDataReport, 10, 250),
  208. newButtonMap("生成1元订单", handleMakePayment, 10, 300),
  209. newButtonMap("主动分享", handleShareAppMessage, 10, 350),
  210. newButtonMap("监听分享传参", handleOnShareAppMessage, 10, 400),
  211. newButtonMap("添加到桌面", handleAddShortcut, 10, 450),
  212. newButtonMap("侧边栏能力", handleNavigateToScene, 10, 500),
  213. newButtonMap("IM客服能力", handleService, 10, 550),
  214. newButtonMap("请求推荐流直出订阅", handleRequestFeedSubscribe, 10, 600),
  215. newButtonMap("检查推荐流直出订阅状态", handleCheckFeedSubscribeStatus, 10, 650),
  216. // newButtonMap("监听推荐流直出订阅状态变化", handleOnFeedStatusChange, 10, 700),
  217. // newButtonMap("取消监听推荐流直出订阅状态变化", handleOffFeedStatusChange, 10, 750),
  218. // newButtonMap("存储推荐流直出订阅数据", handleStoreFeedData, 10, 800),
  219. // newButtonMap("获取游戏是否直玩就绪状态数据", handleGetFeedData, 10, 850),
  220. ];
  221. const canvas = tt.createCanvas();
  222. const ctx = canvas.getContext("2d");
  223. ctx.fillStyle = "#ff0000";
  224. ctx.font = "14px Arial";
  225. ctx.strokeStyle = "#ff0000";
  226. for (let k in buttonList) {
  227. ctx.fillText(buttonList[k].name, 20, buttonList[k].y + 20);
  228. }
  229. tt.onTouchEnd(function (a) {
  230. let cur = a.changedTouches[0];
  231. for (let k in buttonList) {
  232. if (
  233. cur.clientX >= buttonList[k].x &&
  234. cur.clientX <= buttonList[k].x + buttonList[k].w &&
  235. cur.clientY >= buttonList[k].y &&
  236. cur.clientY <= buttonList[k].y + buttonList[k].h
  237. ) {
  238. buttonList[k].callback.call(this);
  239. }
  240. }
  241. });