Files
stemcell/app/code/Stemcell/Commerce/Model/Wechat/Config.php
T
2026-09-15 10:26:34 +08:00

271 lines
7.2 KiB
PHP

<?php
/**
* 2025 D1m Group
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0) that is available
* through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
*
* @copyright 2025 D1m Group
* @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
namespace Stemcell\Commerce\Model\Wechat;
use Magento\Cms\Model\Template\FilterProvider;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;
/**
* 微信小程序配置类
*/
class Config
{
const XML_PATH_WECHAT_APP_ID = 'commerce/wechat/app_id';
const XML_PATH_WECHAT_APP_SECRET = 'commerce/wechat/app_secret';
const XML_PATH_WECHAT_API_URL = 'commerce/wechat/api_url';
const XML_PATH_WECHAT_SWITCH = 'commerce/wechat/switch';
const XML_PATH_WECHAT_BANNER = 'commerce/wechat/banner';
const XML_PATH_WECHAT_UTILITY_TOOLS = 'commerce/wechat/utility-tools';
const XML_PATH_WECHAT_CONTACT_US = 'commerce/wechat/contact-us';
const XML_PATH_WECHAT_TECHNICAL_RESOURCE_MENU = 'commerce/wechat/technical-resource-menu';
const XML_PATH_WECHAT_MARKETING_POSTS = 'commerce/wechat/marketing-posts';
const XML_PATH_WECHAT_MARKETING_NEW_PRODUCT = 'commerce/wechat/new-product';
/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
protected ScopeConfigInterface $scopeConfig,
protected FilterProvider $filterProvider,
) {
// 使用PHP 7.4+构造函数属性提升,无需单独赋值
}
/**
* 获取微信小程序AppID
*
* @param int|null $storeId
* @return string
*/
public function getAppId($storeId = null)
{
return $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_APP_ID,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
/**
* 获取微信小程序AppSecret
*
* @param int|null $storeId
* @return string
*/
public function getAppSecret($storeId = null)
{
return $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_APP_SECRET,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
/**
* 获取微信API地址
*
* @param int|null $storeId
* @return string
*/
public function getApiUrl($storeId = null)
{
return $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_API_URL,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
/**
* 获取微信登录URL
*
* @return string
*/
public function getLoginUrl()
{
return $this->getApiUrl() . '/sns/jscode2session';
}
/**
* 获取微信手机号解密URL
*
* @return string
*/
public function getPhoneNumberUrl()
{
return $this->getApiUrl() . '/wxa/business/getuserphonenumber';
}
/**
* 小程序首页banner、搜索、产品中心、技术资源等是否在小程序首页展示的一些配置
* @return string
*/
public function getWechatConfig()
{
$banner = $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_BANNER,
ScopeInterface::SCOPE_STORE
);
$banner = json_decode($banner, true);
if (!is_array($banner)) {
$banner = [];
}
$config = [
'banner' => $banner,
'entrance' => (int)$this->scopeConfig->getValue(
self::XML_PATH_WECHAT_SWITCH,
ScopeInterface::SCOPE_STORE
),
];
return json_encode($config);
}
/**
* 实用工具页面配置
* @return string
*/
public function getUtilityTools()
{
$config = $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_UTILITY_TOOLS,
ScopeInterface::SCOPE_STORE
);
if (!$config) {
$config = [];
} else {
try {
$config = $this->filterProvider->getPageFilter()->filter($config);
$config = json_decode($config, true);
} catch (\Exception $e) {
$config = [];
}
}
$config = is_array($config) ? $config : [];
return json_encode($config);
}
/**
* 联系我们页面
* @return string
*/
public function getContactUs()
{
$config = $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_CONTACT_US,
ScopeInterface::SCOPE_STORE
);
if (!$config) {
$config = [];
} else {
try {
$config = $this->filterProvider->getPageFilter()->filter($config);
$config = json_decode($config, true);
} catch (\Exception $e) {
$config = [];
}
}
$config = is_array($config) ? $config : [];
if (isset($config['vendors']['data'])) {
$config['vendors']['provinces'] = array_keys((array)$config['vendors']['data']);
}
return json_encode($config);
}
/**
* 技术资源菜单配置
* @return string
*/
public function getTechnicalResourceMenu()
{
$config = $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_TECHNICAL_RESOURCE_MENU,
ScopeInterface::SCOPE_STORE
);
if (!$config) {
$config = [];
} else {
try {
$config = $this->filterProvider->getPageFilter()->filter($config);
$config = json_decode($config, true);
} catch (\Exception $e) {
$config = [];
}
}
$config = is_array($config) ? $config : [];
return json_encode($config);
}
/**
* 技术资源菜单配置
* @return string
*/
public function getMarketingPosts()
{
$config = $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_MARKETING_POSTS,
ScopeInterface::SCOPE_STORE
);
if (!$config) {
$config = [];
} else {
try {
$config = $this->filterProvider->getPageFilter()->filter($config);
$config = json_decode($config, true);
} catch (\Exception $e) {
$config = [];
}
}
$config = is_array($config) ? $config : [];
return json_encode($config);
}
/**
* 新品配置
* @return string
*/
public function getNewProduct()
{
$config = $this->scopeConfig->getValue(
self::XML_PATH_WECHAT_MARKETING_NEW_PRODUCT,
ScopeInterface::SCOPE_STORE
);
if (!$config) {
$config = [];
} else {
try {
$config = $this->filterProvider->getPageFilter()->filter($config);
$config = json_decode($config, true);
} catch (\Exception $e) {
$config = [];
}
}
$config = is_array($config) ? $config : [];
return json_encode($config);
}
}