public function share()
{
if ($this->request->isPost()) {
$shop_id = hsc($this->request->post('shop_id'));
$uid = hsc($this->request->post('uid'));
if (!($shop_id && $uid)) {
return __error('缺少引數');
}
$shop_info = db::name('shop_info')->alias('i')
->join('shop_info_diff d', 'd.shop_info_id = i.id')
->where('i.id', $shop_id)
->where(['d.is_deleted' => 1, 'd.status' => 1])
->field('i.name,d.price,i.image')
->find();
if (empty($shop_info)) {
return __error('商品錯誤');
} else {
if ($shop_info['image']) {
$shop_info_image = CDNDomain() . explode(',', $shop_info['image'])[0];
} else {
$shop_info_image = get_url() . 'static/image/default.jpg';
}
}
$member_info = db::name('member')->where(['is_deleted' => 1, 'status' => 1, 'id' => $uid])->field('nickname,head_img')->find();
if (empty($member_info)) {
return __error('使用者資訊錯誤');
} else {
if ($member_info['head_img']) {
$member_info_image = $member_info['head_img'];
} else {
$member_info_image = get_url() . 'static/image/img.png';
}
}
$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
$data = [
'scene' => "?id=$shop_id&father_id=$uid",
'page' => "pages/shop/detail",
'width' => 430,
'auto_color' => false,
];
$data = json_encode($data);
$result = $this->curl_post_https($url, $data);
$res = chunk_split(base64_encode($result));
$ishas = DB::name('code')->where(['uid' => $uid, 'shop_id' => $shop_id])->field('id,code')->find();
if (!$ishas) {
$ishas['code'] = 'data:image/PNG;base64,' . $res;
$arr['code'] = 'data:image/PNG;base64,' . $res;
$arr['shop_id'] = $shop_id;
$arr['uid'] = $uid;
$newcode = db::name('code')->insertGetId($arr);
}
$has = db::name('code_photo')->where(['uid' => $uid, 'shop_id' => $shop_id])->value('code');
if ($ishas || @$newcode) {
$rand = time() . rand(10000, 99999);
$pathUlr = '/static/uploads/posters/' . $uid . '/' . $rand . '.png';
if (!is_dir(ROOT_PATH . 'public/static/uploads/posters/' . $uid)) {
mkdir(ROOT_PATH . 'public/static/uploads/posters/' . $uid, 0777, true);
}
$path = ROOT_PATH . 'public' . $pathUlr;
if ($has) {
return json(['code' => '0', 'path' => $has]);
} else {
$back = get_url() . '/static/image/back.png';
$resource = imagecreatefromstring(http_request($back));
$sub_image_c = imagecreatefromstring(base64_decode(str_replace('data:image/PNG;base64,', '', $ishas['code'])));
$bool = imagecopyresampled($resource, $sub_image_c, 30, 625, 0, 0, 150, 150, imagesx($sub_image_c), imagesy($sub_image_c));
$sub_image_c = imagecreatefromstring(http_request($member_info_image));
$bool = imagecopyresampled($resource, $sub_image_c, 30, 30, 0, 0, 80, 80, imagesx($sub_image_c), imagesy($sub_image_c));
$sub_image_c = imagecreatefromstring(http_request($shop_info_image));
$bool = imagecopyresampled($resource, $sub_image_c, 30, 140, 0, 0, 500, 280, imagesx($sub_image_c), imagesy($sub_image_c));
imagejpeg($resource, $path, 100);
imagedestroy($resource);
$this->create_text(ROOT_PATH . 'public/' . $pathUlr, $member_info['nickname'], 40, [0, 0, 0], 150, 85, ROOT_PATH . 'public/' . $pathUlr, '/font/hei.ttf', $uid, $rand);
$this->create_text(ROOT_PATH . 'public/' . $pathUlr, '¥' . $shop_info['price'], 30, [0, 0, 0], 30, 480, ROOT_PATH . 'public/' . $pathUlr, '/font/hei.ttf', $uid, $rand);
$this->create_text(ROOT_PATH . 'public/' . $pathUlr, '¥' . $shop_info['name'], 30, [0, 0, 0], 30, 540, ROOT_PATH . 'public/' . $pathUlr, '/font/hei.ttf', $uid, $rand);
$codeArr['uid'] = $uid;
$codeArr['shop_id'] = $shop_id;
$codeArr['code'] = $pathUlr;
db::name('code_photo')->insertGetId($codeArr);
return json(['code' => '0', 'path' => $pathUlr]);
}
} else {
return __error('二維碼生成錯誤');
}
} else {
return __error('錯誤的請求');
}
}
public function create_text($path, $text, $size, $color, $x, $y, $out_image, $font = '/font/hei.ttf', $uid = 0, $rand)
{
$dst = imagecreatefromstring(file_get_contents($path));
$black = imagecolorallocate($dst, $color[0], $color[1], $color[2]);
imagefttext($dst, $size, 0, $x, $y, $black, $font, $text);
imagejpeg($dst, $_SERVER['DOCUMENT_ROOT'] . '/static/uploads/posters/' . $uid . '/' . $rand . '.png', 100);
imagedestroy($dst);
}
public function getAccessToken()
{
$appid = config('wx.appid');
$secret = config('wx.secret');
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$tokeninfo = json_decode($output, true);
cache('token', $tokeninfo['access_token'], 7200);
return $tokeninfo['access_token'];
}
public function curl_post_https($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept-Charset: utf-8']);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return false;
} else {
return $tmpInfo;
}
}
$sub_image_c = imagecreatefromstring(http_request($member_info_image));
$bool = imagecopyresampled($resource, $sub_image_c, 30, 30, 0, 0, 80, 80, imagesx($sub_image_c), imagesy($sub_image_c));
$radius = 50;
$lt_corner = get_lt_rounder_corner($radius);
imagecopymerge($resource, $lt_corner, 30, 30, 0, 0, $radius, $radius, 100);
$lb_corner = imagerotate($lt_corner, 90, 0);
imagecopymerge($resource, $lb_corner, 80, 1874 - $radius, 0, 0, $radius, $radius, 100);
$rb_corner = imagerotate($lt_corner, 180, 0);
imagecopymerge($resource, $rb_corner, 270 - $radius, 1874 - $radius, 0, 0, $radius, $radius, 100);
$rt_corner = imagerotate($lt_corner, 270, 0);
imagecopymerge($resource, $rt_corner, 270 - $radius, 1684, 0, 0, $radius, $radius, 100);
imagejpeg($resource, $path, 100);
function get_lt_rounder_corner($radius) {
$img = imagecreatetruecolor($radius, $radius);
$bgcolor = imagecolorallocate($img, 255, 255, 255);
$fgcolor = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgcolor);
imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);
imagecolortransparent($img, $fgcolor);
return $img;
}