shopxo/app/service/StoreService.php

341 lines
11 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;
use app\service\ConfigService;
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 = [])
{
2021-07-18 23:42:10 +08:00
return MyConfig('shopxo.store_url').self::RequestParamsString($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 = [])
{
2021-07-18 23:42:10 +08:00
return MyConfig('shopxo.store_payment_url').self::RequestParamsString($params);
2020-01-16 15:01:30 +08:00
}
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 = [])
{
2021-07-18 23:42:10 +08:00
return MyConfig('shopxo.store_theme_url').self::RequestParamsString($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
* @param [array] $params [输入参数]
*/
public static function RequestParamsString($params = [])
{
2021-02-23 12:16:31 +08:00
// 当前管理员后台地址
$admin_url = explode('?', __MY_VIEW_URL__);
// 拼接商店请求参数地址
2021-02-27 23:51:19 +08:00
return '?name='.urldecode(base64_encode(MyC('home_site_name'))).'&ver='.urldecode(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,30',
'error_msg' => '账号格式1~30个字符',
],
[
'checked_type' => 'length',
'key_name' => 'common_store_password',
'checked_data' => '6,30',
'error_msg' => '登录密码格式6~30个字符',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 保存商店帐号信息
$ret = ConfigService::ConfigSave($params);
if($ret['code'] != 0)
{
return $ret;
}
2021-04-23 16:43:23 +08:00
// 绑定处理
return self::SiteStoreAccountsBindHandle($params['common_store_accounts'], $params['common_store_password']);
}
/**
* 站点应用商店帐号绑定处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-16
* @desc description
* @param [string] $accounts [帐号]
* @param [string] $password [密码]
*/
public static function SiteStoreAccountsBindHandle($accounts = '', $password = '')
{
// 帐号信息、站点初始化信息接口、帐号信息可以为空
2021-04-23 16:43:23 +08:00
if(empty($accounts))
{
$accounts = MyC('common_store_accounts');
}
if(empty($password))
{
$password = MyC('common_store_password');
}
// 获取信息
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);
return DataReturn('绑定成功', 0);
}
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 = [])
{
// 帐号信息
$accounts = MyC('common_store_accounts');
$password = MyC('common_store_password');
// 获取信息
2021-07-28 00:55:51 +08:00
return self::RemoteStoreData($accounts, $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
// 参数校验
if(empty($params) || empty($params['type']) || empty($params['plugins']) || empty($params['author']) || empty($params['ver']))
{
return DataReturn('插件参数有误', -1);
}
// 帐号信息
$accounts = MyC('common_store_accounts');
$password = MyC('common_store_password');
if(empty($accounts) || empty($password))
{
return DataReturn('请先绑定应用商店帐号', -1);
}
// 获取信息
$request_params = [
2021-04-23 16:43:23 +08:00
'plugins_type' => $params['type'],
'plugins_value' => $params['plugins'],
2021-04-23 16:43:23 +08:00
'plugins_author' => $params['author'],
'plugins_ver' => $params['ver'],
'plugins_config' => $params['config'],
];
2021-07-28 00:55:51 +08:00
return self::RemoteStoreData($accounts, $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']))
{
// 帐号信息
$accounts = MyC('common_store_accounts');
$password = MyC('common_store_password');
if(empty($accounts) || empty($password))
{
return DataReturn('请先绑定应用商店帐号', -1);
}
2021-04-23 16:43:23 +08:00
// 获取更新信息
2021-07-28 00:55:51 +08:00
return self::RemoteStoreData($accounts, $password, MyConfig('shopxo.store_plugins_upgrade_info_url'), $params);
2021-04-23 16:43:23 +08:00
}
return DataReturn('无插件数据', 0);
}
/**
* 远程获取数据
* @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 [额外参数]
*/
public static function RemoteStoreData($accounts, $password, $url, $params = [])
2021-07-09 17:47:15 +08:00
{
// http状态验证
$key = 'cache_store_url_http_code';
$time = 600;
2021-07-18 23:42:10 +08:00
$code = MyCache($key);
2021-07-23 15:58:46 +08:00
if($code === null)
2021-07-09 17:47:15 +08:00
{
$code = GetHttpCode(self::StoreUrl(), 2);
2021-07-18 23:42:10 +08:00
MyCache($key, $code, $time);
2021-07-09 17:47:15 +08:00
}
if($code != 200)
{
return DataReturn('商店网络不通', 0);
}
// 基础数据获取
$bo = new \base\Behavior();
// 请求校验
$data = [
'accounts' => $accounts,
'password' => $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(),
'client_date' => date('Y-m-d H:i:s'),
];
$res = CurlPost($url, array_merge($data, $params));
$result = json_decode($res, true);
if(empty($result))
{
2021-07-09 17:47:15 +08:00
// 网络不通、返回非有效json数据则认为网络不通
2021-07-18 23:42:10 +08:00
MyCache($key, 0, $time);
2021-07-09 17:47:15 +08:00
return DataReturn('商店网络不通', 0);
}
// 是否非数组
if(is_string($result))
{
return DataReturn($result, -1);
}
// 请求成功
if(isset($result['code']) && $result['code'] == 0)
{
if(empty($result['data']))
{
return DataReturn('无对应数据、请稍后再试!', -1);
}
return $result;
}
return DataReturn(empty($result['msg']) ? '异常错误失败、请稍后再试!' : $result['msg'], -1);
}
2019-06-16 00:38:35 +08:00
}
?>