index.vue 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div>
  3. <div v-text="weappHtml"></div>
  4. <!-- <wx-open-launch-weapp style="display: inline-block; border: none" id="launch-btn" username="gh_a994fd2f498e" path="pages/index/index.html?living_code=LIVING_CODE">
  5. <div v-is="'script'" type="text/wxtag-template">
  6. <div v-is="'style'">.openbtn{background-color:#38f;color:#f40;width:100px;height:40px;line-height:40px;}</div>
  7. <div class="openbtn">跳转小程序</div>
  8. </div>
  9. </wx-open-launch-weapp> -->
  10. </div>
  11. </template>
  12. <script lang="ts">
  13. import { defineComponent, ref } from "vue";
  14. import wx from "weixin-js-sdk-ts";
  15. import { openApi } from "@/api/index";
  16. export default defineComponent({
  17. setup() {
  18. openApi
  19. .jspapi({
  20. url: encodeURIComponent(window.location.href.split('#')[0]+'/'),
  21. })
  22. .then((res) => {
  23. let wxConfig = {
  24. debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
  25. appId: res.data.data.appId as string, // 必填,公众号的唯一标识
  26. timestamp: res.data.data.timestamp as number, // 必填,生成签名的时间戳
  27. nonceStr: res.data.data.nonceStr as string, // 必填,生成签名的随机串
  28. signature: res.data.data.signature as string, // 必填,签名
  29. jsApiList: ["previewImage"], // 必填,需要使用的JS接口列表
  30. openTagList: ["wx-open-launch-weapp"], // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
  31. };
  32. console.log(wxConfig);
  33. wx.config({
  34. debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
  35. appId: res.data.data.appId as string, // 必填,公众号的唯一标识
  36. timestamp: res.data.data.timestamp as number, // 必填,生成签名的时间戳
  37. nonceStr: res.data.data.nonceStr as string, // 必填,生成签名的随机串
  38. signature: res.data.data.signature as string, // 必填,签名
  39. jsApiList: ["previewImage", "scanQRCode"], // 必填,需要使用的JS接口列表
  40. openTagList: [], // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
  41. });
  42. wx.error(({ errMsg }) => {
  43. weappHtml.value = errMsg;
  44. wx.ready(() => {
  45. wx.scanQRCode({
  46. needResult: 1,
  47. scanType: ["qrCode", "barCode"],
  48. success(res) {},
  49. });
  50. });
  51. });
  52. });
  53. let isWxReady = ref(false);
  54. let weappHtml = ref("");
  55. return {
  56. weappHtml,
  57. isWxReady,
  58. };
  59. },
  60. methods: {
  61. handleErrorFn(e: Event) {
  62. console.log("fail", e.detail);
  63. },
  64. handleLaunchFn(e: Event) {
  65. console.log("success");
  66. },
  67. },
  68. mounted() {},
  69. });
  70. </script>
  71. <style lang="scss" scoped>
  72. .btn-open-weapp {
  73. width: 100px;
  74. height: 30px;
  75. border-radius: 25px;
  76. background: #085b27;
  77. display: block;
  78. border: 0px;
  79. font-size: 14px;
  80. color: white;
  81. }
  82. </style>