test5.vue 2.9 KB

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