小程序分享朋友圈cavans(随机切换背景图片)

做了个微信小程序的分享朋友圈,第N次做了,记录下来吧

第一次在CSDN上写博客,写的不好凑活着看吧

js代码

const app = new getApp();

Page({

/**

* 页面的初始数据

*/

data: {

selected: [],

posterDakaCount:0,

posterDakaName: '',

posterNickName: '',

showPosterImage: true

},

onLoad: function (options) {

// this.showPoster();

},



onShow: function () {

var _this = this;

app.sendRequest({

url: 'Collect/getmemberid',

data: {},

success: function (res) {

if (res.code == 0) {

_this.setData({

member_id: res.data

})

}

}

})

// this.savePoster();

},

//小程序分享好友

onShareAppMessage: function (res) {

var member_id = this.data.member_id;

if (res.from === 'button') {

// 来自页面内转发按钮

} else {

// 来自菜单栏转发按钮

}

return {

title: '海蓝风加油',

path: '/pages/index/index/index?member_id=' + member_id,

// 设置转发的图片

imageUrl: '/images/2.jpg',

// 成功的回调

success: (res) => { },

// 失败的回调

fail: (res) => { },

// 无论成功与否的回调

complete: (res) => { }

}

},

//弹出层关闭

hidemengceng: function(){

var that = this;

that.setData({

showPosterImage: true

})

},

// 弹出打卡海报

showPoster: function () {

var that = this;

that.setData({

showPosterImage: false

})

app.sendRequest({

url: 'Kaoshi/getimgs',

success: function (res) {

var data=res.data;

if (res.code == 0) {

// console.log(data.mouth)

that.setData({

posterNickName: data.nickName,

year: data.year,

mouth: data.mouth,

days: data.days,

continuous_days: data.continuous_days,

datas:data

})

that.savePoster();

//背景图片

wx.getImageInfo({

src: data.downloadPosterBg,

success: function (res) {

that.setData({

posterBackground: res.path

})

}

})







}

}

})



},

//绘制cavans图片方法

savePoster: function () {

// 获取背景图片信息

var that = this;

var data = that.data.datas;

let promise1 = new Promise(function (resolve, reject) {

wx.getImageInfo({

src: data.downloadPosterBg,

success: function (res) {

resolve(res);

}

})

});

//获取头像信息

let promise2 = new Promise(function (resolve, reject) {

wx.getImageInfo({

src: data.downloadHeaderImage,

success: function (res) {

resolve(res);

}

})

});

//获取二维码

let promise3 = new Promise(function (resolve, reject) {

wx.getImageInfo({

src: data.downloadQRImage,

success: function (res) {

resolve(res);

}

})

});

//es6中类似于事件写法



Promise.all([

promise1, promise2, promise3

]).then(res => {

console.log(res)

wx.hideLoading()

wx.showLoading({

title: '分享图片生成中...',

icon: 'loading',

duration: 1000

})

/* 图片获取成功才执行后续代码 */

var canvas = wx.createCanvasContext('hoCanvas')

// 绘制背景图

canvas.drawImage(res[0].path, 0, 0, 300, 375);

console.log(that.data.mouth)

//绘制年月日

canvas.setFontSize(24)

canvas.setFillStyle('#fff')

canvas.fillText(that.data.year, 20, 60, 160)

canvas.fillText('/', 60, 90, 160)

canvas.fillText(that.data.mouth, 40, 120, 160)

canvas.fillText('/', 60, 150, 160)

canvas.fillText(that.data.days, 40, 180, 160)

// 绘制活动二维码

canvas.save()

canvas.setFillStyle('#fff')

canvas.fillRect(0, 370, 300, 150)

// restore to the previous saved state

canvas.setFontSize(12)

canvas.setFillStyle('#424E75')

canvas.fillText('长按识别二维码', 190, 435, 160)

canvas.fillText('连续打卡天数', 20, 435, 160)

canvas.setFontSize(23)

canvas.setFillStyle('#000')

canvas.fillText('DAY', 20, 475, 160)

canvas.setFontSize(28)

canvas.setFillStyle('red')

// var

canvas.fillText(that.data.continuous_days, 80, 475, 160)

canvas.drawImage(res[2].path, 215, 445, 50, 50)

// 绘制头像

// canvas.save()

canvas.setFillStyle('#fff')

canvas.fillRect(10, 335, 280, 80)

canvas.restore()



// canvas.fillRect(0, 370, 300, 300)

// // (10, 310, 280, 60)



// canvas.fill()

// canvas.arc(50, 740, 30, 0, 2 * Math.PI)

// canvas.arc(32, 350, 30, 0, 2 * Math.PI)

// canvas.clip()

canvas.drawImage(res[1].path, 45, 355, 40, 40);

canvas.restore()

//绘制微信昵称文本

canvas.setFontSize(12)

canvas.setFillStyle("#424E75")

canvas.setStrokeStyle('#424E75')

canvas.fillText(that.data.posterNickName, 100, 365, 160)

// 绘制打卡昵称

canvas.setFontSize(14)

canvas.setFillStyle("#424E75")

canvas.setStrokeStyle('#424E75')

canvas.fillText(that.data.posterDakaName, 110, 390)

canvas.draw()

setTimeout(function () {

wx.canvasToTempFilePath({

x: 0,

y: 0,

width: 375 * wx.getSystemInfoSync().pixelRatio, //导出图片的宽

height: 680 * wx.getSystemInfoSync().pixelRatio, //导出图片的高

destWidth: 375*wx.getSystemInfoSync().pixelRatio, //绘制canvas的时候用的是px, 这里换算成rpx ,导出后非常清晰

destHeight: 680*wx.getSystemInfoSync().pixelRatio, //同上 px 换算成 rpx

// canvasId: 'mycanvas',

quality:'1',

canvasId: 'hoCanvas',

success: function (res) {

that.setData({

postUrl: res.tempFilePath,

})

// that.saveImageToAlbum();

// wx.hideLoading()

},

fail: function (res) { }

})

}, 2000)

})

},

//保存图片

saveImageToAlbum: function () {

var that = this;

//将图片保存到相册

wx.saveImageToPhotosAlbum({

filePath: that.data.postUrl,



success(res) {

wx.showModal({

title: '保存成功',

content: '图片成功保存到相册了,快去分享朋友圈吧',

showCancel: false,

confirmText: '好的',

confirmColor: '#818FFB',

success: function (res) {

if (res.confirm) {

that.setData({

showPosterImage: true

})

}

}

})

}

})

},

})

wxml部分

<view class="fxbox">

<button open-type="share" style="background-color: #fff">

<image src="/images/share_weixin.png"></image>

发送给朋友

</button>

<button bindtap="showPoster" style="background-color: #fff">

<image src="/images/share_pyq.png"></image>

分享到朋友圈

</button>

</view>

<view class="mengceng" hidden="{{showPosterImage}}">

<!-- <canvas canvas-id="hoCanvas" style="width:300px;height:520px;margin:auto;margin-top:30rpx;"></canvas> -->

<canvas canvas-id="hoCanvas" style="height:510px;margin:auto;margin-top:5%" class="canvas"></canvas>



<image src="/images/close.png" bindtap='closeShare' style="width:40rpx;height:40rpx;margin:auto;" class='bottoms' bindtap="hidemengceng"></image>



<view class="btnbox">

<button bindtap="saveImageToAlbum" style="font-size:28rpx;background:blue">保存并分享朋友圈</button>

</view>



</view>

wxss代码

page{background: #fff;}

.calendar-box {

/* position: absolute;

top: 0;

left: 0; */

background: #fff;

z-index: 999;

margin:20rpx;

box-sizing: border-box;

border-radius:10rpx;

box-shadow: 0 10rpx 10rpx rgba(0,0,0,.1)

}

.calendar-wrapper {

width: 100%;

border-top: 1px #f5f5f5 solid;

box-sizing: border-box;

font-size: 26rpx;

background: #fff;

}

.share{

width: 300px;

height: 380px;

position: fixed;

left: 0;

right: 0;

top: 0;

bottom: 100rpx;

margin: auto;

}

.btn-box{

padding: 1060rpx 60rpx 0 60rpx;

}

.bottom{

width: 300px;

height: 40px;

background-color: white;

}

.bottom image {

  width: 70rpx;

  height: 70rpx;

  margin-left: 45%;

  margin-top: 2%;

}

.header {

display: flex;

justify-content: center;

align-items: center;

position: relative;

height: 100rpx;

/* width: 100%; */

background: #fff;

/* padding: 0 30rpx; */

z-index: 10000;

}

.top-jiantou {

width: 100rpx;

height: 100rpx;

text-align: center;

box-sizing: border-box;

line-height: 100rpx;

}

.iconfont {

display: flex;

justify-content: center;

align-items: center;

position: relative;

color: #52b8f5;

width: 53rpx;

height: 53rpx;

}

.iconfont .left, .iconfont .right {

display: block;

width: 0;

height: 0;

border-top: 20rpx solid transparent;

border-bottom: 20rpx solid transparent;

}

.iconfont .left {

border-right: 40rpx solid transparent;

}

.iconfont .right {

border-left: 40rpx solid transparent;

}

.iconfont .left.color {

border-right-color: #fff;

}

.iconfont .right.color {

border-left-color: #fff;

}

.btn {

margin: 0 30rpx;

width: 240rpx;

height: 53rpx;

border: 1rpx solid #52b8f5;

border-radius: 26rpx;

color: #52b8f5;

font-size: 26rpx;

box-sizing: border-box;

}

.calendar-panel {

position: relative;

display: flex;

align-items: center;

justify-content: center;

font-size: 32rpx;

height:150rpx;

background: #31a4ff;

border-radius:10rpx 10rpx 0 0;

}

.backtoday {

position: absolute;

right: 0;

top:50rpx;

padding: 0 10rpx;

padding-left: 20rpx;

height: 50rpx;

line-height: 50rpx;

border: 1px #fff solid;

border-right: none;

font-size: 28rpx;

border-top-left-radius: 50rpx;

border-bottom-left-radius: 50rpx;

color: #fff;

background: rgba(82, 184, 245, 0.1);

}

.date-befor, .date-after {

/* border: 1px red solid; */

display: flex;

justify-content: center;

align-items: center;

height: 80rpx;

width: 80rpx;

text-align: center;

line-height: 80rpx;

}

/* .date-after {

margin-left: 20rpx;

} */

.calendar-panel-box {

display: flex;

color:#fff;

font-size:40rpx

}

.calendar-header {

display: flex;

border-bottom:1px dashed #9de4ff;

}

.calendar-header view {

width: 100%;

text-align: center;

line-height: 80rpx;

color: #333;font-size:34rpx;

}

.calendar-body {

display: flex;

flex-wrap: wrap;

border-bottom: 1px #f5f5f5 solid;

padding-bottom:40rpx;

padding-top:20rpx;

}

.calender-body-date-week {

display: flex;

width: 100%;

}

.date {

position: relative;

width: 100%;

text-align: center;

color: #1c1c1c;

background: #fff;

line-height:100rpx;

font-size: 34rpx

}

.date.active {

background: red;

}

.placeholder {

color: #a4a4a4;

}

.date-current {

background: #52b8f5;

color: #fff;border-radius:50%;

}

.data-circle {

width: 14rpx;

height: 28rpx;

border-right:2px solid #ff5a5f;

border-bottom:2px solid #ff5a5f;

transform: rotate(40deg);

position: absolute;

bottom:20rpx;

left: 12rpx;

right: 0;

margin: auto;

}

.flex-center {

display: flex;

align-items: center;

justify-content: center;

}

.total-card{font-size:40rpx;color:#777;text-align: center;padding:50rpx 0 30rpx 0;}

.bottom{overflow: hidden;position: relative;margin-top:50rpx;color:#666;}

.bottom .continuous{font-size:40rpx;float:left;width:50%;text-align: center;padding-top:20rpx;}

.bottom .continuous text{font-size:80rpx;color:#31a4ff;padding-right:10rpx;}

.packup {

position: absolute;

right:6%;

bottom:0;

color: #31a4ff;

border:3rpx solid #31a4ff; display: inline-block;border-radius:10rpx;padding:10rpx 50rpx;font-size:30rpx;

}

.mengceng{

width: 100%;

height: 100%;

position: fixed;

z-index: 9999;

background: rgba(0,0,0,.5);

top: 0;

}

.fxbox{display: flex;justify-content:space-between;width:50%; position: absolute;top:970rpx;right:30rpx;}

.fxbox>button{font-size: 21rpx;text-align: center;border: none;padding-top: 10rpx;}

.fxbox>button image{width:50rpx;height:50rpx; display:block;margin: auto; }

.bottoms{position: absolute; right:4.5%;top:2%; z-index: 9999999!important;}

.btnbox{ width: 100%;text-align: center;margin-top: 30rpx;}

.btnbox button{ display: inline-block!important;background-color: #31a4ff!important;color:#fff}

以上就是完整的代码了

看下效果图吧

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值