123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- <template>
- <view :style="viewColor">
- <view class='poster-poster' v-if="posterStatus">
- <view class='poster-pop'>
- <image src='../../../static/images/poster-close.png' class='close' @click="posterImageClose"></image>
- <view class="user-code">
- <image class="canvas" :style="{width:wd+'px', height:hg+'px'}" :src="posterImage" v-if="posterImage"></image>
- <canvas class="canvas" :style="{width:wd+'px',height:hg+'px'}" canvas-id="myCanvas" v-else></canvas>
- </view>
-
- <view v-if="posterImage" class='save-poster' @click="savePosterPath(posterImage)">保存到手机</view>
-
-
- <view v-if="posterImage" class="keep">长按图片可以保存到手机</view>
-
- </view>
- </view>
- <view class='mask' v-if="posterImage"></view>
- </view>
- </template>
- <script>
-
-
-
-
-
-
-
-
-
- import { imgToBase } from '@/api/user.js';
- import { mapGetters } from 'vuex';
- export default {
- computed: mapGetters(['viewColor']),
- data() {
- return {
- wd: 225,
- hg: 357,
- posterImage: "",
- posterBag: '../static/images/poster-bag.png',
- }
- },
- props: {
- picData: {
- type: Object
- },
- posterStatus: {
- type: Boolean,
- default: false
- }
- },
- watch: {
- posterStatus(data) {
- if (data) {
- this.posterShow()
- }
- },
- },
- onLoad() {},
- methods: {
- async posterShow() {
- let that = this;
- uni.showLoading({
- title: '海报生成中',
- mask: true
- });
- let arr, bag_pic, goods_img, mp_code
- goods_img = await that.imgToBase(that.picData.goodsPic)
- mp_code = await that.imgToBase(that.picData.codePic)
-
- arr = [that.posterBag, goods_img, mp_code]
-
-
- arr = [that.posterBag, await that.downloadFilestoreImage(that.picData.goodsPic), await that.downloadFilestoreImage(
- that.picData.codePic)]
-
- that.bargainPosterCanvas(arr, that.picData.title, that.picData.label, that.picData.msg, that.picData.price,
- that.wd,
- that.hg,
- (tempFilePath) => {
- that.posterImage = tempFilePath
- });
- },
- async imgToBase(url) {
- let res = await imgToBase({
- image: url
- })
- return res.data.image
- },
- savePosterPath(url) {
- let that = this;
-
- uni.getSetting({
- success(res) {
- if (!res.authSetting['scope.writePhotosAlbum']) {
- uni.authorize({
- scope: 'scope.writePhotosAlbum',
- success() {
- uni.saveImageToPhotosAlbum({
- filePath: url,
- success: function(res) {
- that.$util.Tips({
- title: '保存成功',
- icon: 'success'
- });
- },
- fail: function(res) {
- that.$util.Tips({
- title: '保存失败'
- });
- }
- });
- }
- });
- } else {
- uni.saveImageToPhotosAlbum({
- filePath: url,
- success: function(res) {
- that.$util.Tips({
- title: '保存成功',
- icon: 'success'
- });
- },
- fail: function(res) {
- that.$util.Tips({
- title: '保存失败'
- });
- }
- });
- }
- }
- });
-
-
- uni.saveImageToPhotosAlbum({
- filePath: that.posterImage,
- success: function(res) {
- that.posterImageClose();
- that.$util.Tips({
- title: '保存成功',
- icon: 'success'
- });
- },
- fail: function(res) {
- that.$util.Tips({
- title: '保存失败'
- });
- },
- })
-
- },
- posterImageClose() {
- this.$emit('update:posterStatus', false)
- this.posterImage = ""
- },
-
- downloadFilestoreImage(url) {
-
- let ishttps = url.split('//')[0] == 'https:'
- if (!ishttps) {
- url = 'https://'+url.split('//')[1]
- }
-
- return new Promise((resolve, reject) => {
- let that = this;
- uni.downloadFile({
- url: url,
- success: function(res) {
- resolve(res.tempFilePath);
- },
- fail: function() {
- return that.$util.Tips({
- title: ''
- });
- }
- });
- })
- },
- bargainPosterCanvas(arr2, title, label, msg, price, wd, hg, successFn) {
- let that = this;
- const ctx = uni.createCanvasContext('myCanvas', this);
- ctx.clearRect(0, 0, 0, 0);
-
- ctx.fillStyle = '#ccc';
- ctx.fillRect(0, 0, wd, hg);
- uni.getImageInfo({
- src: arr2[0],
- success: (res) => {
- console.log(arr2)
- const WIDTH = res.width;
- const HEIGHT = res.height;
- ctx.drawImage(arr2[0], 0, 0, wd, hg);
-
- let labelx = 0.6656
- let labely = 0.167
- let pricex = 0.1847
- let pricey = 0.175
- let codex = 0.385
- let codey = 0.77
- let picturex = 0.1571
- let picturey = 0.2916
- let picturebx = 0.6857
- let pictureby = 0.3916
- let msgx = 0.1036
- let msgy = 0.2306
- ctx.drawImage(arr2[1], wd * picturex, hg * picturey, wd * picturebx, hg * pictureby);
- ctx.save();
- ctx.drawImage(arr2[2], wd * codex, hg * codey, 55, 55);
- ctx.save();
-
- const CONTENT_ROW_LENGTH = 30;
- let [contentLeng, contentArray, contentRows] = that.textByteLength(title, CONTENT_ROW_LENGTH);
- if (contentRows > 2) {
- contentRows = 2;
- let textArray = contentArray.slice(0, 2);
- textArray[textArray.length - 1] += '…';
- contentArray = textArray;
- }
- ctx.setTextAlign('left');
- ctx.setFillStyle('#000');
- if (contentArray.length < 2) {
- ctx.setFontSize(14);
- } else {
- ctx.setFontSize(12);
- }
- let contentHh = 3;
- for (let m = 0; m < contentArray.length; m++) {
- if (m) {
- ctx.fillText(contentArray[m], 20, 20 + contentHh * m + 13, 1100);
- } else {
- ctx.fillText(contentArray[m], 20, 20, 1100);
- }
- }
-
- ctx.setTextAlign('left')
- ctx.setFontSize(10);
- ctx.setFillStyle('#FFF');
- ctx.fillText(label, wd * labelx, hg * labely);
- ctx.save();
-
- ctx.setFillStyle('red');
-
- ctx.font = "bold 18px Arial";
- ctx.fillText(price, wd * pricex, hg * pricey);
- ctx.save();
-
- ctx.setFontSize(8);
- ctx.setFillStyle('#3F3F3F');
- ctx.fillText(msg, wd * msgx, hg * msgy);
- ctx.save();
- ctx.draw(true, () => {
- uni.canvasToTempFilePath({
- canvasId: 'myCanvas',
- fileType: 'png',
- quality: 1,
- success: (res) => {
- console.log(res)
- successFn && successFn(res.tempFilePath);
- uni.hideLoading();
- }
- }, this)
- });
- },
- fail: function(err) {
- uni.hideLoading();
- that.Tips({
- title: '无法获取图片信息'
- });
- }
- })
- },
- textByteLength(text, num) {
- let strLength = 0;
- let rows = 1;
- let str = 0;
- let arr = [];
- for (let j = 0; j < text.length; j++) {
- if (text.charCodeAt(j) > 255) {
- strLength += 2;
- if (strLength > rows * num) {
- strLength++;
- arr.push(text.slice(str, j));
- str = j;
- rows++;
- }
- } else {
- strLength++;
- if (strLength > rows * num) {
- arr.push(text.slice(str, j));
- str = j;
- rows++;
- }
- }
- }
- arr.push(text.slice(str, text.length));
- return [strLength, arr, rows]
- },
- }
- }
- </script>
- <style>
- .poster-poster {
- width: 100%;
- height: 100%;
- width: 450rpx;
- height: 714rpx;
- }
- .poster-pop {
- position: fixed;
- left: 50%;
- transform: translateX(-50%);
- top: 50%;
- margin: 0 auto;
- margin-top: -357rpx;
- z-index: 999;
- }
- .poster-pop image {
- width: 100%;
- height: 100%;
- display: block;
- }
- .poster-pop .close {
- width: 46rpx;
- height: 75rpx;
- position: fixed;
- right: 0;
- top: -73rpx;
- display: block;
- z-index: 3999;
- }
- .poster-pop .save-poster {
- background-color: var(--view-theme);
- font-size: :22rpx;
- color: #fff;
- text-align: center;
- height: 76rpx;
- line-height: 76rpx;
- width: 100%;
- margin-top: 20rpx;
- }
- .poster-pop .keep {
- color: #fff;
- text-align: center;
- font-size: 25rpx;
- margin-top: 10rpx;
- }
- .mask {
- z-index: 300 !important;
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background-color: rgba(0, 0, 0, 0.6);
- z-index: 9;
- }
- </style>
|