2018-12-28 18:58:37 +08:00
|
|
|
<?php
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// | ShopXO 国内领先企业级B2C免费开源电商系统
|
|
|
|
// +----------------------------------------------------------------------
|
2019-02-18 13:52:07 +08:00
|
|
|
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
|
2018-12-28 18:58:37 +08:00
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// | Author: Devil
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 小程序服务层
|
|
|
|
* @author Devil
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
* @version 0.0.1
|
|
|
|
* @datetime 2016-12-01T21:51:08+0800
|
|
|
|
*/
|
|
|
|
class AppMiniService
|
|
|
|
{
|
|
|
|
// 当前小程序包名称
|
2019-01-17 00:37:20 +08:00
|
|
|
private static $application_name;
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
// 原包地址/操作地址
|
2019-01-17 00:37:20 +08:00
|
|
|
private static $old_root;
|
|
|
|
private static $new_root;
|
|
|
|
private static $old_path;
|
|
|
|
private static $new_path;
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Devil
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
* @version 1.0.0
|
|
|
|
* @date 2018-12-21
|
|
|
|
* @desc description
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
public static function Init($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
|
|
|
// 当前小程序包名称
|
2019-01-17 00:37:20 +08:00
|
|
|
self::$application_name = isset($params['application_name']) ? $params['application_name'] : 'alipay';
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
// 原包地址/操作地址
|
2020-02-06 16:32:37 +08:00
|
|
|
self::$old_root = ROOT.'sourcecode';
|
2020-02-06 16:44:19 +08:00
|
|
|
self::$new_root = ROOT.'public'.DS.'download'.DS.'sourcecode';
|
2019-01-17 00:37:20 +08:00
|
|
|
self::$old_path = self::$old_root.DS.self::$application_name;
|
|
|
|
self::$new_path = self::$new_root.DS.self::$application_name;
|
2018-12-28 18:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取小程序生成列表
|
|
|
|
* @author Devil
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
* @version 0.0.1
|
|
|
|
* @datetime 2017-05-10T10:24:40+0800
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
public static function DataList($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
|
|
|
// 初始化
|
2019-01-17 00:37:20 +08:00
|
|
|
self::Init($params);
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
// 获取包列表
|
|
|
|
$result = [];
|
2019-01-17 00:37:20 +08:00
|
|
|
if(is_dir(self::$new_path))
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
2019-01-17 00:37:20 +08:00
|
|
|
if($dh = opendir(self::$new_path))
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
|
|
|
while(($temp_file = readdir($dh)) !== false)
|
|
|
|
{
|
|
|
|
if($temp_file != '.' && $temp_file != '..')
|
|
|
|
{
|
2019-01-17 00:37:20 +08:00
|
|
|
$file_path = self::$new_path.DS.$temp_file;
|
2020-02-06 16:44:19 +08:00
|
|
|
$url = __MY_PUBLIC_URL__.'download'.DS.'sourcecode'.DS.self::$application_name.DS.$temp_file;
|
2018-12-28 18:58:37 +08:00
|
|
|
$result[] = [
|
2020-06-22 23:21:23 +08:00
|
|
|
'name' => (substr($temp_file, -4) === '.zip') ? substr($temp_file, 0, strlen($temp_file)-4) : $temp_file,
|
2018-12-28 18:58:37 +08:00
|
|
|
'url' => substr($url, -4) == '.zip' ? $url : '',
|
|
|
|
'size' => FileSizeByteToUnit(filesize($file_path)),
|
|
|
|
'time' => date('Y-m-d H:i:s', filectime($file_path)),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 23:21:23 +08:00
|
|
|
return DataReturn('success', 0, $result);
|
2018-12-28 18:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 源码包生成
|
|
|
|
* @author Devil
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
* @version 1.0.0
|
|
|
|
* @date 2018-12-21
|
|
|
|
* @desc description
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
public static function Created($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
2019-01-18 17:38:41 +08:00
|
|
|
// 是否https
|
2019-06-13 10:59:53 +08:00
|
|
|
if(config('shopxo.is_develop') !== true)
|
2019-01-18 17:38:41 +08:00
|
|
|
{
|
2019-04-29 11:42:37 +08:00
|
|
|
if(__MY_HTTP__ != 'https')
|
|
|
|
{
|
|
|
|
return DataReturn('请使用https协议', -1);
|
|
|
|
}
|
2019-01-18 17:38:41 +08:00
|
|
|
}
|
|
|
|
|
2018-12-28 18:58:37 +08:00
|
|
|
// 初始化
|
2019-01-17 00:37:20 +08:00
|
|
|
self::Init($params);
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
// 配置内容
|
2019-01-11 13:40:32 +08:00
|
|
|
if(empty($params['app_mini_title']) || empty($params['app_mini_describe']))
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
|
|
|
return DataReturn('配置信息不能为空', -1);
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:44:38 +08:00
|
|
|
// 源码目录不存在则创建
|
|
|
|
\base\FileUtil::CreateDir(self::$new_root);
|
|
|
|
|
|
|
|
// 源码目标目录是否存在
|
2019-01-17 00:37:20 +08:00
|
|
|
if(!is_dir(self::$new_root))
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
2020-04-07 12:44:38 +08:00
|
|
|
return DataReturn('源码目标目录不存在['.self::$new_root.']', -1);
|
2018-12-28 18:58:37 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:44:38 +08:00
|
|
|
// 源码目标目录没有权限
|
2019-01-17 00:37:20 +08:00
|
|
|
if(!is_writable(self::$new_root))
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
2020-04-07 12:44:38 +08:00
|
|
|
return DataReturn('源码目标目录没有权限['.self::$new_root.']', -1);
|
2018-12-28 18:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 目录不存在则创建
|
2019-01-17 00:37:20 +08:00
|
|
|
\base\FileUtil::CreateDir(self::$new_path);
|
2018-12-28 18:58:37 +08:00
|
|
|
|
|
|
|
// 复制包目录
|
2019-01-17 00:37:20 +08:00
|
|
|
$new_dir = self::$new_path.DS.date('YmdHis');
|
|
|
|
if(\base\FileUtil::CopyDir(self::$old_path, $new_dir) != true)
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
|
|
|
return DataReturn('项目包复制失败', -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 校验基础文件是否存在
|
|
|
|
if(!file_exists($new_dir.DS.'app.js') || !file_exists($new_dir.DS.'app.json'))
|
|
|
|
{
|
|
|
|
return DataReturn('包基础文件不存在,请重新生成', -3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 替换内容
|
|
|
|
// app.js
|
2020-02-04 17:18:14 +08:00
|
|
|
$search = [
|
|
|
|
'{{request_url}}',
|
|
|
|
'{{application_title}}',
|
|
|
|
'{{application_describe}}',
|
|
|
|
'{{price_symbol}}',
|
|
|
|
];
|
|
|
|
$replace = [
|
|
|
|
__MY_URL__,
|
|
|
|
$params['app_mini_title'],
|
|
|
|
$params['app_mini_describe'],
|
|
|
|
config('shopxo.price_symbol'),
|
|
|
|
];
|
|
|
|
$status = file_put_contents($new_dir.DS.'app.js', str_replace($search, $replace, file_get_contents($new_dir.DS.'app.js')));
|
2018-12-28 18:58:37 +08:00
|
|
|
if($status === false)
|
|
|
|
{
|
|
|
|
return DataReturn('基础配置替换失败', -4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// app.json
|
2019-01-11 13:40:32 +08:00
|
|
|
$status = file_put_contents($new_dir.DS.'app.json', str_replace(['{{application_title}}'], [$params['app_mini_title']], file_get_contents($new_dir.DS.'app.json')));
|
2018-12-28 18:58:37 +08:00
|
|
|
if($status === false)
|
|
|
|
{
|
|
|
|
return DataReturn('基础配置替换失败', -4);
|
|
|
|
}
|
|
|
|
|
2019-08-04 22:11:03 +08:00
|
|
|
// 小程序额外处理
|
|
|
|
switch(self::$application_name)
|
|
|
|
{
|
|
|
|
// 微信
|
|
|
|
case 'weixin' :
|
|
|
|
$ret = self::ExtendHandleWeixin($new_dir);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 默认
|
|
|
|
default :
|
|
|
|
$ret = DataReturn('无需处理', 0);
|
|
|
|
}
|
|
|
|
if(isset($ret['code']) && $ret['code'] != 0)
|
|
|
|
{
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:58:37 +08:00
|
|
|
// 生成压缩包
|
|
|
|
$zip = new \base\ZipFolder();
|
|
|
|
if(!$zip->zip($new_dir.'.zip', $new_dir))
|
|
|
|
{
|
|
|
|
return DataReturn('压缩包生成失败', -100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 生成成功删除目录
|
|
|
|
\base\FileUtil::UnlinkDir($new_dir);
|
|
|
|
|
|
|
|
return DataReturn('生成成功', 0);
|
|
|
|
}
|
|
|
|
|
2019-08-04 22:11:03 +08:00
|
|
|
/**
|
|
|
|
* 扩展处理 - 微信
|
|
|
|
* @author Devil
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
* @version 1.0.0
|
|
|
|
* @date 2018-12-21
|
|
|
|
* @desc description
|
|
|
|
* @param [string] $new_dir [新得源码包目录]
|
|
|
|
*/
|
|
|
|
private static function ExtendHandleWeixin($new_dir)
|
|
|
|
{
|
2020-04-21 13:15:50 +08:00
|
|
|
// 启用好物推荐
|
|
|
|
if(MyC('common_app_is_good_thing', 0) == 1)
|
2019-08-04 22:11:03 +08:00
|
|
|
{
|
|
|
|
// app.json
|
|
|
|
$file = $new_dir.DS.'app.json';
|
|
|
|
$data = json_decode(file_get_contents($file), true);
|
|
|
|
if(is_array($data) && isset($data['plugins']))
|
|
|
|
{
|
|
|
|
$data['plugins']['goodsSharePlugin'] = [
|
2020-06-22 23:21:23 +08:00
|
|
|
'version' => MyC('common_app_is_good_thing_ver', '4.0.1', true),
|
2019-08-04 22:11:03 +08:00
|
|
|
'provider' => 'wx56c8f077de74b07c',
|
|
|
|
];
|
|
|
|
if(file_put_contents($file, JsonFormat($data)) === false)
|
|
|
|
{
|
|
|
|
return DataReturn('好物推荐主配置失败', -50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// goods-detail.json
|
|
|
|
$file = $new_dir.DS.'pages'.DS.'goods-detail'.DS.'goods-detail.json';
|
|
|
|
$data = json_decode(file_get_contents($file), true);
|
|
|
|
if(is_array($data) && isset($data['usingComponents']))
|
|
|
|
{
|
|
|
|
$data['usingComponents']['share-button'] = 'plugin://goodsSharePlugin/share-button';
|
|
|
|
if(file_put_contents($file, JsonFormat($data)) === false)
|
|
|
|
{
|
|
|
|
return DataReturn('好物推荐商品配置失败', -51);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 13:15:50 +08:00
|
|
|
|
|
|
|
// 启用直播
|
|
|
|
if(MyC('common_app_weixin_liveplayer', 0) == 1)
|
|
|
|
{
|
|
|
|
// app.json
|
|
|
|
$file = $new_dir.DS.'app.json';
|
|
|
|
$data = json_decode(file_get_contents($file), true);
|
|
|
|
if(is_array($data) && isset($data['plugins']))
|
|
|
|
{
|
|
|
|
$data['plugins']['live-player-plugin'] = [
|
2020-08-13 13:41:36 +08:00
|
|
|
'version' => MyC('common_app_weixin_liveplayer_ver', '1.1.4', true),
|
2020-04-21 13:15:50 +08:00
|
|
|
'provider' => 'wx2b03c6e691cd7370',
|
|
|
|
];
|
|
|
|
if(file_put_contents($file, JsonFormat($data)) === false)
|
|
|
|
{
|
|
|
|
return DataReturn('直播配置失败', -50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 22:11:03 +08:00
|
|
|
return DataReturn('配置成功', 0);
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:58:37 +08:00
|
|
|
/**
|
|
|
|
* 源码包删除
|
|
|
|
* @author Devil
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
* @version 1.0.0
|
|
|
|
* @date 2018-12-21
|
|
|
|
* @desc description
|
2019-08-04 22:11:03 +08:00
|
|
|
* @param [array] $params [输入参数]
|
2018-12-28 18:58:37 +08:00
|
|
|
*/
|
2019-01-17 00:37:20 +08:00
|
|
|
public static function Delete($params = [])
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
2020-06-22 23:21:23 +08:00
|
|
|
// 参数是否有误
|
|
|
|
if(empty($params['ids']))
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
2020-06-22 23:21:23 +08:00
|
|
|
return DataReturn('操作id有误', -1);
|
|
|
|
}
|
|
|
|
// 是否数组
|
|
|
|
if(!is_array($params['ids']))
|
|
|
|
{
|
|
|
|
$params['ids'] = explode(',', $params['ids']);
|
2018-12-28 18:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 初始化
|
2019-01-17 00:37:20 +08:00
|
|
|
self::Init($params);
|
2018-12-28 18:58:37 +08:00
|
|
|
|
2020-06-22 23:21:23 +08:00
|
|
|
// 循环操作
|
|
|
|
$sucs = 0;
|
|
|
|
$fail = 0;
|
|
|
|
foreach($params['ids'] as $id)
|
2019-01-04 18:16:20 +08:00
|
|
|
{
|
2020-06-22 23:21:23 +08:00
|
|
|
// 目录处理
|
|
|
|
$suffix = '';
|
|
|
|
if(substr($id, -4) === '.zip')
|
|
|
|
{
|
|
|
|
$name = substr($id, 0, strlen($id)-4);
|
|
|
|
$suffix = '.zip';
|
|
|
|
} else {
|
|
|
|
$name = $id;
|
|
|
|
}
|
2019-01-04 18:16:20 +08:00
|
|
|
|
2020-06-22 23:21:23 +08:00
|
|
|
// 防止路径回溯
|
|
|
|
$path_name = self::$new_path.DS.htmlentities(str_replace(array('.', '/', '\\', ':'), '', strip_tags($id)));
|
|
|
|
$path_zip = $path_name.'.zip';
|
2019-01-04 18:16:20 +08:00
|
|
|
|
2020-06-22 23:21:23 +08:00
|
|
|
// 删除包
|
|
|
|
$statusz = \base\FileUtil::UnlinkFile($path_zip);
|
|
|
|
$statusf = \base\FileUtil::UnlinkDir($path_name);
|
|
|
|
if($statusz || $statusf)
|
|
|
|
{
|
|
|
|
$sucs++;
|
|
|
|
} else {
|
|
|
|
$fail++;
|
|
|
|
}
|
2018-12-28 18:58:37 +08:00
|
|
|
}
|
2020-06-22 23:21:23 +08:00
|
|
|
|
|
|
|
// 成功
|
|
|
|
if($sucs == count($params['ids']))
|
2018-12-28 18:58:37 +08:00
|
|
|
{
|
|
|
|
return DataReturn('删除成功');
|
|
|
|
}
|
2020-06-22 23:21:23 +08:00
|
|
|
|
|
|
|
// 失败
|
|
|
|
if($fail == count($params['ids']))
|
|
|
|
{
|
|
|
|
return DataReturn('删除失败', -100);
|
|
|
|
}
|
|
|
|
|
|
|
|
return DataReturn('成功['.$sucs.'],失败['.$fail.']');
|
2018-12-28 18:58:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|