|
|
<?php
|
|
|
|
|
|
namespace app\distribution\service;
|
|
|
|
|
|
use app\distribution\model\DistributionUserCode;
|
|
|
use think\facade\Request;
|
|
|
|
|
|
class Poster extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 生成分销二维码 H5页面
|
|
|
* @date 2022-03-01
|
|
|
*/
|
|
|
public function getH5DistributionCode($user_id = '')
|
|
|
{
|
|
|
if (!$user_id) {
|
|
|
// 默认为请求人的用户ID
|
|
|
$user_id = $this->userId;
|
|
|
}
|
|
|
|
|
|
$oss_param = get_ali_oss_config();
|
|
|
|
|
|
$user_code_model = new DistributionUserCode();
|
|
|
$oss_class = new \ali\oss\Oss($oss_param);
|
|
|
|
|
|
// 获取用户分销二维码 H5页面
|
|
|
$user_h5_code = $user_code_model->getOneData([
|
|
|
['uid', '=', $this->mid],
|
|
|
['user_id', '=', $user_id]
|
|
|
], 'h5_code');
|
|
|
|
|
|
// 如果存在则直接返回
|
|
|
if (!empty($user_h5_code)) {
|
|
|
return sendSuccessArray([
|
|
|
'h5_code' => $user_h5_code
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
|
|
|
// 扫码进入的H5页面
|
|
|
$url = Request::domain() . '/h5/#/pages/user/register/trade?user_id=' . $user_id;
|
|
|
|
|
|
// 生成用户分销二维码 H5页面
|
|
|
$local_code_url = create_qr_code_to_server($url, './temp/uid' . $this->mid . '/distribution_code_h5', 'h5_' . $user_id . '.jpg');
|
|
|
|
|
|
// 本地文件上传至oss
|
|
|
$oss_file_dirname = 'uid' . $this->mid . '/distribution_code';
|
|
|
$oss_file_name = 'h5_' . $user_id . '.jpg';
|
|
|
$result = $oss_class->uploadFile($local_code_url, $oss_file_dirname, $oss_file_name);
|
|
|
|
|
|
// 存储到分销二维码表
|
|
|
$data = [
|
|
|
'uid' => $this->mid,
|
|
|
'user_id' => $user_id,
|
|
|
'h5_code' => $result['url']
|
|
|
];
|
|
|
$res = $user_code_model->updateCode($data);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(2001, '更新二维码失败');
|
|
|
}
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
'h5_code' => $result['url']
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成海报
|
|
|
* @param string $bg_img 背景图
|
|
|
* @param string $project_name 项目名称
|
|
|
* @param string $user_code 用户二维码
|
|
|
* @param array $info 用户信息
|
|
|
* @param string $info ['head_img'] 用户头像
|
|
|
* @param string $info ['nick_name'] 用户昵称
|
|
|
* @date 2022-08-26
|
|
|
*/
|
|
|
public function createPoster($bg_img, $project_name, $user_code, $info)
|
|
|
{
|
|
|
$oss_param = get_ali_oss_config();
|
|
|
|
|
|
$handle_class = new \ali\oss\image\Handle($oss_param);
|
|
|
$oss_class = new \ali\oss\Oss($oss_param);
|
|
|
|
|
|
/* 如果是微信官方的链接头像,需要下载到服务器并保存到OSS
|
|
|
需要线上SSL 环境
|
|
|
https://thirdwx.qlogo.cn为微信官方头像的域名
|
|
|
https://tfs.alipayobjects.com为微信官方头像的域名 */
|
|
|
if (strstr($info['head_img'], 'thirdwx.qlogo.cn') !== false
|
|
|
|| strstr($info['head_img'], 'tfs.alipayobjects.com') !== false) {
|
|
|
// 本地存储路径
|
|
|
$local_file_path = file_save_to_server($info['head_img'], './temp', 'user_' . $this->userId . '.jpg');
|
|
|
|
|
|
// 本地文件上传至oss
|
|
|
$oss_file_dirname = 'uid2/user_head_img';
|
|
|
$oss_file_name = 'user_' . $this->userId . '.jpg';
|
|
|
$result = $oss_class->uploadFile($local_file_path, $oss_file_dirname, $oss_file_name);
|
|
|
|
|
|
// OSS用户头像
|
|
|
$info['head_img'] = $result['url'];
|
|
|
}
|
|
|
|
|
|
// 小程序名称
|
|
|
$project_name_text = base64url_encode($project_name);
|
|
|
|
|
|
// 简介
|
|
|
$brief_text = base64url_encode('扫一扫二维码,加入我的团队');
|
|
|
|
|
|
// 获取用户头像OSS的object
|
|
|
$user_head_image_object_arr = explode('juchenghb1.oss-cn-qingdao.aliyuncs.com/', $info['head_img']);
|
|
|
|
|
|
/* 用户头像水印
|
|
|
缩放:将原图等比缩放为延伸出指定w与h的矩形框外的最小图片,然后将超出的部分进行居中裁剪 宽50 高50
|
|
|
内切圆:半径25
|
|
|
格式转换:png格式 */
|
|
|
$user_head_image = base64url_encode(end($user_head_image_object_arr) . '?x-oss-process=image/resize,m_fill,w_50,h_50/circle,r_25/format,png');
|
|
|
|
|
|
// 简介
|
|
|
$user_nick_name_text = base64url_encode($info['nick_name']);
|
|
|
|
|
|
/* 二维码水印
|
|
|
缩放:将原图缩放为指定w与h的矩形内的最大图片,然后使用指定颜色居中填充空白部分 宽180 高180 */
|
|
|
$user_code_arr = explode('juchenghb1.oss-cn-qingdao.aliyuncs.com/', $user_code);
|
|
|
$user_code_image = base64url_encode(end($user_code_arr) . '?x-oss-process=image/resize,m_pad,w_180,h_180');
|
|
|
|
|
|
/* 缩放:将原图等比缩放为延伸出指定w与h的矩形框外的最小图片,然后将超出的部分进行居中裁剪 宽700 高1000
|
|
|
添加文字水印(小程序名称):$project_name_text 据左上 x=57 y=775 颜色1A1A1A 字体大小40
|
|
|
添加文字水印(简介):$brief_text 据左上 x=57 y=831 颜色4D4D4D 字体大小26
|
|
|
添加图片水印(用户头像):$user_head_image 据左上 x=57 y=891
|
|
|
添加文字水印(用户昵称):$user_head_image 据左上 x=116 y=898 颜色666666 字体大小26
|
|
|
添加图片水印(二维码):$user_code_image 据左上 x=471 y=772 */
|
|
|
$style = "resize,m_fill,w_700,h_1000"
|
|
|
. "/watermark,g_nw,x_57,y_775,text_$project_name_text,color_1A1A1A,size_40"
|
|
|
. "/watermark,g_nw,x_57,y_831,text_$brief_text,color_4D4D4D,size_26"
|
|
|
. "/watermark,g_nw,x_57,y_891,image_" . $user_head_image
|
|
|
. "/watermark,g_nw,x_116,y_898,text_$user_nick_name_text,color_666666,size_26"
|
|
|
. "/watermark,g_nw,x_471,y_772,image_" . $user_code_image;
|
|
|
|
|
|
// 合成的图片路径
|
|
|
$result = $handle_class->handleImage($bg_img, $style);
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
}
|