first commit

This commit is contained in:
GYJ
2024-12-02 10:39:36 +08:00
commit f1f5b666b5
464 changed files with 81372 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
## 3.0.12023-08-12
修复微信小程序问题, 支持微信小程序!
## 3.02023-08-12
修复微信小程序bug 支持微信小程序
## 2.0.22023-07-30
组件说明优化
## 2.0.12023-07-30
组件优化
## 2.0.02023-07-30
组件优化
## 1.0.02023-07-30
组件初始化

View File

@@ -0,0 +1,283 @@
<template>
<view style="background: #FFFFFF;">
<canvas v-if="!tempFilePath" :canvas-id="CanvasID"
:style="{ width: canvasW + 'px', height: canvasH + 'px' }"></canvas>
<image v-else lazy-load :src="tempFilePath" mode="widthFix" class="is-response"
@longpress="toSave(tempFilePath)"></image>
</view>
</template>
<script>
var _this;
export default {
name: 'wm-poster',
props: {
CanvasID: {
//CanvasID 等同于 canvas-id
Type: String,
default: 'PosterCanvas'
},
imgSrc: {
//展示图
Type: String,
default: ''
},
QrSrc: {
//二维码
Type: String,
default: ''
},
Title: {
//文本内容
Type: String,
default: '短剧'
},
TitleColor: {
//标题颜色
Type: String,
default: '#333333'
},
LineType: {
//标题显示行数 大于两行是否省略 注超出2行显示会导致画布布局絮乱
Type: [String, Boolean],
default: true
},
PriceTxt: {
//价格值
Type: String,
default: ''
},
PriceColor: {
//价格颜色
Type: String,
default: '#e31d1a'
},
OriginalTxt: {
//原价值
Type: String,
default: ''
},
OriginalColor: {
//默认颜色(如原价与扫描二维码颜色)
Type: String,
default: '#b8b8b8'
},
Width: {
//画布宽度 (高度根据图片比例计算 单位upx)
Type: String,
default: 750
},
CanvasBg: {
//canvas画布背景色
Type: String,
default: '#ffffff'
},
Referrer: {
//推荐人信息
Type: String,
default: '精选好剧'
},
ViewDetails: {
//描述提示文字
Type: String,
default: '长按或扫描识别二维码观看'
}
},
data() {
return {
tempFilePath: '',
canvasW: 0,
canvasH: 0,
canvasImgSrc: '',
ctx: null
};
},
methods: {
toSave(url) {
//#ifndef H5
uni.getImageInfo({
src: url,
success: function(image) {
console.log('图片信息:', JSON.stringify(image));
uni.saveImageToPhotosAlbum({
filePath: image.path,
success: function() {
console.log('save success');
uni.showToast({
title: '海报已保存相册',
icon: 'success',
duration: 2000
});
}
});
}
});
//#endif
},
async OnCanvas() {
uni.showLoading({
title: '海报生成中...'
})
_this.ctx = uni.createCanvasContext(_this.CanvasID, this);
const C_W = uni.upx2px(_this.Width), //canvas宽度
C_P = uni.upx2px(30), //canvas Paddng 间距
C_Q = uni.upx2px(150); //二维码或太阳码宽高
let _strlineW = 0; //文本宽度
let _imgInfo = await _this.getImageInfo({
imgSrc: _this.imgSrc
}); //广告图
console.log('图片信息0', JSON.stringify(""));
let _QrCode = await _this.getImageInfo({
imgSrc: _this.QrSrc
}); //二维码或太阳码
console.log('图片信息1', JSON.stringify(""));
let r = [_imgInfo.width, _imgInfo.height];
let q = [_QrCode.width, _QrCode.height];
let imgW = C_W - C_P * 2;
if (r[0] != imgW) {
r[1] = Math.floor((imgW / r[0]) * r[1]);
r[0] = imgW;
}
if (q[0] != C_Q) {
q[1] = Math.floor((C_Q / q[0]) * q[1]);
q[0] = C_Q;
}
_this.canvasW = C_W;
_this.canvasH = r[1] + q[1] + 40;
_this.ctx.setFillStyle(_this.CanvasBg); //canvas背景颜色
_this.ctx.fillRect(0, 0, C_W, _this.canvasH); //canvas画布大小
//添加图片展示
_this.ctx.drawImage(_imgInfo.path, C_P, C_P, r[0], r[1]);
//添加图片展示 end
//设置文本
_this.ctx.setFontSize(uni.upx2px(32)); //设置标题字体大小
_this.ctx.setFillStyle(_this.TitleColor); //设置标题文本颜色
let _strLastIndex = 0; //每次开始截取的字符串的索引
let _strHeight = r[1] + C_P * 2 + 10; //绘制字体距离canvas顶部的初始高度
let _num = 1;
// for (let i = 0; i < _this.Title.length; i++) {
// _strlineW += _this.ctx.measureText(_this.Title[i]).width;
// if (_strlineW > r[0]) {
// if (_num == 2 && _this.LineType) {
// //文字换行数量大于二进行省略号处理
// _this.ctx.fillText(_this.Title.substring(_strLastIndex, i - 8) + '...', C_P, _strHeight);
// _strlineW = 0;
// _strLastIndex = i;
// _num++;
// break;
// } else {
// _this.ctx.fillText(_this.Title.substring(_strLastIndex, i), C_P, _strHeight);
// _strlineW = 0;
// _strHeight += 20;
// _strLastIndex = i;
// _num++;
// }
// } else if (i == _this.Title.length - 1) {
// _this.ctx.fillText(_this.Title.substring(_strLastIndex, i + 1), C_P, _strHeight);
// _strlineW = 0;
// }
// }
//设置文本 end
//设置价格
_strlineW = C_P;
_strHeight += uni.upx2px(60);
if (_num == 1) {
_strHeight += 20; //单行标题时占位符
}
// if (_this.PriceTxt != '') {
// //判断是否有销售价格
// _this.ctx.setFillStyle(_this.PriceColor);
// _this.ctx.setFontSize(uni.upx2px(38));
// // _this.ctx.fillText('券后价 ¥' + _this.PriceTxt, _strlineW, _strHeight); //商品价格
// _strlineW += _this.ctx.measureText('券后价 ¥' + _this.PriceTxt).width + uni.upx2px(10);
// }
// // #ifdef H5
// if (_this.PriceTxt != '' && _this.OriginalTxt != '') {
// //判断是否有销售价格且原价
// _this.ctx.setFillStyle(_this.OriginalColor);
// _this.ctx.setFontSize(uni.upx2px(24));
// _this.ctx.fillText(_this.OriginalTxt, _strlineW, _strHeight); //商品原价
// }
// #endif
_this.ctx.strokeStyle = _this.OriginalColor;
_this.ctx.moveTo(_strlineW, _strHeight - uni.upx2px(10)); //起点
_this.ctx.lineTo(_strlineW + _this.ctx.measureText(_this.OriginalTxt).width, _strHeight - uni.upx2px(
10)); //终点
_this.ctx.stroke();
//设置价格 end
//添加二维码
_strHeight += uni.upx2px(20);
let toTop = uni.upx2px(20);
// _this.ctx.drawImage(_QrCode.path, r[0] - q[0] + C_P, _strHeight, q[0], q[1]);
_this.ctx.drawImage(_QrCode.path, r[0] - q[0] + C_P, _this.canvasH - q[1] - toTop, q[0], q[1]);
//添加二维码 end
//添加推荐人与描述
_this.ctx.setFillStyle(_this.TitleColor);
_this.ctx.setFontSize(uni.upx2px(30));
_this.ctx.fillText(_this.Title, C_P, _this.canvasH - q[1] - toTop + q[1] / 2);
_this.ctx.setFillStyle(_this.OriginalColor);
_this.ctx.setFontSize(uni.upx2px(24));
_this.ctx.fillText(_this.ViewDetails, C_P, _this.canvasH - q[1] - toTop + q[1] / 2 + 20);
//添加推荐人与描述 end
//延迟后渲染至canvas上
setTimeout(function() {
_this.ctx.draw(true, ret => {
_this.getNewImage();
});
}, 600);
},
async getImageInfo({
imgSrc
}) {
return new Promise((resolve, errs) => {
uni.getImageInfo({
src: imgSrc,
success: function(image) {
resolve(image);
},
fail(err) {
// errs(err);
_this.$emit('error', err);
}
});
});
},
getNewImage() {
uni.canvasToTempFilePath({
canvasId: _this.CanvasID,
quality: 1,
complete: res => {
_this.tempFilePath = res.tempFilePath;
_this.$emit('success', res);
// uni.showToast({
// title: '长按图片保存海报',
// mask: false,
// duration: 2000,
// icon: "none"
// });
}
},
this
);
}
},
mounted() {
_this = this;
this.OnCanvas();
}
};
</script>
<style></style>

View File

@@ -0,0 +1,89 @@
{
"id": "cc-poster",
"displayName": "自定义精美海报生成组件 根据自定义数据字段生成海报 长按保存海报图片 ",
"version": "3.0.1",
"description": "自定义精美海报生成组件 根据自定义数据字段生成海报 长按保存海报图片 可用于商品推销分享",
"keywords": [
"海报",
"",
"生成海报",
"",
"电商",
"",
"poster",
"",
"商品分享"
],
"repository": "",
"engines": {
"HBuilderX": "^3.7.2"
},
"dcloudext": {
"type": "component-vue",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"Vue": {
"vue2": "y",
"vue3": "y"
},
"App": {
"app-vue": "y",
"app-nvue": "y"
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "y",
"IE": "y",
"Edge": "y",
"Firefox": "y",
"Safari": "y"
},
"小程序": {
"微信": "y",
"阿里": "y",
"百度": "y",
"字节跳动": "y",
"QQ": "y",
"钉钉": "y",
"快手": "y",
"飞书": "y",
"京东": "y"
},
"快应用": {
"华为": "y",
"联盟": "y"
}
}
}
}
}

View File

@@ -0,0 +1,137 @@
# cc-poster
#### 使用方法
```使用方法
<!-- 自定义生成海报组件 -->
<!-- @success成功事件 imgSrc图片地址 QrSrc二维码图片地址 Title标题 PriceTxt价格 OriginalTxt原始价格 LineType -->
<cc-poster @success="posterSuccess" :imgSrc="goods.itempic" :QrSrc="erweimapath" :Title="goods.itemtitle" :PriceTxt="goods.itemendprice" :OriginalTxt="goods.itemprice":LineType="false"></cc-poster>
```
#### HTML代码实现部分
```html
<template>
<view>
<button style="margin: 66px 36px;" @click="showModal"> 生成海报 </button>
<view class="cu-modal" :class="modalName == 'Image' ? 'show' : ''" @tap="hideModal">
<view class="cu-dialog" v-if="goods && erweimapath && haibaoShow" @tap="hideModal">
<view class="bg-img">
<!-- 自定义生成海报组件 -->
<!-- @success成功事件 imgSrc图片地址 QrSrc二维码图片地址 Title标题 PriceTxt价格 OriginalTxt原始价格 LineType -->
<cc-poster @success="posterSuccess" :imgSrc="goods.itempic" :QrSrc="erweimapath"
:Title="goods.itemtitle" :PriceTxt="goods.itemendprice" :OriginalTxt="goods.itemprice"
:LineType="false"></cc-poster>
</view>
</view>
</view>
</view>
</template>
<script>
//高德SDK
export default {
data() {
return {
haibaoImg: null,
modalName: '',
haibaoShow: false,
erweimapath: "https://www.xinhuanet.com/politics/2016-07/19/5119875106653616178_11n.jpg",
goods: {
"itemid": "2nNozWXiotnRwxGUakBuXUD-0npyN5GSNmYDb0pgTxw",
"itemtitle": "开丽产妇卫生巾产后专用刀纸月子安睡裤计量卫生巾孕妇用品安心裤",
"itemshorttitle": "开丽产妇卫生巾月子安睡裤计量卫生",
"itemdesc": "【开丽旗舰店】安心裤型亲肤面料,产后专用不勒刀口,免穿设计方便,超强吸收加长加大款,产妇的优质选择!【赠运费险】",
"itemprice": "39.90",
"todaysale": "3",
"itempic": "https://img.alicdn.com/imgextra/i1/2742920221/O1CN01nFqqJ41DVGNfjYMmu_!!2742920221.jpg",
"itemendprice": "29.91",
}
};
},
onLoad() {
},
methods: {
posterSuccess(haibaoImg) {
this.haibaoImg = haibaoImg;
this.modalName = 'Image';
},
showModal() {
if (!this.haibaoImg) {
this.haibaoShow = true;
uni.showLoading({
title: '海报生成中...'
});
} else {
this.modalName = 'Image';
}
},
hideModal() {
this.modalName = null;
}
}
};
</script>
<style lang="scss">
.cu-modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1110;
opacity: 0;
outline: 0;
text-align: center;
-ms-transform: scale(1.185);
transform: scale(1.185);
backface-visibility: hidden;
perspective: 2000upx;
background: rgba(0, 0, 0, 0.6);
transition: all 0.3s ease-in-out 0s;
pointer-events: none;
}
.cu-modal::before {
content: "\200B";
display: inline-block;
height: 100%;
vertical-align: middle;
}
.cu-modal.show {
opacity: 1;
transition-duration: 0.3s;
-ms-transform: scale(1);
transform: scale(1);
overflow-x: hidden;
overflow-y: auto;
pointer-events: auto;
}
.cu-dialog {
position: relative;
display: inline-block;
vertical-align: middle;
margin-left: auto;
margin-right: auto;
width: 680upx;
max-width: 100%;
background-color: #f8f8f8;
border-radius: 10upx;
overflow: hidden;
}
</style>
```