You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
355 lines
12 KiB
355 lines
12 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace addon\hygl\app\service\admin\config;
|
|
|
|
use addon\hygl\app\model\config\Config;
|
|
|
|
use app\model\dict\Dict;
|
|
use core\base\BaseAdminService;
|
|
use dh2y\qrcode\QRcode;
|
|
use http\Url;
|
|
use think\File;
|
|
|
|
|
|
/**
|
|
* 配置项服务层
|
|
* Class ConfigService
|
|
* @package addon\hygl\app\service\admin\config
|
|
*/
|
|
class ConfigService extends BaseAdminService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = new Config();
|
|
}
|
|
|
|
/**
|
|
* 获取配置项列表
|
|
* @param array $where
|
|
* @return array
|
|
*/
|
|
public function getPage(array $where = [])
|
|
{
|
|
$field = 'id,site_id,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';
|
|
$order = 'id desc';
|
|
|
|
$search_model = $this->model->where([ [ 'site_id' ,"=", $this->site_id ] ])->withSearch([''], $where)->field($field)->order($order);;
|
|
$list = $this->pageQuery($search_model);
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 获取配置项信息
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
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,miniapp_site_url,miniapp_qrcode_url';
|
|
|
|
$info = $this->model->field($field)->where('site_id', '=', $this->site_id)->findOrEmpty()->toArray();
|
|
|
|
//查看基础信息是否完全
|
|
|
|
if (!$info){
|
|
//创建基础信息
|
|
//添加
|
|
$data['site_id'] = $this->site_id;
|
|
$dictionary = Dict::where('key','h5_site_url')
|
|
->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']);//生成H5二维码
|
|
$data['h5_qrcode_url'] =$qRCode['url'];
|
|
$this->add($data);
|
|
$info = $data;
|
|
}
|
|
|
|
|
|
if(isset($info['miniapp_site_url']) && !isset($info['miniapp_qrcode_url']) && isset($info['we_chat_pay_miniapp_id']) && isset($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']);
|
|
}
|
|
|
|
return $info;
|
|
}
|
|
|
|
/**
|
|
* 重新生成H5站点二维码
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
public function resetH5SiteQRCode(int $id)
|
|
{
|
|
//查询h5站点首页域名
|
|
$h5_site_url = $this->model->where('id',$id)->value('h5_site_url');
|
|
$res = $this->createUrlQRCode($h5_site_url);
|
|
return $res;
|
|
}
|
|
|
|
//根据url生成二维码
|
|
private function createUrlQRCode($url){
|
|
$site_id = $this->site_id;
|
|
$date = date('Y_m_d');
|
|
$path = "config_h5_qrcode_url";
|
|
$save_path = "addon/hygl/upload/{$path}/{$site_id}/{$date}";//文件保存的路径,
|
|
// 创建保存目录
|
|
if (!is_dir($save_path)) {
|
|
mkdir($save_path, 0755, true);
|
|
}
|
|
|
|
//将网址url转成二维码
|
|
// 创建一个QR码实例
|
|
$code = new QRcode();
|
|
$source_file = $code->png($url)->getPath();//生成二维码,并返回路径
|
|
$source_file =ltrim($source_file,'/');
|
|
return [
|
|
'url' => $source_file,
|
|
'ol_url' => get_file_url($source_file)
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 重新生成微信小程序二维码
|
|
* @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
|
|
* @return mixed
|
|
*/
|
|
public function add(array $data)
|
|
{
|
|
$data['site_id'] = $this->site_id;
|
|
$res = $this->model->create($data);
|
|
return $res->id;
|
|
|
|
}
|
|
|
|
/**
|
|
* 配置项编辑
|
|
* @param int $id
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public function edit(array $data)
|
|
{
|
|
//检测数据是否存在,存在就编辑,不存在就添加
|
|
$id = $this->model->where([['site_id', '=', $this->site_id]])->value('id');
|
|
if ($id){
|
|
//编辑
|
|
$this->model->where([['site_id', '=', $this->site_id]])->update($data);
|
|
}else{
|
|
//添加
|
|
$data['site_id'] = $this->site_id;
|
|
$dictionary = Dict::where('key','h5_site_url')
|
|
->value('dictionary');
|
|
$h5_site_url = json_decode($dictionary, true)[0]['value'];
|
|
$data['h5_site_url'] = "{$h5_site_url}?site_id={$this->site_id}";//h5站点地址
|
|
|
|
$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']);//生成二维码
|
|
$data['h5_qrcode_url'] =$qRCode['url'];
|
|
|
|
if(isset($data['miniapp_site_url']) && !isset($data['miniapp_qrcode_url']) && isset($data['we_chat_pay_miniapp_id']) && isset($data['we_chat_pay_miniapp_secret'])){
|
|
|
|
$mini_qRCode = $this->createMiniappQrcodeUrl($data['we_chat_pay_miniapp_id'], $data['we_chat_pay_miniapp_secret'], $data['miniapp_site_url']);//生成微信小程序二维码
|
|
if($mini_qRCode['url']){
|
|
$data['miniapp_qrcode_url'] = $mini_qRCode['url'];
|
|
}else{
|
|
$data['miniapp_qrcode_url'] = '';
|
|
}
|
|
}
|
|
|
|
$this->add($data);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除配置项
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function del(int $id)
|
|
{
|
|
$model = $this->model->where([['id', '=', $id],['site_id', '=', $this->site_id]])->find();
|
|
$res = $model->delete();
|
|
return $res;
|
|
}
|
|
|
|
}
|
|
|