admin/addon/giftcard/model/membercard/Poster.php

424 lines
14 KiB
PHP

<?php
/**
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都云之牛科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.cdcloudshop.com
* =========================================================
*/
namespace addon\giftcard\model\membercard;
use addon\giftcard\model\transfer\Blessing;
use app\model\BaseModel;
use app\model\system\Site;
use app\model\upload\Upload;
use extend\Poster as PosterExtend;
/**
* 海报生成类
*/
class Poster extends BaseModel
{
/**
* 海报
*/
public function poster($app_type, $page, $qrcode_param, $promotion_type, $site_id)
{
try {
$blessing_model = new Blessing();
$params = array (
'no' => $qrcode_param[ 'no' ],
'member_id' => $qrcode_param[ 'source_member' ]
);
$card_info = $blessing_model->getBlessingDetail($params)[ 'data' ] ?? [];
if (empty($card_info)) return $this->error('未获取到信息');
$qrcode_info = $this->getQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id);
if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;
//判断海报是否存在或停用
$site_model = new Site();
$condition = array (
[ "site_id", "=", $site_id ]
);
$site_info = $site_model->getSiteInfo($condition);
if (!empty($qrcode_param[ 'source_member' ])) {
$member_info = $this->getMemberInfo($qrcode_param[ 'source_member' ]);
}
$poster_width = 720;
$poster_height = 1280;
$poster = new PosterExtend($poster_width, $poster_height);
$option = [
[
'action' => 'imageCopy', // 背景图
'data' => [
img('upload/poster/bg/promotion_4.jpg'),
0,
0,
720,
1280,
'square',
0,
1
]
],
[
'action' => 'imageCopy', // 商品图
'data' => [
$card_info[ 'card_cover' ],
100,
300,
520,
310,
'square',
5,
1
]
],
[
'action' => 'imageCopy', // 二维码
'data' => [
$qrcode_info[ 'data' ][ 'path' ],
100,
930,
150,
150,
'square',
0,
1
]
],
[
'action' => 'imageText', // 写入礼品卡名称
'data' => [
$card_info[ 'card_name' ],
25,
[ 34, 34, 34 ],
100,
660,
540,
2,
true,
1
]
],
[
'action' => 'imageText', // 写入祝福语
'data' => [
$card_info[ 'blessing' ],
22,
[ 255, 0, 0 ],
150,
800,
480,
1,
false,
1
]
],
[
'action' => 'imageText', // 写入固定文字
'data' => [
'扫码查看礼品详情',
22,
[ 51, 51, 51 ],
290,
1000,
548,
1,
true,
1
]
],
[
'action' => 'imageText', // 写入有效期
'data' => [
'有效期:' . $card_info[ 'valid_time' ] > 0 ? time_to_date($card_info[ 'valid_time' ]) : '永久有效',
18,
[ 51, 51, 51 ],
290,
1060,
548,
1,
false,
1
]
],
];
if (!empty($member_info)) {
$member_option = [
[
'action' => 'imageCircularCopy', // 写入用户头像
'data' => [
!empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : 'public/static/img/default_img/head.png',
86,
90,
101,
101
]
],
[
'action' => 'imageText', // 写入分享人昵称
'data' => [
$member_info[ 'nickname' ],
28,
[ 255, 255, 255 ],
210,
135,
420,
1,
true,
1
]
],
[
'action' => 'imageText',
'data' => [
'送给您一张礼品卡',
22,
[ 255, 255, 255 ],
200,
185,
430,
1,
0,
1
]
]
];
$option = array_merge($option, $member_option);
}
$option_res = $poster->create($option);
if (is_array($option_res)) return $option_res;
$res = $option_res->jpeg('upload/poster/goods', 'goods_' . $promotion_type . '_' . $card_info[ 'no' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . time() . '_' . $app_type);
if ($res[ 'code' ] == 0) {
$upload = new Upload($site_id);
$cloud_res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
if ($cloud_res[ 'code' ] >= 0) {
return $this->success([ "path" => $cloud_res[ 'data' ] ]);
} else {
return $this->error();
}
}
return $res;
} catch (\Exception $e) {
return $this->error($e->getMessage() . $e->getFile() . $e->getLine());
}
}
/**
* 获取用户信息
* @param unknown $member_id
*/
private function getMemberInfo($member_id)
{
$info = model('member')->getInfo([ 'member_id' => $member_id ], 'nickname,headimg');
return $info;
}
/**
* 获取二维码
* @param unknown $app_type 请求类型
* @param unknown $page uniapp页面路径
* @param unknown $qrcode_param 二维码携带参数
* @param string $promotion_type 活动类型 null为无活动
*/
private function getQrcode($app_type, $page, $qrcode_param, $promotion_type, $site_id)
{
$res = event('Qrcode', [
'site_id' => $site_id,
'app_type' => $app_type,
'type' => 'create',
'data' => $qrcode_param,
'page' => $page,
'qrcode_path' => 'upload/qrcode/goods',
'qrcode_name' => 'goods_' . $promotion_type . '_' . $qrcode_param[ 'no' ] . '_' . $qrcode_param[ 'source_member' ] . '_' . $site_id,
], true);
return $res;
}
/**
* 获取海报信息
* @param unknown $template_id
*/
private function getTemplateInfo($template_id)
{
$info = model('poster_template')->getInfo([ 'template_id' => $template_id ], 'template_id,template_status');
return $info;
}
/**
* 分享图片
* @param $page
* @param $qrcode_param
* @param $site_id
* @return array
*/
public function shareImg($page, $qrcode_param, $site_id)
{
try {
$goods_info = $this->getGoodsInfo($qrcode_param[ 'id' ]);
if (empty($goods_info)) return $this->error('未获取到商品信息');
$file_path = 'upload/share_img/bargain_' . $goods_info[ 'pintuan_id' ] . '/sku_' . $goods_info[ 'sku_id' ] . '.jpg';
if (file_exists($file_path)) return $this->success([ 'path' => $file_path ]);
$poster_width = 600;
$poster_height = 480;
$poster = new PosterExtend($poster_width, $poster_height);
$option = [
[
'action' => 'setBackground', // 设背景色
'data' => [ 255, 255, 255 ]
],
[
'action' => 'imageCopy', // 商品图
'data' => [
$goods_info[ 'sku_image' ],
30,
145,
200,
200,
'square',
50,
1
]
],
[
'action' => 'imageText', // 写入商品名称
'data' => [
$goods_info[ 'sku_name' ],
22,
[ 51, 51, 51 ],
250,
190,
330,
2,
false,
1
]
],
[
'action' => 'imageText', // 写入商品价格
'data' => [
'拼团价:¥',
15,
[ 255, 0, 0 ],
250,
300,
300,
2,
false,
1
]
],
[
'action' => 'imageText', // 写入商品价格
'data' => [
$goods_info[ 'pintuan_price' ],
30,
[ 255, 0, 0 ],
345,
300,
300,
2,
false,
1,
PUBLIC_PATH . 'static/font/custom.ttf'
]
],
[
'action' => 'imageText', // 写入商品原价
'data' => [
'原 价:¥',
15,
[ 153, 153, 153 ],
250,
340,
300,
2,
false,
1
]
],
[
'action' => 'imageText', // 写入商品原价
'data' => [
$goods_info[ 'price' ],
16,
[ 153, 153, 153 ],
345,
338,
300,
2,
false,
1,
PUBLIC_PATH . 'static/font/custom.ttf',
]
],
// 划线(两条线)
[
'action' => 'imageline',
'data' => [
325,
330,
325 + imagettfbbox(16, 0, PUBLIC_PATH . 'static/font/custom.ttf', '¥ ' . $goods_info[ 'price' ])[ 2 ],
330,
[ 153, 153, 153 ],
]
],
[
'action' => 'imageline',
'data' => [
325,
331,
325 + imagettfbbox(16, 0, PUBLIC_PATH . 'static/font/custom.ttf', '¥ ' . $goods_info[ 'price' ])[ 2 ],
331,
[ 153, 153, 153 ],
]
],
[
'action' => 'imageCopy', // 背景图
'data' => [
img('upload/share_img/bg/pintuan_1.png'),
0,
0,
600,
480,
'square',
0,
1
]
],
];
$option_res = $poster->create($option);
if (is_array($option_res)) {
return $option_res;
}
$res = $option_res->jpeg('upload/share_img/pintuan_' . $goods_info[ 'pintuan_id' ],
'sku_' . $goods_info[ 'sku_id' ]);
return $res;
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/**
* 删除分享图片
* @param int $pintuan_id
*/
public function clearShareImg(int $pintuan_id)
{
$dir = 'upload/share_img/pintuan_' . $pintuan_id;
@deleteDir($dir);
}
}