uniapp 微信小程序 配置分享朋友和朋友圈

  1. 首先在小程序中配置微信分享,和微信朋友圈, onShareAppMessage, onShareTimeline 这两个API 和 onLoad 同级目录配置
  2. 在小程序每个页面都要设置这两个函数,修改内容比较心烦,不符合代码风格。可以通过mixins 混入的方式给每个页面设置分享的内容
  3. 在使用 mixins 的时候, 可以在全局混入, 也可以在每个页面中混入

配置分享的两个函数

 onShareAppMessage(res) {
    return {
        title: '',
        path: '',
        imageUrl: '',
    }
},
//2.分享到朋友圈
onShareTimeline(res) {
    return {
        title: '',
        path: '',
        imageUrl: '',
    }
},

每个页面中混入的方式

share.js
export default {
  onShareAppMessage: function () {
      const _this = this;
      const tx_title = '热门头像|个性头像|高清头像|海量资源 应用尽有 免费下载'
      const bz_title = '性感壁纸|美女壁纸|炫酷壁纸|省电壁纸|唯美壁纸海量资源  应用尽有 免费下载'
      const shareTitle = this.urlPath == 'bizhi' ? bz_title : tx_title
      return {
        title: shareTitle,
        desc: shareTitle,
        path: `/pages/${this.urlPath}/index?active=${_this.active}`, // 路径,传递参数到指定页面。
      }
    },
       /*分享朋友圈 */
   onShareTimeline: function() {
      const _this = this;
      const tx_title = '热门头像|个性头像|高清头像|海量资源 应用尽有 免费下载'
      const bz_title = '性感壁纸|美女壁纸|炫酷壁纸|省电壁纸|唯美壁纸海量资源  应用尽有 免费下载'
      const shareTitle = this.urlPath == 'bizhi' ? bz_title : tx_title
      return {
        title: shareTitle,
      }
    },
}

设置分享的页面
import share from '../share/index.js'
export default {
  mixins: [share],
}

全局设置分享

share.js
export default {
  data() {
      return {
          // 默认的全局分享内容
          share: {
              title: '国考|社保卡|驾驶证|简历|小二寸|小一寸|二寸|一寸,免费制作标准证电子证件照',
              path: '/pages/index/index',    // 全局分享的路径
              imageUrl: '../../static/banner1.png',    // 全局分享的图片(可本地可网络)
          }
      }
  },
  // 定义全局分享
  // 1.发送给朋友
  onShareAppMessage(res) {
      return {
          title: this.share.title,
          path: this.share.path,
          imageUrl: this.share.imageUrl,
      }
  },
  //2.分享到朋友圈
  onShareTimeline(res) {
      return {
          title: this.share.title,
          path: this.share.path,
          imageUrl: this.share.imageUrl,
      }
  },
}
main.js 
import share from './pages/share/index.js'
import Vue from 'vue'
Vue.mixin(share)

可以根据自己项目页面多少选择方式

下面是我的小程序体验码,希望能和大家共同学习进步

制作头像小程序

制作头像小程序

[项目代码] https://gitee.com/eyes-star/txy-openmp.git
制作头像小程序

[项目代码] https://gitee.com/eyes-star/zjz-openmp.git

posted on 2022-11-16 16:57  不锈钢子  阅读(312)  评论(0编辑  收藏  举报