位置: IT常识 - 正文

uniapp中的分享功能实现(APP,小程序,公众号)(uniapp分享图片)

编辑:rootadmin
uniapp中的分享功能实现(APP,小程序,公众号) uniapp中的分享功能实现(APP,小程序,公众号)

推荐整理分享uniapp中的分享功能实现(APP,小程序,公众号)(uniapp分享图片),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:uniapp分享到微信,uniapp生成图片分享,uniapp分享文件,uniapp分享文件,uniapp分享微信,uniapp的分享功能,uniapp公众号分享,uniapp分享微信,内容如对您有帮助,希望把文章链接给更多的朋友!

1.APP端的分享

app端的分享可以直接使用uniapp封装的方法uni.share,uni-app的App引擎已经封装了微信、QQ、微博的分享SDK,开发者可以直接调用相关功能。可以分享到微信、QQ、微博,每个社交平台被称为分享服务提供商,即provider。可以分享文字、图片、图文横条、音乐、视频等多种形式。同时注意,分享为小程序也使用本API。即在App里可以通过本API把一个内容以小程序(通常为内容页)方式直接分享给微信好友。直接上代码。

<!-- #ifdef APP-PLUS --><view class="item" @click="appShare('WXSceneSession')"><view class="iconfont icon-weixin3"></view><view class="">微信好友</view></view><view class="item" @click="appShare('WXSenceTimeline')"><view class="iconfont icon-pengyouquan"></view><view class="">微信朋友圈</view></view><!-- #endif -->appShare(scene) {let that = thislet routes = getCurrentPages(); // 获取当前打开过的页面路由数组let curRoute = routes[routes.length - 1].$page.fullPath // 获取当前页面路由,也就是最后一个打开的页面路由uni.share({provider: "weixin", //分享服务提供商(即weixin|qq|sinaweibo)scene: scene, //场景,可取值参考下面说明。type: 0, //分享形式href: `${HTTP_IP_URL}${curRoute}&spread=${that.uid}`, //跳转链接title: that.storeInfo.storeName, //分享内容的标题summary: that.storeInfo.storeInfo, //分享内容的摘要imageUrl: that.storeInfo.image, //图片地址success: function(res) {that.posters = false; //成功后关闭底部弹框},fail: function(err) {uni.showToast({title: '分享失败',icon: 'none',duration: 2000})that.posters = false;}});},

type 值说明

说明

provider支持度

0

图文

weixin、sinaweibo

1

纯文字

weixin、qq

2

图片

weixin、qq

3

音乐

weixin、qq

4

uniapp中的分享功能实现(APP,小程序,公众号)(uniapp分享图片)

视频

weixin、sinaweibo

5

小程序

weixin

scene 值说明

说明

WXSceneSession

分享到聊天界面

WXSenceTimeline

分享到朋友圈

WXSceneFavorite

分享到微信收藏

uni.share 在App端各社交平台分享配置说明

打开 manifest.json -> App模块权限配置,勾选 Share(分享);按如下文档具体配置微信、微博、QQ的参数

在 manifest.json 的 App SDK 配置里,勾选微信消息及朋友圈,并填写 appid,如需在iOS平台使用还需要配置通用链接。

2.小程序端的分享

小程序中的分享有两种,一种是通过右上角的胶囊分享,还可以通过在页面中写button,通过open-type="share"方式分享。

//onShareAppMessage 分享给朋友//onShareTimeline 分享到朋友圈// #ifdef MPonShareAppMessage: function(res) { if (res.from === 'button') { // 来自页面内转发按钮 console.log(res.target) } let that = this; return { title:'这是标题', imageUrl: '这是描述', path: '/pages/goods_details/index?id=' + that.id, }},// #endif

3.公众号的分享

公众号中的分享需要使用微信的JS-SDK,可以直接下载js文件引入,也可以通过npm下载。 公众号的分享比较繁琐,我们可以将其封装一下,在需要使用的地方传入对应的title,link和jsapi,就可以简便操作。

新建wechat.js,并在main.js中将其挂载到vue的原型上

// #ifdef H5import WechatJSSDK from "@/plugin/jweixin-module/index.js";import {getWechatConfig,wechatAuth} from "@/api/public";import {WX_AUTH,STATE_KEY,LOGINTYPE,BACK_URL} from '@/config/cache';import {parseQuery} from '@/utils';import store from '@/store';import Cache from '@/utils/cache';class AuthWechat {constructor() {//微信实例化对象this.instance = WechatJSSDK;//是否实例化this.status = false;this.initConfig = {};}isAndroid(){let u = navigator.userAgent;return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;}signLink() {if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') { window.entryUrl = location.href.split('#')[0]}return /(Android)/i.test(navigator.userAgent) ? location.href.split('#')[0] : window.entryUrl;}/** * 初始化wechat(分享配置) */wechat() {return new Promise((resolve, reject) => {// if (this.status && !this.isAndroid()) return resolve(this.instance);getWechatConfig().then(res => {this.instance.config(res.data);this.initConfig = res.data;this.status = true;this.instance.ready(() => {resolve(this.instance);})}).catch(err => {console.log('微信分享配置失败',err);this.status = false;reject(err);});});}/** * 验证是否初始化 */verifyInstance() {let that = this;return new Promise((resolve, reject) => {if (that.instance === null && !that.status) {that.wechat().then(res => {resolve(that.instance);}).catch(() => {return reject();})} else {return resolve(that.instance);}})}// 微信公众号的共享地址openAddress() {return new Promise((resolve, reject) => {this.wechat().then(wx => {this.toPromise(wx.openAddress).then(res => {resolve(res);}).catch(err => {reject(err);});}).catch(err => {reject(err);})});} // 获取经纬度;location(){return new Promise((resolve, reject) => {this.wechat().then(wx => {this.toPromise(wx.getLocation,{type: 'wgs84'}).then(res => {resolve(res);}).catch(err => {reject(err);});}).catch(err => {reject(err);})});} // 使用微信内置地图查看位置接口;seeLocation(config){return new Promise((resolve, reject) => {this.wechat().then(wx => {this.toPromise(wx.openLocation, config).then(res => {resolve(res);}).catch(err => {reject(err);});}).catch(err => {reject(err);})});}/** * 微信支付 * @param {Object} config */pay(config) {return new Promise((resolve, reject) => {this.wechat().then((wx) => { this.toPromise(wx.chooseWXPay, config).then(res => {resolve(res);}).catch(res => {resolve(res);});}).catch(res => {reject(res);});});}toPromise(fn, config = {}) {return new Promise((resolve, reject) => {fn({...config,success(res) {resolve(res);},fail(err) {reject(err);},complete(err) {reject(err);},cancel(err) {reject(err);}});});}/** * 自动去授权 */oAuth(snsapiBase,url) {if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return;const {code} = parseQuery();if (!code || code == uni.getStorageSync('snsapiCode')){return this.toAuth(snsapiBase,url);}else{if(Cache.has('snsapiKey'))return this.auth(code).catch(error=>{uni.showToast({title:error,icon:'none'})})}}clearAuthStatus() {}/** * 授权登录获取token * @param {Object} code */auth(code) {return new Promise((resolve, reject) => {wechatAuth(code, Cache.get("spread")).then(({data}) => {resolve(data);Cache.set(WX_AUTH, code);Cache.clear(STATE_KEY);// Cache.clear('spread');loginType && Cache.clear(LOGINTYPE);}).catch(reject);});}/** * 获取跳转授权后的地址 * @param {Object} appId */getAuthUrl(appId,snsapiBase,backUrl) {let url = `${location.origin}${backUrl}`if(url.indexOf('?') == -1){url = url+'?'}else{url = url+'&'}const redirect_uri = encodeURIComponent(`${url}scope=${snsapiBase}&back_url=` +encodeURIComponent(encodeURIComponent(uni.getStorageSync(BACK_URL) ?uni.getStorageSync(BACK_URL) :location.pathname + location.search)));uni.removeStorageSync(BACK_URL);const state = encodeURIComponent(("" + Math.random()).split(".")[1] + "authorizestate");uni.setStorageSync(STATE_KEY, state);return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;// if(snsapiBase==='snsapi_base'){// return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;// }else{// return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;// } }/** * 跳转自动登录 */toAuth(snsapiBase,backUrl) {let that = this;this.wechat().then(wx => {location.href = this.getAuthUrl(that.initConfig.appId,snsapiBase,backUrl);})}/** * 绑定事件 * @param {Object} name 事件名 * @param {Object} config 参数 */wechatEvevt(name, config) {let that = this;return new Promise((resolve, reject) => {let configDefault = {fail(res) {if (that.instance) return reject({is_ready: true,wx: that.instance});that.verifyInstance().then(wx => {return reject({is_ready: true,wx: wx});})},success(res) {return resolve(res,2222);}};Object.assign(configDefault, config);that.wechat().then(wx => {if (typeof name === 'object') {name.forEach(item => {wx[item] && wx[item](configDefault)})} else {wx[name] && wx[name](configDefault)}})});}isWeixin() {return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;}}export default new AuthWechat();// #endif

在需要使用的地方:

// 微信分享;setOpenShare: function(data) {let that = this;if (that.$wechat.isWeixin()) {let configAppMessage = {desc: data.synopsis,title: data.title,link: location.href,imgUrl: data.img};that.$wechat.wechatEvevt(["updateAppMessageShareData", "updateTimelineShareData"],configAppMessage);}},

微信公众号环境中点击右上角三个点就可以分享,所以setOpenShare事件可以提前让他执行,如果需要通过自定义方式通过按钮点击分享,可以将setOpenShare事件放在按钮的点击事件里面。

h5示例: CRMEB-JAVA. gitee开源地址: CRMEB-JAVA. 都看到这里了,点击上面gitee链接给个star吧

本文链接地址:https://www.jiuchutong.com/zhishi/269209.html 转载请保留说明!

上一篇:如何创建一个空文件(如何创建一个空的dataframe)

下一篇:鸿蒙系统中智能充电模式和反向充电功能怎么使用?(鸿蒙系统智能设备怎么开启)

  • 网络营销的好处与方法(网络营销的好处和优势)

    网络营销的好处与方法(网络营销的好处和优势)

  • 玩转QQ群营销、群排名、群演戏,打造自己的流量“鱼塘”(qq群营销效果怎么样)

    玩转QQ群营销、群排名、群演戏,打造自己的流量“鱼塘”(qq群营销效果怎么样)

  • 腾讯会议离开会议会被发现吗(腾讯会议离开会议主持人会有弹窗吗)

    腾讯会议离开会议会被发现吗(腾讯会议离开会议主持人会有弹窗吗)

  • 电脑图片另存为快捷键(电脑图片另存为jpg格式怎么弄)

    电脑图片另存为快捷键(电脑图片另存为jpg格式怎么弄)

  • 12306错误代码43003是什么(12306错误代码43003)

    12306错误代码43003是什么(12306错误代码43003)

  • 2张图片怎么合并到1张(2张图片怎么合并到1张a4纸上)

    2张图片怎么合并到1张(2张图片怎么合并到1张a4纸上)

  • 电脑平方的小2怎么打出来(电脑上面平方2怎么打出来)

    电脑平方的小2怎么打出来(电脑上面平方2怎么打出来)

  • cpu configuration什么意思

    cpu configuration什么意思

  • wifi符号旁边出现叹号(wifi符号旁边出现箭头)

    wifi符号旁边出现叹号(wifi符号旁边出现箭头)

  • 电池优化充电要关闭吗(优化电池充电坏处)

    电池优化充电要关闭吗(优化电池充电坏处)

  • 手机外放无声耳机有声(手机声音外放没有声音,戴耳机可以听到)

    手机外放无声耳机有声(手机声音外放没有声音,戴耳机可以听到)

  • oppor11长度多少厘米(oppor11的长度和宽度)

    oppor11长度多少厘米(oppor11的长度和宽度)

  • 为什么要使用域名系统(为什么要使用域名,域)

    为什么要使用域名系统(为什么要使用域名,域)

  • toc域开关有什么作用(toc原理)

    toc域开关有什么作用(toc原理)

  • 卡2无法访问移动网络(卡2无法访问移动网络什么意思)

    卡2无法访问移动网络(卡2无法访问移动网络什么意思)

  • 小米抖音看完整版怎么看(小米手机抖音为什么看不到完整版)

    小米抖音看完整版怎么看(小米手机抖音为什么看不到完整版)

  • 苹果11手机浮点怎么设置(苹果手机浮点在哪里打开)

    苹果11手机浮点怎么设置(苹果手机浮点在哪里打开)

  • wadl apk是什么(apk是什么意思?)

    wadl apk是什么(apk是什么意思?)

  • vivox27微信美颜功能设置(vivox27微信视频美颜怎么没有了)

    vivox27微信美颜功能设置(vivox27微信视频美颜怎么没有了)

  • 手机抖屏怎么解决(手机抖屏影响使用吗)

    手机抖屏怎么解决(手机抖屏影响使用吗)

  • 运行环境加载失败什么意思(运行环境加载失败10002)

    运行环境加载失败什么意思(运行环境加载失败10002)

  • oppofdx支持无线充电吗(oppo findx支持无线充电么)

    oppofdx支持无线充电吗(oppo findx支持无线充电么)

  • coloros是什么系统(coloros是什么手机型号)

    coloros是什么系统(coloros是什么手机型号)

  • 普通照片怎么添加水印(普通照片怎么添加水印相机的现场照片)

    普通照片怎么添加水印(普通照片怎么添加水印相机的现场照片)

  • 浅谈DNS域名解析的过程(dns域名解析两种方式)

    浅谈DNS域名解析的过程(dns域名解析两种方式)

  • 一般纳税人开普票税率是3%还是13%
  • 移动平均加权法是什么意思
  • 契税计入税金及附加吗
  • 小规模电子发票一张可以开多少金额
  • 赠送客户样品怎么写文案
  • 通行费机打发票税率
  • 收到投资款的会计科目
  • 收到供应商赠送的发票
  • 降低企业成本的有效途径
  • 小型微利企业如何计算所得税
  • 固定资产出售增值税税率
  • 突然收到银联入账收入怎么办
  • 收到长期股权投资的现金股利
  • 外贸公司的出口清单
  • 企业外购的房屋建筑物是否属于非房地产企业
  • 轿车计提折旧
  • 季度不超9万
  • 抵税必须要有发票
  • 适用差额征税的小规模纳税人有哪些
  • 汇算清缴补交的税怎么做凭证
  • 个人所得税中薪资与实际工资有什么差别
  • 小规模纳税人代账流程
  • 个税申报系统操作流程app
  • 金蝶用户管理怎么设置
  • 微信认证服务费可以开发票吗
  • 没有发票的怎么报账
  • 结转成本是否要等货物卖出后
  • windows10如何开热点
  • 收入的特征包括什么
  • 视同销售收入税法处理
  • 废料收入的成本怎么核算
  • 现金流量表 科目
  • js处理表格数据
  • vue $route
  • node更新到最新版本
  • ai绘画图片
  • linux 运行php
  • php 数学函数
  • 电子承兑利息
  • 无追保理是什么意思
  • 织梦收费5800的解决方法
  • SQL SERVER 将XML变量转为JSON文本
  • 所有者权益的确认依附于什么的确认
  • 普通增值发票可以抵扣进项税吗
  • 税务系统重置密码
  • 厂区道路折旧年限最新规定
  • 印花税步骤
  • 合同可以盖财务章子吗
  • 废料处理没开票销项税
  • 运费不支付会怎么样
  • 待处理财产损溢借贷增减方向
  • 固定资产处置办法
  • 总分包模式和总承包模式
  • 预付账款属于资产项目吗
  • 年末进项大于销项怎么结转
  • 税控盘减免税款需要结转吗
  • case在sql中
  • Windows系统sid修改方法
  • 安装office提示
  • windows 9
  • linux系统安装yum
  • xp系统的本地连接
  • win7旗舰版系统重装
  • window7截图工具无法使用
  • 安装win7系统后进不了系统
  • 平板电脑截图
  • 没有触屏如何使手机充电
  • dos到windows
  • 某网贴出来的u3d面试题目汇总,当时学习下(好多我都不会呢)
  • 面向对象实例化
  • 批处理调用ftp
  • 显示随机数
  • androidui框架
  • js获取鼠标坐标到浏览器底部
  • 轻松实现人生理想生日尾数农历
  • javascript面向
  • 仿微信语音聊天
  • 江苏省国家税务局总局官网
  • 国家税务局查验发票显示网络异常
  • 属于资源税类的税种有哪些
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设