周凯,个人博客

  • 前端
  • 嵌入式
  • 工具
  • 后端
  • 随笔
个人记录
  1. 首页
  2. 前端
  3. uni-app
  4. 正文

uni-app app微信分享 常用方法

2024年 6月 12日 1446点热度 2人点赞 0条评论
import ToastUtil from "@/utils/ToastUtil";

/**
 * 场景。
 * 可取值“WXSceneSession”分享到聊天界面,“WXSceneTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏
 * WXSceneSession: 分享到聊天界面
 * WXSceneTimeline: 分享到朋友圈
 * WXSceneFavorite: 分享到微信收藏
 */
type IScene = 'WXSceneSession' | 'WXSceneTimeline' | 'WXSceneFavorite';

const ShareUtil = {
    _share(options: UniNamespace.ShareOptions) {
        uni.share({
            ...options,
            provider: 'weixin'
        }).then(res => {
            ToastUtil.showToast({
                title: "分享成功"
            })
        }).catch(err => {
            console.log(err, 'err')
            ToastUtil.showToast({
                title: "分享失败",
                icon: 'error'
            })
        })
    },
    /**
     * 图文分享
     * @param title 标题
     * @param imageUrl 图片地址
     * @param summary 摘要
     * @param scene 场景
     */
    shareImageAndText(title: string, imageUrl: string, summary?: string, scene: IScene = 'WXSceneSession') {
        this._share({
            type: 0,
            title,
            imageUrl,
            summary,
            scene
        })
    },
    /**
     * 纯文字分享
     * @param title 标题
     * @param summary 摘要
     * @param scene 场景
     */
    shareText(title: string, summary?: string, scene: IScene = 'WXSceneSession') {
        this._share({
            type: 1,
            title,
            summary,
            scene
        })
    },
    /**
     * 纯图片分享
     * @param imageUrl 图片地址
     * @param title 标题
     * @param summary 摘要
     * @param scene 场景
     */
    shareImage(imageUrl: string, title?: string, summary?: string, scene: IScene = 'WXSceneSession') {
        this._share({
            type: 2,
            imageUrl,
            title,
            summary,
            scene
        })
    },
    /**
     * 分享视频
     * @param title 标题
     * @param mediaUrl 视频地址
     * @param summary 摘要
     * @param scene 场景
     */
    shareVideo(title: string, mediaUrl: string, summary?: string, scene: IScene = 'WXSceneSession') {
        this._share({
            type: 4,
            mediaUrl,
            title,
            summary,
            scene
        })
    }
}

export default ShareUtil

🎯 拓展阅读提示

本文涉及的内容已同步至公众号后台,我会在那里分享更多深度内容和实用技巧

→ 点击关注:一行梦境

公众号二维码
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2024年 6月 12日

周凯

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022-现在 周凯,个人博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蒙ICP备18004897号