game.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import {
  2. init,
  3. loginUserInfo,
  4. dataReport,
  5. makePayment,
  6. shareAppMessage,
  7. payConfig,
  8. navigateToMiniProgram,
  9. getBindPhoneCode,
  10. bindPhone,
  11. onShareAppMessage,
  12. requestSubscribeMessage,
  13. triggerSubscribe,
  14. createAd,
  15. login,
  16. secCheckText,
  17. secCheckMedia,
  18. } from "./dist/rywxsdk.1.0.3.js";
  19. // } from "./index";
  20. // 初始化skd
  21. (async () => {
  22. try {
  23. let data = {
  24. app_version: "1.0.1",
  25. config_id: "wxa0167e87cc5efae3", //测试号id
  26. game_name: "逍遥志",
  27. };
  28. // 静默授权
  29. const res = await init(data);
  30. console.log(res);
  31. // 非静默授权
  32. // const buttonStyle = {
  33. // type: 'text',
  34. // text: '点击进入游戏',
  35. // style: {
  36. // left: 10,
  37. // top: 76,
  38. // width: 200,
  39. // height: 40,
  40. // lineHeight: 40,
  41. // backgroundColor: '#ff0000',
  42. // color: '#FFFFFF',
  43. // textAlign: 'center',
  44. // fontSize: 16,
  45. // borderRadius: 4
  46. // },
  47. // withCredentials: true
  48. // }
  49. // await init(data,buttonStyle);
  50. wx.showToast({
  51. title: "进入游戏成功",
  52. });
  53. //拉起订阅
  54. await requestSubscribeMessage();
  55. //触发订阅
  56. await triggerSubscribe("login");
  57. } catch (error) {
  58. console.log(error);
  59. }
  60. })();
  61. //角色上报按钮
  62. const handleDataReport = async () => {
  63. try {
  64. let info = {
  65. data_type: 2,
  66. server_id: 1,
  67. server_name: "测试1服",
  68. role_id: "10000111",
  69. role_name: "小林",
  70. role_level: 1,
  71. role_currency: "",
  72. game_fee: ""
  73. };
  74. const res = await dataReport(info);
  75. wx.showToast({
  76. title: "角色上报成功",
  77. });
  78. console.log(res);
  79. } catch (error) {
  80. console.log(error);
  81. }
  82. };
  83. //展示下单按钮
  84. const handleMakePayment = async () => {
  85. try {
  86. let info = {
  87. amt: 1,
  88. server_id: 1,
  89. server_name: "测试1服",
  90. role_id: "10000111",
  91. role_name: "小林",
  92. role_level: 1,
  93. ext: String(new Date().getTime()),
  94. product_id: 1,
  95. product_name: "测试商品"
  96. };
  97. wx.showToast({
  98. title: "拉起订单成功",
  99. });
  100. makePayment(info)
  101. .then((res) => {
  102. if (res && Object.keys(res).length) {
  103. console.log("下单类型返回", res);
  104. }
  105. })
  106. .catch((error) => {
  107. console.log(error);
  108. // 如果是节流错误,显示友好提示
  109. if (error.message === "请求过于频繁,请稍后再试") {
  110. wx.showToast({
  111. title: "操作过于频繁,请稍后再试",
  112. icon: "none",
  113. });
  114. }
  115. });
  116. } catch (error) {
  117. console.log(error);
  118. }
  119. };
  120. const handleMakePayment2 = async () => {
  121. try {
  122. let info = {
  123. amt: 2,
  124. server_id: 1,
  125. server_name: "测试1服",
  126. role_id: "10000111",
  127. role_name: "小林",
  128. role_level: 1,
  129. product_id: 1,
  130. product_name: "测试1服",
  131. ext: "aaaaaaa",
  132. };
  133. wx.showToast({
  134. title: "拉起订单成功",
  135. });
  136. makePayment(info)
  137. .then((res) => {
  138. if (res && Object.keys(res).length) {
  139. console.log("下单类型返回", res);
  140. }
  141. })
  142. .catch((error) => {
  143. console.log(error);
  144. // 如果是节流错误,显示友好提示
  145. if (error.message === "请求过于频繁,请稍后再试") {
  146. wx.showToast({
  147. title: "操作过于频繁,请稍后再试",
  148. icon: "none",
  149. });
  150. }
  151. });
  152. } catch (error) {
  153. console.log(error);
  154. }
  155. };
  156. //主动分享
  157. const handleShareAppMessage = async () => {
  158. try {
  159. let info = {
  160. title: "分享测试",
  161. imageUrl: "https://api-user.91fengwan.com/hd/img/632-2.jpg",
  162. ext: "&ceshi=linquan321",
  163. };
  164. await shareAppMessage(info);
  165. wx.showToast({
  166. title: "分享成功",
  167. });
  168. } catch (error) {
  169. console.log(error);
  170. }
  171. };
  172. // 登录
  173. const handleLogin = async () => {
  174. try {
  175. const res = await login();
  176. console.log("登录返回res", res);
  177. } catch (error) {
  178. console.log(error);
  179. }
  180. };
  181. //获取平台登录用户信息
  182. const handleLoginUserInfo = async () => {
  183. try {
  184. const res = await loginUserInfo();
  185. console.log(res);
  186. wx.showToast({
  187. title: "获取成功",
  188. });
  189. } catch (error) {
  190. console.log(error);
  191. }
  192. };
  193. //IOS 获取获取是否开启支付接口
  194. const handlePayConfig = async () => {
  195. try {
  196. const res = await payConfig()
  197. .then((res) => { })
  198. .catch((error) => { });
  199. console.log(res);
  200. wx.showToast({
  201. title: "获取成功",
  202. });
  203. } catch (error) {
  204. console.log(error);
  205. }
  206. };
  207. //展示跳转小程序按钮
  208. const handleNavigateToMiniProgram = async () => {
  209. try {
  210. await navigateToMiniProgram();
  211. wx.showToast({
  212. title: "打开成功",
  213. });
  214. } catch (error) {
  215. console.log(error);
  216. }
  217. };
  218. //展示获取短信
  219. const handleGetBindPhoneCode = async () => {
  220. try {
  221. const res = await getBindPhoneCode({
  222. phone: "15975492315",
  223. });
  224. console.log(res)
  225. wx.showToast({
  226. title: "发送成功",
  227. });
  228. } catch (error) {
  229. wx.showToast({
  230. title: error.msg,
  231. });
  232. console.log(error);
  233. }
  234. };
  235. //展示绑定手机
  236. const handleBindPhoneCode = async () => {
  237. try {
  238. let info = {
  239. phone: "15975492315",
  240. code: "101344",
  241. };
  242. await bindPhone(info);
  243. wx.showToast({
  244. title: "绑定成功",
  245. });
  246. } catch (error) {
  247. wx.showToast({
  248. title: error.msg,
  249. });
  250. console.log(error);
  251. }
  252. };
  253. //主动监听
  254. const handleOnShareAppMessage = () => {
  255. onShareAppMessage({
  256. title: "测试",
  257. imageUrl: "https://api-user.91fengwan.com/hd/img/632-2.jpg",
  258. ext: "&ceshi=test",
  259. });
  260. };
  261. //获取订阅
  262. const handleRequestSubscribeMessage = async () => {
  263. try {
  264. const res = await requestSubscribeMessage();
  265. // await triggerSubscribe('login');
  266. wx.showToast({
  267. title: "获取成功",
  268. });
  269. // console.log(res)
  270. } catch (error) {
  271. console.log(error);
  272. }
  273. };
  274. //创建广告
  275. const handleCreateAd = async () => {
  276. try {
  277. const res = await createAd({
  278. ad_type: "video",
  279. task: "123",
  280. });
  281. console.log(res);
  282. } catch (error) {
  283. console.log(error);
  284. }
  285. };
  286. //文本内容安全检测
  287. const handleSecCheckText = async () => {
  288. try {
  289. const res = await secCheckText({ content: "毛泽东真厉害", scene: 1 });
  290. console.log(res);
  291. } catch (error) {
  292. console.log(error);
  293. }
  294. };
  295. //多媒体内容安全检测
  296. const handleSecCheckMedia = async () => {
  297. try {
  298. const res = await secCheckMedia({
  299. media_url: "https://api-user.91fengwan.com/hd/img/632-2.jpg",
  300. media_type: "image",
  301. scene: "123",
  302. });
  303. console.log(res);
  304. } catch (error) {
  305. console.log(error);
  306. }
  307. };
  308. function newButtonMap(name, callback, x, y, w = 200, h = 30) {
  309. return {
  310. name: name,
  311. callback: callback,
  312. x: x,
  313. y: y,
  314. w: w,
  315. h: h,
  316. };
  317. }
  318. let buttonList = [
  319. newButtonMap("登录", handleLogin, 10, 60),
  320. newButtonMap("上报用户角色", handleDataReport, 10, 450),
  321. newButtonMap("生成1元订单", handleMakePayment, 10, 500),
  322. newButtonMap("生成2元订单", handleMakePayment2, 10, 540),
  323. newButtonMap("主动分享", handleShareAppMessage, 10, 90),
  324. newButtonMap("获取平台登陆用户信息", handleLoginUserInfo, 10, 130),
  325. newButtonMap("IOS获取获取是否开启支付", handlePayConfig, 10, 170),
  326. newButtonMap("跳转小程序", handleNavigateToMiniProgram, 10, 210),
  327. newButtonMap("获取手机验证码", handleGetBindPhoneCode, 10, 250),
  328. newButtonMap("绑定手机", handleBindPhoneCode, 10, 290),
  329. newButtonMap("监听分享传参", handleOnShareAppMessage, 10, 330),
  330. newButtonMap("获取订阅", handleRequestSubscribeMessage, 10, 370),
  331. newButtonMap("创建广告", handleCreateAd, 10, 410),
  332. newButtonMap("文本内容安全检测", handleSecCheckText, 10, 600),
  333. newButtonMap("多媒体内容安全检测", handleSecCheckMedia, 10, 640),
  334. ];
  335. const canvas = wx.createCanvas();
  336. const ctx = canvas.getContext("2d");
  337. ctx.fillStyle = "#ff0000";
  338. ctx.font = "14px Arial";
  339. ctx.strokeStyle = "#ff0000";
  340. for (let k in buttonList) {
  341. // ctx.fillRech(0,0,200,40)
  342. // let path = new Path2D()
  343. // path.rect(buttonList[k].x, buttonList[k].y, buttonList[k].w, buttonList[k].h);
  344. // ctx.stroke(path)
  345. ctx.fillText(buttonList[k].name, 20, buttonList[k].y + 20);
  346. }
  347. wx.onTouchStart(function (a) {
  348. let cur = a.touches[0];
  349. for (let k in buttonList) {
  350. if (
  351. cur.clientX >= buttonList[k].x &&
  352. cur.clientX <= buttonList[k].x + buttonList[k].w &&
  353. cur.clientY >= buttonList[k].y &&
  354. cur.clientY <= buttonList[k].y + buttonList[k].h
  355. ) {
  356. buttonList[k].callback();
  357. }
  358. }
  359. });