Commit a2c48ff8 by 胡悦

修改分享画布问题

1 parent 63e468b1
......@@ -27,8 +27,7 @@
},
"usingComponents": {
"authorize": "components/Authorize/Authorize",
"phone-number": "components/PhoneNumber/PhoneNumber",
"poster": "components/Poster/poster/index"
"phone-number": "components/PhoneNumber/PhoneNumber"
},
"sitemapLocation": "sitemap.json",
"lazyCodeLoading": "requiredComponents"
......
{
"component": true
}
\ No newline at end of file
<!--index.wxml-->
<view class="container">
<canvas canvas-id='canvasid' class="canvas {{debug ? 'debug' : 'pro'}}" style='width: {{pxWidth}}px; height: {{pxHeight}}px;'></canvas>
</view>
.canvas {
width: 750rpx;
height: 750rpx;
}
.canvas.pro {
position: absolute;
bottom: 0;
left: 0;
transform: translate3d(-9999rpx, 0, 0);
}
.canvas.debug {
position: absolute;
bottom: 0;
left: 0;
border: 1rpx solid #ccc;
}
\ No newline at end of file
Component({
properties: {
config: {
type: Object,
value: {},
},
preload: { // 是否预下载图片资源
type: Boolean,
value: false,
},
hideLoading: { // 是否隐藏loading
type: Boolean,
value: false,
}
},
ready() {
if (this.data.preload) {
const poster = this.selectComponent('#poster');
this.downloadStatus = 'doing';
poster.downloadResource(this.data.config).then(() => {
this.downloadStatus = 'success';
this.trigger('downloadSuccess');
}).catch((e) => {
this.downloadStatus = 'fail';
this.trigger('downloadFail', e);
});
}
},
methods: {
trigger(event, data) {
if (this.listener && typeof this.listener[event] === 'function') {
this.listener[event](data);
}
},
once(event, fun) {
if (typeof this.listener === 'undefined') {
this.listener = {};
}
this.listener[event] = fun;
},
downloadResource(reset) {
return new Promise((resolve, reject) => {
if (reset) {
this.downloadStatus = null;
}
const poster = this.selectComponent('#poster');
if (this.downloadStatus && this.downloadStatus !== 'fail') {
if (this.downloadStatus === 'success') {
resolve();
} else {
this.once('downloadSuccess', () => resolve());
this.once('downloadFail', (e) => reject(e));
}
} else {
poster.downloadResource(this.data.config)
.then(() => {
this.downloadStatus = 'success';
resolve();
})
.catch((e) => reject(e));
}
})
},
onCreate(reset = false) {
!this.data.hideLoading && wx.showLoading({ mask: true, title: '生成中' });
return this.downloadResource(typeof reset === 'boolean' && reset).then(() => {
!this.data.hideLoading && wx.hideLoading();
const poster = this.selectComponent('#poster');
poster.create(this.data.config);
})
.catch((err) => {
!this.data.hideLoading && wx.hideLoading();
// wx.showToast({ icon: 'none', title: err.errMsg || '生成失败' });
this.triggerEvent('fail', err);
})
},
onCreateSuccess(e) {
const { detail } = e;
this.triggerEvent('success', detail);
},
onCreateFail(err) {
console.error(err);
this.triggerEvent('fail', err);
}
}
})
\ No newline at end of file
{
"component": true,
"usingComponents": {
"we-canvas": "../index/index"
}
}
\ No newline at end of file
<view bindtap='onCreate'>
<slot/>
</view>
<we-canvas id="poster" bind:success="onCreateSuccess" bind:fail="onCreateFail"/>
\ No newline at end of file
const defaultOptions = {
selector: '#poster'
};
function Poster(options = {}, that) {
options = {
...defaultOptions,
...options,
};
const pages = getCurrentPages();
let ctx = pages[pages.length - 1];
if (that) ctx = that
const poster = ctx.selectComponent(options.selector);
delete options.selector;
return poster;
};
Poster.create = (reset = false, that) => {
const poster = Poster({}, that);
if (!poster) {
console.error('请设置组件的id="poster"!!!');
} else {
return Poster({}, that).onCreate(reset);
}
}
export default Poster;
......@@ -30,7 +30,8 @@ Component({
* 组件的初始数据
*/
data: {
showWechant: false
showWechant: false,
// shareDisabled: true
},
/**
......@@ -97,5 +98,14 @@ Component({
phoneNumber: staffPhone,
});
},
shareClick() {
if (this.data.shareDisabled) {
wx.showToast({
title: '正在生成名片缩略图,请稍后分享',
icon: 'none',
duration: 2000
})
}
}
},
});
......@@ -34,7 +34,8 @@
<view class="phone-text">拨打电话</view>
</view>
<button class="btn-plain btn" bindtap="addPhone">保存名片</button>
<button class="btn-primary btn" open-type="share">分享名片</button>
<button wx:if="{{shareDisabled}}" class="btn-primary btn" bindtap="shareClick">分享名片</button>
<button wx:else class="btn-primary btn" open-type="share">分享名片</button>
</view>
</view>
<authorize buttonType="openSetting" show="{{showAuth}}" isShowCheck="{{false}}"></authorize>
\ No newline at end of file
......@@ -11,7 +11,6 @@ import Draw from "../../../utils/Draw";
import { ssoApi, entmicroApi } from "../../../api/api";
import util from "../../../utils/util";
import { trackData } from "../../../utils/data";
import Poster from '../../../components/Poster/poster/poster';
Page({
behaviors: [menuMixin],
/**
......@@ -64,7 +63,6 @@ Page({
clueCurrent: 0,
awardBanner: [{src: 'company-img7.png', name: '组织荣誉'}, {src: 'company-img71.png', name: '行业奖项'}],
awardCurrent: 0,
posterConfig:{},
shareDisabled: true
},
introduce(e) {
......@@ -163,7 +161,7 @@ Page({
this.getCardDetail();
});
wx.hideShareMenu({
menus: ['shareAppMessage', 'shareTimeline']
menus: ['shareAppMessage']
})
},
......@@ -207,7 +205,6 @@ Page({
* 用户点击右上角分享
*/
onShareAppMessage() {
console.log(util.getStorageSync("scene").cardId)
const cardId = util.getStorageSync("scene").cardId
const merchant = util.getStorageSync("scene").merchantId || util.getStorageSync("scene").merchant
return {
......@@ -228,12 +225,8 @@ Page({
// pixelRatio: 2,
// backgroundColor: '#096'
});
const x = draw_function.getTextWidth(data.staffName, 14) + 22
const drawCard = (callBack) => {
// let index = 0;
// const canvasWidth = 208,
// canvasHeight = 166,
// textBaseline = "top";
let index = 0;
const iconWidth = 9,
iconHeight = 9,
iconX = 18,
......@@ -288,15 +281,15 @@ Page({
{
text: data.staffName,
x: 18,
y: 50,
y: 36,
color: "#2D2D33",
fontWeight: 600,
fontSize: 14,
},
{
text: data.operation,
x: x,
y: 50,
x: 22 + draw_function.getTextWidth(data.staffName, 14),
y: 40,
color: "#6E6E7A",
fontWeight: 400,
fontSize: 8,
......@@ -304,71 +297,52 @@ Page({
{
text: data.staffPhone,
x: 30,
y: 85,
y: 80,
color: "#6E6E7A",
fontSize: 7,
},
{
text: "深圳市南山区留仙大道3370号南山智园崇文园区1号楼14层",
x: 30,
y: 99,
y: 94,
color: "#6E6E7A",
fontSize: 7,
lineNum: 2,
width: 324,
width: 185,
lineHeight: 9,
},
];
const width = 208
const height = 166
this.setData({ posterConfig: { width,
height,texts:textList,images:imgList, pixelRatio: 2} }, () => {
Poster.create();
const drawImages = (callBack) => {
draw_function.drawImages(imgList[index], () => {
index += 1;
if (index != imgList.length) drawImages(callBack);
else callBack();
});
// const drawImages = (callBack) => {
// draw_function.drawImages(imgList[index], () => {
// index += 1;
// if (index != imgList.length) drawImages(callBack);
// else callBack();
// });
// };
// drawImages(() => {
// try {
// textList.map((e, ind) => {
// e.textBaseline = textBaseline;
// draw_function.drawText(e);
// if (ind == textList.length - 1) throw true;
// });
// } catch (error) {
// draw_function.toDataURL(({ path }) => {
// this.setData({
// shareImg: path,
// });
// wx.hideLoading()
// wx.showShareMenu({
// menus: ['shareAppMessage', 'shareTimeline']
// })
// });
// }
// });
};
drawImages(() => {
try {
textList.map((e, ind) => {
e.textBaseline = textBaseline;
draw_function.drawText(e);
if (ind == textList.length - 1) throw true;
});
} catch (error) {
draw_function.toDataURL(({ path }) => {
this.setData({
shareImg: path,
shareDisabled: false
}, () => {
wx.showShareMenu({
menus: ['shareAppMessage']
})
})
});
}
});
};
drawCard();
},
/**
* 异步生成海报
*/
onPosterSuccess(src) {
const { detail } = src;
console.log(detail)
this.setData({
shareImg: detail,
},()=>{
wx.hideLoading()
wx.showShareMenu({
menus: ['shareAppMessage', 'shareTimeline']
})
});
},
// 获取用户昵称
getUserProfile(e) {
this.setData({
......
......@@ -4,7 +4,6 @@
<import src="../../template/menu/menu.wxml" />
<import src="index.skeleton.wxml"/>
<template is="skeleton" wx:if="true" />
<poster id="poster" config="{{posterConfig}}" bind:success="onPosterSuccess" bind:fail="onPosterFail"></poster>
<view class="main-box" wx:if="{{cardInfo}}" >
<user-info data-skeleton-hide="cardInfo" shareDisabled="{{shareDisabled}}" cardInfo="{{cardInfo}}"></user-info>
<view class="main-bottom">
......
......@@ -62,7 +62,7 @@ class Draw {
let image = this[_canvas_].createImage()
new Promise(resolve => {
image.onload = resolve
image.src = url
image.src = `${url}?v=${Date.now()}`
}).then(() => {
this[_ctx_].drawImage(image, x, y, width, height)
this[_ctx_].restore()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!