Files
shopxo/app/service/StoreService.php

529 lines
18 KiB
PHP
Raw Normal View History

2019-06-16 00:38:35 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2019-06-16 00:38:35 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2019-06-16 00:38:35 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
2024-10-21 10:51:14 +08:00
use think\facade\Db;
use app\service\ConfigService;
2024-10-21 10:51:14 +08:00
use app\service\MultilingualService;
2024-12-25 17:35:54 +08:00
use app\service\ThemeAdminService;
2019-06-16 00:38:35 +08:00
/**
* 应用商店服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2019-06-16T00:33:28+0800
*/
class StoreService
{
// 站点商店数据缓存key
public static $site_store_info_key = 'admin_site_store_info_data';
2019-06-16 00:38:35 +08:00
/**
* 应用商店地址
2021-02-15 22:45:33 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-12
* @desc description
* @param [array] $params [输入参数]
2019-06-16 00:38:35 +08:00
*/
public static function StoreUrl($params = [])
{
2024-10-21 10:51:14 +08:00
return self::RequestParamsString(MyConfig('shopxo.store_url'), $params);
2019-06-16 00:38:35 +08:00
}
2020-01-16 15:01:30 +08:00
/**
* 应用商店支付插件地址
2021-02-15 22:45:33 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-12
* @desc description
* @param [array] $params [输入参数]
2020-01-16 15:01:30 +08:00
*/
public static function StorePaymentUrl($params = [])
{
2024-10-21 10:51:14 +08:00
return self::RequestParamsString(MyConfig('shopxo.store_payment_url'), $params);
2020-01-16 15:01:30 +08:00
}
/**
* 应用商店页面设计地址
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-12
* @desc description
* @param [array] $params [输入参数]
*/
public static function StoreDesignUrl($params = [])
{
2024-10-21 10:51:14 +08:00
return self::RequestParamsString(MyConfig('shopxo.store_design_url'), $params);
}
/**
* 应用商店DIY装修地址
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-12
* @desc description
* @param [array] $params [输入参数]
*/
public static function StoreDiyUrl($params = [])
{
return self::RequestParamsString(MyConfig('shopxo.store_diy_url'), $params);
}
2021-02-15 22:45:33 +08:00
2020-01-16 15:01:30 +08:00
/**
* 应用商店主题地址
2021-02-15 22:45:33 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-12
* @desc description
* @param [array] $params [输入参数]
2020-01-16 15:01:30 +08:00
*/
public static function StoreThemeUrl($params = [])
{
2024-10-21 10:51:14 +08:00
return self::RequestParamsString(MyConfig('shopxo.store_theme_url'), $params);
2021-02-15 22:45:33 +08:00
}
/**
* 请求参数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-12
* @desc description
2024-10-21 10:51:14 +08:00
* @param [string] $url [url地址]
2021-02-15 22:45:33 +08:00
* @param [array] $params [输入参数]
*/
2024-10-21 10:51:14 +08:00
public static function RequestParamsString($url, $params = [])
2021-02-15 22:45:33 +08:00
{
2021-02-23 12:16:31 +08:00
// 当前管理员后台地址
$admin_url = explode('?', __MY_VIEW_URL__);
// 拼接商店请求参数地址
2024-10-21 10:51:14 +08:00
$join = (stripos($url, '?') === false) ? '?' : '&';
return $url.$join.'name='.urlencode(base64_encode(MyC('home_site_name'))).'&ver='.urlencode(base64_encode(APPLICATION_VERSION)).'&url='.urlencode(base64_encode(__MY_URL__)).'&host='.urlencode(base64_encode(__MY_HOST__)).'&ip='.urlencode(base64_encode(__MY_ADDR__)).'&admin_url='.urlencode(base64_encode($admin_url[0]));
2020-01-16 15:01:30 +08:00
}
/**
* 获取站点商店信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-16
* @desc description
*/
public static function SiteStoreInfo()
{
2021-07-18 23:42:10 +08:00
$res = MyCache(self::$site_store_info_key);
return empty($res) ? [] : $res;
}
/**
* 站点应用商店帐号绑定
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-16
* @desc description
* @param [array] $params [输入参数]
*/
public static function SiteStoreAccountsBind($params = [])
{
// 请求类型
$p = [
[
'checked_type' => 'length',
'key_name' => 'common_store_accounts',
'checked_data' => '1,80',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('store_bind_form_accounts_message'),
],
[
'checked_type' => 'length',
'key_name' => 'common_store_password',
'checked_data' => '6,30',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('store_bind_form_password_message'),
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 保存商店帐号信息
2023-08-27 16:59:15 +08:00
// 处理转义符号并加密
$save_data = [
'common_store_accounts' => $params['common_store_accounts'],
'common_store_password' => Authcode(htmlspecialchars_decode($params['common_store_password']), 'ENCODE'),
];
$ret = ConfigService::ConfigSave($save_data);
if($ret['code'] != 0)
{
return $ret;
}
2021-04-23 16:43:23 +08:00
// 绑定处理
2024-12-25 17:35:54 +08:00
$ret = self::SiteStoreAccountsBindHandle($params['common_store_accounts'], $params['common_store_password'], 'bind');
if($ret['code'] == 0)
{
// 清除插件缓存
$plugins = Db::name('Plugins')->column('plugins');
if(!empty($plugins) && is_array($plugins))
{
foreach($plugins as $v)
{
MyCache('plugins_legal_check_'.$v, null);
}
}
// 清除支付方式缓存
$payment = Db::name('Payment')->column('payment');
if(!empty($payment) && is_array($payment))
{
foreach($payment as $v)
{
MyCache('payment_legal_check_'.$v, null);
}
}
// 清除DIY缓存
$diy = Db::name('Diy')->column('md5_key');
if(!empty($diy) && is_array($diy))
{
foreach($diy as $v)
{
MyCache('diy_legal_check_'.$v, null);
}
}
// 清除主题缓存
$theme = ThemeAdminService::ThemeAdminList();
if(!empty($theme) && is_array($theme))
{
foreach($theme as $v)
{
if(!empty($v['theme']))
{
MyCache('theme_legal_check_'.$v['theme'], null);
}
}
}
}
return $ret;
2021-04-23 16:43:23 +08:00
}
2023-08-27 16:59:15 +08:00
/**
* 账号数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2023-08-17
* @desc description
*/
public static function AccountsData()
{
// 数据库配置中读取
$accounts = MyC('common_store_accounts');
$password = MyC('common_store_password');
2024-10-21 10:51:14 +08:00
// 缓存没有则从数据库读取
if(empty($accounts) || empty($password))
{
$info = Db::name('Config')->where('only_tag', 'in', ['common_store_accounts', 'common_store_password'])->column('value', 'only_tag');
if(!empty($info['common_store_accounts']))
{
$accounts = $info['common_store_accounts'];
}
if(!empty($info['common_store_password']))
{
$password = $info['common_store_password'];
}
}
2023-08-27 16:59:15 +08:00
// 存在密码则解密
if(!empty($password))
{
$password = Authcode($password, 'DECODE');
}
return [
'accounts' => $accounts,
'password' => $password,
];
}
2021-04-23 16:43:23 +08:00
/**
* 站点应用商店帐号绑定处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-16
* @desc description
* @param [string] $accounts [帐号]
* @param [string] $password [密码]
2024-07-03 22:46:04 +08:00
* @param [string] $type [类型 bind 主动绑定、auto 自动获取绑定信息]
2021-04-23 16:43:23 +08:00
*/
2024-07-03 22:46:04 +08:00
public static function SiteStoreAccountsBindHandle($accounts = '', $password = '', $type = 'bind')
2021-04-23 16:43:23 +08:00
{
2024-07-03 22:46:04 +08:00
// 是否存在失败缓存记录
if($type == 'auto')
{
$fail = MyCache(self::$site_store_info_key.'_fail');
if(!empty($fail))
{
return DataReturn($fail, -1);
}
}
// 帐号信息、站点初始化信息接口、帐号信息可以为空
2023-08-27 16:59:15 +08:00
if(empty($accounts) || empty($password))
2021-04-23 16:43:23 +08:00
{
2023-08-27 16:59:15 +08:00
$user = self::AccountsData();
$accounts = $user['accounts'];
$password = $user['password'];
2021-04-23 16:43:23 +08:00
}
// 获取信息
2021-07-28 00:55:51 +08:00
$res = self::RemoteStoreData($accounts, $password, MyConfig('shopxo.store_site_info_url'));
if($res['code'] == 0)
{
2021-05-26 19:54:59 +08:00
// 存储缓存、取远程给的时间未拿到时间则默认60分钟
$cache_time = (empty($res['data']['base']) || empty($res['data']['base']['cache_time'])) ? 3600 : intval($res['data']['base']['cache_time']);
2021-07-18 23:42:10 +08:00
MyCache(self::$site_store_info_key, $res['data'], $cache_time);
2023-02-04 16:47:48 +08:00
return DataReturn(MyLang('bind_success'), 0);
}
2024-07-03 22:46:04 +08:00
// 失败记录,自动执行则缓存时间期间不再次请求
MyCache(self::$site_store_info_key.'_fail', $res['msg'], 3600);
return $res;
}
/**
* 站点检查更新
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-18
* @desc description
* @param [array] $params [输入参数]
*/
public static function SiteInspectUpgrade($params = [])
{
// 帐号信息
2023-08-27 16:59:15 +08:00
$user = self::AccountsData();
// 获取信息
2023-08-27 16:59:15 +08:00
return self::RemoteStoreData($user['accounts'], $user['password'], MyConfig('shopxo.store_inspect_upgrade_url'));
}
/**
* 插件安全合法校验
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-19
* @desc description
* @param [array] $params [输入参数]
*/
2021-04-23 16:43:23 +08:00
public static function PluginsLegalCheck($params = [])
{
2021-04-23 16:43:23 +08:00
// 参数校验
2024-10-21 10:51:14 +08:00
if(empty($params) || empty($params['type']) || empty($params['plugins']))
2021-04-23 16:43:23 +08:00
{
2024-10-21 10:51:14 +08:00
return DataReturn(MyLang('store_plugins_params_error_tips'), -1);
2021-04-23 16:43:23 +08:00
}
// 帐号信息
2023-08-27 16:59:15 +08:00
$user = self::AccountsData();
if(empty($user['accounts']) || empty($user['password']))
{
2023-02-11 17:01:04 +08:00
return DataReturn(MyLang('store_account_not_bind_tips'), -300);
}
// 获取信息
$request_params = [
2021-04-23 16:43:23 +08:00
'plugins_type' => $params['type'],
'plugins_value' => $params['plugins'],
2024-10-21 10:51:14 +08:00
'plugins_author' => empty($params['author']) ? '' : $params['author'],
'plugins_ver' => empty($params['ver']) ? '' : empty($params['ver']),
'plugins_config' => isset($params['config']) ? $params['config'] : '',
];
2023-08-27 16:59:15 +08:00
return self::RemoteStoreData($user['accounts'], $user['password'], MyConfig('shopxo.store_plugins_legal_check_url'), $request_params);
}
2021-04-23 16:43:23 +08:00
/**
* 插件更新信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-21
* @desc description
* @param [array] $params [输入参数、插件信息]
*/
public static function PluginsUpgradeInfo($params = [])
{
if(!empty($params) && !empty($params['plugins_type']) && !empty($params['plugins_data']) && is_array($params['plugins_data']))
{
// 帐号信息
2023-08-27 16:59:15 +08:00
$user = self::AccountsData();
if(empty($user['accounts']) || empty($user['password']))
{
2023-02-11 17:01:04 +08:00
return DataReturn(MyLang('store_account_not_bind_tips'), -300);
}
2021-04-23 16:43:23 +08:00
// 获取更新信息
2023-08-27 16:59:15 +08:00
return self::RemoteStoreData($user['accounts'], $user['password'], MyConfig('shopxo.store_plugins_upgrade_info_url'), $params);
2021-04-23 16:43:23 +08:00
}
2023-02-11 17:01:04 +08:00
return DataReturn(MyLang('plugins_no_data_tips'), 0);
2021-04-23 16:43:23 +08:00
}
/**
* 远程获取数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-13
* @desc description
* @param [string] $accounts [帐号]
* @param [string] $password [密码]
* @param [string] $url [请求地址]
* @param [array] $params [额外参数]
2023-08-27 16:59:15 +08:00
* @param [array] $data_type[请求数据类型]
*/
2023-08-27 16:59:15 +08:00
public static function RemoteStoreData($accounts, $password, $url, $params = [], $data_type = 0)
2023-02-11 17:01:04 +08:00
{
2021-07-09 17:47:15 +08:00
// http状态验证
$key = 'cache_store_url_http_code';
$time = 600;
2021-10-24 11:36:22 +08:00
$ret = MyCache($key);
if(empty($ret))
2021-07-09 17:47:15 +08:00
{
2022-01-03 16:02:05 +08:00
$ret = GetHttpCode(self::StoreUrl(), 5);
2021-10-24 11:36:22 +08:00
MyCache($key, $ret, $time);
2021-07-09 17:47:15 +08:00
}
2021-10-24 11:36:22 +08:00
if(!in_array($ret['data'], [200, 301, 302, 307, 308]))
2021-07-09 17:47:15 +08:00
{
2023-02-11 17:01:04 +08:00
$ret['msg'] = MyLang('store_content_error_tips').'[ '.$ret['msg'].' ]';
2021-10-24 11:36:22 +08:00
return $ret;
2021-07-09 17:47:15 +08:00
}
// 基础数据获取
$bo = new \base\Behavior();
// 请求校验
$data = [
'accounts' => $accounts,
2022-05-10 11:42:10 +08:00
'authdata' => empty($password) ? '' : htmlspecialchars_decode($password),
'host' => __MY_HOST__,
'url' => __MY_URL__,
'ver' => APPLICATION_VERSION,
'server_port' => $bo->GetServerPort(),
'server_ip' => $bo->GetServerIP(),
'client_ip' => $bo->GetClientIP(),
'os' => $bo->GetOs(),
'browser' => $bo->GetBrowser(),
'scheme' => $bo->GetScheme(),
'version' => $bo->GetHttpVersion(),
'client' => $bo->GetClinet(),
'php_os' => PHP_OS,
'php_version' => PHP_VERSION,
'php_sapi_name' => php_sapi_name(),
2024-10-21 10:51:14 +08:00
'lang' => MultilingualService::GetUserMultilingualValue(),
'client_date' => date('Y-m-d H:i:s'),
];
2024-12-25 17:35:54 +08:00
$ret = CurlPost($url, array_merge($data, $params), $data_type, 60);
2021-10-24 11:36:22 +08:00
if($ret['code'] != 0)
{
// 网络不通
MyCache($key, 0, $ret['data']);
2023-02-11 17:01:04 +08:00
$ret['msg'] = MyLang('store_content_error_tips').'[ '.$ret['msg'].' ]';
2021-10-24 11:36:22 +08:00
return $ret;
}
2023-02-11 17:01:04 +08:00
2021-10-24 11:36:22 +08:00
// 数据解析
$result = json_decode($ret['data'], true);
if(empty($result))
{
2023-02-11 17:01:04 +08:00
return DataReturn(MyLang('store_respond_data_error_tips').(empty($ret['data']) ? '' : '('.$ret['data'].')'), -1);
}
// 是否非数组
if(is_string($result))
{
2023-02-11 17:01:04 +08:00
return DataReturn(MyLang('store_respond_data_invalid_tips').'[ '.$result.' ]', -1);
}
// 请求成功
if(isset($result['code']) && $result['code'] == 0)
{
if(empty($result['data']))
{
2023-02-11 17:01:04 +08:00
return DataReturn(MyLang('store_respond_empty_tips'), -1);
}
return $result;
}
2023-02-11 17:01:04 +08:00
return DataReturn(empty($result['msg']) ? MyLang('store_respond_data_empty_tips') : MyLang('store_respond_result_tips').'[ '.$result['msg'].' ]', -1);
}
2024-10-21 10:51:14 +08:00
/**
* 包数数据列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-04-19
* @desc description
* @param [array] $params [输入参数]
*/
public static function PackageDataList($params = [])
{
// 参数处理
$params['page_size'] = empty($params['page_size']) ? 18 : min(100, intval($params['page_size']));
// 帐号信息
$user = self::AccountsData();
if(empty($user['accounts']) || empty($user['password']))
{
return DataReturn(MyLang('store_account_not_bind_tips'), -300);
}
// 获取信息
$res = self::RemoteStoreData($user['accounts'], $user['password'], MyConfig('shopxo.store_package_data_list_url'), $params);
if($res['code'] == 0)
{
$data = $res['data'];
if(!empty($data['data_list']) && is_array($data['data_list']))
{
foreach($data['data_list'] as &$v)
{
// 拼接商品应用商店来源地址
$v['goods_url'] = self::RequestParamsString($v['goods_url']);
}
}
$result = [
'page' => empty($data['page']) ? 0 : $data['page'],
'page_start' => empty($data['page_start']) ? 0 : $data['page_start'],
'page_size' => empty($data['page_size']) ? 0 : $data['page_size'],
'page_total' => empty($data['page_total']) ? 0 : $data['page_total'],
'data_total' => empty($data['data_total']) ? 0 : $data['data_total'],
'data_list' => empty($data['data_list']) ? [] : $data['data_list'],
];
return DataReturn('success', 0, $result);
}
return $res;
}
2019-06-16 00:38:35 +08:00
}
?>