index.vue 3.1 KB

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