|
|
|
@ -17,6 +17,7 @@ use app\model\dict\Dict; |
|
|
|
use core\base\BaseAdminService; |
|
|
|
use dh2y\qrcode\QRcode; |
|
|
|
use http\Url; |
|
|
|
use think\File; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -54,7 +55,7 @@ class ConfigService extends BaseAdminService |
|
|
|
*/ |
|
|
|
public function getInfo(int $id) |
|
|
|
{ |
|
|
|
$field = 'id,site_id,h5_qrcode_url,we_chat_pay_appid,we_chat_pay_app_id,we_chat_pay_miniapp_id,we_chat_pay_mch_id,we_chat_pay_key,we_chat_pay_miniapp_secret,we_chat_pay_notify_url,alipay_appId,alipay_rsa_private_key,alipay_public_key,alipay_notify_url,create_time,update_time,delete_time,we_chat_pay_mch_secret_cert,we_chat_pay_mch_public_cert_path,h5_site_url'; |
|
|
|
$field = 'id,site_id,h5_qrcode_url,we_chat_pay_appid,we_chat_pay_app_id,we_chat_pay_miniapp_id,we_chat_pay_mch_id,we_chat_pay_key,we_chat_pay_miniapp_secret,we_chat_pay_notify_url,alipay_appId,alipay_rsa_private_key,alipay_public_key,alipay_notify_url,create_time,update_time,delete_time,we_chat_pay_mch_secret_cert,we_chat_pay_mch_public_cert_path,h5_site_url,miniapp_site_url,miniapp_qrcode_url'; |
|
|
|
|
|
|
|
$info = $this->model->field($field)->where('site_id', '=', $this->site_id)->findOrEmpty()->toArray(); |
|
|
|
|
|
|
|
@ -68,18 +69,38 @@ class ConfigService extends BaseAdminService |
|
|
|
->value('dictionary'); |
|
|
|
$h5_site_url = json_decode($dictionary, true)[0]['value']; |
|
|
|
$data['h5_site_url'] = "{$h5_site_url}?site_id={$this->site_id}";//h5站点地址 |
|
|
|
$data['miniapp_site_url'] = "pages/wxPay/index?site_id={$this->site_id}";//微信小程序跳转路径 |
|
|
|
|
|
|
|
|
|
|
|
$we_chat_pay_notify_url = Dict::where('key', 'we_chat_pay_notify_url') |
|
|
|
->value('dictionary');//微信支付异步回调根路径 |
|
|
|
$we_chat_pay_notify_url = json_decode($we_chat_pay_notify_url, true)[0]['value']; |
|
|
|
$data['we_chat_pay_notify_url'] = "{$we_chat_pay_notify_url}/{$this->site_id}";//微信异步回调地址 |
|
|
|
|
|
|
|
$qRCode = $this->createUrlQRCode($data['h5_site_url']);//生成二维码 |
|
|
|
$qRCode = $this->createUrlQRCode($data['h5_site_url']);//生成H5二维码 |
|
|
|
$data['h5_qrcode_url'] =$qRCode['url']; |
|
|
|
$this->add($data); |
|
|
|
$info = $data; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if($info['miniapp_site_url'] && !$info['miniapp_qrcode_url'] && $info['we_chat_pay_miniapp_id'] && $info['we_chat_pay_miniapp_secret']){ |
|
|
|
|
|
|
|
$mini_qRCode = $this->createMiniappQrcodeUrl($info['we_chat_pay_miniapp_id'], $info['we_chat_pay_miniapp_secret'], $info['miniapp_site_url']);//生成微信小程序二维码 |
|
|
|
if($mini_qRCode['url']){ |
|
|
|
$info['miniapp_qrcode_url'] = $mini_qRCode['url']; |
|
|
|
$this->edit([ |
|
|
|
'miniapp_qrcode_url'=>$mini_qRCode['url'] |
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$info['ol_miniapp_qrcode_url'] = ''; |
|
|
|
if (!empty($info['miniapp_qrcode_url'])){ |
|
|
|
$info['ol_miniapp_qrcode_url'] = get_file_url($info['miniapp_qrcode_url']); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$info['ol_h5_qrcode_url'] = ''; |
|
|
|
if (!empty($info['h5_qrcode_url'])){ |
|
|
|
$info['ol_h5_qrcode_url'] = get_file_url($info['h5_qrcode_url']); |
|
|
|
@ -123,6 +144,144 @@ class ConfigService extends BaseAdminService |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 重新生成微信小程序二维码 |
|
|
|
* @param int $id |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
public function resetMiniAppQRCode(int $id) |
|
|
|
{ |
|
|
|
//查询站点 |
|
|
|
$info = Config::where('id',$id)->field('id,miniapp_site_url,we_chat_pay_miniapp_id,we_chat_pay_miniapp_secret')->find(); |
|
|
|
|
|
|
|
if (!$info['miniapp_site_url'] || !$info['we_chat_pay_miniapp_id'] || !$info['we_chat_pay_miniapp_secret']){ |
|
|
|
return [ |
|
|
|
'msg'=>'缺少必填参数', |
|
|
|
'url' => '', |
|
|
|
'ol_url' => '' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
$res = $this->createMiniappQrcodeUrl($info['we_chat_pay_miniapp_id'],$info['we_chat_pay_miniapp_secret'],$info['miniapp_site_url']); |
|
|
|
return $res; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 生成微信小程序二维码 |
|
|
|
* @param string $appid 微信小程序appid |
|
|
|
* @param string $secret 微信小程序secret |
|
|
|
* @param string $path 例如$path = "pages_tool/login/register?age=18" |
|
|
|
* @return string |
|
|
|
* @throws \Exception |
|
|
|
*/ |
|
|
|
private function createMiniappQrcodeUrl(string $appid, string $secret, string $path) |
|
|
|
{ |
|
|
|
$accessToken = $this->getMiniAppAccessToken($appid, $secret); |
|
|
|
if (empty($accessToken)) { |
|
|
|
return [ |
|
|
|
'msg'=>'无法获取accessToken', |
|
|
|
'url' => '', |
|
|
|
'ol_url' => '' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
$width = 430; // 二维码宽度,默认430px |
|
|
|
$codeUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=$accessToken"; |
|
|
|
$postData = json_encode([ |
|
|
|
'path' => $path, |
|
|
|
'width' => $width, |
|
|
|
'is_hyaline' => false // 是否透明底色 |
|
|
|
]); |
|
|
|
|
|
|
|
$ch = curl_init(); |
|
|
|
curl_setopt($ch, CURLOPT_URL, $codeUrl); |
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); |
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 仅用于调试,生产环境不建议 |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 仅用于调试,生产环境不建议 |
|
|
|
|
|
|
|
$qr_code_response = curl_exec($ch); |
|
|
|
|
|
|
|
if ($qr_code_response === false) { |
|
|
|
$curl_error = curl_error($ch); |
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
|
|
curl_close($ch); |
|
|
|
return [ |
|
|
|
'url' => '', |
|
|
|
'ol_url' => '' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
|
|
if ($httpCode != 200) { |
|
|
|
curl_close($ch); |
|
|
|
return [ |
|
|
|
'url' => '', |
|
|
|
'ol_url' => '' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
curl_close($ch); |
|
|
|
|
|
|
|
// 创建临时文件并将二维码内容写入 |
|
|
|
$tempFilePath = tempnam(sys_get_temp_dir(), 'qr_'); |
|
|
|
if (file_put_contents($tempFilePath, $qr_code_response) === false) { |
|
|
|
return [ |
|
|
|
'url' => '', |
|
|
|
'ol_url' => '' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
// 创建 File 对象 |
|
|
|
$file = new File($tempFilePath); |
|
|
|
|
|
|
|
// 定义保存路径和文件名 |
|
|
|
$filename = time() . '_xcxcode.png'; // 使用时间戳确保文件名唯一 |
|
|
|
$site_id = $this->site_id; |
|
|
|
$date = date('Y_m_d'); |
|
|
|
$save_path = "addon/hygl/upload/config_miniapp_qrcode_url/{$site_id}/{$date}"; // 文件保存的路径 |
|
|
|
|
|
|
|
// 确保目标目录存在且可写 |
|
|
|
if (!is_dir($save_path)) { |
|
|
|
if (!mkdir($save_path, 0755, true)) { |
|
|
|
return [ |
|
|
|
'url' => '', |
|
|
|
'ol_url' => '' |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$info = $file->move($save_path, $filename); |
|
|
|
if ($info) { |
|
|
|
// 返回保存成功的消息和文件路径 |
|
|
|
$url = "{$save_path}/$filename"; |
|
|
|
return [ |
|
|
|
'url' => $url, |
|
|
|
'ol_url' => get_file_url($url) |
|
|
|
]; |
|
|
|
} else { |
|
|
|
// 返回失败消息 |
|
|
|
return [ |
|
|
|
'url' => '', |
|
|
|
'ol_url' => '' |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//微信小程序生成access_token |
|
|
|
private function getMiniAppAccessToken($appId, $appSecret) { |
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}"; |
|
|
|
$response = file_get_contents($url); |
|
|
|
$data = json_decode($response, true); |
|
|
|
|
|
|
|
$access_token = ''; |
|
|
|
if (isset($data['access_token'])) { |
|
|
|
$access_token = $data['access_token']; |
|
|
|
} |
|
|
|
return $access_token; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 添加配置项 |
|
|
|
* @param array $data |
|
|
|
|