微信小程序 分享海报
const app = getApp();
const template = require('../../template/templates.js');
Page({
/**
* 页面的初始数据
*/
data: {
hidden: true,
dataList: {
img_bg: '../../img/share_picture/shareListPicbg1.jpg',
title: '',
img: '',
qrCodeUrl: '',
plusPrice: null, //PLUS价格
faceValue: null, //代金券面值 },
referrerVOid: null,
saveImgBtnHidden: false,
openSettingBtnHidden: true,
referrerVOidShow: ""
},
getVouchersId: function(vouchersId) {
wx.request({
url: app.data.urls + 'vouchers/detail/id',
data: vouchersId,
method: "POST",
header: {
'content-type': 'application/json' // 默认值
},
success: (res) => {
console.log(res)
let dataList = this.data.dataList,
data = {
status: 1,
customerId: app.data.customerId,
type: 1
}
dataList.name = res.data.data.restaurant.name;
if (dataList.name.length > 10) {
dataList.name = `${dataList.name.substr(0, 9)}...`;
}
let payMoney = res.data.data.deductionPrice / 100,
useDiscountPrice = res.data.data.useDiscountPrice / 100;
//plus价格
let price = res.data.data.vipcardPrice / 100;
//地址 和电话
let address = res.data.data.restaurant.address;
let moblie = `电话:${res.data.data.restaurant.moblie}`;
dataList.address = address
dataList.moblie = moblie dataList.content = `立省${payMoney - price}元`;
dataList.originalCost = `原价:${payMoney}元`;
dataList.realPrice = `${price}元`;
dataList.useDiscountPrice = `( 消费满${useDiscountPrice}元可用 )`;
dataList.deductionPriceShow = `${payMoney}元代金券`;
template.getDistributorMsg(data, res => {
console.log(res)
this.setData({
plusPrice: price,
faceValue: payMoney,
referrerVOidShow: res.data.data.referrerVO.id,
// referrerVOid: res.data.data.referrerVO.id
})
this.referrerGenQRCode(vouchersId, res.data.data.referrerVO.id, res => {
// 获取店铺或者二维码
console.log(res)
let qrCodeUrl = `https://www.yooeepoint.com.cn${res.data.localQrCodeUrl}`; // 小程序二维码
let img = `https://www.yooeepoint.com.cn${res.data.localTitleImageUrl}` // 店铺图片
//背景图片
let vouchers_background = 'https://www.yooeepoint.com.cn/images/vouchers_background.png' this.downLoadImage(qrCodeUrl, res => { // 网络图片下载本地(canvas限制图片使用本地路径)
dataList.qrCodeUrl = res.tempFilePath;
this.downLoadImage(img, res => {
dataList.img = res.tempFilePath;
this.downLoadImage(vouchers_background, res => {
dataList.vouchers_background = res.tempFilePath; //背景图片
this.getSystemInfo(res => {
dataList.windowWidth = res.windowWidth
dataList.windowHeight = res.windowHeight
this.creatCanvas(dataList);
}) }) })
}) })
})
}
})
},
// 绘制分享图片
creatCanvas: function(data) { const ctx = wx.createCanvasContext('shareImg');
//背景图
// ctx.setFillStyle('#fff')
// ctx.fillRect(0, 0, data.windowWidth, data.windowHeight * 0.78)
ctx.drawImage(data.vouchers_background, 0, 0, data.windowWidth, data.windowHeight);
//banner图
//banner图
ctx.drawImage(data.img, 0, 10, data.windowWidth, data.windowHeight * 0.315);
//店铺名称
ctx.setFontSize(data.windowWidth / (750 / 35));
ctx.setTextAlign('center');
ctx.setFillStyle('#000000');
ctx.fillText(data.name, data.windowWidth / 2, data.windowHeight * 0.39 + 6);
//多少元代金券
ctx.setFontSize(data.windowWidth / (750 / 30));
ctx.setTextAlign('center');
ctx.setFillStyle('#000000');
ctx.fillText(data.deductionPriceShow, data.windowWidth / 2, data.windowHeight * 0.39 + 30); // 满减条件
ctx.setFillStyle('#000000');
ctx.setFontSize(data.windowWidth / (750 / 25));
ctx.fillText(data.useDiscountPrice, data.windowWidth / 2, data.windowHeight * 0.39 + 45); //原价
ctx.setFontSize(data.windowWidth / (750 / 22));
ctx.setFillStyle('#666666');
ctx.fillText(data.originalCost, data.windowWidth / 2 + 120, data.windowHeight * 0.50); // 立省
ctx.setFillStyle('#f10c0c');
ctx.setFontSize(data.windowWidth / (750 / 22));
ctx.fillText(data.content, data.windowWidth / 2 + 120, data.windowHeight * 0.50+ 15); // plus价
ctx.setFontSize(data.windowWidth / (750 / 55));
ctx.setFillStyle('#f10c0c');
ctx.fillText(data.realPrice, data.windowWidth / 2, data.windowHeight * 0.60-15 ); // 地址和电话
ctx.setFontSize(data.windowWidth / (750 / 25));
ctx.setFillStyle('#323232');
ctx.fillText(data.address, data.windowWidth / 2, data.windowHeight *0.60+15); ctx.setFontSize(data.windowWidth / (750 / 20));
ctx.setFillStyle('#323232');
ctx.fillText(data.moblie, data.windowWidth / 2, data.windowHeight *0.60+35); // 二维码图
ctx.drawImage(data.qrCodeUrl, data.windowWidth * 0.38-2, data.windowHeight * 0.73, data.windowWidth * 0.25, data.windowWidth * 0.25); // tip
ctx.setTextAlign('center');
ctx.setFillStyle('#fff');
ctx.setFontSize(data.windowWidth / (750 / 22));
ctx.fillText("长按识别图中小程序码购买", data.windowWidth / 2, data.windowHeight * 0.95); // tip
ctx.setTextAlign('center');
ctx.setFillStyle('#fff');
ctx.setFontSize(data.windowWidth / (750 / 18));
ctx.fillText("想要吃点喝点,首选搜城天下", data.windowWidth / 2, data.windowHeight * 0.97); //
// ctx.setTextAlign('right');
// ctx.setFillStyle('#282828');
// ctx.setFontSize(data.windowWidth / (750 / 22));
// ctx.fillText("长按识别二维码", data.windowWidth * 0.89, data.windowHeight * 0.60 + 35);
//结束绘制/展示
ctx.stroke();
ctx.draw(false, () => {
this.toShare();
});
}, // 获取二维码/店铺图片
referrerGenQRCode: function(vouchersId, referrerId, successCallBack) {
let data = {
referrerId: referrerId,
serviceType: 3,
vouchersId: vouchersId
}
wx.request({
url: app.data.urls + `html5/toShareImagePage`,
data: data,
method: 'POST',
success: (res) => {
successCallBack(res)
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
wx.showLoading({
title: '加载中',
mask: true
});
console.log(options) this.getVouchersId(options.vouchersId)
console.log(options.referrerId)
this.setData({
vouchersId: options.vouchersId,
referrerVOid: options.referrerId,
partnersId: options.partnersId,
isFreeObtain: options.isFreeObtain,
restaurantId: options.restaurantId
})
console.log(this.data.referrerVOid)
// 初始化数据
}, // 动态设置画布宽高
getSystemInfo: function(successCallBack) {
wx.getSystemInfo({
success: (res) => {
successCallBack(res)
}
})
}, // 网络图片下载本地(canvas限制图片使用本地路径)
downLoadImage: function(data, successCallBack) {
wx.downloadFile({
url: data,
success: function(res) {
successCallBack(res)
}
}) },
// 分享图片生成后展示
toShare: function() {
wx.canvasToTempFilePath({
x: 0,
y: 0,
canvasId: 'shareImg',
success: (res) => {
console.log(res)
wx.hideLoading();
this.setData({
prurl: res.tempFilePath,
hidden: false
})
}
})
},
// 分享图片保存到相册
savePic: function() {
let that = this
wx.getSetting({
success: res => {
if (!res.authSetting['scope.writePhotosAlbum']) { //用户同意授权
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
this.saveImageToPhotosAlbum();
},
fail: () => { //这里是用户拒绝授权后的回调
that.setData({
saveImgBtnHidden: true,
openSettingBtnHidden: false
})
}
})
} else { //用户已经授权过
this.saveImageToPhotosAlbum();
}
}
})
},
saveImageToPhotosAlbum: function() {
wx.saveImageToPhotosAlbum({
filePath: this.data.prurl,
success(res) {
this.onShareAppMessage()
}
})
},
handleSetting: function(e) {
let that = this;
// 对用户的设置进行判断,如果没有授权,即使用户返回到保存页面,显示的也是“去授权”按钮;同意授权之后才显示保存按钮
if (!e.detail.authSetting['scope.writePhotosAlbum']) {
wx.showModal({
title: '提示',
content: '若不打开授权,则无法将图片保存在相册中!',
showCancel: false
})
that.setData({
saveImgBtnHidden: true,
openSettingBtnHidden: false
})
} else {
wx.showModal({
title: '提示',
content: '您已授权,赶紧将图片保存在相册中吧!',
showCancel: false
})
that.setData({
saveImgBtnHidden: false,
openSettingBtnHidden: true
})
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
// return {
// title: '【搜城天下】' + this.data.dataList.name,
// path: '/pages/goinSharePic/goinSharePic?vouchersId=' + this.data.vouchersId + "&referrerVOid=" + this.data.referrerVOid,
// imageUrl: this.data.prurl
// }
console.log("kankan" + this.data.referrerVOidShow)
return {
title: '【' + this.data.plusPrice + '元抢购】' + this.data.faceValue + '元代金券',
path: '/pages/vouchersDetails/vouchersDetails?vouchersId=' + this.data.vouchersId + "&referrerId=" + this.data.referrerVOidShow + '&restaurantId=' + this.data.restaurantId + '&isFreeObtain=' + this.data.isFreeObtain + '&partnersId=' + this.data.partnersId,
imageUrl: this.data.prurl
} },
onReady: function() {
wx.hideLoading();
}
})
微信小程序 分享海报的更多相关文章
- 关于微信小程序分享/转发功能的实现方法
实现微信小程序分享,可以有两个入口: 1. 小程序右上角菜单自带的分享 这个入口是默认关闭的,需要在当前页面中调用showShareMenu方法,开启分享 onLoad: function () { ...
- golang-vue实现微信小程序分享到朋友圈
最近涉及到微信小程序分享到朋友圈,不知道微信为什么不直接接口分享,咱也不敢佛,咱也不敢问,只能百度问度娘,看官方文档,网上的一些分享五花八门,每一个重点的,所以整理了一下到底怎样生成二维码分享图片才是 ...
- 微信小程序分享转发用法大全——自定义分享、全局分享、组合分享
官方提供的自定义分享 使用隐式页面配置函数实现的全局分享-推荐 使用隐式路由实现的全局分享-不推荐,仅供了解隐式路由 前言: 目前微信小程序只开放了页面自定义分享的API,为了能够更灵活的进行分享配置 ...
- 微信小程序分享至朋友圈的方法
最近研究怎么实现微信小程序分享至朋友圈,对就是朋友圈. 微信小程序目前没有直接提供方法来将小程序分享至朋友圈,不过可以采用曲线救国的方式来达到目的. 方法分两步: 1.通过浏览器将希望分享的东西风向至 ...
- 微信小程序--分享功能
微信小程序--分享功能 微信小程序前段时间开放了小程序右上角的分享功能, 可以分享任意一个页面到好友或者群聊, 但是不能分享到朋友圈 这里有微信开发文档链接:点击跳转到微信分享功能API 入口方法: ...
- 微信小程序分享支持自定义封面图
微信小程序又发布更新了,刚好昨天支付宝也发布小程序,不能让它抢了风头的节奏.微信小程序主要更新如下:“小程序分享支持自定义封面图,公众号及小程序客服可发送小程序卡片.同时,我们还开放了获取发票抬头,指 ...
- 微信小程序--分享报错(thirdScriptError Cannot read property 'from' of undefined;at pages/index/index page onShareAppMessage function TypeError: Cannot read property 'from' of undefined)
分享功能: onShareAppMessage: function (res) { if (res.from === 'button') { // 来自页面内转发按钮 console.log(res. ...
- 微信小程序分享朋友圈的实现思路与解决办法
实现思路 那么既然小程序没有分享到朋友圈的api,我们怎么实现分享到朋友圈呢,下面我介绍一下实现思路. 既然没有捷径,那就走复杂一点的路线,那就是需要用户手动分享到朋友圈,问题又来了,用户手动分享的话 ...
- 微信小程序分享功能的path路径
表示被微信小程序坑惨了 糟心的开始写,老板说先不上太复杂,就先显示了两个页面,然后开开心心的发布,测试了几遍,没啥问题呀.结果,一上上去,就被老板批了! 啥呀! 这分享怎么这个鬼样子!明明我看文档都是 ...
随机推荐
- 1.Vector(同步)和ArrayList(异步)异同
Vector线程同步,线程安全 ArrayList的运行速度较快,因为没有使用线程 请求方式:同步 异步未响应=假死 原因:1.占用内存过多,内存无法进行处理 2.并发量(本机的浏览器进行访 ...
- Web API Filter
在Web Api中,有三种Filter Filter类型 实现的接口 描述 Authorization IAuthorizationFilter 最先运行的Filter,被用作请求权限校验 Actio ...
- es6——map-set与对象对比
{ //map,set,object对比 let item={t:1}; let map=new Map(); let set=new Set(); let obj={} ...
- Python笔记24-----迭代器、生成器的使用(如嵌套列表的展开、树的遍历等)
1.递归yield使用: 嵌套列表展开 def flatten(nested): if type(nested)==list: for sublist in nested: for i in flat ...
- npx命令
npx命令 查了一下, 英文资料: https://www.npmjs.com/package/npx 中文资料: 什么是npx 第一次看到npx命令是在 babel 的文档里 Note: If yo ...
- CF449D Jzzhu and Numbers (状压DP+容斥)
题目大意: 给出一个长度为n的序列,构造出一个序列使得它们的位与和为0,求方案数 也就是从序列里面选出一个非空子集使这些数按位与起来为0. 看了好久才明白题解在干嘛,我们先要表示出两两组合位与和为0的 ...
- Linux 进程及作业管理
进程简介: 内核的功用:进程管理.文件系统.网络功能.内存管理.驱动程序.安全功能 进程(Process):什么是进程,进程是程序的执行实例,即运行中的程序,同时也是程序的一个副本:程序是放置于磁 ...
- 页面关闭或刷新时触发javascript的事件
当页面在关闭或刷新时提示 window.onbeforeunload(function(){ //判断是关闭还是刷新 1.满足关闭,否则是刷新 if(event.clientX>document ...
- CF735E Ostap and Tree
比较毒瘤的树形DP,子状态难想.这是主要是搬运一篇题解. 用\(f[i][j]\)表示\(i\)的子树中离\(i\)最近黑点的距离为\(j\),且距离超过\(j\)的点都被满足的方案数.转移时新建一个 ...
- DDL表结构修改
*1)创建表 create table 表名( 字段名 类型, .... ); //以现有表复制一个新表 create table j012 as se ...