[小程序开发]发送给朋友和分享朋友圈功能实现

2024年3月20日 09:16 by wst

小程序

为了推广小程序,很多老板要求小程序可以发送给朋友,或者转发到朋友圈。

微信还真提供了这个功能,开发文档在这里

下面以实际例子来说明它的用法:

import { request } from "../../utils/request";
// 获取应用实例
const app = getApp()

Page({
  data: {
    promotion:{},
    detail: {},
    categories: []
  },
  // 事件处理函数
  viewDetail(e) {
    console.log("e:", e)
    wx.navigateTo({
      url: '../comDetail/comDetail?cid=' + e.currentTarget.dataset.id
    })
  },

  viewCenter() {
    wx.navigateTo({
      url: '../userCenter/userCenter'
    })
  },

  getPromotion(){
    request({
      url: "/mini/commodity/promotion/get"
    }).then(res => {
      console.log("promotion:", res)
      if (res.code == 1) {
        this.setData({
          promotion: res.data[0]
        })
      }
    })
  },

  getCategory(){
    request({
      url: "/mini/commodity/hot"
    }).then(res => {
      console.log("commodity:", res)
      if (res.code == 1) {
        this.setData({
          categories: res.data
        })
      }
    })
  },

  onLoad() {

    // 获取场景值
    var sence = wx.getLaunchOptionsSync()
    console.log("sence:", sence)
    if(sence.scene == 1154){
      console.log("来源于朋友圈分享")
      this.setData({
        promotion: {
          "id": 3,
          "name": "虔诚文化·DIY",
          "cat_id": 1,
          "pic_url": "https://candy.wanshitao.com/static/images/1709826116_gk%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20221205114848-3.jpg",
          "pic_list": ["https://candy.wanshitao.com/static/images/1709826116_gk%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20221205114848-3.jpg"],
          "content": "<p>伟大传经典之作</p>",
          "slogan": "福报,越来越好。",
          "status": 1,
          "create_time": "2023-02-22 09:42:43",
          "update_time": "2023-06-03 19:57:36",
        }
      })
    } else {
      this.getPromotion()
      this.getCategory()
    }


  },

  onShareAppMessage: function (res) {
    console.log('onShareAppMessage:',res);
    return {
      title: '越来越好',
      path: '/pages/index/index',
      imageUrl: 'https://candy.wanshitao.com/static/images/1709826116_gk%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20221205114848-3.jpg'
    }
  },

  onShareTimeline: function() {
    return {
      title: '美丽人生茶生活',
      query: 'suid='+(getApp().globalData.uid)+'&share=true',
      imageUrl: 'https://goods.wanshitao.com/static/images/1685794702_nk%E7%A6%8F%E7%A6%84%E5%AF%BF.jpeg'
    }
  },

})

注意其中的onShareAppMessage(转发给朋友)、onShareTimeline(分享到朋友圈)用法。


Comments(0) Add Your Comment

Not Comment!