189 lines
7.9 KiB
PHP
189 lines
7.9 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeshop开源商城系统
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||
// | github下载:https://github.com/likeshop-github
|
||
// | 访问官网:https://www.likeshop.cn
|
||
// | 访问社区:https://home.likeshop.cn
|
||
// | 访问手册:http://doc.likeshop.cn
|
||
// | 微信公众号:likeshop技术社区
|
||
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
|
||
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
|
||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||
// | likeshop团队版权所有并拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeshop.cn.team
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\admin\logic\wechat;
|
||
|
||
use app\common\basics\Logic;
|
||
use app\common\server\ConfigServer;
|
||
use app\common\server\UrlServer;
|
||
use think\facade\Db;
|
||
|
||
class MnpLogic extends Logic
|
||
{
|
||
/**
|
||
* 获取小程序配置
|
||
*/
|
||
public static function getMnp()
|
||
{
|
||
$domain_name = $_SERVER['SERVER_NAME'];
|
||
$qr_code = ConfigServer::get('mnp', 'qr_code', '');
|
||
$config = [
|
||
'name' => ConfigServer::get('mnp', 'name', ''),
|
||
'original_id' => ConfigServer::get('mnp', 'original_id', ''),
|
||
'qr_code' => $qr_code,
|
||
'abs_qr_code' => UrlServer::getFileUrl($qr_code),
|
||
'app_id' => ConfigServer::get('mnp', 'app_id', ''),
|
||
'app_secret' => ConfigServer::get('mnp', 'secret', ''),
|
||
'request_domain' => 'https://'.$domain_name,
|
||
'socket_domain' => 'wss://'.$domain_name,
|
||
'uploadfile_domain' => 'https://'.$domain_name,
|
||
'downloadfile_domain' => 'https://'.$domain_name,
|
||
'udp_domain' => 'udp://'.$domain_name,
|
||
'tcp_domain' => 'tcp://'.$domain_name,
|
||
'business_domain' => $domain_name,
|
||
'url' => url('api/wechat/index',[],'',true),
|
||
'token' => ConfigServer::get('mnp', 'token', 'LikeMall'),
|
||
'encoding_ses_key' => ConfigServer::get('mnp', 'encoding_ses_key', ''),
|
||
'encryption_type' => ConfigServer::get('mnp', 'encryption_type', ''),
|
||
'data_type' => ConfigServer::get('mnp', 'data_type', ''),
|
||
|
||
// 小程序同步发货开关 1开启 0关闭
|
||
'express_send_sync' => ConfigServer::get('mnp', 'express_send_sync', 1),
|
||
//代码上传密钥
|
||
'private_key' => ConfigServer::get('mnp', 'private_key', ''),
|
||
];
|
||
return $config;
|
||
}
|
||
|
||
public static function setMnp($post)
|
||
{
|
||
if($post){
|
||
$encryption_type = $post['encryption_type'] ?? '';
|
||
$data_type = $post['data_type'] ?? '';
|
||
|
||
ConfigServer::set('mnp','name',$post['name']);
|
||
ConfigServer::set('mnp','original_id',$post['original_id']);
|
||
ConfigServer::set('mnp','qr_code', $post['qr_code']);
|
||
ConfigServer::set('mnp','app_id',$post['app_id']);
|
||
ConfigServer::set('mnp','secret',$post['app_secret']);
|
||
// ConfigServer::set('mnp','token',$post['token']);
|
||
// ConfigServer::set('mnp','encoding_ses_key',$post['encoding_ses_key']);
|
||
// ConfigServer::set('mnp','encryption_type', $encryption_type);
|
||
// ConfigServer::set('mnp','data_type', $data_type);
|
||
|
||
ConfigServer::set('mnp','express_send_sync', $post['express_send_sync'] ?? 1);
|
||
|
||
if (!empty($post['private_key'])) {
|
||
$saveDir = '../extend/miniprogram-ci/';
|
||
if (!file_exists($saveDir)) {
|
||
mkdir($saveDir, 0775, true);
|
||
}
|
||
//保存文件
|
||
$savePath = $saveDir.'private.'.$post['app_id'].'.key';
|
||
$f = fopen($savePath, 'w');
|
||
fwrite($f, $post['private_key']);
|
||
fclose($f);
|
||
|
||
ConfigServer::set('mnp','private_key',$post['private_key']);
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
//一键上传
|
||
public static function uploadMnp($post)
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
//校验是否已安装miniprogram-ci工具
|
||
if (!file_exists('../extend/miniprogram-ci/node_modules/miniprogram-ci')) {
|
||
throw new \think\Exception('请先安装miniprogram-ci工具');
|
||
}
|
||
|
||
$baseUrl = 'mp-weixin/common/vendor.js';
|
||
//判断是否已存在 vendor_example.js 备份文件,如果不存在则备份
|
||
$exampleUrl = 'mp-weixin/common/vendor_example.js';
|
||
if(!file_exists($exampleUrl)){
|
||
copy($baseUrl, $exampleUrl);
|
||
}
|
||
|
||
//更换小程序域名
|
||
$baseUrlData = file_get_contents($exampleUrl);
|
||
$domain = request()->domain(true);
|
||
$baseUrlData = str_replace("[baseUrl]",$domain,$baseUrlData);
|
||
$f = fopen($baseUrl,"w");
|
||
fwrite($f,$baseUrlData);
|
||
fclose($f);
|
||
|
||
//插入上传日志
|
||
$uploadMnpLogId = Db::name('upload_mnp_log')->insertGetId([
|
||
'admin_id' => $post['admin_id'],
|
||
'version' => $post['version'],
|
||
'create_time' => time(),
|
||
]);
|
||
|
||
//上传小程序代码
|
||
$data = [
|
||
'version' => $post['version'],
|
||
'desc' => $post['upload_desc'] ?? '',
|
||
'appid' => ConfigServer::get('mnp', 'app_id', ''),
|
||
];
|
||
$json_data = json_encode($data);
|
||
$command = 'node ../extend/miniprogram-ci/upload.js '.escapeshellarg($json_data).' 2>&1';
|
||
$output=null;
|
||
$retval = null;
|
||
exec($command, $output, $retval);
|
||
|
||
//获取错误信息
|
||
$errors = preg_grep('/\[error\]/', is_array($output) ? $output : [$output]);
|
||
if ($retval || !empty($errors)) {
|
||
//错误
|
||
Db::name('upload_mnp_log')->where('id', $uploadMnpLogId)->update([
|
||
'status' => 2,
|
||
'fail_reason' => is_array($output) ? json_encode($output) : $output,
|
||
]);
|
||
} else {
|
||
//成功
|
||
Db::name('upload_mnp_log')->where('id', $uploadMnpLogId)->update([
|
||
'status' => 1
|
||
]);
|
||
}
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
//小程序上传日志
|
||
public static function uploadMnpLog($get)
|
||
{
|
||
$lists = Db::name('upload_mnp_log')
|
||
->field('admin_id,version,status,fail_reason,create_time')
|
||
->withAttr('admin_desc', function ($value,$data){
|
||
return Db::name('admin')->where(['id'=>$data['admin_id']])->value('name');
|
||
})
|
||
->withAttr('status_desc', function ($value,$data){
|
||
$statusDesc = ['上传中','上传成功','上传失败'];
|
||
return $statusDesc[$data['status']];
|
||
})
|
||
->withAttr('create_time', function ($value,$data){
|
||
return date('Y-m-d H:i:s', $data['create_time']);
|
||
})
|
||
->page($get['page'], $get['limit'])
|
||
->order('id', 'desc')
|
||
->select();
|
||
$count = Db::name('upload_mnp_log')->count();
|
||
|
||
return ['count' => $count, 'lists' => $lists];
|
||
}
|
||
} |