test3.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="container" v-html="html"></view>
  3. </template>
  4. <script>
  5. import { getSignPackage } from '@/apis/app.js';
  6. export default {
  7. name: 'ShareIndex',
  8. data() {
  9. return {
  10. html: '',
  11. username: 'gh_170e5722c167',
  12. path: 'pages/index/index', //争霸赛小程序初始进入路径
  13. }
  14. },
  15. onReady() {
  16. this.signInit();
  17. // 微信 config 信息验证后,渲染开放标签
  18. uni.$on('jweixinReady', () => {
  19. this.init()
  20. })
  21. },
  22. methods: {
  23. //进入争霸赛按钮
  24. init() {
  25. this.html =
  26. `<wx-open-launch-weapp id="launch-btn" username="${this.username}" path="${this.path}">
  27. <template>
  28. <style>
  29. .open_img {
  30. width:50px;
  31. height:50px;
  32. color: #f30;
  33. }
  34. </style>
  35. <div class="open_img">测试</div>
  36. </template>
  37. </wx-open-launch-weapp>`;
  38. console.log('html', this.html)
  39. },
  40. //进入争霸赛小程序
  41. signInit() {
  42. let script = document.createElement('script')
  43. script.type = 'text/javascript'
  44. script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
  45. document.getElementsByTagName('head')[0].appendChild(script)
  46. let _this = this;
  47. let link = window.location.href;
  48. getSignPackage({
  49. url: link
  50. }).then(res => {
  51. if (res.code == 200) {
  52. let data = JSON.parse(res.data);
  53. // 判断是否支持分享
  54. wx.config({
  55. debug: false, //开启debug模式.正式环境设置为false,测试环境设置为true
  56. appId: data.appId,
  57. timestamp: data.timestamp,
  58. nonceStr: data.nonceStr,
  59. signature: data.signature,
  60. jsApiList: ['wx-open-launch-weapp'], //根据需要看需要哪些SDK的功能
  61. openTagList: ['wx-open-launch-weapp'] // 跳转小程序时必填
  62. });
  63. wx.ready(() => {
  64. uni.showToast({
  65. title: '获取成功',
  66. })
  67. uni.$emit('jweixinReady', res)
  68. this.$nextTick(() => {
  69. uni.showToast({
  70. title: '拿到了',
  71. })
  72. let btn = document.getElementById('launch-btn');
  73. btn.addEventListener('launch', e => {
  74. uni.showToast({
  75. title: '小程序打开成功',
  76. })
  77. console.log('success');
  78. });
  79. btn.addEventListener('error', e => {
  80. uni.showToast({
  81. title: '小程序打开失败',
  82. })
  83. console.log('fail', e.detail);
  84. });
  85. });
  86. })
  87. wx.error(err => {
  88. uni.showToast({
  89. title: '获取失败',
  90. })
  91. console.log(err, 'err')
  92. uni.showModal({
  93. content: err || '失败',
  94. showCancel: false
  95. })
  96. });
  97. } else {
  98. uni.showModal({
  99. content: res.msg || '分享失败',
  100. showCancel: false
  101. })
  102. }
  103. });
  104. },
  105. }
  106. }
  107. </script>
  108. <style scoped lang="scss">
  109. #share_container {
  110. width: 100%;
  111. height: 100%;
  112. display: flex;
  113. justify-content: space-between;
  114. flex-direction: column;
  115. }
  116. #imgWrapper {
  117. flex: 1;
  118. @include flex();
  119. .showCan {
  120. width: 680rpx;
  121. height: 960rpx;
  122. border-radius: 14rpx;
  123. box-shadow:0px 0px 23px 2px rgba(194,133,58,0.5);
  124. }
  125. }
  126. .sharetext {
  127. font-size: 80rpx;
  128. text-align: center;
  129. margin: 8rpx 0;
  130. box-sizing: border-box;
  131. padding: 30rpx 24rpx;
  132. }
  133. </style>