Files
shop-platform/src/addon/fenxiao/model/Poster.php

260 lines
10 KiB
PHP

<?php
/**
*/
namespace addon\fenxiao\model;
use app\model\BaseModel;
use extend\Poster as PosterExtend;
use app\model\upload\Upload;
/**
* 海报生成类
*/
class Poster extends BaseModel
{
/**
* 分销海报
* @param $app_type
* @param $page
* @param $qrcode_param
* @param $site_id
* @param null $template_info
* @return array|\extend\multitype|mixed|string|void
*/
public function distribution($app_type, $page, $qrcode_param, $site_id, $template_info = null)
{
try {
$qrcode_info = $this->getQrcode($app_type, $page, $qrcode_param, $site_id);
if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;
$member_info = $this->getMemberInfo($qrcode_param[ 'source_member' ]);
if (empty($member_info)) return $this->error('未获取到会员信息');
$poster = new PosterExtend(720, 1280);
if (empty($template_info)) {
$option = [
[
'action' => 'imageCopy', // 背景图
'data' => [
'upload/poster/bg/fenxiao_2.png',
0,
0,
720,
1280,
'square',
0,
1
]
],
[
'action' => 'imageCopy', // 写入二维码
'data' => [
$qrcode_info[ 'data' ][ 'path' ],
492,
1035,
150,
150,
'square',
0,
1
]
],
[
'action' => 'imageCircularCopy', // 写入用户头像
'data' => [
!empty($member_info[ 'headimg' ]) ? $member_info[ 'headimg' ] : 'public/static/img/default_img/head.png',
65,
1020,
100,
100
]
],
[
'action' => 'imageText', // 写入分享人昵称
'data' => [
$member_info[ 'nickname' ],
22,
[ 255, 129, 61 ],
180,
1060,
440,
1,
true,
1
]
]
];
} else {
$params = $template_info;
$poster = new PosterExtend(740, 1250);
$fontRate = 0.725;
$nickname_color = is_array($params[ 'template_json' ][ 'nickname_color' ]) ? $params[ 'template_json' ][ 'nickname_color' ] : hex2rgb($params[ 'template_json' ][ 'nickname_color' ]);
$option = [
[
'action' => 'imageCopy', // 写入背景图
'data' => [
img($params[ 'background' ]),
0,
0,
740,
1250,
'square',
0,
1
]
],
[
'action' => 'imageCopy', // 写入二维码
'data' => [
$qrcode_info[ 'data' ][ 'path' ],
(int) $params[ 'qrcode_left' ] * 2,
(int) $params[ 'qrcode_top' ] * 2,
(int) $params[ 'qrcode_width' ] * 2,
(int) $params[ 'qrcode_height' ] * 2,
'square',
0,
1
]
],
[
'action' => 'imageText', // 写入分享语
'data' => [
$params[ 'template_json' ][ 'share_content' ],
$params[ 'template_json' ][ 'share_content_font_size' ] * $fontRate * 2,
is_array($params[ 'template_json' ][ 'share_content_color' ]) ? $params[ 'template_json' ][ 'share_content_color' ] : hex2rgb($params[ 'template_json' ][ 'share_content_color' ]),
$params[ 'template_json' ][ 'share_content_left' ] * 2,
( $params[ 'template_json' ][ 'share_content_top' ] + $params[ 'template_json' ][ 'share_content_font_size' ] ) * 2,
$params[ 'template_json' ][ 'share_content_width' ] * 2,
1
]
],
[
'action' => 'imageCopy', // 写入用户头像
'data' => [
!empty($member_info[ 'headimg' ]) ? img($member_info[ 'headimg' ]) : img('public/static/img/default_img/head.png'),
$params[ 'template_json' ][ 'headimg_left' ] * 2,
$params[ 'template_json' ][ 'headimg_top' ] * 2,
$params[ 'template_json' ][ 'headimg_width' ] * 2,
$params[ 'template_json' ][ 'headimg_height' ] * 2,
!empty($params[ 'template_json' ][ 'headimg_shape' ]) ? $params[ 'template_json' ][ 'headimg_shape' ] : 'square',
0,
$params[ 'template_json' ][ 'headimg_is_show' ]
]
],
[
'action' => 'imageText', // 写入分享人昵称
'data' => [
$member_info[ 'nickname' ],
$params[ 'template_json' ][ 'nickname_font_size' ] * $fontRate * 2,
$nickname_color,
$params[ 'template_json' ][ 'nickname_left' ] * 2,
( $params[ 'template_json' ][ 'nickname_top' ] + $params[ 'template_json' ][ 'nickname_font_size' ] ) * 2,
$params[ 'template_json' ][ 'nickname_width' ] * 2,
1,
true,
$params[ 'template_json' ][ 'nickname_is_show' ]
]
],
];
}
$option_res = $poster->create($option);
if (is_array($option_res)) return $option_res;
$pic_name = "";
if (!empty($params)) {
$pic_name = md5(json_encode([
'id' => $params[ 'template_id' ],
'poster_name' => $params[ 'poster_name' ]
]));
$pic_name = "_" . $pic_name;
}
$res = $option_res->jpeg('upload/poster/distribution', 'distribution_' . $qrcode_param[ 'source_member' ] . $pic_name . '_' . $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 $member_id
* @return mixed
*/
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, $site_id)
{
$res = event('Qrcode', [
'site_id' => $site_id,
'app_type' => $app_type,
'type' => 'get',
'data' => $qrcode_param,
'page' => $page,
'qrcode_path' => 'upload/qrcode/distribution',
'qrcode_name' => 'distribution' . '_' . $qrcode_param[ 'source_member' ] . '_' . $site_id,
], true);
return $res;
}
/**
* 获取分销海报
* @param $param
* @return array|\extend\multitype|PosterExtend|mixed|string|void
*/
public function getFenxiaoPoster($param)
{
$app_type = $param[ 'app_type' ] ?? 'h5';
$qrcode_param = $param[ 'qrcode_param' ] ?? [];
$site_id = $param[ 'site_id' ] ?? 0;
$page = $param[ 'page' ] ?? '';
$template_id = $param[ 'template_id' ] ?? 'default';
if ($template_id == 'default') {
//默认海报
$template_info = null;
} else {
//设置的海报
$template_info = model('poster_template')->getInfo([
[ 'site_id', '=', $site_id ],
[ 'template_type', '=', 'fenxiao' ],
[ 'template_status', '=', 1 ],
[ 'template_id', '=', $template_id ],
]);
if (empty($template_info)) return $this->error(null, '模板信息有误');
$template_info[ 'template_json' ] = json_decode($template_info[ 'template_json' ], true);
}
$res = $this->distribution($app_type, $page, $qrcode_param, $site_id, $template_info);
return $res;
}
}