index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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),
  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"], // 必填,需要使用的JS接口列表
  40. openTagList: [], // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
  41. });
  42. wx.error(({ errMsg }) => {
  43. weappHtml.value = errMsg;
  44. });
  45. });
  46. let isWxReady = ref(false);
  47. let weappHtml = ref("");
  48. return {
  49. weappHtml,
  50. isWxReady,
  51. };
  52. },
  53. methods: {
  54. handleErrorFn(e: Event) {
  55. console.log("fail", e.detail);
  56. },
  57. handleLaunchFn(e: Event) {
  58. console.log("success");
  59. },
  60. },
  61. mounted() {},
  62. });
  63. </script>
  64. <style lang="scss" scoped>
  65. .btn-open-weapp {
  66. width: 100px;
  67. height: 30px;
  68. border-radius: 25px;
  69. background: #085b27;
  70. display: block;
  71. border: 0px;
  72. font-size: 14px;
  73. color: white;
  74. }
  75. </style>